From bea9f67c0281aaf31f1aa56ab82a46a7ff1ee538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Jul 2021 09:38:04 +0200 Subject: [PATCH] Fix assorted path issues --- Common/File/DiskFree.cpp | 17 ++++++-- Common/File/DiskFree.h | 5 ++- Core/Debugger/SymbolMap.cpp | 5 ++- Core/FileLoaders/DiskCachingFileLoader.cpp | 3 +- Core/FileSystems/DirectoryFileSystem.cpp | 50 +++++++--------------- Core/SaveState.cpp | 1 - GPU/Common/PresentationCommon.cpp | 1 - UI/MainScreen.cpp | 1 - 8 files changed, 36 insertions(+), 47 deletions(-) diff --git a/Common/File/DiskFree.cpp b/Common/File/DiskFree.cpp index 7c571d4a3d..3a5fb9e722 100644 --- a/Common/File/DiskFree.cpp +++ b/Common/File/DiskFree.cpp @@ -16,20 +16,29 @@ #include #include #endif +#include +#include "Common/Log.h" +#include "Common/File/Path.h" +#include "Common/File/AndroidStorage.h" #include "Common/Data/Encoding/Utf8.h" -bool free_disk_space(const std::string &dir, uint64_t &space) { +bool free_disk_space(const Path &path, uint64_t &space) { #ifdef _WIN32 - const std::wstring w32path = ConvertUTF8ToWString(dir); ULARGE_INTEGER free; - if (GetDiskFreeSpaceExW(w32path.c_str(), &free, nullptr, nullptr)) { + if (GetDiskFreeSpaceExW(path.ToWString().c_str(), &free, nullptr, nullptr)) { space = free.QuadPart; return true; } #else + if (path.Type() == PathType::CONTENT_URI) { + space = Android_GetFreeSpaceByContentUri(path.ToString()); + INFO_LOG(COMMON, "Free space at '%s': %" PRIu64, path.c_str(), space); + return space >= 0; + } + struct statvfs diskstat; - int res = statvfs(dir.c_str(), &diskstat); + int res = statvfs(path.c_str(), &diskstat); if (res == 0) { #ifndef __ANDROID__ diff --git a/Common/File/DiskFree.h b/Common/File/DiskFree.h index df671fa3ed..5405fc4dc4 100644 --- a/Common/File/DiskFree.h +++ b/Common/File/DiskFree.h @@ -1,6 +1,7 @@ #pragma once -#include #include -bool free_disk_space(const std::string &dir, uint64_t &space); +#include "Common/File/Path.h" + +bool free_disk_space(const Path &path, uint64_t &space); diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index 5b9fb0a818..16bd9a6715 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -70,7 +70,8 @@ bool SymbolMap::LoadSymbolMap(const Path &filename) { std::lock_guard guard(lock_); - // TODO(scoped): We're screwed here + // TODO(scoped): Use gzdopen instead. + #if defined(_WIN32) && defined(UNICODE) gzFile f = gzopen_w(filename.ToWString().c_str(), "r"); #else @@ -195,10 +196,10 @@ void SymbolMap::SaveSymbolMap(const Path &filename) const { return; } + // TODO(scoped): Use gzdopen #if defined(_WIN32) && defined(UNICODE) gzFile f = gzopen_w(filename.ToWString().c_str(), "w9"); #else - // TODO(scoped): Use gzdopen? If we care, otherwise just compress into a buffer. gzFile f = gzopen(filename.c_str(), "w9"); #endif diff --git a/Core/FileLoaders/DiskCachingFileLoader.cpp b/Core/FileLoaders/DiskCachingFileLoader.cpp index 275ce11cb6..06b140e0e5 100644 --- a/Core/FileLoaders/DiskCachingFileLoader.cpp +++ b/Core/FileLoaders/DiskCachingFileLoader.cpp @@ -754,9 +754,8 @@ u64 DiskCachingFileLoaderCache::FreeDiskSpace() { dir = GetSysDirectory(DIRECTORY_CACHE); } - // TODO(scoped): uint64_t result = 0; - if (free_disk_space(dir.ToString(), result)) { + if (free_disk_space(dir, result)) { return result; } diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index 031f43add3..f8c1841b1c 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -546,11 +546,6 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) { fullName = GetLocalPath(fullPath); #endif -/*#ifdef _WIN32 - return RemoveDirectory(fullName.c_str()) == TRUE; -#else - return 0 == rmdir(fullName.c_str()); -#endif*/ bool result = File::DeleteDirRecursively(fullName); return ReplayApplyDisk(ReplayAction::RMDIR, result, CoreTiming::GetGlobalTimeUs()) != 0; } @@ -582,14 +577,10 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & Path fullToPath = GetLocalPath(fullTo); -#ifdef _WIN32 - bool retValue = (MoveFileEx(fullFrom.ToWString().c_str(), fullToPath.ToWString().c_str(), 0) == TRUE); -#else - bool retValue = (0 == rename(fullFrom.c_str(), fullToPath.c_str())); -#endif + bool retValue = File::Rename(fullFrom, fullToPath); #if HOST_IS_CASE_SENSITIVE - if (! retValue) + if (!retValue) { // May have failed due to case sensitivity on FROM, so try again. Check error code? std::string fullFromPath = from; @@ -597,11 +588,7 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & return ReplayApplyDisk(ReplayAction::FILE_RENAME, -1, CoreTiming::GetGlobalTimeUs()); fullFrom = GetLocalPath(fullFromPath); -#ifdef _WIN32 - retValue = (MoveFile(fullFrom.c_str(), fullToPath.c_str()) == TRUE); -#else - retValue = (0 == rename(fullFrom.c_str(), fullToPath.c_str())); -#endif + retValue = File::Rename(fullFrom, fullToPath); } #endif @@ -611,27 +598,20 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & } bool DirectoryFileSystem::RemoveFile(const std::string &filename) { - Path fullName = GetLocalPath(filename); -#ifdef _WIN32 - bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - bool retValue = (0 == unlink(fullName.c_str())); -#endif + Path localPath = GetLocalPath(filename); + + bool retValue = File::Delete(localPath); #if HOST_IS_CASE_SENSITIVE - if (! retValue) + if (!retValue) { // May have failed due to case sensitivity, so try again. Try even if it fails? std::string fullNamePath = filename; if (!FixPathCase(basePath.ToString(), fullNamePath, FPC_FILE_MUST_EXIST)) return (bool)ReplayApplyDisk(ReplayAction::FILE_REMOVE, false, CoreTiming::GetGlobalTimeUs()); - fullName = GetLocalPath(fullNamePath); + localPath = GetLocalPath(fullNamePath); -#ifdef _WIN32 - retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - retValue = (0 == unlink(fullName.c_str())); -#endif + retValue = File::Delete(localPath); } #endif @@ -657,7 +637,7 @@ int DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const #else logError = (int)errno; #endif - ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile(%s): FAILED, %i - access = %d '%s'", filename.c_str(), logError, (int)access, errorString.c_str()); + ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile('%s'): FAILED, %d - access = %d '%s'", filename.c_str(), logError, (int)access, errorString.c_str()); return err; } else { #ifdef _WIN32 @@ -718,7 +698,7 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size, int &use size_t bytesRead = iter->second.hFile.Read(pointer,size); return bytesRead; } else { - //This shouldn't happen... + // This shouldn't happen... ERROR_LOG(FILESYS,"Cannot read file that hasn't been opened: %08x", handle); return 0; } @@ -731,8 +711,7 @@ size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) { EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) - { + if (iter != entries.end()) { size_t bytesWritten = iter->second.hFile.Write(pointer,size); return bytesWritten; } else { @@ -770,6 +749,9 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { return ReplayApplyDiskFileInfo(x, CoreTiming::GetGlobalTimeUs()); #endif } + + // TODO: Consolidate to just a File::GetFileInfo call. + x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL; x.exists = true; @@ -973,7 +955,7 @@ std::vector DirectoryFileSystem::GetDirListing(std::string path) { u64 DirectoryFileSystem::FreeSpace(const std::string &path) { uint64_t result = 0; - if (free_disk_space(GetLocalPath(path).ToString(), result)) { + if (free_disk_space(GetLocalPath(path), result)) { return ReplayApplyDisk64(ReplayAction::FREESPACE, result, CoreTiming::GetGlobalTimeUs()); } diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 9db711e466..6dbea6ac77 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -863,7 +863,6 @@ namespace SaveState case SAVESTATE_SAVE_SCREENSHOT: { int maxRes = g_Config.iInternalResolution > 2 ? 2 : -1; - // TODO(scoped): Pass the path properly into TakeGameScreenshot. tempResult = TakeGameScreenshot(op.filename, ScreenshotFormat::JPG, SCREENSHOT_DISPLAY, nullptr, nullptr, maxRes); callbackResult = tempResult ? Status::SUCCESS : Status::FAILURE; if (!tempResult) { diff --git a/GPU/Common/PresentationCommon.cpp b/GPU/Common/PresentationCommon.cpp index b6e9ecbdfc..3384302ea6 100644 --- a/GPU/Common/PresentationCommon.cpp +++ b/GPU/Common/PresentationCommon.cpp @@ -203,7 +203,6 @@ void PresentationCommon::CalculatePostShaderUniforms(int bufferWidth, int buffer static std::string ReadShaderSrc(const Path &filename) { size_t sz = 0; - // TODO(scoped): VFS paths not handled well. char *data = (char *)VFSReadFile(filename.c_str(), &sz); if (!data) { return ""; diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 8872fdd716..8596294209 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -786,7 +786,6 @@ void GameBrowser::Refresh() { // Add any pinned paths before other directories. auto pinnedPaths = GetPinnedPaths(); for (auto it = pinnedPaths.begin(), end = pinnedPaths.end(); it != end; ++it) { - // TODO(scoped): Hmm gameList_->Add(new DirButton(*it, GetBaseName((*it).ToString()), *gridStyle_, new UI::LinearLayoutParams(UI::FILL_PARENT, UI::FILL_PARENT)))-> OnClick.Handle(this, &GameBrowser::NavigateClick); }