GameList: Allow recursive scans to be cancelled
Some checks are pending
🐧 Linux Builds / AppImage (push) Waiting to run
🐧 Linux Builds / Flatpak (push) Waiting to run
🍎 MacOS Builds / Defaults (push) Waiting to run
🖥️ Windows Builds / Lint VS Project Files (push) Waiting to run
🖥️ Windows Builds / SSE4 (push) Blocked by required conditions
🖥️ Windows Builds / AVX2 (push) Blocked by required conditions
🖥️ Windows Builds / CMake (push) Waiting to run

This commit is contained in:
TellowKrinkle 2025-02-25 22:33:04 -06:00 committed by lightningterror
parent 8f19976c10
commit 7587581d1f
3 changed files with 21 additions and 16 deletions

View File

@ -1354,7 +1354,7 @@ static u32 TranslateWin32Attributes(u32 Win32Attributes)
} }
static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path, const char* path, const char* pattern, static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path, const char* path, const char* pattern,
u32 flags, FileSystem::FindResultsArray* results, std::vector<std::string>& visited) u32 flags, FileSystem::FindResultsArray* results, std::vector<std::string>& visited, ProgressCallback* cancel)
{ {
std::string search_dir; std::string search_dir;
if (path) if (path)
@ -1378,6 +1378,9 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
return 0; return 0;
if (cancel && cancel->IsCancelled())
return 0;
// small speed optimization for '*' case // small speed optimization for '*' case
bool hasWildCards = false; bool hasWildCards = false;
bool wildCardMatchAll = false; bool wildCardMatchAll = false;
@ -1427,11 +1430,11 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
if (parent_path) if (parent_path)
{ {
const std::string recurse_dir = fmt::format("{}\\{}", parent_path, path); const std::string recurse_dir = fmt::format("{}\\{}", parent_path, path);
nFiles += RecursiveFindFiles(origin_path, recurse_dir.c_str(), utf8_filename.c_str(), pattern, flags, results, visited); nFiles += RecursiveFindFiles(origin_path, recurse_dir.c_str(), utf8_filename.c_str(), pattern, flags, results, visited, cancel);
} }
else else
{ {
nFiles += RecursiveFindFiles(origin_path, path, utf8_filename.c_str(), pattern, flags, results, visited); nFiles += RecursiveFindFiles(origin_path, path, utf8_filename.c_str(), pattern, flags, results, visited, cancel);
} }
} }
} }
@ -1494,7 +1497,7 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
return nFiles; return nFiles;
} }
bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results) bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results, ProgressCallback* cancel)
{ {
// has a path // has a path
if (path[0] == '\0') if (path[0] == '\0')
@ -1514,7 +1517,7 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
} }
// enter the recursive function // enter the recursive function
if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0) if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited, cancel) == 0)
return false; return false;
if (flags & FILESYSTEM_FIND_SORT_BY_NAME) if (flags & FILESYSTEM_FIND_SORT_BY_NAME)
@ -2046,7 +2049,7 @@ bool FileSystem::DeleteSymbolicLink(const char* path, Error* error)
static_assert(sizeof(off_t) == sizeof(s64)); static_assert(sizeof(off_t) == sizeof(s64));
static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, const char* Path, const char* Pattern, static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, const char* Path, const char* Pattern,
u32 Flags, FileSystem::FindResultsArray* pResults, std::vector<std::string>& visited) u32 Flags, FileSystem::FindResultsArray* pResults, std::vector<std::string>& visited, ProgressCallback* cancel)
{ {
std::string tempStr; std::string tempStr;
if (Path) if (Path)
@ -2065,6 +2068,9 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
if (!pDir) if (!pDir)
return 0; return 0;
if (cancel && cancel->IsCancelled())
return 0;
// small speed optimization for '*' case // small speed optimization for '*' case
bool hasWildCards = false; bool hasWildCards = false;
bool wildCardMatchAll = false; bool wildCardMatchAll = false;
@ -2118,11 +2124,11 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
if (ParentPath) if (ParentPath)
{ {
const std::string recursive_dir = fmt::format("{}/{}", ParentPath, Path); const std::string recursive_dir = fmt::format("{}/{}", ParentPath, Path);
nFiles += RecursiveFindFiles(OriginPath, recursive_dir.c_str(), pDirEnt->d_name, Pattern, Flags, pResults, visited); nFiles += RecursiveFindFiles(OriginPath, recursive_dir.c_str(), pDirEnt->d_name, Pattern, Flags, pResults, visited, cancel);
} }
else else
{ {
nFiles += RecursiveFindFiles(OriginPath, Path, pDirEnt->d_name, Pattern, Flags, pResults, visited); nFiles += RecursiveFindFiles(OriginPath, Path, pDirEnt->d_name, Pattern, Flags, pResults, visited, cancel);
} }
} }
} }
@ -2177,7 +2183,7 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
return nFiles; return nFiles;
} }
bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results) bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results, ProgressCallback* cancel)
{ {
// has a path // has a path
if (path[0] == '\0') if (path[0] == '\0')
@ -2197,7 +2203,7 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
} }
// enter the recursive function // enter the recursive function
if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0) if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited, cancel) == 0)
return false; return false;
if (flags & FILESYSTEM_FIND_SORT_BY_NAME) if (flags & FILESYSTEM_FIND_SORT_BY_NAME)

View File

@ -67,7 +67,7 @@ namespace FileSystem
std::vector<std::string> GetRootDirectoryList(); std::vector<std::string> GetRootDirectoryList();
/// Search for files /// Search for files
bool FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results); bool FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results, ProgressCallback* cancel = nullptr);
/// Stat file /// Stat file
bool StatFile(const char* path, struct stat* st); bool StatFile(const char* path, struct stat* st);

View File

@ -593,15 +593,14 @@ void GameList::ScanDirectory(const char* path, bool recursive, bool only_cache,
progress->PushState(); progress->PushState();
progress->SetStatusText(fmt::format( progress->SetStatusText(fmt::format(
recursive ? TRANSLATE_FS("GameList", "Scanning directory {} (recursively)...") : recursive ? TRANSLATE_FS("GameList", "Scanning directory {} (recursively)...") :
TRANSLATE_FS("GameList", "Scanning directory {}..."), TRANSLATE_FS("GameList", "Scanning directory {}..."),
path) path).c_str());
.c_str());
FileSystem::FindResultsArray files; FileSystem::FindResultsArray files;
FileSystem::FindFiles(path, "*", FileSystem::FindFiles(path, "*",
recursive ? (FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RECURSIVE) : recursive ? (FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RECURSIVE) :
(FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES), (FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES),
&files); &files, progress);
u32 files_scanned = 0; u32 files_scanned = 0;
progress->SetProgressRange(static_cast<u32>(files.size())); progress->SetProgressRange(static_cast<u32>(files.size()));