strings_t fs_t::pimpl_t::list(const std::string& path,bool (*test)(const char*)) { strings_t list; #ifdef WIN32 if(DIR *dir = opendir(path.c_str())) { while(dirent* entry = readdir(dir)) { if((entry->d_name[0] != '.') && test(fs.join(path,entry->d_name).c_str())) list.push_back(entry->d_name); } closedir(dir); } else data_error("list_dirs("<<path<<"): cannot open"); #else struct dirent64 **eps; if(int n = scandir64(path.c_str(),&eps,_one,alphasort64)) { if(-1 == n) c_error("list_dirs("<<path<<")"); for(int i=0; i<n; i++) { if((eps[i]->d_name[0] != '.') && test(fs.join(path,eps[i]->d_name).c_str())) list.push_back(eps[i]->d_name); free(eps[i]); } free(eps); } #endif return list; }
TEST(dirent, scandir_scandir64) { // Get everything from /proc/self... dirent** entries; int entry_count = scandir("/proc/self", &entries, NULL, alphasort); ASSERT_GE(entry_count, 0); dirent64** entries64; int entry_count64 = scandir64("/proc/self", &entries64, NULL, alphasort64); ASSERT_EQ(entry_count, entry_count64); // Turn the directory entries into a set and vector of the names. std::set<std::string> name_set; std::vector<std::string> unsorted_name_list; ScanEntries(entries, entry_count, name_set, unsorted_name_list); // No duplicates. ASSERT_EQ(name_set.size(), unsorted_name_list.size()); // All entries sorted. std::vector<std::string> sorted_name_list(unsorted_name_list); std::sort(sorted_name_list.begin(), sorted_name_list.end()); ASSERT_EQ(sorted_name_list, unsorted_name_list); // scandir64 returned the same results as scandir. std::set<std::string> name_set64; std::vector<std::string> unsorted_name_list64; ScanEntries(entries64, entry_count64, name_set64, unsorted_name_list64); ASSERT_EQ(name_set, name_set64); ASSERT_EQ(unsorted_name_list, unsorted_name_list64); CheckProcSelf(name_set); }