mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-30 21:30:04 +00:00 
			
		
		
		
	Merge pull request #1256 from archshift/refactor-scandir
Refactor ScanDirectoryTreeAndCallback to separate errors and retvals
This commit is contained in:
		
						commit
						48265fa94c
					
				
					 3 changed files with 62 additions and 57 deletions
				
			
		|  | @ -119,13 +119,14 @@ void GameList::LoadInterfaceLayout(QSettings& settings) | ||||||
| 
 | 
 | ||||||
| void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan) | void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan) | ||||||
| { | { | ||||||
|     const auto callback = [&](const std::string& directory, |     const auto callback = [&](unsigned* num_entries_out, | ||||||
|                                      const std::string& virtual_name) -> int { |                               const std::string& directory, | ||||||
|  |                               const std::string& virtual_name) -> bool { | ||||||
| 
 | 
 | ||||||
|         std::string physical_name = directory + DIR_SEP + virtual_name; |         std::string physical_name = directory + DIR_SEP + virtual_name; | ||||||
| 
 | 
 | ||||||
|         if (stop_processing) |         if (stop_processing) | ||||||
|             return -1; // A negative return value breaks the callback loop.
 |             return false; // Breaks the callback loop.
 | ||||||
| 
 | 
 | ||||||
|         if (deep_scan && FileUtil::IsDirectory(physical_name)) { |         if (deep_scan && FileUtil::IsDirectory(physical_name)) { | ||||||
|             AddFstEntriesToGameList(physical_name, true); |             AddFstEntriesToGameList(physical_name, true); | ||||||
|  | @ -135,11 +136,11 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool d | ||||||
| 
 | 
 | ||||||
|             Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension); |             Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension); | ||||||
|             if (guessed_filetype == Loader::FileType::Unknown) |             if (guessed_filetype == Loader::FileType::Unknown) | ||||||
|                 return 0; |                 return true; | ||||||
|             Loader::FileType filetype = Loader::IdentifyFile(physical_name); |             Loader::FileType filetype = Loader::IdentifyFile(physical_name); | ||||||
|             if (filetype == Loader::FileType::Unknown) { |             if (filetype == Loader::FileType::Unknown) { | ||||||
|                 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str()); |                 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str()); | ||||||
|                 return 0; |                 return true; | ||||||
|             } |             } | ||||||
|             if (guessed_filetype != filetype) { |             if (guessed_filetype != filetype) { | ||||||
|                 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str()); |                 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str()); | ||||||
|  | @ -152,9 +153,10 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool d | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return 0; // We don't care about the found entries
 |         return true; | ||||||
|     }; |     }; | ||||||
|     FileUtil::ScanDirectoryTreeAndCallback(dir_path, callback); | 
 | ||||||
|  |     FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void GameListWorker::run() | void GameListWorker::run() | ||||||
|  |  | ||||||
|  | @ -420,11 +420,13 @@ bool CreateEmptyFile(const std::string &filename) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback) | bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback) | ||||||
| { | { | ||||||
|     LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); |     LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); | ||||||
|  | 
 | ||||||
|     // How many files + directories we found
 |     // How many files + directories we found
 | ||||||
|     int found_entries = 0; |     unsigned found_entries = 0; | ||||||
|  | 
 | ||||||
| #ifdef _WIN32 | #ifdef _WIN32 | ||||||
|     // Find the first file in the directory.
 |     // Find the first file in the directory.
 | ||||||
|     WIN32_FIND_DATA ffd; |     WIN32_FIND_DATA ffd; | ||||||
|  | @ -432,7 +434,7 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int | ||||||
|     HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); |     HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); | ||||||
|     if (handle_find == INVALID_HANDLE_VALUE) { |     if (handle_find == INVALID_HANDLE_VALUE) { | ||||||
|         FindClose(handle_find); |         FindClose(handle_find); | ||||||
|         return found_entries; |         return false; | ||||||
|     } |     } | ||||||
|     // windows loop
 |     // windows loop
 | ||||||
|     do { |     do { | ||||||
|  | @ -442,25 +444,20 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int | ||||||
| 
 | 
 | ||||||
|     DIR *dirp = opendir(directory.c_str()); |     DIR *dirp = opendir(directory.c_str()); | ||||||
|     if (!dirp) |     if (!dirp) | ||||||
|         return 0; |         return false; | ||||||
| 
 | 
 | ||||||
|     // non windows loop
 |     // non windows loop
 | ||||||
|     while (!readdir_r(dirp, &dirent, &result) && result) { |     while (!readdir_r(dirp, &dirent, &result) && result) { | ||||||
|         const std::string virtual_name(result->d_name); |         const std::string virtual_name(result->d_name); | ||||||
| #endif | #endif | ||||||
|         // check for "." and ".."
 | 
 | ||||||
|         if (((virtual_name[0] == '.') && (virtual_name[1] == '\0')) || |         if (virtual_name == "." || virtual_name == "..") | ||||||
|                 ((virtual_name[0] == '.') && (virtual_name[1] == '.') && |  | ||||||
|                  (virtual_name[2] == '\0'))) |  | ||||||
|             continue; |             continue; | ||||||
| 
 | 
 | ||||||
|         int ret = callback(directory, virtual_name); |         unsigned ret_entries; | ||||||
|         if (ret < 0) { |         if (!callback(&ret_entries, directory, virtual_name)) | ||||||
|             if (ret != -1) |  | ||||||
|                 found_entries = ret; |  | ||||||
|             break; |             break; | ||||||
|         } |         found_entries += ret_entries; | ||||||
|         found_entries += ret; |  | ||||||
| 
 | 
 | ||||||
| #ifdef _WIN32 | #ifdef _WIN32 | ||||||
|     } while (FindNextFile(handle_find, &ffd) != 0); |     } while (FindNextFile(handle_find, &ffd) != 0); | ||||||
|  | @ -469,16 +466,18 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int | ||||||
|     } |     } | ||||||
|     closedir(dirp); |     closedir(dirp); | ||||||
| #endif | #endif | ||||||
|     // Return number of entries found.
 | 
 | ||||||
|     return found_entries; |     // 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; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) | unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) | ||||||
| { | { | ||||||
|     const auto callback = [&parent_entry](const std::string& directory, |     const auto callback = [&parent_entry](unsigned* num_entries_out, | ||||||
|                                           const std::string& virtual_name) -> int { |                                           const std::string& directory, | ||||||
|  |                                           const std::string& virtual_name) -> bool { | ||||||
|         FSTEntry entry; |         FSTEntry entry; | ||||||
|         int found_entries = 0; |  | ||||||
|         entry.virtualName = virtual_name; |         entry.virtualName = virtual_name; | ||||||
|         entry.physicalName = directory + DIR_SEP + virtual_name; |         entry.physicalName = directory + DIR_SEP + virtual_name; | ||||||
| 
 | 
 | ||||||
|  | @ -486,41 +485,40 @@ int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) | ||||||
|             entry.isDirectory = true; |             entry.isDirectory = true; | ||||||
|             // is a directory, lets go inside
 |             // is a directory, lets go inside
 | ||||||
|             entry.size = ScanDirectoryTree(entry.physicalName, entry); |             entry.size = ScanDirectoryTree(entry.physicalName, entry); | ||||||
|             found_entries += (int)entry.size; |             *num_entries_out += (int)entry.size; | ||||||
|         } else { // is a file
 |         } else { // is a file
 | ||||||
|             entry.isDirectory = false; |             entry.isDirectory = false; | ||||||
|             entry.size = GetSize(entry.physicalName); |             entry.size = GetSize(entry.physicalName); | ||||||
|         } |         } | ||||||
|         ++found_entries; |         (*num_entries_out)++; | ||||||
|  | 
 | ||||||
|         // Push into the tree
 |         // Push into the tree
 | ||||||
|         parent_entry.children.push_back(entry); |         parent_entry.children.push_back(entry); | ||||||
|         return found_entries; |         return true; | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     return ScanDirectoryTreeAndCallback(directory, callback); |     unsigned num_entries; | ||||||
|  |     return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| bool DeleteDirRecursively(const std::string &directory) | bool DeleteDirRecursively(const std::string &directory) | ||||||
| { | { | ||||||
|     const static auto callback = [](const std::string& directory, |     const static auto callback = [](unsigned* num_entries_out, | ||||||
|                                     const std::string& virtual_name) -> int { |                                     const std::string& directory, | ||||||
|  |                                     const std::string& virtual_name) -> bool { | ||||||
|         std::string new_path = directory + DIR_SEP_CHR + virtual_name; |         std::string new_path = directory + DIR_SEP_CHR + virtual_name; | ||||||
|         if (IsDirectory(new_path)) { |         if (IsDirectory(new_path)) | ||||||
|             if (!DeleteDirRecursively(new_path)) { |             return DeleteDirRecursively(new_path); | ||||||
|                 return -2; | 
 | ||||||
|             } |         return Delete(new_path); | ||||||
|         } else if (!Delete(new_path)) { |  | ||||||
|             return -2; |  | ||||||
|         } |  | ||||||
|         return 0; |  | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     if (ScanDirectoryTreeAndCallback(directory, callback) == -2) { |     if (!ForeachDirectoryEntry(nullptr, directory, callback)) | ||||||
|         return false; |         return false; | ||||||
|     } |  | ||||||
|     FileUtil::DeleteDir(directory); |  | ||||||
| 
 | 
 | ||||||
|  |     // Delete the outermost directory
 | ||||||
|  |     FileUtil::DeleteDir(directory); | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -98,19 +98,24 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename); | ||||||
| bool CreateEmptyFile(const std::string &filename); | bool CreateEmptyFile(const std::string &filename); | ||||||
| 
 | 
 | ||||||
| /**
 | /**
 | ||||||
|  * Scans the directory tree, calling the callback for each file/directory found. |  * @param num_entries_out to be assigned by the callable with the number of iterated directory entries, never null | ||||||
|  * The callback must return the number of files and directories which the provided path contains. |  * @param directory the path to the enclosing directory | ||||||
|  * If the callback's return value is -1, the callback loop is broken immediately. |  * @param virtual_name the entry name, without any preceding directory info | ||||||
|  * If the callback's return value is otherwise negative, the callback loop is broken immediately |  * @return whether handling the entry succeeded | ||||||
|  * and the callback's return value is returned from this function (to allow for error handling). |  | ||||||
|  * @param directory the parent directory to start scanning from |  | ||||||
|  * @param callback The callback which will be called for each file/directory. It is called |  | ||||||
|  *     with the arguments (const std::string& directory, const std::string& virtual_name). |  | ||||||
|  *     The `directory `parameter is the path to the directory which contains the file/directory. |  | ||||||
|  *     The `virtual_name` parameter is the incomplete file path, without any directory info. |  | ||||||
|  * @return the total number of files/directories found |  | ||||||
|  */ |  */ | ||||||
| int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback); | using DirectoryEntryCallable = std::function<bool(unsigned* num_entries_out, | ||||||
|  |                                                  const std::string& directory, | ||||||
|  |                                                  const std::string& virtual_name)>; | ||||||
|  | 
 | ||||||
|  | /**
 | ||||||
|  |  * Scans a directory, calling the callback for each file/directory contained within. | ||||||
|  |  * If the callback returns failure, scanning halts and this function returns failure as well | ||||||
|  |  * @param num_entries_out assigned by the function with the number of iterated directory entries, can be null | ||||||
|  |  * @param directory the directory to scan | ||||||
|  |  * @param callback The callback which will be called for each entry | ||||||
|  |  * @return whether scanning the directory succeeded | ||||||
|  |  */ | ||||||
|  | bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback); | ||||||
| 
 | 
 | ||||||
| /**
 | /**
 | ||||||
|  * Scans the directory tree, storing the results. |  * Scans the directory tree, storing the results. | ||||||
|  | @ -118,7 +123,7 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int | ||||||
|  * @param parent_entry FSTEntry where the filesystem tree results will be stored. |  * @param parent_entry FSTEntry where the filesystem tree results will be stored. | ||||||
|  * @return the total number of files/directories found |  * @return the total number of files/directories found | ||||||
|  */ |  */ | ||||||
| int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry); | unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry); | ||||||
| 
 | 
 | ||||||
| // deletes the given directory and anything under it. Returns true on success.
 | // deletes the given directory and anything under it. Returns true on success.
 | ||||||
| bool DeleteDirRecursively(const std::string &directory); | bool DeleteDirRecursively(const std::string &directory); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue