lint: Use early exit/continue

This commit is contained in:
Kurtis Rader 2016-10-30 14:52:36 -07:00
parent 225caa2fe8
commit eab836864e

View file

@ -939,43 +939,45 @@ void history_t::populate_from_mmap(void) {
/// if successful. Returns the mapped memory region by reference. /// if successful. Returns the mapped memory region by reference.
bool history_t::map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len, bool history_t::map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len,
file_id_t *file_id) { file_id_t *file_id) {
bool result = false;
wcstring filename = history_filename(name, L""); wcstring filename = history_filename(name, L"");
if (!filename.empty()) { if (filename.empty()) {
int fd = wopen_cloexec(filename, O_RDONLY); return false;
if (fd >= 0) { }
// Get the file ID if requested.
if (file_id != NULL) *file_id = file_id_for_fd(fd);
// Take a read lock to guard against someone else appending. This is released when the int fd = wopen_cloexec(filename, O_RDONLY);
// file is closed (below). We will read the file after releasing the lock, but that's if (fd == -1) {
// not a problem, because we never modify already written data. In short, the purpose of return false;
// this lock is to ensure we don't see the file size change mid-update. }
//
// We may fail to lock (e.g. on lockless NFS - see bool result = false;
// https://github.com/fish-shell/fish-shell/issues/685 ). In that case, we proceed as // Get the file ID if requested.
// if it did not fail. The risk is that we may get an incomplete history item; this if (file_id != NULL) *file_id = file_id_for_fd(fd);
// is unlikely because we only treat an item as valid if it has a terminating
// newline. // Take a read lock to guard against someone else appending. This is released when the file
// // is closed (below). We will read the file after releasing the lock, but that's not a
// Simulate a failing lock in chaos_mode. // problem, because we never modify already written data. In short, the purpose of this lock
if (!chaos_mode) history_file_lock(fd, F_RDLCK); // is to ensure we don't see the file size change mid-update.
off_t len = lseek(fd, 0, SEEK_END); //
if (len != (off_t)-1) { // We may fail to lock (e.g. on lockless NFS - see issue #685. In that case, we proceed as
size_t mmap_length = (size_t)len; // if it did not fail. The risk is that we may get an incomplete history item; this is
if (lseek(fd, 0, SEEK_SET) == 0) { // unlikely because we only treat an item as valid if it has a terminating newline.
char *mmap_start; //
if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd, // Simulate a failing lock in chaos_mode.
0)) != MAP_FAILED) { if (!chaos_mode) history_file_lock(fd, F_RDLCK);
result = true; off_t len = lseek(fd, 0, SEEK_END);
*out_map_start = mmap_start; if (len != (off_t)-1) {
*out_map_len = mmap_length; size_t mmap_length = (size_t)len;
} if (lseek(fd, 0, SEEK_SET) == 0) {
} char *mmap_start;
if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd,
0)) != MAP_FAILED) {
result = true;
*out_map_start = mmap_start;
*out_map_len = mmap_length;
} }
close(fd);
} }
} }
close(fd);
return result; return result;
} }