unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) { const auto callback = [&parent_entry](unsigned* num_entries_out, const std::string& directory, const std::string& virtual_name) -> bool { FSTEntry entry; entry.virtualName = virtual_name; entry.physicalName = directory + DIR_SEP + virtual_name; if (IsDirectory(entry.physicalName)) { entry.isDirectory = true; // is a directory, lets go inside entry.size = ScanDirectoryTree(entry.physicalName, entry); *num_entries_out += (int)entry.size; } else { // is a file entry.isDirectory = false; entry.size = GetSize(entry.physicalName); } (*num_entries_out)++; // Push into the tree parent_entry.children.push_back(entry); return true; }; unsigned num_entries; return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; }
int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) { const auto callback = [&parent_entry](const std::string& directory, const std::string& virtual_name) -> int { FSTEntry entry; int found_entries = 0; entry.virtualName = virtual_name; entry.physicalName = directory + DIR_SEP + virtual_name; if (IsDirectory(entry.physicalName)) { entry.isDirectory = true; // is a directory, lets go inside entry.size = ScanDirectoryTree(entry.physicalName, entry); found_entries += (int)entry.size; } else { // is a file entry.isDirectory = false; entry.size = GetSize(entry.physicalName); } ++found_entries; // Push into the tree parent_entry.children.push_back(entry); return found_entries; }; return ScanDirectoryTreeAndCallback(directory, callback); }
// Scans the directory tree gets, starting from _Directory and adds the // results into parentEntry. Returns the number of files+directories found u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) { INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str()); // How many files + directories we found u32 foundEntries = 0; #ifdef _WIN32 // Find the first file in the directory. WIN32_FIND_DATA ffd; HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return foundEntries; } // windows loop do { FSTEntry entry; const std::string virtualName(TStrToUTF8(ffd.cFileName)); #else struct dirent dirent, *result = NULL; DIR *dirp = opendir(directory.c_str()); if (!dirp) return 0; // non windows loop while (!readdir_r(dirp, &dirent, &result) && result) { FSTEntry entry; const std::string virtualName(result->d_name); #endif // check for "." and ".." if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) continue; entry.virtualName = virtualName; entry.physicalName = directory; entry.physicalName += DIR_SEP + entry.virtualName; if (IsDirectory(entry.physicalName.c_str())) { entry.isDirectory = true; // is a directory, lets go inside entry.size = ScanDirectoryTree(entry.physicalName, entry); foundEntries += (u32)entry.size; } else { // is a file entry.isDirectory = false; entry.size = GetSize(entry.physicalName.c_str()); } ++foundEntries; // Push into the tree parentEntry.children.push_back(entry); #ifdef _WIN32 } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); #else } closedir(dirp); #endif // Return number of entries found. return foundEntries; }
// Recursive or non-recursive list of files and directories under directory. FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive) { INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str()); FSTEntry parent_entry; parent_entry.physicalName = directory; parent_entry.isDirectory = true; parent_entry.size = 0; #ifdef _WIN32 // Find the first file in the directory. WIN32_FIND_DATA ffd; HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return parent_entry; } // Windows loop do { const std::string virtual_name(TStrToUTF8(ffd.cFileName)); #else DIR* dirp = opendir(directory.c_str()); if (!dirp) return parent_entry; // non Windows loop while (dirent* result = readdir(dirp)) { const std::string virtual_name(result->d_name); #endif if (virtual_name == "." || virtual_name == "..") continue; auto physical_name = directory + DIR_SEP + virtual_name; FSTEntry entry; const FileInfo file_info(physical_name); entry.isDirectory = file_info.IsDirectory(); if (entry.isDirectory) { if (recursive) entry = ScanDirectoryTree(physical_name, true); else entry.size = 0; parent_entry.size += entry.size; } else { entry.size = file_info.GetSize(); } entry.virtualName = virtual_name; entry.physicalName = physical_name; ++parent_entry.size; // Push into the tree parent_entry.children.push_back(entry); #ifdef _WIN32 } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); #else } closedir(dirp); #endif return parent_entry; }