Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace use of readdir()/dirent.d_type with stat()/st_mode. #1874

Merged
merged 5 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions source/common/runtime/runtime_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ void SnapshotImpl::walkDirectory(const std::string& path, const std::string& pre
full_prefix = prefix + "." + entry->d_name;
}

if (entry->d_type == DT_DIR && std::string(entry->d_name) != "." &&
struct stat stat_result;
int rc = ::stat(full_path.c_str(), &stat_result);
if (rc != 0) {
throw EnvoyException(fmt::format("unable to stat file: '{}'", full_path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this case? Perhaps move to utility function so can test with a bogus path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add some test coverage if I move stat() to OsSysCalls.

I think in the real world, the only way this fails is if someone deletes/moves the file after readdir() has returned it but before we stat() it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or make it RELEASE_ASSERT. Up to you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I test hack would be to just move the stat call to a small helper, and then pass in a bad file name, and I think it should fail and throw).

}

if (S_ISDIR(stat_result.st_mode) && std::string(entry->d_name) != "." &&
std::string(entry->d_name) != "..") {
walkDirectory(full_path, full_prefix);
} else if (entry->d_type == DT_REG) {
} else if (S_ISREG(stat_result.st_mode)) {
// Suck the file into a string. This is not very efficient but it should be good enough
// for small files. Also, as noted elsewhere, none of this is non-blocking which could
// theoretically lead to issues.
Expand Down
8 changes: 6 additions & 2 deletions test/test_common/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ std::vector<std::string> TestUtility::listFiles(const std::string& path, bool re
dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
std::string file_name = fmt::format("{}/{}", path, std::string(entry->d_name));
if (recursive && entry->d_type == DT_DIR && std::string(entry->d_name) != "." &&
struct stat stat_result;
int rc = ::stat(file_name.c_str(), &stat_result);
EXPECT_EQ(rc, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would make this ASSERT_EQ, since if this fails, the code below is using junk.


if (recursive && S_ISDIR(stat_result.st_mode) && std::string(entry->d_name) != "." &&
std::string(entry->d_name) != "..") {
std::vector<std::string> more_file_names = listFiles(file_name, recursive);
file_names.insert(file_names.end(), more_file_names.begin(), more_file_names.end());
continue;
} else if (entry->d_type == DT_DIR) {
} else if (S_ISDIR(stat_result.st_mode)) {
continue;
}

Expand Down