From ce6de862c1e487072931e1c231ad4732f2b67f18 Mon Sep 17 00:00:00 2001 From: Jay Zhuang Date: Fri, 26 Mar 2021 18:45:19 -0700 Subject: [PATCH] Avoid checking errno on success call (#8119) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/8119 Reviewed By: sushilpa Differential Revision: D27365407 Pulled By: jay-zhuang fbshipit-source-id: 327c09bf76834ce0be4287680640adc8b88bcec2 --- env/fs_posix.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/env/fs_posix.cc b/env/fs_posix.cc index 53d8781d82..f3f7f152e8 100644 --- a/env/fs_posix.cc +++ b/env/fs_posix.cc @@ -620,9 +620,10 @@ class PosixFileSystem : public FileSystem { } } - const auto pre_read_errno = errno; // errno may be modified by readdir + // reset errno before calling readdir() + errno = 0; struct dirent* entry; - while ((entry = readdir(d)) != nullptr && errno == pre_read_errno) { + while ((entry = readdir(d)) != nullptr) { // filter out '.' and '..' directory entries // which appear only on some platforms const bool ignore = @@ -631,19 +632,20 @@ class PosixFileSystem : public FileSystem { if (!ignore) { result->push_back(entry->d_name); } + errno = 0; // reset errno if readdir() success } // always attempt to close the dir const auto pre_close_errno = errno; // errno may be modified by closedir const int close_result = closedir(d); - if (pre_close_errno != pre_read_errno) { - // error occured during readdir + if (pre_close_errno != 0) { + // error occurred during readdir return IOError("While readdir", dir, pre_close_errno); } if (close_result != 0) { - // error occured during closedir + // error occurred during closedir return IOError("While closedir", dir, errno); }