yuzu-emu
/
yuzu-mainline
Archived
1
0
Fork 0

Split up FileUtil::ScanDirectoryTree to be able to use callbacks for custom behavior

Converted FileUtil::ScanDirectoryTree and FileUtil::DeleteDirRecursively
to use the new ScanDirectoryTreeAndCallback function internally.
This commit is contained in:
archshift 2015-08-31 18:29:23 -07:00
parent bba12520c4
commit 7134a17fc6
2 changed files with 86 additions and 106 deletions

View File

@ -420,28 +420,23 @@ bool CreateEmptyFile(const std::string &filename)
} }
// Scans the directory tree gets, starting from _Directory and adds the int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback)
// results into parentEntry. Returns the number of files+directories found
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
{ {
LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str()); LOG_TRACE(Common_Filesystem, "directory %s", directory.c_str());
// How many files + directories we found // How many files + directories we found
u32 foundEntries = 0; int found_entries = 0;
#ifdef _WIN32 #ifdef _WIN32
// Find the first file in the directory. // Find the first file in the directory.
WIN32_FIND_DATA ffd; WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) if (handle_find == INVALID_HANDLE_VALUE) {
{ FindClose(handle_find);
FindClose(hFind); return found_entries;
return foundEntries;
} }
// windows loop // windows loop
do do {
{ const std::string virtual_name(Common::TStrToUTF8(ffd.cFileName));
FSTEntry entry;
const std::string virtualName(Common::TStrToUTF8(ffd.cFileName));
#else #else
struct dirent dirent, *result = nullptr; struct dirent dirent, *result = nullptr;
@ -450,115 +445,80 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
return 0; return 0;
// non windows loop // non windows loop
while (!readdir_r(dirp, &dirent, &result) && result) while (!readdir_r(dirp, &dirent, &result) && result) {
{ const std::string virtual_name(result->d_name);
FSTEntry entry;
const std::string virtualName(result->d_name);
#endif #endif
// check for "." and ".." // check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || if (((virtual_name[0] == '.') && (virtual_name[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') && ((virtual_name[0] == '.') && (virtual_name[1] == '.') &&
(virtualName[2] == '\0'))) (virtual_name[2] == '\0')))
continue; continue;
entry.virtualName = virtualName;
entry.physicalName = directory;
entry.physicalName += DIR_SEP + entry.virtualName;
if (IsDirectory(entry.physicalName.c_str())) int ret = callback(directory, virtual_name);
{ if (ret < 0) {
entry.isDirectory = true; if (ret != -1)
// is a directory, lets go inside found_entries = ret;
entry.size = ScanDirectoryTree(entry.physicalName, entry); break;
foundEntries += (u32)entry.size;
} }
else found_entries += ret;
{ // is a file
entry.isDirectory = false;
entry.size = GetSize(entry.physicalName.c_str());
}
++foundEntries;
// Push into the tree
parentEntry.children.push_back(entry);
#ifdef _WIN32 #ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0); } while (FindNextFile(handle_find, &ffd) != 0);
FindClose(hFind); FindClose(handle_find);
#else #else
} }
closedir(dirp); closedir(dirp);
#endif #endif
// Return number of entries found. // Return number of entries found.
return foundEntries; return found_entries;
}
int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry)
{
const auto callback = [&parent_entry](const std::string& directory,
const std::string& virtual_name) -> int {
FSTEntry entry;
int found_entries = 0;
entry.virtualName = virtual_name;
entry.physicalName = directory + DIR_SEP + virtual_name;
if (IsDirectory(entry.physicalName)) {
entry.isDirectory = true;
// is a directory, lets go inside
entry.size = ScanDirectoryTree(entry.physicalName, entry);
found_entries += (int)entry.size;
} else { // is a file
entry.isDirectory = false;
entry.size = GetSize(entry.physicalName);
}
++found_entries;
// Push into the tree
parent_entry.children.push_back(entry);
return found_entries;
};
return ScanDirectoryTreeAndCallback(directory, callback);
} }
// Deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string &directory) bool DeleteDirRecursively(const std::string &directory)
{ {
LOG_TRACE(Common_Filesystem, "%s", directory.c_str()); const static auto callback = [](const std::string& directory,
#ifdef _WIN32 const std::string& virtual_name) -> int {
// Find the first file in the directory. std::string new_path = directory + DIR_SEP_CHR + virtual_name;
WIN32_FIND_DATA ffd; if (IsDirectory(new_path)) {
HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (!DeleteDirRecursively(new_path)) {
return -2;
}
} else if (!Delete(new_path)) {
return -2;
}
return 0;
};
if (hFind == INVALID_HANDLE_VALUE) if (ScanDirectoryTreeAndCallback(directory, callback) == -2) {
{
FindClose(hFind);
return false; return false;
} }
// windows loop
do
{
const std::string virtualName(Common::TStrToUTF8(ffd.cFileName));
#else
struct dirent dirent, *result = nullptr;
DIR *dirp = opendir(directory.c_str());
if (!dirp)
return false;
// non windows loop
while (!readdir_r(dirp, &dirent, &result) && result)
{
const std::string virtualName = result->d_name;
#endif
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
std::string newPath = directory + DIR_SEP_CHR + virtualName;
if (IsDirectory(newPath))
{
if (!DeleteDirRecursively(newPath))
{
#ifndef _WIN32
closedir(dirp);
#endif
return false;
}
}
else
{
if (!FileUtil::Delete(newPath))
{
#ifndef _WIN32
closedir(dirp);
#endif
return false;
}
}
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
#else
}
closedir(dirp);
#endif
FileUtil::DeleteDir(directory); FileUtil::DeleteDir(directory);
return true; return true;

View File

@ -6,6 +6,7 @@
#include <array> #include <array>
#include <fstream> #include <fstream>
#include <functional>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <string> #include <string>
@ -96,9 +97,28 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename);
// creates an empty file filename, returns true on success // creates an empty file filename, returns true on success
bool CreateEmptyFile(const std::string &filename); bool CreateEmptyFile(const std::string &filename);
// Scans the directory tree gets, starting from _Directory and adds the /**
// results into parentEntry. Returns the number of files+directories found * Scans the directory tree, calling the callback for each file/directory found.
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry); * The callback must return the number of files and directories which the provided path contains.
* If the callback's return value is -1, the callback loop is broken immediately.
* If the callback's return value is otherwise negative, the callback loop is broken immediately
* and the callback's return value is returned from this function (to allow for error handling).
* @param directory the parent directory to start scanning from
* @param callback The callback which will be called for each file/directory. It is called
* with the arguments (const std::string& directory, const std::string& virtual_name).
* The `directory `parameter is the path to the directory which contains the file/directory.
* The `virtual_name` parameter is the incomplete file path, without any directory info.
* @return the total number of files/directories found
*/
int ScanDirectoryTreeAndCallback(const std::string &directory, std::function<int(const std::string&, const std::string&)> callback);
/**
* Scans the directory tree, storing the results.
* @param directory the parent directory to start scanning from
* @param parent_entry FSTEntry where the filesystem tree results will be stored.
* @return the total number of files/directories found
*/
int ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry);
// deletes the given directory and anything under it. Returns true on success. // deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string &directory); bool DeleteDirRecursively(const std::string &directory);