summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
authorGravatar archshift2015-11-26 00:34:26 -0800
committerGravatar archshift2015-11-27 13:33:38 -0800
commitb3cfcf55ead41d0458f83d26d7258a1701356e3b (patch)
tree9091b7807c44f585957037466dcfc88116490118 /src/common/file_util.cpp
parentMerge pull request #1254 from bunnei/fix-gl-uniforms (diff)
downloadyuzu-b3cfcf55ead41d0458f83d26d7258a1701356e3b.tar.gz
yuzu-b3cfcf55ead41d0458f83d26d7258a1701356e3b.tar.xz
yuzu-b3cfcf55ead41d0458f83d26d7258a1701356e3b.zip
Refactor ScanDirectoryTreeAndCallback to separate errors and retvals
ScanDirectoryTreeAndCallback, before this change, coupled error/return codes and actual return values (number of entries found). This caused confusion and difficulty interpreting the precise way the function worked. Supersedes, and closes #1255.
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp72
1 files changed, 35 insertions, 37 deletions
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