Skip to content

Commit

Permalink
GameList: Allow recursive scans to be cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
TellowKrinkle authored and lightningterror committed Mar 2, 2025
1 parent 8f19976 commit 7587581
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
26 changes: 16 additions & 10 deletions common/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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;
if (path)
Expand All @@ -1378,6 +1378,9 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
if (hFind == INVALID_HANDLE_VALUE)
return 0;

if (cancel && cancel->IsCancelled())
return 0;

// small speed optimization for '*' case
bool hasWildCards = false;
bool wildCardMatchAll = false;
Expand Down Expand Up @@ -1427,11 +1430,11 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
if (parent_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
{
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);
}
}
}
Expand Down Expand Up @@ -1494,7 +1497,7 @@ static u32 RecursiveFindFiles(const char* origin_path, const char* parent_path,
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
if (path[0] == '\0')
Expand All @@ -1514,7 +1517,7 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
}

// 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;

if (flags & FILESYSTEM_FIND_SORT_BY_NAME)
Expand Down Expand Up @@ -2046,7 +2049,7 @@ bool FileSystem::DeleteSymbolicLink(const char* path, Error* error)
static_assert(sizeof(off_t) == sizeof(s64));

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;
if (Path)
Expand All @@ -2065,6 +2068,9 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
if (!pDir)
return 0;

if (cancel && cancel->IsCancelled())
return 0;

// small speed optimization for '*' case
bool hasWildCards = false;
bool wildCardMatchAll = false;
Expand Down Expand Up @@ -2118,11 +2124,11 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
if (ParentPath)
{
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
{
nFiles += RecursiveFindFiles(OriginPath, Path, pDirEnt->d_name, Pattern, Flags, pResults, visited);
nFiles += RecursiveFindFiles(OriginPath, Path, pDirEnt->d_name, Pattern, Flags, pResults, visited, cancel);
}
}
}
Expand Down Expand Up @@ -2177,7 +2183,7 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
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
if (path[0] == '\0')
Expand All @@ -2197,7 +2203,7 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
}

// 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;

if (flags & FILESYSTEM_FIND_SORT_BY_NAME)
Expand Down
2 changes: 1 addition & 1 deletion common/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace FileSystem
std::vector<std::string> GetRootDirectoryList();

/// 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
bool StatFile(const char* path, struct stat* st);
Expand Down
9 changes: 4 additions & 5 deletions pcsx2/GameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,14 @@ void GameList::ScanDirectory(const char* path, bool recursive, bool only_cache,
progress->PushState();
progress->SetStatusText(fmt::format(
recursive ? TRANSLATE_FS("GameList", "Scanning directory {} (recursively)...") :
TRANSLATE_FS("GameList", "Scanning directory {}..."),
path)
.c_str());
TRANSLATE_FS("GameList", "Scanning directory {}..."),
path).c_str());

FileSystem::FindResultsArray files;
FileSystem::FindFiles(path, "*",
recursive ? (FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RECURSIVE) :
(FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES),
&files);
(FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES),
&files, progress);

u32 files_scanned = 0;
progress->SetProgressRange(static_cast<u32>(files.size()));
Expand Down

0 comments on commit 7587581

Please sign in to comment.