fix: util::read_files_in_dir does not always return a correct result (#21883)

* fix bug

* use de->d_type==DT_REG

* Revert "use de->d_type==DT_REG"

This reverts commit ecb38c8230.
old-commit-hash: 83710b14ee
commatwo_master
Dean Lee 4 years ago committed by GitHub
parent 2e285bea20
commit 48b7754ab5
  1. 17
      selfdrive/common/tests/test_util.cc
  2. 2
      selfdrive/common/util.cc

@ -75,3 +75,20 @@ TEST_CASE("util::file_exists") {
}
::remove(filename);
}
TEST_CASE("util::read_files_in_dir") {
char tmp_path[] = "/tmp/test_XXXXXX";
const std::string test_path = mkdtemp(tmp_path);
const std::string files[] = {".test1", "'test2'", "test3"};
for (auto fn : files) {
std::ofstream{test_path + "/" + fn} << fn;
}
mkdir((test_path + "/dir").c_str(), 0777);
std::map<std::string, std::string> result = util::read_files_in_dir(test_path);
REQUIRE(result.find("dir") == result.end());
REQUIRE(result.size() == std::size(files));
for (auto& [k, v] : result) {
REQUIRE(k == v);
}
}

@ -85,7 +85,7 @@ std::map<std::string, std::string> read_files_in_dir(const std::string &path) {
struct dirent *de = NULL;
while ((de = readdir(d))) {
if (isalnum(de->d_name[0])) {
if (de->d_type != DT_DIR) {
ret[de->d_name] = util::read_file(path + "/" + de->d_name);
}
}

Loading…
Cancel
Save