summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar bunnei2015-11-28 23:12:45 -0500
committerGravatar bunnei2015-11-28 23:12:45 -0500
commit48265fa94c808e0caea1f540fda0b8076c8db6cf (patch)
tree9091b7807c44f585957037466dcfc88116490118
parentMerge pull request #1254 from bunnei/fix-gl-uniforms (diff)
parentRefactor ScanDirectoryTreeAndCallback to separate errors and retvals (diff)
downloadyuzu-48265fa94c808e0caea1f540fda0b8076c8db6cf.tar.gz
yuzu-48265fa94c808e0caea1f540fda0b8076c8db6cf.tar.xz
yuzu-48265fa94c808e0caea1f540fda0b8076c8db6cf.zip
Merge pull request #1256 from archshift/refactor-scandir
Refactor ScanDirectoryTreeAndCallback to separate errors and retvals
-rw-r--r--src/citra_qt/game_list.cpp16
-rw-r--r--src/common/file_util.cpp72
-rw-r--r--src/common/file_util.h31
3 files changed, 62 insertions, 57 deletions
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp
index e925f08a7..1f8d69a03 100644
--- a/src/citra_qt/game_list.cpp
+++ b/src/citra_qt/game_list.cpp
@@ -119,13 +119,14 @@ void GameList::LoadInterfaceLayout(QSettings& settings)
119 119
120void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan) 120void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan)
121{ 121{
122 const auto callback = [&](const std::string& directory, 122 const auto callback = [&](unsigned* num_entries_out,
123 const std::string& virtual_name) -> int { 123 const std::string& directory,
124 const std::string& virtual_name) -> bool {
124 125
125 std::string physical_name = directory + DIR_SEP + virtual_name; 126 std::string physical_name = directory + DIR_SEP + virtual_name;
126 127
127 if (stop_processing) 128 if (stop_processing)
128 return -1; // A negative return value breaks the callback loop. 129 return false; // Breaks the callback loop.
129 130
130 if (deep_scan && FileUtil::IsDirectory(physical_name)) { 131 if (deep_scan && FileUtil::IsDirectory(physical_name)) {
131 AddFstEntriesToGameList(physical_name, true); 132 AddFstEntriesToGameList(physical_name, true);
@@ -135,11 +136,11 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool d
135 136
136 Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension); 137 Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension);
137 if (guessed_filetype == Loader::FileType::Unknown) 138 if (guessed_filetype == Loader::FileType::Unknown)
138 return 0; 139 return true;
139 Loader::FileType filetype = Loader::IdentifyFile(physical_name); 140 Loader::FileType filetype = Loader::IdentifyFile(physical_name);
140 if (filetype == Loader::FileType::Unknown) { 141 if (filetype == Loader::FileType::Unknown) {
141 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str()); 142 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str());
142 return 0; 143 return true;
143 } 144 }
144 if (guessed_filetype != filetype) { 145 if (guessed_filetype != filetype) {
145 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str()); 146 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
152 }); 153 });
153 } 154 }
154 155
155 return 0; // We don't care about the found entries 156 return true;
156 }; 157 };
157 FileUtil::ScanDirectoryTreeAndCallback(dir_path, callback); 158
159 FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback);
158} 160}
159 161
160void GameListWorker::run() 162void GameListWorker::run()
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 1e0d33313..4c7113390 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -420,11 +420,13 @@ bool CreateEmptyFile(const std::string &filename)
420} 420}
421 421
422 422
423int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback) 423bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback)
424{ 424{
425 LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); 425 LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str());
426
426 // How many files + directories we found 427 // How many files + directories we found
427 int found_entries = 0; 428 unsigned found_entries = 0;
429
428#ifdef _WIN32 430#ifdef _WIN32
429 // Find the first file in the directory. 431 // Find the first file in the directory.
430 WIN32_FIND_DATA ffd; 432 WIN32_FIND_DATA ffd;
@@ -432,7 +434,7 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
432 HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); 434 HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd);
433 if (handle_find == INVALID_HANDLE_VALUE) { 435 if (handle_find == INVALID_HANDLE_VALUE) {
434 FindClose(handle_find); 436 FindClose(handle_find);
435 return found_entries; 437 return false;
436 } 438 }
437 // windows loop 439 // windows loop
438 do { 440 do {
@@ -442,25 +444,20 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
442 444
443 DIR *dirp = opendir(directory.c_str()); 445 DIR *dirp = opendir(directory.c_str());
444 if (!dirp) 446 if (!dirp)
445 return 0; 447 return false;
446 448
447 // non windows loop 449 // non windows loop
448 while (!readdir_r(dirp, &dirent, &result) && result) { 450 while (!readdir_r(dirp, &dirent, &result) && result) {
449 const std::string virtual_name(result->d_name); 451 const std::string virtual_name(result->d_name);
450#endif 452#endif
451 // check for "." and ".." 453
452 if (((virtual_name[0] == '.') && (virtual_name[1] == '\0')) || 454 if (virtual_name == "." || virtual_name == "..")
453 ((virtual_name[0] == '.') && (virtual_name[1] == '.') &&
454 (virtual_name[2] == '\0')))
455 continue; 455 continue;
456 456
457 int ret = callback(directory, virtual_name); 457 unsigned ret_entries;
458 if (ret < 0) { 458 if (!callback(&ret_entries, directory, virtual_name))
459 if (ret != -1)
460 found_entries = ret;
461 break; 459 break;
462 } 460 found_entries += ret_entries;
463 found_entries += ret;
464 461
465#ifdef _WIN32 462#ifdef _WIN32
466 } while (FindNextFile(handle_find, &ffd) != 0); 463 } while (FindNextFile(handle_find, &ffd) != 0);
@@ -469,16 +466,18 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
469 } 466 }
470 closedir(dirp); 467 closedir(dirp);
471#endif 468#endif
472 // Return number of entries found. 469
473 return found_entries; 470 // num_entries_out is allowed to be specified nullptr, in which case we shouldn't try to set it
471 if (num_entries_out != nullptr)
472 *num_entries_out = found_entries;
474} 473}
475 474
476int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) 475unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry)
477{ 476{
478 const auto callback = [&parent_entry](const std::string& directory, 477 const auto callback = [&parent_entry](unsigned* num_entries_out,
479 const std::string& virtual_name) -> int { 478 const std::string& directory,
479 const std::string& virtual_name) -> bool {
480 FSTEntry entry; 480 FSTEntry entry;
481 int found_entries = 0;
482 entry.virtualName = virtual_name; 481 entry.virtualName = virtual_name;
483 entry.physicalName = directory + DIR_SEP + virtual_name; 482 entry.physicalName = directory + DIR_SEP + virtual_name;
484 483
@@ -486,41 +485,40 @@ int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry)
486 entry.isDirectory = true; 485 entry.isDirectory = true;
487 // is a directory, lets go inside 486 // is a directory, lets go inside
488 entry.size = ScanDirectoryTree(entry.physicalName, entry); 487 entry.size = ScanDirectoryTree(entry.physicalName, entry);
489 found_entries += (int)entry.size; 488 *num_entries_out += (int)entry.size;
490 } else { // is a file 489 } else { // is a file
491 entry.isDirectory = false; 490 entry.isDirectory = false;
492 entry.size = GetSize(entry.physicalName); 491 entry.size = GetSize(entry.physicalName);
493 } 492 }
494 ++found_entries; 493 (*num_entries_out)++;
494
495 // Push into the tree 495 // Push into the tree
496 parent_entry.children.push_back(entry); 496 parent_entry.children.push_back(entry);
497 return found_entries; 497 return true;
498 }; 498 };
499 499
500 return ScanDirectoryTreeAndCallback(directory, callback); 500 unsigned num_entries;
501 return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0;
501} 502}
502 503
503 504
504bool DeleteDirRecursively(const std::string &directory) 505bool DeleteDirRecursively(const std::string &directory)
505{ 506{
506 const static auto callback = [](const std::string& directory, 507 const static auto callback = [](unsigned* num_entries_out,
507 const std::string& virtual_name) -> int { 508 const std::string& directory,
509 const std::string& virtual_name) -> bool {
508 std::string new_path = directory + DIR_SEP_CHR + virtual_name; 510 std::string new_path = directory + DIR_SEP_CHR + virtual_name;
509 if (IsDirectory(new_path)) { 511 if (IsDirectory(new_path))
510 if (!DeleteDirRecursively(new_path)) { 512 return DeleteDirRecursively(new_path);
511 return -2; 513
512 } 514 return Delete(new_path);
513 } else if (!Delete(new_path)) {
514 return -2;
515 }
516 return 0;
517 }; 515 };
518 516
519 if (ScanDirectoryTreeAndCallback(directory, callback) == -2) { 517 if (!ForeachDirectoryEntry(nullptr, directory, callback))
520 return false; 518 return false;
521 }
522 FileUtil::DeleteDir(directory);
523 519
520 // Delete the outermost directory
521 FileUtil::DeleteDir(directory);
524 return true; 522 return true;
525} 523}
526 524
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 3d617f573..a85121aa6 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -98,19 +98,24 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename);
98bool CreateEmptyFile(const std::string &filename); 98bool CreateEmptyFile(const std::string &filename);
99 99
100/** 100/**
101 * Scans the directory tree, calling the callback for each file/directory found. 101 * @param num_entries_out to be assigned by the callable with the number of iterated directory entries, never null
102 * The callback must return the number of files and directories which the provided path contains. 102 * @param directory the path to the enclosing directory
103 * If the callback's return value is -1, the callback loop is broken immediately. 103 * @param virtual_name the entry name, without any preceding directory info
104 * If the callback's return value is otherwise negative, the callback loop is broken immediately 104 * @return whether handling the entry succeeded
105 * and the callback's return value is returned from this function (to allow for error handling). 105 */
106 * @param directory the parent directory to start scanning from 106using DirectoryEntryCallable = std::function<bool(unsigned* num_entries_out,
107 * @param callback The callback which will be called for each file/directory. It is called 107 const std::string& directory,
108 * with the arguments (const std::string& directory, const std::string& virtual_name). 108 const std::string& virtual_name)>;
109 * The `directory `parameter is the path to the directory which contains the file/directory. 109
110 * The `virtual_name` parameter is the incomplete file path, without any directory info. 110/**
111 * @return the total number of files/directories found 111 * Scans a directory, calling the callback for each file/directory contained within.
112 * If the callback returns failure, scanning halts and this function returns failure as well
113 * @param num_entries_out assigned by the function with the number of iterated directory entries, can be null
114 * @param directory the directory to scan
115 * @param callback The callback which will be called for each entry
116 * @return whether scanning the directory succeeded
112 */ 117 */
113int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback); 118bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback);
114 119
115/** 120/**
116 * Scans the directory tree, storing the results. 121 * Scans the directory tree, storing the results.
@@ -118,7 +123,7 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
118 * @param parent_entry FSTEntry where the filesystem tree results will be stored. 123 * @param parent_entry FSTEntry where the filesystem tree results will be stored.
119 * @return the total number of files/directories found 124 * @return the total number of files/directories found
120 */ 125 */
121int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry); 126unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry);
122 127
123// deletes the given directory and anything under it. Returns true on success. 128// deletes the given directory and anything under it. Returns true on success.
124bool DeleteDirRecursively(const std::string &directory); 129bool DeleteDirRecursively(const std::string &directory);