diff --git a/selfdrive/common/tests/test_util.cc b/selfdrive/common/tests/test_util.cc index d61ca38886..abc5398db6 100644 --- a/selfdrive/common/tests/test_util.cc +++ b/selfdrive/common/tests/test_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 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); + } +} diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index 83114c9189..99cf2ae887 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -85,7 +85,7 @@ std::map 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); } }