Change parameter of DeleteDir and friends to Path

This commit is contained in:
Henrik Rydgård 2021-05-09 15:02:23 +02:00
parent b7fe72bfc9
commit 0d80362c30
7 changed files with 54 additions and 45 deletions

View file

@ -405,25 +405,30 @@ bool CreateFullPath(const Path &path) {
}
// Deletes a directory filename, returns true on success
bool DeleteDir(const std::string &filename)
{
INFO_LOG(COMMON, "DeleteDir: directory %s", filename.c_str());
bool DeleteDir(const Path &path) {
switch (path.Type()) {
case PathType::NATIVE:
break; // OK
default:
return false;
}
INFO_LOG(COMMON, "DeleteDir: directory %s", path.c_str());
// check if a directory
if (!File::IsDirectory(Path(filename)))
if (!File::IsDirectory(path))
{
ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", filename.c_str());
ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", path.c_str());
return false;
}
#ifdef _WIN32
if (::RemoveDirectory(ConvertUTF8ToWString(filename).c_str()))
if (::RemoveDirectory(path.ToWString().c_str()))
return true;
#else
if (rmdir(filename.c_str()) == 0)
if (rmdir(path.c_str()) == 0)
return true;
#endif
ERROR_LOG(COMMON, "DeleteDir: %s: %s", filename.c_str(), GetLastErrorMsg().c_str());
ERROR_LOG(COMMON, "DeleteDir: %s: %s", path.c_str(), GetLastErrorMsg().c_str());
return false;
}
@ -719,15 +724,15 @@ bool DeleteDirRecursively(const std::string &directory)
}
closedir(dirp);
#endif
return File::DeleteDir(directory);
return File::DeleteDir(Path(directory));
}
void OpenFileInEditor(const std::string& fileName) {
void OpenFileInEditor(const Path &fileName) {
#if defined(_WIN32)
#if PPSSPP_PLATFORM(UWP)
// Do nothing.
#else
ShellExecuteW(nullptr, L"open", ConvertUTF8ToWString(fileName).c_str(), nullptr, nullptr, SW_SHOW);
ShellExecuteW(nullptr, L"open", fileName.ToWString().c_str(), nullptr, nullptr, SW_SHOW);
#endif
#elif !defined(MOBILE_DEVICE)
std::string iniFile;
@ -736,7 +741,7 @@ void OpenFileInEditor(const std::string& fileName) {
#else
iniFile = "xdg-open ";
#endif
iniFile.append(fileName);
iniFile.append(fileName.ToString());
NOTICE_LOG(BOOT, "Launching %s", iniFile.c_str());
int retval = system(iniFile.c_str());
if (retval != 0) {
@ -745,8 +750,7 @@ void OpenFileInEditor(const std::string& fileName) {
#endif
}
const std::string &GetExeDirectory()
{
const std::string &GetExeDirectory() {
static std::string ExePath;
if (ExePath.empty()) {
@ -814,7 +818,6 @@ const std::string &GetExeDirectory()
return ExePath;
}
IOFile::IOFile()
: m_file(NULL), m_good(true)
{}
@ -928,8 +931,8 @@ bool IOFile::Resize(uint64_t size)
return m_good;
}
bool ReadFileToString(bool text_file, const char *filename, std::string &str) {
FILE *f = File::OpenCFile(Path(filename), text_file ? "r" : "rb");
bool ReadFileToString(bool text_file, const Path &filename, std::string &str) {
FILE *f = File::OpenCFile(filename, text_file ? "r" : "rb");
if (!f)
return false;
// Warning: some files, like in /sys/, may return a fixed size like 4096.
@ -983,8 +986,8 @@ uint8_t *ReadLocalFile(const char *filename, size_t * size) {
return contents;
}
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename) {
FILE *f = File::OpenCFile(Path(filename), text_file ? "w" : "wb");
bool WriteStringToFile(bool text_file, const std::string &str, const Path &filename) {
FILE *f = File::OpenCFile(filename, text_file ? "w" : "wb");
if (!f)
return false;
size_t len = str.size();
@ -997,8 +1000,8 @@ bool WriteStringToFile(bool text_file, const std::string &str, const char *filen
return true;
}
bool WriteDataToFile(bool text_file, const void* data, const unsigned int size, const char *filename) {
FILE *f = File::OpenCFile(Path(filename), text_file ? "w" : "wb");
bool WriteDataToFile(bool text_file, const void* data, const unsigned int size, const Path &filename) {
FILE *f = File::OpenCFile(filename, text_file ? "w" : "wb");
if (!f)
return false;
size_t len = size;

View file

@ -84,7 +84,10 @@ bool Delete(const Path &filename);
// Deletes a directory filename, returns true on success
// Directory must be empty.
bool DeleteDir(const std::string &filename);
bool DeleteDir(const Path &filename);
// Deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const Path &directory);
// renames file srcFilename to destFilename, returns true on success
bool Rename(const std::string &srcFilename, const std::string &destFilename);
@ -95,12 +98,9 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename);
// creates an empty file filename, returns true on success
bool CreateEmptyFile(const Path &filename);
// deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string &directory);
// Opens ini file (cheats, texture replacements etc.)
// TODO: Belongs in System or something.
void OpenFileInEditor(const std::string& fileName);
void OpenFileInEditor(const Path &fileName);
// TODO: Belongs in System or something.
const std::string &GetExeDirectory();
@ -185,10 +185,10 @@ private:
// TODO: Refactor, this was moved from the old file_util.cpp.
// Whole-file reading/writing
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
bool WriteDataToFile(bool text_file, const void* data, const unsigned int size, const char *filename);
bool WriteStringToFile(bool text_file, const std::string &str, const Path &filename);
bool WriteDataToFile(bool text_file, const void* data, const unsigned int size, const Path &filename);
bool ReadFileToString(bool text_file, const char *filename, std::string &str);
bool ReadFileToString(bool text_file, const Path &filename, std::string &str);
// Return value must be delete[]-d.
uint8_t *ReadLocalFile(const char *filename, size_t *size);

View file

@ -80,6 +80,10 @@ public:
bool StartsWith(const Path &other) const;
bool operator <(const Path &other) const {
return path_ < other.path_;
}
private:
// The internal representation is currently always the plain string.
// For CPU efficiency we could keep an AndroidStorageContentURI too,

View file

@ -528,7 +528,7 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) {
#else
return 0 == rmdir(fullName.c_str());
#endif*/
bool result = File::DeleteDirRecursively(fullName.ToString());
bool result = File::DeleteDirRecursively(fullName);
return ReplayApplyDisk(ReplayAction::RMDIR, result, CoreTiming::GetGlobalTimeUs()) != 0;
}

View file

@ -450,10 +450,10 @@ namespace Reporting
postdata.Add("savestate_used", SaveState::HasLoadedState());
}
void AddScreenshotData(MultipartFormDataEncoder &postdata, std::string filename)
void AddScreenshotData(MultipartFormDataEncoder &postdata, const Path &filename)
{
std::string data;
if (!filename.empty() && File::ReadFileToString(false, filename.c_str(), data))
if (!filename.empty() && File::ReadFileToString(false, filename, data))
postdata.Add("screenshot", data, "screenshot.jpg", "image/jpeg");
const std::string iconFilename = "disc0:/PSP_GAME/ICON0.PNG";
@ -502,7 +502,7 @@ namespace Reporting
postdata.Add("gameplay", StringFromFormat("%d", payload.int3));
postdata.Add("crc", StringFromFormat("%08x", RetrieveCRCUnlessPowerSaving(PSP_CoreParameter().fileToStart)));
postdata.Add("suggestions", payload.string1 != "perfect" && payload.string1 != "playable" ? "1" : "0");
AddScreenshotData(postdata, payload.string2);
AddScreenshotData(postdata, Path(payload.string2));
payload.string1.clear();
payload.string2.clear();

View file

@ -115,7 +115,7 @@ bool GameManager::Uninstall(std::string name) {
return false;
}
bool success = File::DeleteDirRecursively(gameDir.ToString());
bool success = File::DeleteDirRecursively(gameDir);
if (success) {
INFO_LOG(HLE, "Successfully deleted game '%s'", name.c_str());
g_Config.CleanRecent();
@ -517,7 +517,7 @@ bool GameManager::InstallMemstickGame(struct zip *z, const std::string &zipfile,
};
// Create all the directories first in one pass
std::set<std::string> createdDirs;
std::set<Path> createdDirs;
for (int i = 0; i < info.numFiles; i++) {
const char *fn = zip_get_name(z, i, 0);
std::string zippedName = fn;
@ -530,9 +530,11 @@ bool GameManager::InstallMemstickGame(struct zip *z, const std::string &zipfile,
if (!isDir && outFilename.find("/") != std::string::npos) {
outFilename = outFilename.substr(0, outFilename.rfind('/'));
}
if (createdDirs.find(outFilename) == createdDirs.end()) {
File::CreateFullPath(Path(outFilename));
createdDirs.insert(outFilename);
Path outPath(outFilename);
if (createdDirs.find(outPath) == createdDirs.end()) {
File::CreateFullPath(Path(outPath));
createdDirs.insert(Path(outPath));
}
if (!isDir && fileAllowed(fn)) {
struct zip_stat zstat;
@ -543,7 +545,7 @@ bool GameManager::InstallMemstickGame(struct zip *z, const std::string &zipfile,
}
// Now, loop through again in a second pass, writing files.
std::vector<std::string> createdFiles;
std::vector<Path> createdFiles;
for (int i = 0; i < info.numFiles; i++) {
const char *fn = zip_get_name(z, i, 0);
// Note that we do NOT write files that are not in a directory, to avoid random
@ -558,7 +560,7 @@ bool GameManager::InstallMemstickGame(struct zip *z, const std::string &zipfile,
if (!ExtractFile(z, i, Path(outFilename), &bytesCopied, allBytes)) {
goto bail;
} else {
createdFiles.push_back(outFilename);
createdFiles.push_back(Path(outFilename));
}
}
}
@ -580,10 +582,10 @@ bail:
zip_close(z);
// We don't delete the original in this case. Try to delete the files we created so far.
for (size_t i = 0; i < createdFiles.size(); i++) {
File::Delete(Path(createdFiles[i]));
File::Delete(createdFiles[i]);
}
for (auto iter = createdDirs.begin(); iter != createdDirs.end(); ++iter) {
File::DeleteDir(iter->c_str());
for (auto const &iter : createdDirs) {
File::DeleteDir(iter);
}
SetInstallError(sy->T("Storage full"));
return false;

View file

@ -71,7 +71,7 @@ bool GameInfo::Delete() {
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
{
// TODO: This could be handled by Core/Util/GameManager too somehow.
std::string directoryToRemove = ResolvePBPDirectory(filePath_.ToString());
Path directoryToRemove = Path(ResolvePBPDirectory(filePath_.ToString()));
INFO_LOG(SYSTEM, "Deleting %s", directoryToRemove.c_str());
if (!File::DeleteDirRecursively(directoryToRemove)) {
ERROR_LOG(SYSTEM, "Failed to delete file");
@ -239,7 +239,7 @@ bool GameInfo::DeleteAllSaveData() {
File::Delete(Path(fileInfo[i].fullName));
}
File::DeleteDir(saveDataDir[j].c_str());
File::DeleteDir(saveDataDir[j]);
}
return true;
}