diff options
author | bunnei <bunneidev@gmail.com> | 2016-05-31 02:59:10 +0200 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2016-05-31 02:59:10 +0200 |
commit | 08e09184df753c9622df263e1731e1890afd3eb2 (patch) | |
tree | 7640df3c67947ff7ee9ef0d4003c33ac2fbf7c96 /src/common/file_util.cpp | |
parent | Merge pull request #1692 from Subv/rm_getpointer2 (diff) | |
parent | Common: Make recursive FileUtil functions take a maximum recursion (diff) | |
download | yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar.gz yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar.bz2 yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar.lz yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar.xz yuzu-08e09184df753c9622df263e1731e1890afd3eb2.tar.zst yuzu-08e09184df753c9622df263e1731e1890afd3eb2.zip |
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r-- | src/common/file_util.cpp | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 6e2867658..17af7c385 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -434,7 +434,7 @@ bool CreateEmptyFile(const std::string &filename) } -bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback) +bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback, unsigned int recursion) { LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); @@ -472,7 +472,7 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo continue; unsigned ret_entries = 0; - if (!callback(&ret_entries, directory, virtual_name)) { + if (!callback(&ret_entries, directory, virtual_name, recursion)) { callback_error = true; break; } @@ -486,30 +486,34 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo closedir(dirp); #endif - if (!callback_error) { - // num_entries_out is allowed to be specified nullptr, in which case we shouldn't try to set it - if (num_entries_out != nullptr) - *num_entries_out = found_entries; - return true; - } else { + if (callback_error) return false; - } + + // num_entries_out is allowed to be specified nullptr, in which case we shouldn't try to set it + if (num_entries_out != nullptr) + *num_entries_out = found_entries; + return true; } -unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) +unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry, unsigned int recursion) { const auto callback = [&parent_entry](unsigned* num_entries_out, const std::string& directory, - const std::string& virtual_name) -> bool { + const std::string& virtual_name, + unsigned int recursion) -> 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; + // is a directory, lets go inside if we didn't recurse to often + if (recursion > 0) { + entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1); + *num_entries_out += (int)entry.size; + } else { + entry.size = 0; + } } else { // is a file entry.isDirectory = false; entry.size = GetSize(entry.physicalName); @@ -522,23 +526,27 @@ unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) }; unsigned num_entries; - return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; + return ForeachDirectoryEntry(&num_entries, directory, callback, recursion) ? num_entries : 0; } -bool DeleteDirRecursively(const std::string &directory) +bool DeleteDirRecursively(const std::string &directory, unsigned int recursion) { const static auto callback = [](unsigned* num_entries_out, const std::string& directory, - const std::string& virtual_name) -> bool { + const std::string& virtual_name, + unsigned int recursion) -> bool { std::string new_path = directory + DIR_SEP_CHR + virtual_name; - if (IsDirectory(new_path)) - return DeleteDirRecursively(new_path); + if (IsDirectory(new_path)) { + if (recursion == 0) + return false; + return DeleteDirRecursively(new_path, recursion - 1); + } return Delete(new_path); }; - if (!ForeachDirectoryEntry(nullptr, directory, callback)) + if (!ForeachDirectoryEntry(nullptr, directory, callback, recursion)) return false; // Delete the outermost directory |