Change Copy, Rename to use Path. Remove std::string version of Exists().

Buildfixes

Buildfix
This commit is contained in:
Henrik Rydgård 2021-05-09 15:02:46 +02:00
parent 0d80362c30
commit ae06499a0c
24 changed files with 176 additions and 128 deletions

View file

@ -75,7 +75,7 @@ const char syscpupresentfile[] = "/sys/devices/system/cpu/present";
std::string GetCPUString() { std::string GetCPUString() {
std::string procdata; std::string procdata;
bool readSuccess = File::ReadFileToString(true, procfile, procdata); bool readSuccess = File::ReadFileToString(true, Path(procfile), procdata);
std::istringstream file(procdata); std::istringstream file(procdata);
std::string cpu_string; std::string cpu_string;
@ -98,7 +98,7 @@ std::string GetCPUString() {
std::string GetCPUBrandString() { std::string GetCPUBrandString() {
std::string procdata; std::string procdata;
bool readSuccess = File::ReadFileToString(true, procfile, procdata); bool readSuccess = File::ReadFileToString(true, Path(procfile), procdata);
std::istringstream file(procdata); std::istringstream file(procdata);
std::string brand_string; std::string brand_string;
@ -128,7 +128,7 @@ unsigned char GetCPUImplementer()
unsigned char implementer = 0; unsigned char implementer = 0;
std::string procdata; std::string procdata;
if (!File::ReadFileToString(true, procfile, procdata)) if (!File::ReadFileToString(true, Path(procfile), procdata))
return 0; return 0;
std::istringstream file(procdata); std::istringstream file(procdata);
@ -151,7 +151,7 @@ unsigned short GetCPUPart()
unsigned short part = 0; unsigned short part = 0;
std::string procdata; std::string procdata;
if (!File::ReadFileToString(true, procfile, procdata)) if (!File::ReadFileToString(true, Path(procfile), procdata))
return 0; return 0;
std::istringstream file(procdata); std::istringstream file(procdata);
@ -173,7 +173,7 @@ bool CheckCPUFeature(const std::string& feature)
std::string line, marker = "Features\t: "; std::string line, marker = "Features\t: ";
std::string procdata; std::string procdata;
if (!File::ReadFileToString(true, procfile, procdata)) if (!File::ReadFileToString(true, Path(procfile), procdata))
return false; return false;
std::istringstream file(procdata); std::istringstream file(procdata);
while (std::getline(file, line)) while (std::getline(file, line))
@ -199,7 +199,7 @@ int GetCoreCount()
int cores = 1; int cores = 1;
std::string presentData; std::string presentData;
bool presentSuccess = File::ReadFileToString(true, syscpupresentfile, presentData); bool presentSuccess = File::ReadFileToString(true, Path(syscpupresentfile), presentData);
std::istringstream presentFile(presentData); std::istringstream presentFile(presentData);
if (presentSuccess) { if (presentSuccess) {
@ -213,7 +213,7 @@ int GetCoreCount()
} }
std::string procdata; std::string procdata;
if (!File::ReadFileToString(true, procfile, procdata)) if (!File::ReadFileToString(true, Path(procfile), procdata))
return 1; return 1;
std::istringstream file(procdata); std::istringstream file(procdata);

View file

@ -524,7 +524,7 @@ bool IniFile::Load(const Path &path)
// Open file // Open file
std::string data; std::string data;
if (!File::ReadFileToString(true, path.ToString().c_str(), data)) { if (!File::ReadFileToString(true, path, data)) {
return false; return false;
} }
std::stringstream sstream(data); std::stringstream sstream(data);

View file

@ -434,36 +434,60 @@ bool DeleteDir(const Path &path) {
} }
// renames file srcFilename to destFilename, returns true on success // renames file srcFilename to destFilename, returns true on success
bool Rename(const std::string &srcFilename, const std::string &destFilename) bool Rename(const Path &srcFilename, const Path &destFilename)
{ {
INFO_LOG(COMMON, "Rename: %s --> %s", if (srcFilename.Type() != destFilename.Type()) {
srcFilename.c_str(), destFilename.c_str()); // Impossible.
return false;
}
switch (srcFilename.Type()) {
case PathType::NATIVE:
break; // OK
default:
return false;
}
INFO_LOG(COMMON, "Rename: %s --> %s", srcFilename.c_str(), destFilename.c_str());
#if defined(_WIN32) && defined(UNICODE) #if defined(_WIN32) && defined(UNICODE)
std::wstring srcw = ConvertUTF8ToWString(srcFilename); std::wstring srcw = srcFilename.ToWString();
std::wstring destw = ConvertUTF8ToWString(destFilename); std::wstring destw = destFilename.ToWString();
if (_wrename(srcw.c_str(), destw.c_str()) == 0) if (_wrename(srcw.c_str(), destw.c_str()) == 0)
return true; return true;
#else #else
if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
return true; return true;
#endif #endif
ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s", ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s",
srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str()); srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str());
return false; return false;
} }
// copies file srcFilename to destFilename, returns true on success // copies file srcFilename to destFilename, returns true on success
bool Copy(const std::string &srcFilename, const std::string &destFilename) bool Copy(const Path &srcFilename, const Path &destFilename)
{ {
INFO_LOG(COMMON, "Copy: %s --> %s", switch (srcFilename.Type()) {
srcFilename.c_str(), destFilename.c_str()); case PathType::NATIVE:
break; // OK
default:
return false;
}
switch (destFilename.Type()) {
case PathType::NATIVE:
break; // OK
default:
return false;
}
INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str());
#ifdef _WIN32 #ifdef _WIN32
#if PPSSPP_PLATFORM(UWP) #if PPSSPP_PLATFORM(UWP)
if (CopyFile2(ConvertUTF8ToWString(srcFilename).c_str(), ConvertUTF8ToWString(destFilename).c_str(), nullptr)) if (CopyFile2(srcFilename.ToWString().c_str(), destFilename.ToWString().c_str(), nullptr))
return true; return true;
return false; return false;
#else #else
if (CopyFile(ConvertUTF8ToWString(srcFilename).c_str(), ConvertUTF8ToWString(destFilename).c_str(), FALSE)) if (CopyFile(srcFilename.ToWString().c_str(), destFilename.ToWString().c_str(), FALSE))
return true; return true;
#endif #endif
ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s",
@ -472,23 +496,21 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
#else #else
// buffer size // buffer size
#define BSIZE 1024 #define BSIZE 4096
char buffer[BSIZE]; char buffer[BSIZE];
// Open input file // Open input file
FILE *input = fopen(srcFilename.c_str(), "rb"); FILE *input = OpenCFile(srcFilename, "rb");
if (!input) if (!input) {
{
ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s", ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s",
srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str()); srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str());
return false; return false;
} }
// open output file // open output file
FILE *output = fopen(destFilename.c_str(), "wb"); FILE *output = OpenCFile(destFilename, "wb");
if (!output) if (!output) {
{
fclose(input); fclose(input);
ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s", ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s",
srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str()); srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str());
@ -496,14 +518,11 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
} }
// copy loop // copy loop
while (!feof(input)) while (!feof(input)) {
{
// read input // read input
int rnum = fread(buffer, sizeof(char), BSIZE, input); int rnum = fread(buffer, sizeof(char), BSIZE, input);
if (rnum != BSIZE) if (rnum != BSIZE) {
{ if (ferror(input) != 0) {
if (ferror(input) != 0)
{
ERROR_LOG(COMMON, ERROR_LOG(COMMON,
"Copy: failed reading from source, %s --> %s: %s", "Copy: failed reading from source, %s --> %s: %s",
srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str()); srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str());
@ -515,8 +534,7 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
// write output // write output
int wnum = fwrite(buffer, sizeof(char), rnum, output); int wnum = fwrite(buffer, sizeof(char), rnum, output);
if (wnum != rnum) if (wnum != rnum) {
{
ERROR_LOG(COMMON, ERROR_LOG(COMMON,
"Copy: failed writing to output, %s --> %s: %s", "Copy: failed writing to output, %s --> %s: %s",
srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str()); srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg().c_str());
@ -525,13 +543,23 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
return false; return false;
} }
} }
// close flushs // close flushes
fclose(input); fclose(input);
fclose(output); fclose(output);
return true; return true;
#endif #endif
} }
bool Move(const Path &srcFilename, const Path &destFilename) {
if (Rename(srcFilename, destFilename)) {
return true;
} else if (Copy(srcFilename, destFilename)) {
return Delete(srcFilename);
} else {
return false;
}
}
std::string GetDir(const std::string &path) { std::string GetDir(const std::string &path) {
if (path == "/") if (path == "/")
return path; return path;
@ -654,8 +682,7 @@ bool CreateEmptyFile(const Path &filename) {
} }
// 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 Path &directory) {
{
//Removed check, it prevents the UWP from deleting store downloads //Removed check, it prevents the UWP from deleting store downloads
INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory.c_str()); INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory.c_str());
@ -663,16 +690,13 @@ bool DeleteDirRecursively(const std::string &directory)
// 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(ConvertUTF8ToWString(directory + "\\*").c_str(), &ffd); HANDLE hFind = FindFirstFile((directory.ToWString() + L"\\*").c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
if (hFind == INVALID_HANDLE_VALUE)
{
return false; return false;
} }
// windows loop // windows loop
do do {
{
const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName); const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName);
#else #else
struct dirent *result = NULL; struct dirent *result = NULL;
@ -681,8 +705,7 @@ bool DeleteDirRecursively(const std::string &directory)
return false; return false;
// non windows loop // non windows loop
while ((result = readdir(dirp))) while ((result = readdir(dirp))) {
{
const std::string virtualName = result->d_name; const std::string virtualName = result->d_name;
#endif #endif
// check for "." and ".." // check for "." and ".."
@ -691,11 +714,9 @@ bool DeleteDirRecursively(const std::string &directory)
(virtualName[2] == '\0'))) (virtualName[2] == '\0')))
continue; continue;
std::string newPath = directory + DIR_SEP + virtualName; Path newPath = directory / virtualName;
if (IsDirectory(Path(newPath))) if (IsDirectory(Path(newPath))) {
{ if (!DeleteDirRecursively(newPath)) {
if (!DeleteDirRecursively(newPath))
{
#ifndef _WIN32 #ifndef _WIN32
closedir(dirp); closedir(dirp);
#else #else
@ -704,10 +725,8 @@ bool DeleteDirRecursively(const std::string &directory)
return false; return false;
} }
} }
else else {
{ if (!File::Delete(Path(newPath))) {
if (!File::Delete(Path(newPath)))
{
#ifndef _WIN32 #ifndef _WIN32
closedir(dirp); closedir(dirp);
#else #else
@ -959,6 +978,8 @@ bool ReadFileToString(bool text_file, const Path &filename, std::string &str) {
return success; return success;
} }
// This is an odd one, mainly used for asset reading, so doesn't really
// need to support Path.
uint8_t *ReadLocalFile(const char *filename, size_t * size) { uint8_t *ReadLocalFile(const char *filename, size_t * size) {
FILE *file = File::OpenCFile(Path(filename), "rb"); FILE *file = File::OpenCFile(Path(filename), "rb");
if (!file) { if (!file) {

View file

@ -44,7 +44,6 @@ FILE *OpenCFile(const Path &filename, const char *mode);
std::string ResolvePath(const std::string &path); std::string ResolvePath(const std::string &path);
// Returns true if file filename exists // Returns true if file filename exists
bool Exists(const std::string &path);
bool Exists(const Path &path); bool Exists(const Path &path);
// Returns true if file filename exists in directory path. // Returns true if file filename exists in directory path.
@ -89,11 +88,18 @@ bool DeleteDir(const Path &filename);
// 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 Path &directory); bool DeleteDirRecursively(const Path &directory);
// renames file srcFilename to destFilename, returns true on success // Renames file srcFilename to destFilename, returns true on success
bool Rename(const std::string &srcFilename, const std::string &destFilename); // Will usually only work with in the same partition or other unit of storage,
// so you might have to fall back to copy/delete.
bool Rename(const Path &srcFilename, const Path &destFilename);
// copies file srcFilename to destFilename, returns true on success // copies file srcFilename to destFilename, returns true on success
bool Copy(const std::string &srcFilename, const std::string &destFilename); bool Copy(const Path &srcFilename, const Path &destFilename);
// Tries to rename srcFilename to destFilename, if that fails,
// it tries to copy and delete the src if succeeded. If that fails too,
// returns false, otherwise returns true.
bool Move(const Path &srcFilename, const Path &destFilename);
// creates an empty file filename, returns true on success // creates an empty file filename, returns true on success
bool CreateEmptyFile(const Path &filename); bool CreateEmptyFile(const Path &filename);

View file

@ -4,7 +4,9 @@
#include "Common/Data/Encoding/Utf8.h" #include "Common/Data/Encoding/Utf8.h"
Path::Path(const std::string &str) { Path::Path(const std::string &str) {
if (startsWith(str, "http://") || startsWith(str, "https://")) { if (str.empty()) {
type_ = PathType::UNDEFINED;
} else if (startsWith(str, "http://") || startsWith(str, "https://")) {
type_ = PathType::HTTP; type_ = PathType::HTTP;
} else { } else {
type_ = PathType::NATIVE; type_ = PathType::NATIVE;
@ -51,6 +53,15 @@ Path Path::WithExtraExtension(const std::string &ext) const {
return Path(path_ + "." + ext); return Path(path_ + "." + ext);
} }
Path Path::WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const {
if (endsWithNoCase(path_, "." + oldExtension)) {
std::string newPath = path_.substr(path_.size() - oldExtension.size() - 1);
return Path(newPath + "." + newExtension);
} else {
return Path(*this);
}
}
std::string Path::GetFileExtension() const { std::string Path::GetFileExtension() const {
size_t pos = path_.rfind("."); size_t pos = path_.rfind(".");
if (pos == std::string::npos) { if (pos == std::string::npos) {

View file

@ -39,6 +39,10 @@ public:
type_ = PathType::UNDEFINED; type_ = PathType::UNDEFINED;
path_.clear(); path_.clear();
} }
size_t size() const {
return path_.size();
}
// WARNING: Unsafe usage. // WARNING: Unsafe usage.
const char *c_str() const { const char *c_str() const {
return path_.c_str(); return path_.c_str();
@ -54,6 +58,7 @@ public:
// File extension manipulation. // File extension manipulation.
Path WithExtraExtension(const std::string &ext) const; Path WithExtraExtension(const std::string &ext) const;
Path WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;
// Removes the last component. // Removes the last component.
Path Directory() const; Path Directory() const;

View file

@ -1212,7 +1212,7 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
}); });
iRunCount++; iRunCount++;
if (!File::Exists(currentDirectory)) if (!File::Exists(Path(currentDirectory)))
currentDirectory = ""; currentDirectory = "";
Section *log = iniFile.GetOrCreateSection(logSectionName); Section *log = iniFile.GetOrCreateSection(logSectionName);
@ -1249,7 +1249,7 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
for (auto it = pinnedPaths.begin(), end = pinnedPaths.end(); it != end; ++it) { for (auto it = pinnedPaths.begin(), end = pinnedPaths.end(); it != end; ++it) {
// Unpin paths that are deleted automatically. // Unpin paths that are deleted automatically.
const std::string &path = it->second; const std::string &path = it->second;
if (startsWith(path, "http://") || startsWith(path, "https://") || File::Exists(path)) { if (startsWith(path, "http://") || startsWith(path, "https://") || File::Exists(Path(path))) {
vPinnedPaths.push_back(File::ResolvePath(path)); vPinnedPaths.push_back(File::ResolvePath(path));
} }
} }

View file

@ -70,7 +70,7 @@
static bool FixFilenameCase(const std::string &path, std::string &filename) static bool FixFilenameCase(const std::string &path, std::string &filename)
{ {
// Are we lucky? // Are we lucky?
if (File::Exists(path + filename)) if (File::Exists(Path(path + filename)))
return true; return true;
size_t filenameSize = filename.size(); // size in bytes, not characters size_t filenameSize = filename.size(); // size in bytes, not characters
@ -512,7 +512,7 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) {
#if HOST_IS_CASE_SENSITIVE #if HOST_IS_CASE_SENSITIVE
// Maybe we're lucky? // Maybe we're lucky?
if (File::DeleteDirRecursively(fullName.ToString())) if (File::DeleteDirRecursively(fullName))
return (bool)ReplayApplyDisk(ReplayAction::RMDIR, true, CoreTiming::GetGlobalTimeUs()); return (bool)ReplayApplyDisk(ReplayAction::RMDIR, true, CoreTiming::GetGlobalTimeUs());
// Nope, fix case and try again. Should we try again? // Nope, fix case and try again. Should we try again?

View file

@ -87,12 +87,13 @@ static std::vector<PluginInfo> FindPlugins(const std::string &gameID, const std:
std::vector<PluginInfo> found; std::vector<PluginInfo> found;
for (auto subdir : pluginDirs) { for (auto subdir : pluginDirs) {
if (!subdir.isDirectory || !File::Exists(subdir.fullName + "/plugin.ini")) Path subdirFullName(subdir.fullName);
if (!subdir.isDirectory || !File::Exists(subdirFullName / "plugin.ini"))
continue; continue;
IniFile ini; IniFile ini;
if (!ini.Load(subdir.fullName + "/plugin.ini")) { if (!ini.Load(subdirFullName / "plugin.ini")) {
ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/plugin.ini", subdir.fullName.c_str()); ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/plugin.ini", subdirFullName.c_str());
continue; continue;
} }
@ -117,8 +118,8 @@ static std::vector<PluginInfo> FindPlugins(const std::string &gameID, const std:
std::set<std::string> langMatches; std::set<std::string> langMatches;
for (const std::string &subini : matches) { for (const std::string &subini : matches) {
if (!ini.Load(subdir.fullName + "/" + subini)) { if (!ini.Load(subdirFullName / subini)) {
ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/%s", subdir.fullName.c_str(), subini.c_str()); ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/%s", subdirFullName.c_str(), subini.c_str());
continue; continue;
} }
@ -132,8 +133,8 @@ static std::vector<PluginInfo> FindPlugins(const std::string &gameID, const std:
} }
for (const std::string &subini : langMatches) { for (const std::string &subini : langMatches) {
if (!ini.Load(subdir.fullName + "/" + subini)) { if (!ini.Load(subdirFullName / subini)) {
ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/%s", subdir.fullName.c_str(), subini.c_str()); ERROR_LOG(SYSTEM, "Failed to load plugin ini: %s/%s", subdirFullName.c_str(), subini.c_str());
continue; continue;
} }

View file

@ -19,6 +19,7 @@
#include <cstdio> #include <cstdio>
#include "Common/File/FileUtil.h" #include "Common/File/FileUtil.h"
#include "Common/File/Path.h"
#include "Common/StringUtils.h" #include "Common/StringUtils.h"
#include "Core/FileLoaders/CachingFileLoader.h" #include "Core/FileLoaders/CachingFileLoader.h"
#include "Core/FileLoaders/DiskCachingFileLoader.h" #include "Core/FileLoaders/DiskCachingFileLoader.h"
@ -101,20 +102,20 @@ IdentifiedFileType Identify_File(FileLoader *fileLoader) {
// First, check if it's a directory with an EBOOT.PBP in it. // First, check if it's a directory with an EBOOT.PBP in it.
if (fileLoader->IsDirectory()) { if (fileLoader->IsDirectory()) {
std::string filename = fileLoader->GetPath(); Path filename = Path(fileLoader->GetPath());
if (filename.size() > 4) { if (filename.size() > 4) {
// Check for existence of EBOOT.PBP, as required for "Directory games". // Check for existence of EBOOT.PBP, as required for "Directory games".
if (File::Exists((filename + "/EBOOT.PBP").c_str())) { if (File::Exists(filename / "EBOOT.PBP")) {
return IdentifiedFileType::PSP_PBP_DIRECTORY; return IdentifiedFileType::PSP_PBP_DIRECTORY;
} }
// check if it's a disc directory // check if it's a disc directory
if (File::Exists((filename + "/PSP_GAME").c_str())) { if (File::Exists(filename / "PSP_GAME")) {
return IdentifiedFileType::PSP_DISC_DIRECTORY; return IdentifiedFileType::PSP_DISC_DIRECTORY;
} }
// Not that, okay, let's guess it's a savedata directory if it has a param.sfo... // Not that, okay, let's guess it's a savedata directory if it has a param.sfo...
if (File::Exists((filename + "/PARAM.SFO").c_str())) { if (File::Exists(filename / "PARAM.SFO")) {
return IdentifiedFileType::PSP_SAVEDATA_DIRECTORY; return IdentifiedFileType::PSP_SAVEDATA_DIRECTORY;
} }
} }

View file

@ -449,15 +449,14 @@ bool Load_PSP_ELF_PBP(FileLoader *fileLoader, std::string *error_string) {
Path oldNamePrefix = savestateDir / StringFromFormat("%s_%d", homebrewName.c_str(), i); Path oldNamePrefix = savestateDir / StringFromFormat("%s_%d", homebrewName.c_str(), i);
Path oldIDPrefix = savestateDir / StringFromFormat("%s_1.00_%d", madeUpID.c_str(), i); Path oldIDPrefix = savestateDir / StringFromFormat("%s_1.00_%d", madeUpID.c_str(), i);
// TODO(scoped): ... if (oldIDPrefix != newPrefix && File::Exists(oldIDPrefix.WithExtraExtension("ppst")))
if (oldIDPrefix != newPrefix && File::Exists(oldIDPrefix.ToString() + ".ppst")) File::Rename(oldIDPrefix.WithExtraExtension("ppst"), newPrefix.WithExtraExtension("ppst"));
File::Rename(oldIDPrefix.ToString() + ".ppst", newPrefix.ToString() + ".ppst"); else if (File::Exists(oldNamePrefix.WithExtraExtension("ppst")))
else if (File::Exists(oldNamePrefix.ToString() + ".ppst")) File::Rename(oldNamePrefix.WithExtraExtension("ppst"), newPrefix.WithExtraExtension("ppst"));
File::Rename(oldNamePrefix.ToString() + ".ppst", newPrefix.ToString() + ".ppst"); if (oldIDPrefix != newPrefix && File::Exists(oldIDPrefix.WithExtraExtension("jpg")))
if (oldIDPrefix != newPrefix && File::Exists(oldIDPrefix.ToString() + ".jpg")) File::Rename(oldIDPrefix.WithExtraExtension("jpg"), newPrefix.WithExtraExtension("jpg"));
File::Rename(oldIDPrefix.ToString() + ".jpg", newPrefix.ToString() + ".jpg"); else if (File::Exists(oldNamePrefix.WithExtraExtension("jpg")))
else if (File::Exists(oldNamePrefix.ToString() + ".jpg")) File::Rename(oldNamePrefix.WithExtraExtension("jpg"), newPrefix.WithExtraExtension("jpg"));
File::Rename(oldNamePrefix.ToString() + ".jpg", newPrefix.ToString() + ".jpg");
} }
PSPLoaders_Shutdown(); PSPLoaders_Shutdown();

View file

@ -458,16 +458,16 @@ namespace SaveState
static void RenameIfExists(const Path &from, const Path &to) { static void RenameIfExists(const Path &from, const Path &to) {
if (File::Exists(from)) { if (File::Exists(from)) {
File::Rename(from.ToString(), to.ToString()); File::Rename(from, to);
} }
} }
static void SwapIfExists(const Path &from, const Path &to) { static void SwapIfExists(const Path &from, const Path &to) {
std::string temp = from.ToString() + ".tmp"; Path temp = from.WithExtraExtension("tmp");
if (File::Exists(from)) { if (File::Exists(from)) {
File::Rename(from.ToString(), temp); File::Rename(from, temp);
File::Rename(to.ToString(), from.ToString()); File::Rename(to, from);
File::Rename(temp, to.ToString()); File::Rename(temp, to);
} }
} }
@ -486,7 +486,7 @@ namespace SaveState
} else { } else {
DeleteIfExists(fn); DeleteIfExists(fn);
} }
File::Rename(fn.WithExtraExtension("tmp").ToString(), fn.ToString()); File::Rename(fn.WithExtraExtension("tmp"), fn);
} }
if (callback) { if (callback) {
callback(status, message, data); callback(status, message, data);

View file

@ -57,10 +57,12 @@ void TextureReplacer::NotifyConfigChanged() {
if (enabled_) { if (enabled_) {
basePath_ = Path(GetSysDirectory(DIRECTORY_TEXTURES)) / gameID_; basePath_ = Path(GetSysDirectory(DIRECTORY_TEXTURES)) / gameID_;
Path newTextureDir = basePath_ / NEW_TEXTURE_DIR;
// If we're saving, auto-create the directory. // If we're saving, auto-create the directory.
if (g_Config.bSaveNewTextures && !File::Exists((basePath_ / NEW_TEXTURE_DIR).ToString())) { if (g_Config.bSaveNewTextures && !File::Exists(newTextureDir)) {
File::CreateFullPath(basePath_ / NEW_TEXTURE_DIR); File::CreateFullPath(newTextureDir);
File::CreateEmptyFile(basePath_ / NEW_TEXTURE_DIR / ".nomedia"); File::CreateEmptyFile(newTextureDir / ".nomedia");
} }
enabled_ = File::Exists(basePath_) && File::IsDirectory(basePath_); enabled_ = File::Exists(basePath_) && File::IsDirectory(basePath_);

View file

@ -260,7 +260,7 @@ bool GameManager::InstallGame(const std::string &url, const std::string &fileNam
return false; return false;
} }
if (!File::Exists(fileName)) { if (!File::Exists(Path(fileName))) {
ERROR_LOG(HLE, "Game file '%s' doesn't exist", fileName.c_str()); ERROR_LOG(HLE, "Game file '%s' doesn't exist", fileName.c_str());
return false; return false;
} }
@ -634,9 +634,9 @@ bool GameManager::InstallGameOnThread(std::string url, std::string fileName, boo
} }
bool GameManager::InstallRawISO(const std::string &file, const std::string &originalName, bool deleteAfter) { bool GameManager::InstallRawISO(const std::string &file, const std::string &originalName, bool deleteAfter) {
std::string destPath = g_Config.currentDirectory + "/" + originalName; Path destPath = Path(g_Config.currentDirectory) / originalName;
// TODO: To save disk space, we should probably attempt a move first. // TODO: To save disk space, we should probably attempt a move first.
if (File::Copy(file, destPath)) { if (File::Copy(Path(file), destPath)) {
if (deleteAfter) { if (deleteAfter) {
File::Delete(Path(file)); File::Delete(Path(file));
} }

View file

@ -63,7 +63,7 @@ void CwCheatScreen::LoadCheatInfo() {
// We won't parse this, just using it to detect changes to the file. // We won't parse this, just using it to detect changes to the file.
std::string str; std::string str;
if (File::ReadFileToString(true, engine_->CheatFilename().c_str(), str)) { if (File::ReadFileToString(true, engine_->CheatFilename(), str)) {
fileCheckHash_ = XXH3_64bits(str.c_str(), str.size()); fileCheckHash_ = XXH3_64bits(str.c_str(), str.size());
} }
fileCheckCounter_ = 0; fileCheckCounter_ = 0;
@ -120,7 +120,7 @@ void CwCheatScreen::update() {
if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL && engine_) { if (fileCheckCounter_++ >= FILE_CHECK_FRAME_INTERVAL && engine_) {
// Check if the file has changed. If it has, we'll reload. // Check if the file has changed. If it has, we'll reload.
std::string str; std::string str;
if (File::ReadFileToString(true, engine_->CheatFilename().c_str(), str)) { if (File::ReadFileToString(true, engine_->CheatFilename(), str)) {
uint64_t newHash = XXH3_64bits(str.c_str(), str.size()); uint64_t newHash = XXH3_64bits(str.c_str(), str.size());
if (newHash != fileCheckHash_) { if (newHash != fileCheckHash_) {
// This will update the hash. // This will update the hash.
@ -176,7 +176,7 @@ UI::EventReturn CwCheatScreen::OnEditCheatFile(UI::EventParams &params) {
#if PPSSPP_PLATFORM(UWP) #if PPSSPP_PLATFORM(UWP)
LaunchBrowser(engine_->CheatFilename().c_str()); LaunchBrowser(engine_->CheatFilename().c_str());
#else #else
File::OpenFileInEditor(engine_->CheatFilename().ToString()); File::OpenFileInEditor(engine_->CheatFilename());
#endif #endif
} }
return UI::EVENT_DONE; return UI::EVENT_DONE;

View file

@ -404,9 +404,9 @@ public:
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png"); Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png");
// Try using png/jpg screenshots first // Try using png/jpg screenshots first
if (File::Exists(screenshot_png)) if (File::Exists(screenshot_png))
File::ReadFileToString(false, screenshot_png.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_png, info_->icon.data);
else if (File::Exists(screenshot_jpg)) else if (File::Exists(screenshot_jpg))
File::ReadFileToString(false, screenshot_jpg.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_jpg, info_->icon.data);
else else
// Read standard icon // Read standard icon
ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock); ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock);
@ -451,9 +451,9 @@ handleELF:
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png"); Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png");
// Try using png/jpg screenshots first // Try using png/jpg screenshots first
if (File::Exists(screenshot_png)) { if (File::Exists(screenshot_png)) {
File::ReadFileToString(false, screenshot_png.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_png, info_->icon.data);
} else if (File::Exists(screenshot_jpg)) { } else if (File::Exists(screenshot_jpg)) {
File::ReadFileToString(false, screenshot_jpg.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_jpg, info_->icon.data);
} else { } else {
// Read standard icon // Read standard icon
VERBOSE_LOG(LOADER, "Loading unknown.png because there was an ELF"); VERBOSE_LOG(LOADER, "Loading unknown.png because there was an ELF");
@ -492,9 +492,9 @@ handleELF:
std::lock_guard<std::mutex> guard(info_->lock); std::lock_guard<std::mutex> guard(info_->lock);
// Let's use the screenshot as an icon, too. // Let's use the screenshot as an icon, too.
std::string screenshotPath = ReplaceAll(gamePath_.ToString(), ".ppst", ".jpg"); Path screenshotPath = gamePath_.WithReplacedExtension("ppst", "jpg");
if (File::Exists(screenshotPath)) { if (File::Exists(screenshotPath)) {
if (File::ReadFileToString(false, screenshotPath.c_str(), info_->icon.data)) { if (File::ReadFileToString(false, Path(screenshotPath), info_->icon.data)) {
info_->icon.dataLoaded = true; info_->icon.dataLoaded = true;
} else { } else {
ERROR_LOG(G3D, "Error loading screenshot data: '%s'", screenshotPath.c_str()); ERROR_LOG(G3D, "Error loading screenshot data: '%s'", screenshotPath.c_str());
@ -579,9 +579,9 @@ handleELF:
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png"); Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png");
// Try using png/jpg screenshots first // Try using png/jpg screenshots first
if (File::Exists(screenshot_png)) if (File::Exists(screenshot_png))
File::ReadFileToString(false, screenshot_png.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_png, info_->icon.data);
else if (File::Exists(screenshot_jpg)) else if (File::Exists(screenshot_jpg))
File::ReadFileToString(false, screenshot_jpg.c_str(), info_->icon.data); File::ReadFileToString(false, screenshot_jpg, info_->icon.data);
else { else {
DEBUG_LOG(LOADER, "Loading unknown.png because no icon was found"); DEBUG_LOG(LOADER, "Loading unknown.png because no icon was found");
ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock); ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock);

View file

@ -421,7 +421,7 @@ void SetBackgroundPopupScreen::update() {
if (pic) { if (pic) {
const Path bgPng = GetSysDirectory(DIRECTORY_SYSTEM) / "background.png"; const Path bgPng = GetSysDirectory(DIRECTORY_SYSTEM) / "background.png";
File::WriteStringToFile(false, pic->data, bgPng.c_str()); File::WriteStringToFile(false, pic->data, bgPng);
} }
NativeMessageReceived("bgImage_updated", ""); NativeMessageReceived("bgImage_updated", "");

View file

@ -1120,7 +1120,7 @@ UI::EventReturn GameSettingsScreen::OnChangeMemStickDir(UI::EventParams &e) {
pendingMemstickFolder_ = newPath; pendingMemstickFolder_ = newPath;
std::string promptMessage = sy->T("ChangingMemstickPath", "Save games, save states, and other data will not be copied to this folder.\n\nChange the Memory Stick folder?"); std::string promptMessage = sy->T("ChangingMemstickPath", "Save games, save states, and other data will not be copied to this folder.\n\nChange the Memory Stick folder?");
if (!File::Exists(newPath)) { if (!File::Exists(Path(newPath))) {
promptMessage = sy->T("ChangingMemstickPathNotExists", "That folder doesn't exist yet.\n\nSave games, save states, and other data will not be copied to this folder.\n\nCreate a new Memory Stick folder?"); promptMessage = sy->T("ChangingMemstickPathNotExists", "That folder doesn't exist yet.\n\nSave games, save states, and other data will not be copied to this folder.\n\nCreate a new Memory Stick folder?");
} }
// Add the path for clarity and proper confirmation. // Add the path for clarity and proper confirmation.
@ -1299,16 +1299,16 @@ void GameSettingsScreen::CallbackMemstickFolder(bool yes) {
std::string testWriteFile = pendingMemstickFolder_ + "/.write_verify_file"; std::string testWriteFile = pendingMemstickFolder_ + "/.write_verify_file";
// Already, create away. // Already, create away.
if (!File::Exists(pendingMemstickFolder_)) { if (!File::Exists(Path(pendingMemstickFolder_))) {
File::CreateFullPath(Path(pendingMemstickFolder_)); File::CreateFullPath(Path(pendingMemstickFolder_));
} }
if (!File::WriteDataToFile(true, "1", 1, testWriteFile.c_str())) { if (!File::WriteDataToFile(true, "1", 1, Path(testWriteFile))) {
settingInfo_->Show(sy->T("ChangingMemstickPathInvalid", "That path couldn't be used to save Memory Stick files."), nullptr); settingInfo_->Show(sy->T("ChangingMemstickPathInvalid", "That path couldn't be used to save Memory Stick files."), nullptr);
return; return;
} }
File::Delete(Path(testWriteFile)); File::Delete(Path(testWriteFile));
File::WriteDataToFile(true, pendingMemstickFolder_.c_str(), (unsigned int)pendingMemstickFolder_.size(), memstickDirFile.c_str()); File::WriteDataToFile(true, pendingMemstickFolder_.c_str(), (unsigned int)pendingMemstickFolder_.size(), Path(memstickDirFile));
// Save so the settings, at least, are transferred. // Save so the settings, at least, are transferred.
g_Config.memStickDirectory = Path(pendingMemstickFolder_); g_Config.memStickDirectory = Path(pendingMemstickFolder_);
g_Config.Save("MemstickPathChanged"); g_Config.Save("MemstickPathChanged");
@ -1729,7 +1729,7 @@ UI::EventReturn DeveloperToolsScreen::OnOpenTexturesIniFile(UI::EventParams &e)
std::string gameID = g_paramSFO.GetDiscID(); std::string gameID = g_paramSFO.GetDiscID();
Path generatedFilename; Path generatedFilename;
if (TextureReplacer::GenerateIni(gameID, &generatedFilename)) { if (TextureReplacer::GenerateIni(gameID, &generatedFilename)) {
File::OpenFileInEditor(generatedFilename.ToString()); File::OpenFileInEditor(generatedFilename);
} }
return UI::EVENT_DONE; return UI::EVENT_DONE;
} }
@ -1772,8 +1772,8 @@ UI::EventReturn DeveloperToolsScreen::OnCopyStatesToRoot(UI::EventParams &e) {
GetFilesInDir(savestate_dir, &files, nullptr, 0); GetFilesInDir(savestate_dir, &files, nullptr, 0);
for (const File::FileInfo &file : files) { for (const File::FileInfo &file : files) {
std::string src = file.fullName; Path src = Path(file.fullName);
std::string dst = root_dir.ToString() + file.name; Path dst = root_dir / file.name;
INFO_LOG(SYSTEM, "Copying file '%s' to '%s'", src.c_str(), dst.c_str()); INFO_LOG(SYSTEM, "Copying file '%s' to '%s'", src.c_str(), dst.c_str());
File::Copy(src, dst); File::Copy(src, dst);
} }

View file

@ -613,11 +613,11 @@ void GameBrowser::Draw(UIContext &dc) {
} }
} }
static bool IsValidPBP(const std::string &path, bool allowHomebrew) { static bool IsValidPBP(const Path &path, bool allowHomebrew) {
if (!File::Exists(path)) if (!File::Exists(path))
return false; return false;
std::unique_ptr<FileLoader> loader(ConstructFileLoader(path)); std::unique_ptr<FileLoader> loader(ConstructFileLoader(path.ToString()));
PBPReader pbp(loader.get()); PBPReader pbp(loader.get());
std::vector<u8> sfoData; std::vector<u8> sfoData;
if (!pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) if (!pbp.GetSubFile(PBP_PARAM_SFO, &sfoData))
@ -719,11 +719,11 @@ void GameBrowser::Refresh() {
bool isGame = !fileInfo[i].isDirectory; bool isGame = !fileInfo[i].isDirectory;
bool isSaveData = false; bool isSaveData = false;
// Check if eboot directory // Check if eboot directory
if (!isGame && path_.GetPath().size() >= 4 && IsValidPBP(path_.GetPath() + fileInfo[i].name + "/EBOOT.PBP", true)) if (!isGame && path_.GetPath().size() >= 4 && IsValidPBP(Path(path_.GetPath()) / fileInfo[i].name / "EBOOT.PBP", true))
isGame = true; isGame = true;
else if (!isGame && File::Exists(path_.GetPath() + fileInfo[i].name + "/PSP_GAME/SYSDIR")) else if (!isGame && File::Exists(Path(path_.GetPath() + fileInfo[i].name + "/PSP_GAME/SYSDIR")))
isGame = true; isGame = true;
else if (!isGame && File::Exists(path_.GetPath() + fileInfo[i].name + "/PARAM.SFO")) else if (!isGame && File::Exists(Path(path_.GetPath() + fileInfo[i].name + "/PARAM.SFO")))
isSaveData = true; isSaveData = true;
if (!isGame && !isSaveData) { if (!isGame && !isSaveData) {

View file

@ -386,7 +386,7 @@ static void CheckFailedGPUBackends() {
if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) { if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
std::string data; std::string data;
if (File::ReadFileToString(true, cache.c_str(), data)) if (File::ReadFileToString(true, cache, data))
g_Config.sFailedGPUBackends = data; g_Config.sFailedGPUBackends = data;
} }
@ -414,7 +414,7 @@ static void CheckFailedGPUBackends() {
// Let's try to create, in case it doesn't exist. // Let's try to create, in case it doesn't exist.
if (!File::Exists(GetSysDirectory(DIRECTORY_APP_CACHE))) if (!File::Exists(GetSysDirectory(DIRECTORY_APP_CACHE)))
File::CreateDir(GetSysDirectory(DIRECTORY_APP_CACHE)); File::CreateDir(GetSysDirectory(DIRECTORY_APP_CACHE));
File::WriteStringToFile(true, g_Config.sFailedGPUBackends, cache.c_str()); File::WriteStringToFile(true, g_Config.sFailedGPUBackends, cache);
} else { } else {
// Just save immediately, since we have storage. // Just save immediately, since we have storage.
g_Config.Save("got storage permission"); g_Config.Save("got storage permission");
@ -499,13 +499,15 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch
g_Config.memStickDirectory = Path(external_dir); g_Config.memStickDirectory = Path(external_dir);
g_Config.flash0Directory = Path(external_dir) / "flash0"; g_Config.flash0Directory = Path(external_dir) / "flash0";
std::string memstickDirFile = g_Config.internalDataDirectory + "/memstick_dir.txt"; Path memstickDirFile = Path(g_Config.internalDataDirectory) / "memstick_dir.txt";
if (File::Exists(memstickDirFile)) { if (File::Exists(memstickDirFile)) {
std::string memstickDir; std::string memstickDir;
File::ReadFileToString(true, memstickDirFile.c_str(), memstickDir); File::ReadFileToString(true, memstickDirFile, memstickDir);
Path memstickPath(memstickDir); Path memstickPath(memstickDir);
if (!memstickPath.empty() && File::Exists(memstickPath)) { if (!memstickPath.empty() && File::Exists(memstickPath)) {
g_Config.memStickDirectory = memstickPath; g_Config.memStickDirectory = memstickPath;
} else {
ERROR_LOG(SYSTEM, "Couldn't read directory '%s' specified by memstick_dir.txt.", memstickDir.c_str());
} }
} }
#elif PPSSPP_PLATFORM(IOS) #elif PPSSPP_PLATFORM(IOS)
@ -1165,7 +1167,7 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) {
if (msg == "bgImage_updated") { if (msg == "bgImage_updated") {
if (!value.empty()) { if (!value.empty()) {
Path dest = GetSysDirectory(DIRECTORY_SYSTEM) / (endsWithNoCase(value, ".jpg") ? "background.jpg" : "background.png"); Path dest = GetSysDirectory(DIRECTORY_SYSTEM) / (endsWithNoCase(value, ".jpg") ? "background.jpg" : "background.png");
File::Copy(value, dest.ToString()); File::Copy(Path(value), dest);
} }
UIBackgroundShutdown(); UIBackgroundShutdown();
// It will init again automatically. We can't init outside a frame on Vulkan. // It will init again automatically. We can't init outside a frame on Vulkan.

View file

@ -145,7 +145,7 @@ bool RunTests() {
PSP_EndHostFrame(); PSP_EndHostFrame();
std::string expect_results; std::string expect_results;
if (!File::ReadFileToString(true, expectedFile.c_str(), expect_results)) { if (!File::ReadFileToString(true, expectedFile, expect_results)) {
ERROR_LOG(SYSTEM, "Error opening expectedFile %s", expectedFile.c_str()); ERROR_LOG(SYSTEM, "Error opening expectedFile %s", expectedFile.c_str());
break; break;
} }

View file

@ -278,7 +278,7 @@ bool CompareOutput(const std::string &bootFilename, const std::string &output, b
printf("%s", output.c_str()); printf("%s", output.c_str());
printf("============== expected output:\n"); printf("============== expected output:\n");
std::string fullExpected; std::string fullExpected;
if (File::ReadFileToString(true, expect_filename.c_str(), fullExpected)) if (File::ReadFileToString(true, Path(expect_filename), fullExpected))
printf("%s", fullExpected.c_str()); printf("%s", fullExpected.c_str());
printf("===============================\n"); printf("===============================\n");
} }

View file

@ -427,10 +427,10 @@ int main(int argc, const char* argv[])
#ifdef __ANDROID__ #ifdef __ANDROID__
// For some reason the debugger installs it with this name? // For some reason the debugger installs it with this name?
if (File::Exists("/data/app/org.ppsspp.ppsspp-2.apk")) { if (File::Exists(Path("/data/app/org.ppsspp.ppsspp-2.apk"))) {
VFSRegister("", new ZipAssetReader("/data/app/org.ppsspp.ppsspp-2.apk", "assets/")); VFSRegister("", new ZipAssetReader("/data/app/org.ppsspp.ppsspp-2.apk", "assets/"));
} }
if (File::Exists("/data/app/org.ppsspp.ppsspp.apk")) { if (File::Exists(Path("/data/app/org.ppsspp.ppsspp.apk"))) {
VFSRegister("", new ZipAssetReader("/data/app/org.ppsspp.ppsspp.apk", "assets/")); VFSRegister("", new ZipAssetReader("/data/app/org.ppsspp.ppsspp.apk", "assets/"));
} }
#endif #endif

View file

@ -594,7 +594,7 @@ static bool TestPath() {
EXPECT_EQ_STR(path3.WithExtraExtension("txt").ToString(), std::string("/asdf/jkl/foo/bar.txt")); EXPECT_EQ_STR(path3.WithExtraExtension("txt").ToString(), std::string("/asdf/jkl/foo/bar.txt"));
EXPECT_EQ_STR(Path("foo.bar/hello").GetFileExtension(), std::string("")); EXPECT_EQ_STR(Path("foo.bar/hello").GetFileExtension(), std::string(""));
EXPECT_EQ_STR(Path("foo.bar/hello.txt").WithReplacedExtension("txt", "html").ToString(), std::string("foo.bar/hello.html"));
return true; return true;
} }