Io: Provide directory existence with listing.

Sometimes, you need to tell the difference between an empty directory and
one that doesn't exist at all.  We can do this in a single call.
This commit is contained in:
Unknown W. Brackets
2022-10-09 09:08:18 -07:00
parent 7b8350f8a8
commit 4942692558
11 changed files with 66 additions and 29 deletions

View File

@@ -661,8 +661,7 @@ static void tmFromFiletime(tm &dest, FILETIME &src)
}
#endif
std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)
{
std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(const std::string &path, bool *exists) {
std::vector<PSPFileInfo> myVector;
// TODO(scoped): Switch this over to GetFilesInDir!
@@ -681,9 +680,14 @@ std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)
hFind = FindFirstFileEx(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
#endif
if (hFind == INVALID_HANDLE_VALUE) {
if (exists)
*exists = false;
return myVector; //the empty list
}
if (exists)
*exists = true;
for (BOOL retval = 1; retval; retval = FindNextFile(hFind, &findData)) {
if (!wcscmp(findData.cFileName, L"..") || !wcscmp(findData.cFileName, L".")) {
continue;
@@ -697,6 +701,7 @@ std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)
}
entry.access = 0555;
entry.exists = true;
entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32);
entry.name = ConvertWStringToUTF8(findData.cFileName);
tmFromFiletime(entry.atime, findData.ftLastAccessTime);
@@ -717,18 +722,24 @@ std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)
DIR *dp = opendir(localPath.c_str());
#if HOST_IS_CASE_SENSITIVE
if(dp == NULL && FixPathCase(basePath, path, FPC_FILE_MUST_EXIST)) {
std::string fixedPath = path;
if(dp == NULL && FixPathCase(basePath, fixedPath, FPC_FILE_MUST_EXIST)) {
// May have failed due to case sensitivity, try again
localPath = GetLocalPath(path);
localPath = GetLocalPath(fixedPath);
dp = opendir(localPath.c_str());
}
#endif
if (dp == NULL) {
ERROR_LOG(FILESYS,"Error opening directory %s\n", path.c_str());
if (exists)
*exists = false;
return myVector;
}
if (exists)
*exists = true;
while ((dirp = readdir(dp)) != NULL) {
if (!strcmp(dirp->d_name, "..") || !strcmp(dirp->d_name, ".")) {
continue;
@@ -736,13 +747,14 @@ std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)
PSPFileInfo entry;
struct stat s;
std::string fullName = (GetLocalPath(path) / std::string(dirp->d_name)).ToString();
std::string fullName = (localPath / std::string(dirp->d_name)).ToString();
stat(fullName.c_str(), &s);
if (S_ISDIR(s.st_mode))
entry.type = FILETYPE_DIRECTORY;
else
entry.type = FILETYPE_NORMAL;
entry.access = 0555;
entry.exists = true;
entry.name = dirp->d_name;
entry.size = s.st_size;
localtime_r((time_t*)&s.st_atime,&entry.atime);