From 6192bdf6df97c6e6ac444870cd58a82fcdf7b398 Mon Sep 17 00:00:00 2001 From: Sacha Date: Wed, 28 Nov 2012 04:57:30 +1000 Subject: [PATCH] sceIoRename function --- Core/FileSystems/DirectoryFileSystem.cpp | 40 +++++++++++++++++------- Core/FileSystems/DirectoryFileSystem.h | 5 +-- Core/FileSystems/FileSystem.h | 2 ++ Core/FileSystems/ISOFileSystem.h | 1 + Core/FileSystems/MetaFileSystem.cpp | 15 +++++++++ Core/FileSystems/MetaFileSystem.h | 1 + Core/HLE/sceIo.cpp | 7 +++-- 7 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index d1a4e02091..f1fb84ac7d 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -35,17 +35,16 @@ std::string DirectoryFileSystem::GetLocalPath(std::string localpath) if (localpath.empty()) return basePath; - if (localpath[0] == '/') - localpath.erase(0,1); + if (localpath[0] == '/') + localpath.erase(0,1); //Convert slashes #ifdef _WIN32 for (size_t i = 0; i < localpath.size(); i++) { if (localpath[i] == '/') localpath[i] = '\\'; - } + } #endif - return basePath + localpath; } @@ -56,8 +55,8 @@ bool DirectoryFileSystem::MkDir(const std::string &dirname) #ifdef _WIN32 return CreateDirectory(fullName.c_str(), NULL) == TRUE; #else - mkdir(fullName.c_str(), 0777); - return true; + mkdir(fullName.c_str(), 0777); + return true; #endif } @@ -67,7 +66,26 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) #ifdef _WIN32 return RemoveDirectory(fullName.c_str()) == TRUE; #else - return 0 == rmdir(fullName.c_str()); + return 0 == rmdir(fullName.c_str()); +#endif +} + +bool DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) +{ + std::string fullFrom = GetLocalPath(from); + std::string fullTo = to; + // TO filename may not include path. Intention is that it uses FROM's path + if (to.find("/") != std::string::npos) { + int offset = from.find_last_of("/"); + if (offset >= 0) { + fullTo = from.substr(0, offset + 1) + to; + } + } + fullTo = GetLocalPath(fullTo); +#ifdef _WIN32 + return MoveFile(fullFrom.c_str(), fullTo.c_str()) == TRUE; +#else + return 0 == rename(fullFrom.c_str(), fullTo.c_str()); #endif } @@ -77,7 +95,7 @@ bool DirectoryFileSystem::DeleteFile(const std::string &filename) #ifdef _WIN32 return DeleteFile(fullName.c_str()) == TRUE; #else - return 0 == unlink(fullName.c_str()); + return 0 == unlink(fullName.c_str()); #endif } @@ -243,7 +261,7 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) x.name = filename; - std::string fullName = GetLocalPath(filename); + std::string fullName = GetLocalPath(filename); if (!File::Exists(fullName)) { return x; } @@ -257,8 +275,8 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) x.size = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); #else - x.size = File::GetSize(fullName); - //TODO + x.size = File::GetSize(fullName); + //TODO #endif return x; diff --git a/Core/FileSystems/DirectoryFileSystem.h b/Core/FileSystems/DirectoryFileSystem.h index e0598cbde7..df81ccab34 100644 --- a/Core/FileSystems/DirectoryFileSystem.h +++ b/Core/FileSystems/DirectoryFileSystem.h @@ -36,7 +36,7 @@ class DirectoryFileSystem : public IFileSystem #ifdef _WIN32 HANDLE hFile; #else - FILE *hFile; + FILE *hFile; #endif }; @@ -61,6 +61,7 @@ public: bool OwnsHandle(u32 handle); bool MkDir(const std::string &dirname); bool RmDir(const std::string &dirname); + bool RenameFile(const std::string &from, const std::string &to); bool DeleteFile(const std::string &filename); }; - \ No newline at end of file + diff --git a/Core/FileSystems/FileSystem.h b/Core/FileSystems/FileSystem.h index 8024461d1c..069296d7de 100644 --- a/Core/FileSystems/FileSystem.h +++ b/Core/FileSystems/FileSystem.h @@ -83,6 +83,7 @@ public: virtual bool OwnsHandle(u32 handle) = 0; virtual bool MkDir(const std::string &dirname) = 0; virtual bool RmDir(const std::string &dirname) = 0; + virtual bool RenameFile(const std::string &from, const std::string &to) = 0; virtual bool DeleteFile(const std::string &filename) = 0; }; @@ -100,6 +101,7 @@ public: bool OwnsHandle(u32 handle) {return false;} virtual bool MkDir(const std::string &dirname) {return false;} virtual bool RmDir(const std::string &dirname) {return false;} + virtual bool RenameFile(const std::string &from, const std::string &to) {return false;} virtual bool DeleteFile(const std::string &filename) {return false;} }; diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index 530e152145..61f30016f1 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -82,5 +82,6 @@ public: virtual bool MkDir(const std::string &dirname) {return false;} virtual bool RmDir(const std::string &dirname) {return false;} + virtual bool RenameFile(const std::string &from, const std::string &to) {return false;} virtual bool DeleteFile(const std::string &filename) {return false;} }; diff --git a/Core/FileSystems/MetaFileSystem.cpp b/Core/FileSystems/MetaFileSystem.cpp index 7ca1a9a619..55895968fc 100644 --- a/Core/FileSystems/MetaFileSystem.cpp +++ b/Core/FileSystems/MetaFileSystem.cpp @@ -177,6 +177,21 @@ bool MetaFileSystem::RmDir(const std::string &dirname) } } +bool MetaFileSystem::RenameFile(const std::string &from, const std::string &to) +{ + std::string of; + std::string rf; + IFileSystem *system; + if (MapFilePath(from, of, &system) && MapFilePath(to, rf, &system)) + { + return system->RenameFile(of, rf); + } + else + { + return false; + } +} + bool MetaFileSystem::DeleteFile(const std::string &filename) { std::string of; diff --git a/Core/FileSystems/MetaFileSystem.h b/Core/FileSystems/MetaFileSystem.h index 35ed0e55b5..dee552e098 100644 --- a/Core/FileSystems/MetaFileSystem.h +++ b/Core/FileSystems/MetaFileSystem.h @@ -56,6 +56,7 @@ public: virtual bool MkDir(const std::string &dirname); virtual bool RmDir(const std::string &dirname); + virtual bool RenameFile(const std::string &from, const std::string &to); virtual bool DeleteFile(const std::string &filename); // TODO: void IoCtl(...) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 771b2af590..6b5501cfe2 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -700,8 +700,11 @@ void sceIoRename() //(const char *oldname, const char *newname); { const char *from = Memory::GetCharPointer(PARAM(0)); const char *to = Memory::GetCharPointer(PARAM(1)); - ERROR_LOG(HLE,"UNIMPL sceIoRename(%s, %s)", from, to); - RETURN(0); + if (pspFileSystem.RenameFile(from, to)) + RETURN(0); + else + RETURN(-1); + DEBUG_LOG(HLE,"sceIoRename(%s, %s)", from, to); } void sceIoChdir()