summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp80
1 files changed, 44 insertions, 36 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 1e0d33313..052c0ecd6 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -420,11 +420,16 @@ 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
430 // Save the status of callback function
431 bool callback_error = false;
432
428#ifdef _WIN32 433#ifdef _WIN32
429 // Find the first file in the directory. 434 // Find the first file in the directory.
430 WIN32_FIND_DATA ffd; 435 WIN32_FIND_DATA ffd;
@@ -432,7 +437,7 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
432 HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); 437 HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd);
433 if (handle_find == INVALID_HANDLE_VALUE) { 438 if (handle_find == INVALID_HANDLE_VALUE) {
434 FindClose(handle_find); 439 FindClose(handle_find);
435 return found_entries; 440 return false;
436 } 441 }
437 // windows loop 442 // windows loop
438 do { 443 do {
@@ -442,25 +447,22 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
442 447
443 DIR *dirp = opendir(directory.c_str()); 448 DIR *dirp = opendir(directory.c_str());
444 if (!dirp) 449 if (!dirp)
445 return 0; 450 return false;
446 451
447 // non windows loop 452 // non windows loop
448 while (!readdir_r(dirp, &dirent, &result) && result) { 453 while (!readdir_r(dirp, &dirent, &result) && result) {
449 const std::string virtual_name(result->d_name); 454 const std::string virtual_name(result->d_name);
450#endif 455#endif
451 // check for "." and ".." 456
452 if (((virtual_name[0] == '.') && (virtual_name[1] == '\0')) || 457 if (virtual_name == "." || virtual_name == "..")
453 ((virtual_name[0] == '.') && (virtual_name[1] == '.') &&
454 (virtual_name[2] == '\0')))
455 continue; 458 continue;
456 459
457 int ret = callback(directory, virtual_name); 460 unsigned ret_entries;
458 if (ret < 0) { 461 if (!callback(&ret_entries, directory, virtual_name)) {
459 if (ret != -1) 462 callback_error = true;
460 found_entries = ret;
461 break; 463 break;
462 } 464 }
463 found_entries += ret; 465 found_entries += ret_entries;
464 466
465#ifdef _WIN32 467#ifdef _WIN32
466 } while (FindNextFile(handle_find, &ffd) != 0); 468 } while (FindNextFile(handle_find, &ffd) != 0);
@@ -469,16 +471,23 @@ int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int
469 } 471 }
470 closedir(dirp); 472 closedir(dirp);
471#endif 473#endif
472 // Return number of entries found. 474
473 return found_entries; 475 if (!callback_error) {
476 // num_entries_out is allowed to be specified nullptr, in which case we shouldn't try to set it
477 if (num_entries_out != nullptr)
478 *num_entries_out = found_entries;
479 return true;
480 } else {
481 return false;
482 }
474} 483}
475 484
476int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry) 485unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry)
477{ 486{
478 const auto callback = [&parent_entry](const std::string& directory, 487 const auto callback = [&parent_entry](unsigned* num_entries_out,
479 const std::string& virtual_name) -> int { 488 const std::string& directory,
489 const std::string& virtual_name) -> bool {
480 FSTEntry entry; 490 FSTEntry entry;
481 int found_entries = 0;
482 entry.virtualName = virtual_name; 491 entry.virtualName = virtual_name;
483 entry.physicalName = directory + DIR_SEP + virtual_name; 492 entry.physicalName = directory + DIR_SEP + virtual_name;
484 493
@@ -486,41 +495,40 @@ int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry)
486 entry.isDirectory = true; 495 entry.isDirectory = true;
487 // is a directory, lets go inside 496 // is a directory, lets go inside
488 entry.size = ScanDirectoryTree(entry.physicalName, entry); 497 entry.size = ScanDirectoryTree(entry.physicalName, entry);
489 found_entries += (int)entry.size; 498 *num_entries_out += (int)entry.size;
490 } else { // is a file 499 } else { // is a file
491 entry.isDirectory = false; 500 entry.isDirectory = false;
492 entry.size = GetSize(entry.physicalName); 501 entry.size = GetSize(entry.physicalName);
493 } 502 }
494 ++found_entries; 503 (*num_entries_out)++;
504
495 // Push into the tree 505 // Push into the tree
496 parent_entry.children.push_back(entry); 506 parent_entry.children.push_back(entry);
497 return found_entries; 507 return true;
498 }; 508 };
499 509
500 return ScanDirectoryTreeAndCallback(directory, callback); 510 unsigned num_entries;
511 return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0;
501} 512}
502 513
503 514
504bool DeleteDirRecursively(const std::string &directory) 515bool DeleteDirRecursively(const std::string &directory)
505{ 516{
506 const static auto callback = [](const std::string& directory, 517 const static auto callback = [](unsigned* num_entries_out,
507 const std::string& virtual_name) -> int { 518 const std::string& directory,
519 const std::string& virtual_name) -> bool {
508 std::string new_path = directory + DIR_SEP_CHR + virtual_name; 520 std::string new_path = directory + DIR_SEP_CHR + virtual_name;
509 if (IsDirectory(new_path)) { 521 if (IsDirectory(new_path))
510 if (!DeleteDirRecursively(new_path)) { 522 return DeleteDirRecursively(new_path);
511 return -2; 523
512 } 524 return Delete(new_path);
513 } else if (!Delete(new_path)) {
514 return -2;
515 }
516 return 0;
517 }; 525 };
518 526
519 if (ScanDirectoryTreeAndCallback(directory, callback) == -2) { 527 if (!ForeachDirectoryEntry(nullptr, directory, callback))
520 return false; 528 return false;
521 }
522 FileUtil::DeleteDir(directory);
523 529
530 // Delete the outermost directory
531 FileUtil::DeleteDir(directory);
524 return true; 532 return true;
525} 533}
526 534