From 8955fedf4428dcecdbf1810b4366e8bb6f2174a3 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 23 Jul 2013 17:24:33 +0200 Subject: [PATCH 1/8] Open directory as disc image --- Core/FileSystems/DirectoryFileSystem.cpp | 139 ++++++++++++++++++++++- Core/FileSystems/DirectoryFileSystem.h | 18 ++- Core/FileSystems/ISOFileSystem.cpp | 2 +- Core/FileSystems/ISOFileSystem.h | 1 + Core/Loaders.cpp | 10 +- Core/Loaders.h | 2 + Core/PSPLoaders.cpp | 24 +++- Core/System.cpp | 2 +- UI/MainScreen.cpp | 2 +- UI/MenuScreens.cpp | 2 +- Windows/WndMainWindow.cpp | 25 +++- Windows/WndMainWindow.h | 2 +- Windows/ppsspp.rc | Bin 38622 -> 38746 bytes Windows/resource.h | Bin 25386 -> 25476 bytes 14 files changed, 214 insertions(+), 15 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index c35ed2f302..9a4ffca5ee 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -18,6 +18,7 @@ #include "ChunkFile.h" #include "FileUtil.h" #include "DirectoryFileSystem.h" +#include "ISOFileSystem.h" #include "Core/HLE/sceKernel.h" #include "file/zip_read.h" @@ -133,7 +134,17 @@ bool DirectoryFileSystem::FixPathCase(std::string &path, FixPathCaseBehavior beh #endif -DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) { +DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath, bool _virtualDisc) + : basePath(_basePath),virtualDisc(_virtualDisc),currentBlockIndex(0) { + +#ifdef _WIN32 + if (basePath.size() > 0 && basePath[basePath.size()-1] != '\\') + basePath = basePath + "\\"; +#else + if (basePath.size() > 0 && basePath[basePath.size()-1] != '/') + basePath = basePath + "/"; +#endif + File::CreateFullPath(basePath); hAlloc = _hAlloc; } @@ -288,6 +299,80 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { } u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { + + if (filename.compare(0,8,"/sce_lbn") == 0) + { + u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; + parseLBN(filename, §orStart, &readSize); + + OpenFileEntry entry; + entry.lbnFile = true; + entry.size = readSize; + entry.curOffset = 0; + + std::string actualFileName = ""; + for (int i = 0; i < fileBlocks.size(); i++) + { + if (fileBlocks[i].firstBlock <= sectorStart) + { + u32 sectorOffset = (sectorStart-fileBlocks[i].firstBlock)*2048; + u32 endOffset = sectorOffset+readSize; + if (endOffset <= fileBlocks[i].totalSize) + { + entry.startOffset = sectorOffset; + actualFileName = fileBlocks[i].fileName; + break; + } + } + } + + // no file info has been gathered? shouldn't happen + if (actualFileName == "") + { + ERROR_LOG(FILESYS, "sce_lbn used without calling fileinfo."); + return 0; + } + +#if HOST_IS_CASE_SENSITIVE + if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) + { + DEBUG_LOG(HLE, "Checking case for path %s", filename.c_str()); + if ( ! FixPathCase(actualFileName, FPC_PATH_MUST_EXIST) ) + return 0; // or go on and attempt (for a better error code than just 0?) + } + // else we try fopen first (in case we're lucky) before simulating case insensitivity +#endif + + // now we just need an actual file handle + std::string fullName = GetLocalPath(actualFileName); + +#ifdef _WIN32 + entry.hFile = CreateFile(fullName.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); + bool success = entry.hFile != INVALID_HANDLE_VALUE; +#else + entry.hFile = fopen(fullName.c_str(), "rb"); + bool success = entry.hFile != 0; +#endif + + if (!success) + { + ERROR_LOG(HLE, "DiscDirectoryFileSystem::OpenFile: FAILED, %i", GetLastError()); + return 0; + } + + // seek to start +#ifdef _WIN32 + DWORD newPos = SetFilePointer(entry.hFile, (LONG)entry.startOffset, 0, FILE_BEGIN); +#else + fseek(entry.hFile, entry.startOffset, SEEK_SET); +#endif + + u32 newHandle = hAlloc->GetNewHandle(); + entries[newHandle] = entry; + + return newHandle; + } + #if HOST_IS_CASE_SENSITIVE if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) { @@ -303,6 +388,7 @@ u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const INFO_LOG(HLE,"Actually opening %s (%s)", fullNameC, filename.c_str()); OpenFileEntry entry; + entry.lbnFile = false; //TODO: tests, should append seek to end of file? seeking in a file opened for append? #ifdef _WIN32 @@ -430,6 +516,11 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { #else bytesRead = fread(pointer, 1, size, iter->second.hFile); #endif + if (iter->second.lbnFile) + { + iter->second.curOffset += bytesRead; + } + return bytesRead; } else { //This shouldn't happen... @@ -459,6 +550,23 @@ size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { + if (iter->second.lbnFile) + { + switch (type) { + case FILEMOVE_BEGIN: iter->second.curOffset = position; break; + case FILEMOVE_CURRENT: iter->second.curOffset += position; break; + case FILEMOVE_END: iter->second.curOffset = iter->second.size-position; break; + } + +#ifdef _WIN32 + u32 off = iter->second.startOffset+iter->second.curOffset; + DWORD newPos = SetFilePointer((*iter).second.hFile, (LONG)off, 0, FILE_BEGIN); +#else + fseek(iter->second.hFile, iter->second.curOffset+iter->second.startOffset, SEEK_SET); +#endif + return iter->second.curOffset; + } + #ifdef _WIN32 DWORD moveMethod = 0; switch (type) { @@ -485,6 +593,19 @@ size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { } } +u32 DirectoryFileSystem::GetFileLbn(std::string& fileName) +{ + if (!virtualDisc) return -1; + + for (int i = 0; i < fileBlocks.size(); i++) + { + if (fileBlocks[i].fileName == fileName) + return fileBlocks[i].firstBlock; + } + + return -1; +} + PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { PSPFileInfo x; x.name = filename; @@ -517,6 +638,22 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { #else x.size = s.st_size; #endif + if (virtualDisc) + { + x.startSector = GetFileLbn(filename); + x.numSectors = (x.size+2047)/2048; + if (x.startSector == -1) + { + x.startSector = currentBlockIndex; + currentBlockIndex += x.numSectors; + + LbnMapEntry entry; + entry.fileName = filename; + entry.firstBlock = x.startSector; + entry.totalSize = x.size; + fileBlocks.push_back(entry); + } + } x.access = s.st_mode & 0x1FF; localtime_r((time_t*)&s.st_atime,&x.atime); localtime_r((time_t*)&s.st_ctime,&x.ctime); diff --git a/Core/FileSystems/DirectoryFileSystem.h b/Core/FileSystems/DirectoryFileSystem.h index c0dd2ae17e..08032ec7cc 100644 --- a/Core/FileSystems/DirectoryFileSystem.h +++ b/Core/FileSystems/DirectoryFileSystem.h @@ -49,7 +49,7 @@ typedef void * HANDLE; class DirectoryFileSystem : public IFileSystem { public: - DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); + DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath, bool _virtualDisc = false); ~DirectoryFileSystem(); void DoState(PointerWrap &p); @@ -69,18 +69,34 @@ public: bool GetHostPath(const std::string &inpath, std::string &outpath); private: + u32 GetFileLbn(std::string& fileName); + struct OpenFileEntry { #ifdef _WIN32 HANDLE hFile; #else FILE *hFile; #endif + bool lbnFile; + u32 startOffset; // only used by lbn files + u32 curOffset; // only used by lbn files; + u32 size; // only used by lbn files }; typedef std::map EntryMap; EntryMap entries; std::string basePath; IHandleAllocator *hAlloc; + bool virtualDisc; + + typedef struct { + std::string fileName; + u32 firstBlock; + u32 totalSize; + } LbnMapEntry; + + std::vector fileBlocks; + u32 currentBlockIndex; // In case of Windows: Translate slashes, etc. std::string GetLocalPath(std::string localpath); diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 1f32cc5f1e..2d3d8ea94b 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -26,7 +26,7 @@ const int sectorSize = 2048; -static bool parseLBN(std::string filename, u32 *sectorStart, u32 *readSize) +bool parseLBN(std::string filename, u32 *sectorStart, u32 *readSize) { // The format of this is: "/sce_lbn" "0x"? HEX* ANY* "_size" "0x"? HEX* ANY* // That means that "/sce_lbn/_size1/" is perfectly valid. diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index a53fe454b4..a7aca0ca8a 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -24,6 +24,7 @@ #include "BlockDevices.h" +bool parseLBN(std::string filename, u32 *sectorStart, u32 *readSize); class ISOFileSystem : public IFileSystem { diff --git a/Core/Loaders.cpp b/Core/Loaders.cpp index 59c32f8437..ec32f612d4 100644 --- a/Core/Loaders.cpp +++ b/Core/Loaders.cpp @@ -57,7 +57,14 @@ EmuFileType Identify_File(std::string &filename) if (ebootInfo.exists) { return FILETYPE_PSP_PBP_DIRECTORY; } - } + } + + // check if it's a disc directory + if (getFileInfo((filename + "/PSP_GAME").c_str(), &ebootInfo)) { + if (ebootInfo.exists) { + return FILETYPE_PSP_DISC_DIRECTORY; + } + } } FILE *f = fopen(filename.c_str(), "rb"); @@ -157,6 +164,7 @@ bool LoadFile(std::string &filename, std::string *error_string) { case FILETYPE_PSP_ISO: case FILETYPE_PSP_ISO_NP: + case FILETYPE_PSP_DISC_DIRECTORY: // behaves the same as the mounting is already done by now pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR"); return Load_PSP_ISO(filename.c_str(), error_string); diff --git a/Core/Loaders.h b/Core/Loaders.h index 8693f88eb2..7e16509693 100644 --- a/Core/Loaders.h +++ b/Core/Loaders.h @@ -28,6 +28,8 @@ enum EmuFileType FILETYPE_PSP_ISO, FILETYPE_PSP_ISO_NP, + FILETYPE_PSP_DISC_DIRECTORY, + FILETYPE_UNKNOWN_BIN, FILETYPE_UNKNOWN_ELF, diff --git a/Core/PSPLoaders.cpp b/Core/PSPLoaders.cpp index 6afe7c79e6..b010d30b12 100644 --- a/Core/PSPLoaders.cpp +++ b/Core/PSPLoaders.cpp @@ -27,6 +27,7 @@ #include "MIPS/MIPSAnalyst.h" #include "MIPS/MIPSCodeUtils.h" +#include "file/file_util.h" #include "StringUtils.h" #include "Host.h" @@ -44,11 +45,24 @@ // to determine if the emulator should enable extra memory and // double-sized texture coordinates. void InitMemoryForGameISO(std::string fileToStart) { - auto bd = constructBlockDevice(fileToStart.c_str()); - // Can't init anything without a block device... - if (!bd) - return; - ISOFileSystem *umd2 = new ISOFileSystem(&pspFileSystem, bd); + IFileSystem* umd2; + + // check if it's a disc directory + FileInfo info; + if (!getFileInfo(fileToStart.c_str(), &info)) return; + + if (info.isDirectory) + { + umd2 = new DirectoryFileSystem(&pspFileSystem, fileToStart,true); + } + else + { + auto bd = constructBlockDevice(fileToStart.c_str()); + // Can't init anything without a block device... + if (!bd) + return; + umd2 = new ISOFileSystem(&pspFileSystem, bd); + } // Parse PARAM.SFO diff --git a/Core/System.cpp b/Core/System.cpp index 7a6c48ae92..c9b66bed73 100644 --- a/Core/System.cpp +++ b/Core/System.cpp @@ -83,7 +83,7 @@ bool PSP_Init(const CoreParameter &coreParam, std::string *error_string) std::string filename = coreParam.fileToStart; EmuFileType type = Identify_File(filename); - if(type == FILETYPE_PSP_ISO || type == FILETYPE_PSP_ISO_NP) + if(type == FILETYPE_PSP_ISO || type == FILETYPE_PSP_ISO_NP || type == FILETYPE_PSP_DISC_DIRECTORY) InitMemoryForGameISO(filename); Memory::Init(); diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 3f32666d2b..169c7c00ee 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -38,7 +38,7 @@ #ifdef _WIN32 namespace MainWindow { - void BrowseAndBoot(std::string defaultPath); + void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false); } #endif diff --git a/UI/MenuScreens.cpp b/UI/MenuScreens.cpp index 378a296c46..f25ce9414a 100644 --- a/UI/MenuScreens.cpp +++ b/UI/MenuScreens.cpp @@ -74,7 +74,7 @@ #ifdef _WIN32 namespace MainWindow { extern HWND hwndMain; - void BrowseAndBoot(std::string defaultPath); + void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false); } #endif diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index 186ef02bca..0e8916b80e 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -303,7 +303,7 @@ namespace MainWindow return TRUE; } - void BrowseAndBoot(std::string defaultPath) + void BrowseAndBoot(std::string defaultPath, bool browseDirectory) { std::string fn; std::string filter = "PSP ROMs (*.iso *.cso *.pbp *.elf)|*.pbp;*.elf;*.iso;*.cso;*.prx|All files (*.*)|*.*||"; @@ -323,7 +323,24 @@ namespace MainWindow Core_EnableStepping(true); } - if (W32Util::BrowseForFileName(true, GetHWND(), "Load File", defaultPath.size() ? defaultPath.c_str() : 0, filter.c_str(),"*.pbp;*.elf;*.iso;*.cso;",fn)) + if (browseDirectory) + { + std::string dir = W32Util::BrowseForFolder(GetHWND(),"Choose directory"); + if (dir == "" && !isPaused) + { + Core_EnableStepping(false); + } + else + { + if (globalUIState == UISTATE_INGAME || globalUIState == UISTATE_PAUSEMENU) + { + Core_EnableStepping(false); + } + + NativeMessageReceived("boot", dir.c_str()); + } + } + else if (W32Util::BrowseForFileName(true, GetHWND(), "Load File", defaultPath.size() ? defaultPath.c_str() : 0, filter.c_str(),"*.pbp;*.elf;*.iso;*.cso;",fn)) { if (globalUIState == UISTATE_INGAME || globalUIState == UISTATE_PAUSEMENU) { @@ -572,6 +589,10 @@ namespace MainWindow BrowseAndBoot(""); break; + case ID_FILE_LOAD_DIR: + BrowseAndBoot("",true); + break; + case ID_FILE_LOAD_MEMSTICK: { std::string memStickDir, flash0dir; diff --git a/Windows/WndMainWindow.h b/Windows/WndMainWindow.h index 1b0485492a..99f3e97ec2 100644 --- a/Windows/WndMainWindow.h +++ b/Windows/WndMainWindow.h @@ -18,7 +18,7 @@ namespace MainWindow HINSTANCE GetHInstance(); HWND GetDisplayHWND(); void SetPlaying(const char*text); - void BrowseAndBoot(std::string defaultPath); + void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false); void SaveStateActionFinished(bool result, void *userdata); void _ViewFullScreen(HWND hWnd); void _ViewNormal(HWND hWnd); diff --git a/Windows/ppsspp.rc b/Windows/ppsspp.rc index 699669b4783c038e2733e19e858b8fa508f6f34b..350ab3ccb415ec15f57b65bf9f7a371e30ba9f86 100644 GIT binary patch delta 60 zcmcb&mg&|yrVXp2r3)BR8S)qu7}OY27%~}(fV^ae5{7&rt8((gIQ7X5aU87i3@!|w OlNTmxZ(b2?W(@#68WNBI delta 14 Wcmcb$j_KZ7rVXp2H><^%SOWkzTn2Ff diff --git a/Windows/resource.h b/Windows/resource.h index f95a7243be5e6b7d1af304b885adf732f80dcfc6..8ebcf38a605dfcb95d0d3e5767164f2f179676b7 100644 GIT binary patch delta 56 zcmZ2=jIrf7A7J9xY#<)u0{~ Date: Tue, 23 Jul 2013 17:35:43 +0200 Subject: [PATCH 2/8] Display icon in UI --- UI/GameInfoCache.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/UI/GameInfoCache.cpp b/UI/GameInfoCache.cpp index 28235ece09..ca90e9aefd 100644 --- a/UI/GameInfoCache.cpp +++ b/UI/GameInfoCache.cpp @@ -225,6 +225,31 @@ public: break; + case FILETYPE_PSP_DISC_DIRECTORY: + { + info_->fileType = FILETYPE_PSP_ISO; + SequentialHandleAllocator handles; + DirectoryFileSystem umd(&handles,gamePath_.c_str(),true); + + // Alright, let's fetch the PARAM.SFO. + std::string paramSFOcontents; + if (ReadFileToString(&umd, "/PSP_GAME/PARAM.SFO", ¶mSFOcontents, 0)) { + lock_guard lock(info_->lock); + info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size()); + info_->title = info_->paramSFO.GetValueString("TITLE"); + info_->id = info_->paramSFO.GetValueString("DISC_ID"); + info_->id_version = info_->paramSFO.GetValueString("DISC_ID") + "_" + info_->paramSFO.GetValueString("DISC_VERSION"); + + info_->paramSFOLoaded = true; + } + + ReadFileToString(&umd, "/PSP_GAME/ICON0.PNG", &info_->iconTextureData, &info_->lock); + if (info_->wantBG) { + ReadFileToString(&umd, "/PSP_GAME/PIC0.PNG", &info_->pic0TextureData, &info_->lock); + } + ReadFileToString(&umd, "/PSP_GAME/PIC1.PNG", &info_->pic1TextureData, &info_->lock); + break; + } case FILETYPE_PSP_ISO: case FILETYPE_PSP_ISO_NP: { From 0afd7dea2846ebacec23f4276aa30f84ae3d2908 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 23 Jul 2013 17:53:55 +0200 Subject: [PATCH 3/8] Add error message when opening entire ISO --- Core/FileSystems/DirectoryFileSystem.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index 9a4ffca5ee..89c2cf64e0 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -300,6 +300,12 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { + if (filename == "") + { + ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: Opening entire ISO not supported"); + return 0; + } + if (filename.compare(0,8,"/sce_lbn") == 0) { u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; @@ -329,7 +335,7 @@ u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const // no file info has been gathered? shouldn't happen if (actualFileName == "") { - ERROR_LOG(FILESYS, "sce_lbn used without calling fileinfo."); + ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: sce_lbn used without calling fileinfo."); return 0; } From 97b89c50df394ee179b9d59d4d62c646f02802b7 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 23 Jul 2013 18:16:21 +0200 Subject: [PATCH 4/8] Fix stupid mistake --- Windows/WndMainWindow.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index 0e8916b80e..4fe6aec2ff 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -326,9 +326,10 @@ namespace MainWindow if (browseDirectory) { std::string dir = W32Util::BrowseForFolder(GetHWND(),"Choose directory"); - if (dir == "" && !isPaused) + if (dir == "") { - Core_EnableStepping(false); + if (!isPaused) + Core_EnableStepping(false); } else { From be548fdd2ee5767e643f02e98178d300c2261f53 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 23 Jul 2013 20:24:15 +0200 Subject: [PATCH 5/8] Disable invalid file operations for virtual discs --- Core/FileSystems/DirectoryFileSystem.cpp | 35 ++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index 89c2cf64e0..bc94bc2f6d 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -145,7 +145,7 @@ DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string basePath = basePath + "/"; #endif - File::CreateFullPath(basePath); + if (!virtualDisc) File::CreateFullPath(basePath); hAlloc = _hAlloc; } @@ -176,6 +176,11 @@ std::string DirectoryFileSystem::GetLocalPath(std::string localpath) { } bool DirectoryFileSystem::MkDir(const std::string &dirname) { + if (virtualDisc) + { + ERROR_LOG(HLE,"Cannot create directory on virtual disc"); + return false; + } #if HOST_IS_CASE_SENSITIVE // Must fix case BEFORE attempting, because MkDir would create @@ -192,6 +197,12 @@ bool DirectoryFileSystem::MkDir(const std::string &dirname) { } bool DirectoryFileSystem::RmDir(const std::string &dirname) { + if (virtualDisc) + { + ERROR_LOG(HLE,"Cannot remove directory on virtual disc"); + return false; + } + std::string fullName = GetLocalPath(dirname); #if HOST_IS_CASE_SENSITIVE @@ -216,6 +227,12 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) { } int DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) { + if (virtualDisc) + { + ERROR_LOG(HLE,"Cannot rename file on virtual disc"); + return false; + } + std::string fullTo = to; // Rename ignores the path (even if specified) on to. @@ -271,6 +288,12 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & } bool DirectoryFileSystem::RemoveFile(const std::string &filename) { + if (virtualDisc) + { + ERROR_LOG(HLE,"Cannot remove file on virtual disc"); + return false; + } + std::string fullName = GetLocalPath(filename); #ifdef _WIN32 bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE); @@ -300,13 +323,13 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { - if (filename == "") + if (virtualDisc && filename == "") { ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: Opening entire ISO not supported"); return 0; } - if (filename.compare(0,8,"/sce_lbn") == 0) + if (virtualDisc && filename.compare(0,8,"/sce_lbn") == 0) { u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; parseLBN(filename, §orStart, &readSize); @@ -536,6 +559,12 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { } size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { + if (virtualDisc) + { + ERROR_LOG(HLE,"Cannot write to file on virtual disc"); + return 0; + } + EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { From e76ffe6b7cbca262a503824158f63799f171bf79 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 23 Jul 2013 22:23:12 +0200 Subject: [PATCH 6/8] Supprt opening whole disc, assume file info is gathered before it's used --- Core/FileSystems/DirectoryFileSystem.cpp | 88 +++++++++++++++++++++++- Core/FileSystems/DirectoryFileSystem.h | 4 +- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index bc94bc2f6d..d692012e5b 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -321,12 +321,38 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { return retValue; } +bool DirectoryFileSystem::findVirtualFileData(u32 accessBlock, u32 accessSize, std::string& fileName, u32& firstBlock) +{ + for (int i = 0; i < fileBlocks.size(); i++) + { + if (fileBlocks[i].firstBlock <= accessBlock) + { + u32 sectorOffset = (accessBlock-fileBlocks[i].firstBlock)*2048; + u32 endOffset = sectorOffset+accessSize; + if (endOffset <= fileBlocks[i].totalSize) + { + firstBlock = fileBlocks[i].firstBlock; + fileName = fileBlocks[i].fileName; + return true; + } + } + } + + return false; +} + u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { if (virtualDisc && filename == "") { - ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: Opening entire ISO not supported"); - return 0; + OpenFileEntry entry; + entry.lbnFile = false; + entry.wholeIso = true; + + u32 newHandle = hAlloc->GetNewHandle(); + entries[newHandle] = entry; + + return newHandle; } if (virtualDisc && filename.compare(0,8,"/sce_lbn") == 0) @@ -336,6 +362,7 @@ u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const OpenFileEntry entry; entry.lbnFile = true; + entry.wholeIso = false; entry.size = readSize; entry.curOffset = 0; @@ -418,6 +445,7 @@ u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const OpenFileEntry entry; entry.lbnFile = false; + entry.wholeIso = false; //TODO: tests, should append seek to end of file? seeking in a file opened for append? #ifdef _WIN32 @@ -539,6 +567,51 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { + // it's the whole iso... it could reference any of the files on the disc. + // For now let's just open and close the files on demand. Can certainly be done + // better though + if (iter->second.wholeIso) + { + std::string fileName; + u32 firstBlock; + + if (findVirtualFileData(iter->second.curOffset,size*2048,fileName,firstBlock) == false) + { + ERROR_LOG(HLE,"DiscDirectoryFileSystem: Reading from unknown address", handle); + return 0; + } + + std::string fullName = GetLocalPath(fileName); + +#ifdef _WIN32 + HANDLE hFile = CreateFile(fullName.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); + bool success = hFile != INVALID_HANDLE_VALUE; +#else + FILE* hFile = fopen(fullName.c_str(), "rb"); + bool success = hFile != 0; +#endif + + if (!success) + { + ERROR_LOG(HLE,"DiscDirectoryFileSystem: Error opening file %s", fileName.c_str()); + return 0; + } + + u32 startOffset = (iter->second.curOffset-firstBlock)*2048; + size_t bytesRead; +#ifdef _WIN32 + SetFilePointer(hFile, (LONG)startOffset, 0, FILE_BEGIN); + ::ReadFile(hFile, (LPVOID)pointer, (DWORD)size*2048, (LPDWORD)&bytesRead, 0); + CloseHandle(hFile); +#else + fseek(hFile, startOffset, SEEK_SET); + bytesRead = fread(pointer, 1, size*2048, hFile); + fclose(hFile); +#endif + + return bytesRead; + } + size_t bytesRead; #ifdef _WIN32 ::ReadFile(iter->second.hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesRead, 0); @@ -585,6 +658,17 @@ size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { + if (iter->second.wholeIso) + { + switch (type) { + case FILEMOVE_BEGIN: iter->second.curOffset = position; break; + case FILEMOVE_CURRENT: iter->second.curOffset += position; break; + case FILEMOVE_END: return -1; // unsupported + } + + return iter->second.curOffset; + } + if (iter->second.lbnFile) { switch (type) { diff --git a/Core/FileSystems/DirectoryFileSystem.h b/Core/FileSystems/DirectoryFileSystem.h index 08032ec7cc..ae5035f57d 100644 --- a/Core/FileSystems/DirectoryFileSystem.h +++ b/Core/FileSystems/DirectoryFileSystem.h @@ -70,6 +70,7 @@ public: private: u32 GetFileLbn(std::string& fileName); + bool findVirtualFileData(u32 accessBlock, u32 accessSize, std::string& fileName, u32& firstBlock); struct OpenFileEntry { #ifdef _WIN32 @@ -77,9 +78,10 @@ private: #else FILE *hFile; #endif + bool wholeIso; bool lbnFile; u32 startOffset; // only used by lbn files - u32 curOffset; // only used by lbn files; + u32 curOffset; // only used by lbn files and whole iso; u32 size; // only used by lbn files }; From 5ddc9afba0220b1304706b5aa0eb26d0bda3bc9a Mon Sep 17 00:00:00 2001 From: Kingcom Date: Wed, 24 Jul 2013 22:49:45 +0200 Subject: [PATCH 7/8] -save state support -abstracted file access -moved to VirtualDiscFileSystem class --- Core/FileSystems/DirectoryFileSystem.cpp | 1004 +++++++++++++--------- Core/FileSystems/DirectoryFileSystem.h | 104 ++- Core/PSPLoaders.cpp | 2 +- UI/GameInfoCache.cpp | 2 +- 4 files changed, 696 insertions(+), 416 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index d692012e5b..7379f8d925 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -32,8 +32,12 @@ #include #endif - #if HOST_IS_CASE_SENSITIVE +typedef enum { + FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from) + FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to) + FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive) +} FixPathCaseBehavior; static bool FixFilenameCase(const std::string &path, std::string &filename) { @@ -83,7 +87,7 @@ static bool FixFilenameCase(const std::string &path, std::string &filename) return retValue; } -bool DirectoryFileSystem::FixPathCase(std::string &path, FixPathCaseBehavior behavior) +bool FixPathCase(std::string &path, FixPathCaseBehavior behavior) { size_t len = path.size(); @@ -99,8 +103,7 @@ bool DirectoryFileSystem::FixPathCase(std::string &path, FixPathCaseBehavior beh } std::string fullPath; - fullPath.reserve(basePath.size() + len + 1); - fullPath.append(basePath); + fullPath.reserve(len + 1); size_t start = 0; while (start < len) @@ -134,28 +137,161 @@ bool DirectoryFileSystem::FixPathCase(std::string &path, FixPathCaseBehavior beh #endif -DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath, bool _virtualDisc) - : basePath(_basePath),virtualDisc(_virtualDisc),currentBlockIndex(0) { - -#ifdef _WIN32 - if (basePath.size() > 0 && basePath[basePath.size()-1] != '\\') - basePath = basePath + "\\"; -#else - if (basePath.size() > 0 && basePath[basePath.size()-1] != '/') - basePath = basePath + "/"; +bool DirectoryFileHandle::Open(std::string& fileName, FileAccess access) +{ +#if HOST_IS_CASE_SENSITIVE + if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) + { + DEBUG_LOG(HLE, "Checking case for path %s", fileName.c_str()); + if ( ! FixPathCase(fileName, FPC_PATH_MUST_EXIST) ) + return false; // or go on and attempt (for a better error code than just 0?) + } + // else we try fopen first (in case we're lucky) before simulating case insensitivity #endif - if (!virtualDisc) File::CreateFullPath(basePath); + INFO_LOG(HLE,"Actually opening %s", fileName.c_str()); + + //TODO: tests, should append seek to end of file? seeking in a file opened for append? +#ifdef _WIN32 + // Convert parameters to Windows permissions and access + DWORD desired = 0; + DWORD sharemode = 0; + DWORD openmode = 0; + if (access & FILEACCESS_READ) { + desired |= GENERIC_READ; + sharemode |= FILE_SHARE_READ; + } + if (access & FILEACCESS_WRITE) { + desired |= GENERIC_WRITE; + sharemode |= FILE_SHARE_WRITE; + } + if (access & FILEACCESS_CREATE) { + openmode = OPEN_ALWAYS; + } else { + openmode = OPEN_EXISTING; + } + //Let's do it! + hFile = CreateFile(fileName.c_str(), desired, sharemode, 0, openmode, 0, 0); + bool success = hFile != INVALID_HANDLE_VALUE; +#else + // Convert flags in access parameter to fopen access mode + const char *mode = NULL; + if (access & FILEACCESS_APPEND) { + if (access & FILEACCESS_READ) + mode = "ab+"; // append+read, create if needed + else + mode = "ab"; // append only, create if needed + } else if (access & FILEACCESS_WRITE) { + if (access & FILEACCESS_READ) { + // FILEACCESS_CREATE is ignored for read only, write only, and append + // because C++ standard fopen's nonexistant file creation can only be + // customized for files opened read+write + if (access & FILEACCESS_CREATE) + mode = "wb+"; // read+write, create if needed + else + mode = "rb+"; // read+write, but don't create + } else { + mode = "wb"; // write only, create if needed + } + } else { // neither write nor append, so default to read only + mode = "rb"; // read only, don't create + } + + hFile = fopen(fileName.c_str(), mode); + bool success = hFile != 0; +#endif + +#if HOST_IS_CASE_SENSITIVE + if (!success && + !(access & FILEACCESS_APPEND) && + !(access & FILEACCESS_CREATE) && + !(access & FILEACCESS_WRITE)) + { + if ( ! FixPathCase(fileName, FPC_PATH_MUST_EXIST) ) + return 0; // or go on and attempt (for a better error code than just 0?) + const char* fullNameC = fileName.c_str(); + + DEBUG_LOG(HLE, "Case may have been incorrect, second try opening %s (%s)", fullNameC, fileName.c_str()); + + // And try again with the correct case this time +#ifdef _WIN32 + hFile = CreateFile(fullNameC, desired, sharemode, 0, openmode, 0, 0); + success = hFile != INVALID_HANDLE_VALUE; +#else + hFile = fopen(fullNameC, mode); + success = hFile != 0; +#endif + } +#endif + + return success; +} + +size_t DirectoryFileHandle::Read(u8* pointer, s64 size) +{ + size_t bytesRead; +#ifdef _WIN32 + ::ReadFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesRead, 0); +#else + bytesRead = fread(pointer, 1, size, hFile); +#endif + return bytesRead; +} + +size_t DirectoryFileHandle::Write(const u8* pointer, s64 size) +{ + size_t bytesWritten; +#ifdef _WIN32 + ::WriteFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesWritten, 0); +#else + bytesWritten = fwrite(pointer, 1, size, hFile); +#endif + return bytesWritten; +} + +size_t DirectoryFileHandle::Seek(s32 position, FileMove type) +{ +#ifdef _WIN32 + DWORD moveMethod = 0; + switch (type) { + case FILEMOVE_BEGIN: moveMethod = FILE_BEGIN; break; + case FILEMOVE_CURRENT: moveMethod = FILE_CURRENT; break; + case FILEMOVE_END: moveMethod = FILE_END; break; + } + DWORD newPos = SetFilePointer(hFile, (LONG)position, 0, moveMethod); + return newPos; +#else + int moveMethod = 0; + switch (type) { + case FILEMOVE_BEGIN: moveMethod = SEEK_SET; break; + case FILEMOVE_CURRENT: moveMethod = SEEK_CUR; break; + case FILEMOVE_END: moveMethod = SEEK_END; break; + } + fseek(hFile, position, moveMethod); + return ftell(hFile); +#endif +} + +void DirectoryFileHandle::Close() +{ +#ifdef _WIN32 + if (hFile != (HANDLE)-1) + CloseHandle(hFile); +#else + if (hFile != 0) + fclose(hFile); +#endif +} + + +DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) { + File::CreateFullPath(basePath); hAlloc = _hAlloc; } DirectoryFileSystem::~DirectoryFileSystem() { for (auto iter = entries.begin(); iter != entries.end(); ++iter) { -#ifdef _WIN32 - CloseHandle((*iter).second.hFile); -#else - fclose((*iter).second.hFile); -#endif + iter->second.hFile.Close(); } } @@ -176,12 +312,6 @@ std::string DirectoryFileSystem::GetLocalPath(std::string localpath) { } bool DirectoryFileSystem::MkDir(const std::string &dirname) { - if (virtualDisc) - { - ERROR_LOG(HLE,"Cannot create directory on virtual disc"); - return false; - } - #if HOST_IS_CASE_SENSITIVE // Must fix case BEFORE attempting, because MkDir would create // duplicate (different case) directories @@ -197,12 +327,6 @@ bool DirectoryFileSystem::MkDir(const std::string &dirname) { } bool DirectoryFileSystem::RmDir(const std::string &dirname) { - if (virtualDisc) - { - ERROR_LOG(HLE,"Cannot remove directory on virtual disc"); - return false; - } - std::string fullName = GetLocalPath(dirname); #if HOST_IS_CASE_SENSITIVE @@ -227,12 +351,6 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) { } int DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) { - if (virtualDisc) - { - ERROR_LOG(HLE,"Cannot rename file on virtual disc"); - return false; - } - std::string fullTo = to; // Rename ignores the path (even if specified) on to. @@ -288,12 +406,6 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & } bool DirectoryFileSystem::RemoveFile(const std::string &filename) { - if (virtualDisc) - { - ERROR_LOG(HLE,"Cannot remove file on virtual disc"); - return false; - } - std::string fullName = GetLocalPath(filename); #ifdef _WIN32 bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE); @@ -321,205 +433,13 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { return retValue; } -bool DirectoryFileSystem::findVirtualFileData(u32 accessBlock, u32 accessSize, std::string& fileName, u32& firstBlock) -{ - for (int i = 0; i < fileBlocks.size(); i++) - { - if (fileBlocks[i].firstBlock <= accessBlock) - { - u32 sectorOffset = (accessBlock-fileBlocks[i].firstBlock)*2048; - u32 endOffset = sectorOffset+accessSize; - if (endOffset <= fileBlocks[i].totalSize) - { - firstBlock = fileBlocks[i].firstBlock; - fileName = fileBlocks[i].fileName; - return true; - } - } - } - - return false; -} - u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { - if (virtualDisc && filename == "") - { - OpenFileEntry entry; - entry.lbnFile = false; - entry.wholeIso = true; - - u32 newHandle = hAlloc->GetNewHandle(); - entries[newHandle] = entry; - - return newHandle; - } - - if (virtualDisc && filename.compare(0,8,"/sce_lbn") == 0) - { - u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; - parseLBN(filename, §orStart, &readSize); - - OpenFileEntry entry; - entry.lbnFile = true; - entry.wholeIso = false; - entry.size = readSize; - entry.curOffset = 0; - - std::string actualFileName = ""; - for (int i = 0; i < fileBlocks.size(); i++) - { - if (fileBlocks[i].firstBlock <= sectorStart) - { - u32 sectorOffset = (sectorStart-fileBlocks[i].firstBlock)*2048; - u32 endOffset = sectorOffset+readSize; - if (endOffset <= fileBlocks[i].totalSize) - { - entry.startOffset = sectorOffset; - actualFileName = fileBlocks[i].fileName; - break; - } - } - } - - // no file info has been gathered? shouldn't happen - if (actualFileName == "") - { - ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: sce_lbn used without calling fileinfo."); - return 0; - } - -#if HOST_IS_CASE_SENSITIVE - if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) - { - DEBUG_LOG(HLE, "Checking case for path %s", filename.c_str()); - if ( ! FixPathCase(actualFileName, FPC_PATH_MUST_EXIST) ) - return 0; // or go on and attempt (for a better error code than just 0?) - } - // else we try fopen first (in case we're lucky) before simulating case insensitivity -#endif - - // now we just need an actual file handle - std::string fullName = GetLocalPath(actualFileName); - -#ifdef _WIN32 - entry.hFile = CreateFile(fullName.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); - bool success = entry.hFile != INVALID_HANDLE_VALUE; -#else - entry.hFile = fopen(fullName.c_str(), "rb"); - bool success = entry.hFile != 0; -#endif - - if (!success) - { - ERROR_LOG(HLE, "DiscDirectoryFileSystem::OpenFile: FAILED, %i", GetLastError()); - return 0; - } - - // seek to start -#ifdef _WIN32 - DWORD newPos = SetFilePointer(entry.hFile, (LONG)entry.startOffset, 0, FILE_BEGIN); -#else - fseek(entry.hFile, entry.startOffset, SEEK_SET); -#endif - - u32 newHandle = hAlloc->GetNewHandle(); - entries[newHandle] = entry; - - return newHandle; - } - -#if HOST_IS_CASE_SENSITIVE - if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) - { - DEBUG_LOG(HLE, "Checking case for path %s", filename.c_str()); - if ( ! FixPathCase(filename, FPC_PATH_MUST_EXIST) ) - return 0; // or go on and attempt (for a better error code than just 0?) - } - // else we try fopen first (in case we're lucky) before simulating case insensitivity -#endif - std::string fullName = GetLocalPath(filename); const char *fullNameC = fullName.c_str(); - INFO_LOG(HLE,"Actually opening %s (%s)", fullNameC, filename.c_str()); OpenFileEntry entry; - entry.lbnFile = false; - entry.wholeIso = false; - - //TODO: tests, should append seek to end of file? seeking in a file opened for append? -#ifdef _WIN32 - // Convert parameters to Windows permissions and access - DWORD desired = 0; - DWORD sharemode = 0; - DWORD openmode = 0; - if (access & FILEACCESS_READ) { - desired |= GENERIC_READ; - sharemode |= FILE_SHARE_READ; - } - if (access & FILEACCESS_WRITE) { - desired |= GENERIC_WRITE; - sharemode |= FILE_SHARE_WRITE; - } - if (access & FILEACCESS_CREATE) { - openmode = OPEN_ALWAYS; - } else { - openmode = OPEN_EXISTING; - } - //Let's do it! - entry.hFile = CreateFile(fullNameC, desired, sharemode, 0, openmode, 0, 0); - bool success = entry.hFile != INVALID_HANDLE_VALUE; -#else - // Convert flags in access parameter to fopen access mode - const char *mode = NULL; - if (access & FILEACCESS_APPEND) { - if (access & FILEACCESS_READ) - mode = "ab+"; // append+read, create if needed - else - mode = "ab"; // append only, create if needed - } else if (access & FILEACCESS_WRITE) { - if (access & FILEACCESS_READ) { - // FILEACCESS_CREATE is ignored for read only, write only, and append - // because C++ standard fopen's nonexistant file creation can only be - // customized for files opened read+write - if (access & FILEACCESS_CREATE) - mode = "wb+"; // read+write, create if needed - else - mode = "rb+"; // read+write, but don't create - } else { - mode = "wb"; // write only, create if needed - } - } else { // neither write nor append, so default to read only - mode = "rb"; // read only, don't create - } - - entry.hFile = fopen(fullNameC, mode); - bool success = entry.hFile != 0; -#endif - -#if HOST_IS_CASE_SENSITIVE - if (!success && - !(access & FILEACCESS_APPEND) && - !(access & FILEACCESS_CREATE) && - !(access & FILEACCESS_WRITE)) - { - if ( ! FixPathCase(filename, FPC_PATH_MUST_EXIST) ) - return 0; // or go on and attempt (for a better error code than just 0?) - fullName = GetLocalPath(filename); - fullNameC = fullName.c_str(); - - DEBUG_LOG(HLE, "Case may have been incorrect, second try opening %s (%s)", fullNameC, filename.c_str()); - - // And try again with the correct case this time -#ifdef _WIN32 - entry.hFile = CreateFile(fullNameC, desired, sharemode, 0, openmode, 0, 0); - success = entry.hFile != INVALID_HANDLE_VALUE; -#else - entry.hFile = fopen(fullNameC, mode); - success = entry.hFile != 0; -#endif - } -#endif + bool success = entry.hFile.Open(fullName,access); if (!success) { #ifdef _WIN32 @@ -532,7 +452,7 @@ u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const } else { #ifdef _WIN32 if (access & FILEACCESS_APPEND) - SetFilePointer(entry.hFile, 0, NULL, FILE_END); + entry.hFile.Seek(0,FILEMOVE_END); #endif u32 newHandle = hAlloc->GetNewHandle(); @@ -546,11 +466,7 @@ void DirectoryFileSystem::CloseFile(u32 handle) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { hAlloc->FreeHandle(handle); -#ifdef _WIN32 - CloseHandle((*iter).second.hFile); -#else - fclose((*iter).second.hFile); -#endif + iter->second.hFile.Close(); entries.erase(iter); } else { //This shouldn't happen... @@ -567,62 +483,7 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { - // it's the whole iso... it could reference any of the files on the disc. - // For now let's just open and close the files on demand. Can certainly be done - // better though - if (iter->second.wholeIso) - { - std::string fileName; - u32 firstBlock; - - if (findVirtualFileData(iter->second.curOffset,size*2048,fileName,firstBlock) == false) - { - ERROR_LOG(HLE,"DiscDirectoryFileSystem: Reading from unknown address", handle); - return 0; - } - - std::string fullName = GetLocalPath(fileName); - -#ifdef _WIN32 - HANDLE hFile = CreateFile(fullName.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); - bool success = hFile != INVALID_HANDLE_VALUE; -#else - FILE* hFile = fopen(fullName.c_str(), "rb"); - bool success = hFile != 0; -#endif - - if (!success) - { - ERROR_LOG(HLE,"DiscDirectoryFileSystem: Error opening file %s", fileName.c_str()); - return 0; - } - - u32 startOffset = (iter->second.curOffset-firstBlock)*2048; - size_t bytesRead; -#ifdef _WIN32 - SetFilePointer(hFile, (LONG)startOffset, 0, FILE_BEGIN); - ::ReadFile(hFile, (LPVOID)pointer, (DWORD)size*2048, (LPDWORD)&bytesRead, 0); - CloseHandle(hFile); -#else - fseek(hFile, startOffset, SEEK_SET); - bytesRead = fread(pointer, 1, size*2048, hFile); - fclose(hFile); -#endif - - return bytesRead; - } - - size_t bytesRead; -#ifdef _WIN32 - ::ReadFile(iter->second.hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesRead, 0); -#else - bytesRead = fread(pointer, 1, size, iter->second.hFile); -#endif - if (iter->second.lbnFile) - { - iter->second.curOffset += bytesRead; - } - + size_t bytesRead = iter->second.hFile.Read(pointer,size); return bytesRead; } else { //This shouldn't happen... @@ -632,21 +493,10 @@ size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { } size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { - if (virtualDisc) - { - ERROR_LOG(HLE,"Cannot write to file on virtual disc"); - return 0; - } - EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { - size_t bytesWritten; -#ifdef _WIN32 - ::WriteFile(iter->second.hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesWritten, 0); -#else - bytesWritten = fwrite(pointer, 1, size, iter->second.hFile); -#endif + size_t bytesWritten = iter->second.hFile.Write(pointer,size); return bytesWritten; } else { //This shouldn't happen... @@ -658,53 +508,7 @@ size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { EntryMap::iterator iter = entries.find(handle); if (iter != entries.end()) { - if (iter->second.wholeIso) - { - switch (type) { - case FILEMOVE_BEGIN: iter->second.curOffset = position; break; - case FILEMOVE_CURRENT: iter->second.curOffset += position; break; - case FILEMOVE_END: return -1; // unsupported - } - - return iter->second.curOffset; - } - - if (iter->second.lbnFile) - { - switch (type) { - case FILEMOVE_BEGIN: iter->second.curOffset = position; break; - case FILEMOVE_CURRENT: iter->second.curOffset += position; break; - case FILEMOVE_END: iter->second.curOffset = iter->second.size-position; break; - } - -#ifdef _WIN32 - u32 off = iter->second.startOffset+iter->second.curOffset; - DWORD newPos = SetFilePointer((*iter).second.hFile, (LONG)off, 0, FILE_BEGIN); -#else - fseek(iter->second.hFile, iter->second.curOffset+iter->second.startOffset, SEEK_SET); -#endif - return iter->second.curOffset; - } - -#ifdef _WIN32 - DWORD moveMethod = 0; - switch (type) { - case FILEMOVE_BEGIN: moveMethod = FILE_BEGIN; break; - case FILEMOVE_CURRENT: moveMethod = FILE_CURRENT; break; - case FILEMOVE_END: moveMethod = FILE_END; break; - } - DWORD newPos = SetFilePointer((*iter).second.hFile, (LONG)position, 0, moveMethod); - return newPos; -#else - int moveMethod = 0; - switch (type) { - case FILEMOVE_BEGIN: moveMethod = SEEK_SET; break; - case FILEMOVE_CURRENT: moveMethod = SEEK_CUR; break; - case FILEMOVE_END: moveMethod = SEEK_END; break; - } - fseek(iter->second.hFile, position, moveMethod); - return ftell(iter->second.hFile); -#endif + return iter->second.hFile.Seek(position,type); } else { //This shouldn't happen... ERROR_LOG(HLE,"Cannot seek in file that hasn't been opened: %08x", handle); @@ -712,19 +516,6 @@ size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { } } -u32 DirectoryFileSystem::GetFileLbn(std::string& fileName) -{ - if (!virtualDisc) return -1; - - for (int i = 0; i < fileBlocks.size(); i++) - { - if (fileBlocks[i].fileName == fileName) - return fileBlocks[i].firstBlock; - } - - return -1; -} - PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { PSPFileInfo x; x.name = filename; @@ -757,22 +548,7 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { #else x.size = s.st_size; #endif - if (virtualDisc) - { - x.startSector = GetFileLbn(filename); - x.numSectors = (x.size+2047)/2048; - if (x.startSector == -1) - { - x.startSector = currentBlockIndex; - currentBlockIndex += x.numSectors; - LbnMapEntry entry; - entry.fileName = filename; - entry.firstBlock = x.startSector; - entry.totalSize = x.size; - fileBlocks.push_back(entry); - } - } x.access = s.st_mode & 0x1FF; localtime_r((time_t*)&s.st_atime,&x.atime); localtime_r((time_t*)&s.st_ctime,&x.ctime); @@ -887,6 +663,464 @@ void DirectoryFileSystem::DoState(PointerWrap &p) { } + +VirtualDiscFileSystem::VirtualDiscFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) + : basePath(_basePath),currentBlockIndex(0) { + +#ifdef _WIN32 + if (basePath.size() > 0 && basePath[basePath.size()-1] != '\\') + basePath = basePath + "\\"; +#else + if (basePath.size() > 0 && basePath[basePath.size()-1] != '/') + basePath = basePath + "/"; +#endif + + hAlloc = _hAlloc; +} + +VirtualDiscFileSystem::~VirtualDiscFileSystem() { + for (auto iter = entries.begin(); iter != entries.end(); ++iter) { + if (!iter->second.type != VFILETYPE_ISO) + iter->second.hFile.Close(); + } +} + +void VirtualDiscFileSystem::DoState(PointerWrap &p) +{ + int fileListSize = fileList.size(); + int entryCount = entries.size(); + + p.Do(fileListSize); + p.Do(entryCount); + p.Do(currentBlockIndex); + + if (p.mode == p.MODE_READ) + { + fileList.clear(); + entries.clear(); + + for (int i = 0; i < fileListSize; i++) + { + FileListEntry entry; + p.Do(entry.fileName); + p.Do(entry.firstBlock); + p.Do(entry.totalSize); + fileList.push_back(entry); + } + + for (int i = 0; i < entryCount; i++) + { + u32 fd; + OpenFileEntry of; + + p.Do(fd); + p.Do(of.fileIndex); + p.Do(of.type); + p.Do(of.curOffset); + p.Do(of.startOffset); + p.Do(of.size); + + // open file + if (of.type != VFILETYPE_ISO) + { + std::string fullName = GetLocalPath(fileList[of.fileIndex].fileName); + bool success = of.hFile.Open(fullName,FILEACCESS_READ); + if (!success) + { + ERROR_LOG(FILESYS, "Failed to create file handle for %s.",fullName); + } else { + if (of.type == VFILETYPE_LBN) + { + of.hFile.Seek(of.curOffset+of.startOffset,FILEMOVE_BEGIN); + } else { + of.hFile.Seek(of.curOffset,FILEMOVE_BEGIN); + } + } + } + + entries[fd] = of; + } + } else { + for (int i = 0; i < fileListSize; i++) + { + p.Do(fileList[i].fileName); + p.Do(fileList[i].firstBlock); + p.Do(fileList[i].totalSize); + } + + for (EntryMap::iterator it = entries.begin(), end = entries.end(); it != end; ++it) + { + OpenFileEntry &of = it->second; + + p.Do(it->first); + p.Do(of.fileIndex); + p.Do(of.type); + p.Do(of.curOffset); + p.Do(of.startOffset); + p.Do(of.size); + } + } + + p.DoMarker("VirtualDiscFileSystem"); +} + +std::string VirtualDiscFileSystem::GetLocalPath(std::string localpath) { + if (localpath.empty()) + return basePath; + + 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; +} + + +int VirtualDiscFileSystem::getFileListIndex(std::string& fileName) +{ + for (int i = 0; i < fileList.size(); i++) + { + if (fileList[i].fileName == fileName) + return i; + } + + // unknown file - add it + std::string fullName = GetLocalPath(fileName); + if (! File::Exists(fullName)) { +#if HOST_IS_CASE_SENSITIVE + if (! FixPathCase(fileName, FPC_FILE_MUST_EXIST)) + return -1; + fullName = GetLocalPath(fileName); + + if (! File::Exists(fullName)) + return -1; +#else + return -1; +#endif + } + + FileType type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL; + if (type == FILETYPE_DIRECTORY) + return -1; + + FileListEntry entry; + entry.fileName = fileName; + + struct stat s; + + stat(fullName.c_str(), &s); +#ifdef _WIN32 + WIN32_FILE_ATTRIBUTE_DATA data; + GetFileAttributesEx(fullName.c_str(), GetFileExInfoStandard, &data); + + entry.totalSize = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); +#else + entry.totalSize = s.st_size; +#endif + + entry.firstBlock = currentBlockIndex; + currentBlockIndex += (entry.totalSize+2047)/2048; + + fileList.push_back(entry); + + return fileList.size()-1; +} + +int VirtualDiscFileSystem::getFileListIndex(u32 accessBlock, u32 accessSize) +{ + for (int i = 0; i < fileList.size(); i++) + { + if (fileList[i].firstBlock <= accessBlock) + { + u32 sectorOffset = (accessBlock-fileList[i].firstBlock)*2048; + u32 endOffset = sectorOffset+accessSize; + if (endOffset <= fileList[i].totalSize) + { + return i; + } + } + } + + return -1; +} + +u32 VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) +{ + if (filename == "") + { + OpenFileEntry entry; + entry.type = VFILETYPE_ISO; + + u32 newHandle = hAlloc->GetNewHandle(); + entries[newHandle] = entry; + + return newHandle; + } + + if (filename.compare(0,8,"/sce_lbn") == 0) + { + u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; + parseLBN(filename, §orStart, &readSize); + + OpenFileEntry entry; + entry.type = VFILETYPE_LBN; + entry.size = readSize; + entry.curOffset = 0; + + entry.fileIndex= getFileListIndex(sectorStart,readSize); + if (entry.fileIndex == -1) + { + ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: sce_lbn used without calling fileinfo."); + return 0; + } + + entry.startOffset = (sectorStart-fileList[entry.fileIndex].firstBlock)*2048; + + // now we just need an actual file handle + std::string fullName = GetLocalPath(fileList[entry.fileIndex].fileName); + bool success = entry.hFile.Open(fullName,FILEACCESS_READ); + + if (!success) + { + ERROR_LOG(HLE, "DiscDirectoryFileSystem::OpenFile: FAILED, %i", GetLastError()); + return 0; + } + + // seek to start + entry.hFile.Seek(entry.startOffset,FILEMOVE_BEGIN); + + u32 newHandle = hAlloc->GetNewHandle(); + entries[newHandle] = entry; + + return newHandle; + } + + std::string fullName = GetLocalPath(filename); + const char *fullNameC = fullName.c_str(); + + OpenFileEntry entry; + entry.type = VFILETYPE_NORMAL; + bool success = entry.hFile.Open(fullName,access); + + if (!success) { +#ifdef _WIN32 + ERROR_LOG(HLE, "VirtualDiscFileSystem::OpenFile: FAILED, %i - access = %i", GetLastError(), (int)access); +#else + ERROR_LOG(HLE, "VirtualDiscFileSystem::OpenFile: FAILED, access = %i", (int)access); +#endif + //wwwwaaaaahh!! + return 0; + } else { + entry.fileIndex = getFileListIndex(filename); + + u32 newHandle = hAlloc->GetNewHandle(); + entries[newHandle] = entry; + + return newHandle; + } +} + +size_t VirtualDiscFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { + EntryMap::iterator iter = entries.find(handle); + if (iter != entries.end()) { + switch (iter->second.type) + { + case VFILETYPE_NORMAL: + { + return iter->second.hFile.Seek(position,type); + } + case VFILETYPE_LBN: + { + switch (type) + { + case FILEMOVE_BEGIN: iter->second.curOffset = position; break; + case FILEMOVE_CURRENT: iter->second.curOffset += position; break; + case FILEMOVE_END: iter->second.curOffset = iter->second.size-position; break; + } + + u32 off = iter->second.startOffset+iter->second.curOffset; + iter->second.hFile.Seek(off,FILEMOVE_BEGIN); + return iter->second.curOffset; + } + case VFILETYPE_ISO: + { + switch (type) + { + case FILEMOVE_BEGIN: iter->second.curOffset = position; break; + case FILEMOVE_CURRENT: iter->second.curOffset += position; break; + case FILEMOVE_END: return -1; // unsupported + } + + return iter->second.curOffset; + } + } + } else { + //This shouldn't happen... + ERROR_LOG(HLE,"Cannot seek in file that hasn't been opened: %08x", handle); + return 0; + } +} + +size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { + EntryMap::iterator iter = entries.find(handle); + if (iter != entries.end()) + { + // it's the whole iso... it could reference any of the files on the disc. + // For now let's just open and close the files on demand. Can certainly be done + // better though + if (iter->second.type == VFILETYPE_ISO) + { + int fileIndex = getFileListIndex(iter->second.curOffset,size*2048); + if (fileIndex == -1) + { + ERROR_LOG(HLE,"VirtualDiscFileSystem: Reading from unknown address", handle); + return 0; + } + + std::string fullName = GetLocalPath(fileList[fileIndex].fileName); + DirectoryFileHandle hFile; + bool success = hFile.Open(fullName,FILEACCESS_READ); + + if (!success) + { + ERROR_LOG(HLE,"VirtualDiscFileSystem: Error opening file %s", fileList[fileIndex].fileName.c_str()); + return 0; + } + + u32 startOffset = (iter->second.curOffset-fileList[fileIndex].firstBlock)*2048; + size_t bytesRead; + + hFile.Seek(startOffset,FILEMOVE_BEGIN); + bytesRead = hFile.Read(pointer,size*2048); + hFile.Close(); + + return bytesRead; + } + + size_t bytesRead = iter->second.hFile.Read(pointer,size); + iter->second.curOffset += bytesRead; + return bytesRead; + } else { + //This shouldn't happen... + ERROR_LOG(HLE,"Cannot read file that hasn't been opened: %08x", handle); + return 0; + } +} + +void VirtualDiscFileSystem::CloseFile(u32 handle) { + EntryMap::iterator iter = entries.find(handle); + if (iter != entries.end()) { + hAlloc->FreeHandle(handle); + iter->second.hFile.Close(); + entries.erase(iter); + } else { + //This shouldn't happen... + ERROR_LOG(HLE,"Cannot close file that hasn't been opened: %08x", handle); + } +} + +bool VirtualDiscFileSystem::OwnsHandle(u32 handle) { + EntryMap::iterator iter = entries.find(handle); + return (iter != entries.end()); +} + +PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) { + PSPFileInfo x; + x.name = filename; + + std::string fullName = GetLocalPath(filename); + if (! File::Exists(fullName)) { +#if HOST_IS_CASE_SENSITIVE + if (! FixPathCase(filename, FPC_FILE_MUST_EXIST)) + return x; + fullName = GetLocalPath(filename); + + if (! File::Exists(fullName)) + return x; +#else + return x; +#endif + } + + x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL; + x.exists = true; + + if (x.type != FILETYPE_DIRECTORY) + { + struct stat s; + stat(fullName.c_str(), &s); +#ifdef _WIN32 + WIN32_FILE_ATTRIBUTE_DATA data; + GetFileAttributesEx(fullName.c_str(), GetFileExInfoStandard, &data); + + x.size = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); +#else + x.size = s.st_size; +#endif + + int fileIndex = getFileListIndex(filename); + x.startSector = fileList[fileIndex].firstBlock; + x.numSectors = (x.size+2047)/2048; + + x.access = s.st_mode & 0x1FF; + localtime_r((time_t*)&s.st_atime,&x.atime); + localtime_r((time_t*)&s.st_ctime,&x.ctime); + localtime_r((time_t*)&s.st_mtime,&x.mtime); + } + + return x; +} + +bool VirtualDiscFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) +{ + outpath = GetLocalPath(inpath); + return true; +} + +std::vector VirtualDiscFileSystem::GetDirListing(std::string path) +{ + // todo + std::vector result; + return result; +} + +size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) +{ + ERROR_LOG(HLE,"Cannot write to file on virtual disc"); + return 0; +} + +bool VirtualDiscFileSystem::MkDir(const std::string &dirname) +{ + ERROR_LOG(HLE,"Cannot create directory on virtual disc"); + return false; +} + +bool VirtualDiscFileSystem::RmDir(const std::string &dirname) +{ + ERROR_LOG(HLE,"Cannot remove directory on virtual disc"); + return false; +} + +int VirtualDiscFileSystem::RenameFile(const std::string &from, const std::string &to) +{ + ERROR_LOG(HLE,"Cannot rename file on virtual disc"); + return -1; +} + +bool VirtualDiscFileSystem::RemoveFile(const std::string &filename) +{ + ERROR_LOG(HLE,"Cannot remove file on virtual disc"); + return false; +} + + + VFSFileSystem::VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) { INFO_LOG(HLE, "Creating VFS file system"); hAlloc = _hAlloc; diff --git a/Core/FileSystems/DirectoryFileSystem.h b/Core/FileSystems/DirectoryFileSystem.h index ae5035f57d..8784151c22 100644 --- a/Core/FileSystems/DirectoryFileSystem.h +++ b/Core/FileSystems/DirectoryFileSystem.h @@ -47,9 +47,32 @@ typedef void * HANDLE; #endif +typedef struct sDirectoryFileHandle +{ +#ifdef _WIN32 + HANDLE hFile; +#else + FILE* hFile; +#endif + sDirectoryFileHandle() + { +#ifdef _WIN32 + hFile = (HANDLE)-1; +#else + hFile = 0; +#endif + } + + bool Open(std::string& fileName, FileAccess access); + size_t Read(u8* pointer, s64 size); + size_t Write(const u8* pointer, s64 size); + size_t Seek(s32 position, FileMove type); + void Close(); +} DirectoryFileHandle; + class DirectoryFileSystem : public IFileSystem { public: - DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath, bool _virtualDisc = false); + DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); ~DirectoryFileSystem(); void DoState(PointerWrap &p); @@ -69,48 +92,71 @@ public: bool GetHostPath(const std::string &inpath, std::string &outpath); private: - u32 GetFileLbn(std::string& fileName); - bool findVirtualFileData(u32 accessBlock, u32 accessSize, std::string& fileName, u32& firstBlock); - struct OpenFileEntry { -#ifdef _WIN32 - HANDLE hFile; -#else - FILE *hFile; -#endif - bool wholeIso; - bool lbnFile; - u32 startOffset; // only used by lbn files - u32 curOffset; // only used by lbn files and whole iso; - u32 size; // only used by lbn files + DirectoryFileHandle hFile; }; typedef std::map EntryMap; EntryMap entries; std::string basePath; IHandleAllocator *hAlloc; - bool virtualDisc; + // In case of Windows: Translate slashes, etc. + std::string GetLocalPath(std::string localpath); +}; + +class VirtualDiscFileSystem: public IFileSystem +{ +public: + VirtualDiscFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); + ~VirtualDiscFileSystem(); + + void DoState(PointerWrap &p); + u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL); + size_t SeekFile(u32 handle, s32 position, FileMove type); + size_t ReadFile(u32 handle, u8 *pointer, s64 size); + void CloseFile(u32 handle); + PSPFileInfo GetFileInfo(std::string filename); + bool OwnsHandle(u32 handle); + bool GetHostPath(const std::string &inpath, std::string &outpath); + std::vector GetDirListing(std::string path); + + // unsupported operations + size_t WriteFile(u32 handle, const u8 *pointer, s64 size); + bool MkDir(const std::string &dirname); + bool RmDir(const std::string &dirname); + int RenameFile(const std::string &from, const std::string &to); + bool RemoveFile(const std::string &filename); + +private: + int getFileListIndex(std::string& fileName); + int getFileListIndex(u32 accessBlock, u32 accessSize); + std::string GetLocalPath(std::string localpath); + + typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType; + + struct OpenFileEntry { + DirectoryFileHandle hFile; + VirtualFileType type; + u32 fileIndex; + u32 curOffset; + u32 startOffset; // only used by lbn files + u32 size; // only used by lbn files + }; + + typedef std::map EntryMap; + EntryMap entries; + IHandleAllocator *hAlloc; + std::string basePath; + typedef struct { std::string fileName; u32 firstBlock; u32 totalSize; - } LbnMapEntry; + } FileListEntry; - std::vector fileBlocks; + std::vector fileList; u32 currentBlockIndex; - - // In case of Windows: Translate slashes, etc. - std::string GetLocalPath(std::string localpath); - -#if HOST_IS_CASE_SENSITIVE - typedef enum { - FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from) - FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to) - FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive) - } FixPathCaseBehavior; - bool FixPathCase(std::string &path, FixPathCaseBehavior behavior); -#endif }; // VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts. diff --git a/Core/PSPLoaders.cpp b/Core/PSPLoaders.cpp index b010d30b12..d4ec9500a1 100644 --- a/Core/PSPLoaders.cpp +++ b/Core/PSPLoaders.cpp @@ -53,7 +53,7 @@ void InitMemoryForGameISO(std::string fileToStart) { if (info.isDirectory) { - umd2 = new DirectoryFileSystem(&pspFileSystem, fileToStart,true); + umd2 = new VirtualDiscFileSystem(&pspFileSystem, fileToStart); } else { diff --git a/UI/GameInfoCache.cpp b/UI/GameInfoCache.cpp index ca90e9aefd..81ab0ed339 100644 --- a/UI/GameInfoCache.cpp +++ b/UI/GameInfoCache.cpp @@ -229,7 +229,7 @@ public: { info_->fileType = FILETYPE_PSP_ISO; SequentialHandleAllocator handles; - DirectoryFileSystem umd(&handles,gamePath_.c_str(),true); + VirtualDiscFileSystem umd(&handles,gamePath_.c_str()); // Alright, let's fetch the PARAM.SFO. std::string paramSFOcontents; From b24f4a64d48cf50a5c96a55765c04d79244e4842 Mon Sep 17 00:00:00 2001 From: Kingcom Date: Thu, 25 Jul 2013 13:45:30 +0200 Subject: [PATCH 8/8] Linux fixes and small changes --- Core/FileSystems/DirectoryFileSystem.cpp | 167 ++++++++++++----------- Core/FileSystems/DirectoryFileSystem.h | 3 +- 2 files changed, 87 insertions(+), 83 deletions(-) diff --git a/Core/FileSystems/DirectoryFileSystem.cpp b/Core/FileSystems/DirectoryFileSystem.cpp index 7379f8d925..5c63da888a 100644 --- a/Core/FileSystems/DirectoryFileSystem.cpp +++ b/Core/FileSystems/DirectoryFileSystem.cpp @@ -87,7 +87,7 @@ static bool FixFilenameCase(const std::string &path, std::string &filename) return retValue; } -bool FixPathCase(std::string &path, FixPathCaseBehavior behavior) +bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior) { size_t len = path.size(); @@ -103,7 +103,8 @@ bool FixPathCase(std::string &path, FixPathCaseBehavior behavior) } std::string fullPath; - fullPath.reserve(len + 1); + fullPath.reserve(basePath.size() + len + 1); + fullPath.append(basePath); size_t start = 0; while (start < len) @@ -137,19 +138,37 @@ bool FixPathCase(std::string &path, FixPathCaseBehavior behavior) #endif -bool DirectoryFileHandle::Open(std::string& fileName, FileAccess access) +std::string DirectoryFileHandle::GetLocalPath(std::string& basePath, std::string localpath) +{ + if (localpath.empty()) + return basePath; + + 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; +} + +bool DirectoryFileHandle::Open(std::string& basePath, std::string& fileName, FileAccess access) { #if HOST_IS_CASE_SENSITIVE if (access & (FILEACCESS_APPEND|FILEACCESS_CREATE|FILEACCESS_WRITE)) { DEBUG_LOG(HLE, "Checking case for path %s", fileName.c_str()); - if ( ! FixPathCase(fileName, FPC_PATH_MUST_EXIST) ) + if ( ! FixPathCase(basePath, fileName, FPC_PATH_MUST_EXIST) ) return false; // or go on and attempt (for a better error code than just 0?) } // else we try fopen first (in case we're lucky) before simulating case insensitivity #endif - INFO_LOG(HLE,"Actually opening %s", fileName.c_str()); + std::string fullName = GetLocalPath(basePath,fileName); + INFO_LOG(HLE,"Actually opening %s", fullName.c_str()); //TODO: tests, should append seek to end of file? seeking in a file opened for append? #ifdef _WIN32 @@ -171,7 +190,7 @@ bool DirectoryFileHandle::Open(std::string& fileName, FileAccess access) openmode = OPEN_EXISTING; } //Let's do it! - hFile = CreateFile(fileName.c_str(), desired, sharemode, 0, openmode, 0, 0); + hFile = CreateFile(fullName.c_str(), desired, sharemode, 0, openmode, 0, 0); bool success = hFile != INVALID_HANDLE_VALUE; #else // Convert flags in access parameter to fopen access mode @@ -197,7 +216,7 @@ bool DirectoryFileHandle::Open(std::string& fileName, FileAccess access) mode = "rb"; // read only, don't create } - hFile = fopen(fileName.c_str(), mode); + hFile = fopen(fullName.c_str(), mode); bool success = hFile != 0; #endif @@ -207,9 +226,10 @@ bool DirectoryFileHandle::Open(std::string& fileName, FileAccess access) !(access & FILEACCESS_CREATE) && !(access & FILEACCESS_WRITE)) { - if ( ! FixPathCase(fileName, FPC_PATH_MUST_EXIST) ) + if ( ! FixPathCase(basePath,fileName, FPC_PATH_MUST_EXIST) ) return 0; // or go on and attempt (for a better error code than just 0?) - const char* fullNameC = fileName.c_str(); + fullName = GetLocalPath(basePath,fileName); + const char* fullNameC = fullName.c_str(); DEBUG_LOG(HLE, "Case may have been incorrect, second try opening %s (%s)", fullNameC, fileName.c_str()); @@ -283,6 +303,19 @@ void DirectoryFileHandle::Close() #endif } +u64 getFileSize(std::string& fileName) +{ +#ifdef _WIN32 + WIN32_FILE_ATTRIBUTE_DATA data; + GetFileAttributesEx(fileName.c_str(), GetFileExInfoStandard, &data); + + return data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); +#else + struct stat s; + stat(fileName.c_str(), &s); + return s.st_size; +#endif +} DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) { File::CreateFullPath(basePath); @@ -317,7 +350,7 @@ bool DirectoryFileSystem::MkDir(const std::string &dirname) { // duplicate (different case) directories std::string fixedCase = dirname; - if ( ! FixPathCase(fixedCase, FPC_PARTIAL_ALLOWED) ) + if ( ! FixPathCase(basePath,fixedCase, FPC_PARTIAL_ALLOWED) ) return false; return File::CreateFullPath(GetLocalPath(fixedCase)); @@ -336,7 +369,7 @@ bool DirectoryFileSystem::RmDir(const std::string &dirname) { // Nope, fix case and try again fullName = dirname; - if ( ! FixPathCase(fullName, FPC_FILE_MUST_EXIST) ) + if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) ) return false; // or go on and attempt (for a better error code than just false?) fullName = GetLocalPath(fullName); @@ -371,7 +404,7 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & #if HOST_IS_CASE_SENSITIVE // In case TO should overwrite a file with different case - if ( ! FixPathCase(fullTo, FPC_PATH_MUST_EXIST) ) + if ( ! FixPathCase(basePath,fullTo, FPC_PATH_MUST_EXIST) ) return -1; // or go on and attempt (for a better error code than just false?) #endif @@ -389,7 +422,7 @@ int DirectoryFileSystem::RenameFile(const std::string &from, const std::string & { // May have failed due to case sensitivity on FROM, so try again fullFrom = from; - if ( ! FixPathCase(fullFrom, FPC_FILE_MUST_EXIST) ) + if ( ! FixPathCase(basePath,fullFrom, FPC_FILE_MUST_EXIST) ) return -1; // or go on and attempt (for a better error code than just false?) fullFrom = GetLocalPath(fullFrom); @@ -418,7 +451,7 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { { // May have failed due to case sensitivity, so try again fullName = filename; - if ( ! FixPathCase(fullName, FPC_FILE_MUST_EXIST) ) + if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) ) return false; // or go on and attempt (for a better error code than just false?) fullName = GetLocalPath(fullName); @@ -434,12 +467,8 @@ bool DirectoryFileSystem::RemoveFile(const std::string &filename) { } u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { - - std::string fullName = GetLocalPath(filename); - const char *fullNameC = fullName.c_str(); - OpenFileEntry entry; - bool success = entry.hFile.Open(fullName,access); + bool success = entry.hFile.Open(basePath,filename,access); if (!success) { #ifdef _WIN32 @@ -523,7 +552,7 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { std::string fullName = GetLocalPath(filename); if (! File::Exists(fullName)) { #if HOST_IS_CASE_SENSITIVE - if (! FixPathCase(filename, FPC_FILE_MUST_EXIST)) + if (! FixPathCase(basePath,filename, FPC_FILE_MUST_EXIST)) return x; fullName = GetLocalPath(filename); @@ -540,15 +569,8 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { { struct stat s; stat(fullName.c_str(), &s); -#ifdef _WIN32 - WIN32_FILE_ATTRIBUTE_DATA data; - GetFileAttributesEx(fullName.c_str(), GetFileExInfoStandard, &data); - - x.size = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); -#else - x.size = s.st_size; -#endif + x.size = getFileSize(fullName); x.access = s.st_mode & 0x1FF; localtime_r((time_t*)&s.st_atime,&x.atime); localtime_r((time_t*)&s.st_ctime,&x.ctime); @@ -621,7 +643,7 @@ std::vector DirectoryFileSystem::GetDirListing(std::string path) { DIR *dp = opendir(localPath.c_str()); #if HOST_IS_CASE_SENSITIVE - if(dp == NULL && FixPathCase(path, FPC_FILE_MUST_EXIST)) { + if(dp == NULL && FixPathCase(basePath,path, FPC_FILE_MUST_EXIST)) { // May have failed due to case sensitivity, try again localPath = GetLocalPath(path); dp = opendir(localPath.c_str()); @@ -723,11 +745,10 @@ void VirtualDiscFileSystem::DoState(PointerWrap &p) // open file if (of.type != VFILETYPE_ISO) { - std::string fullName = GetLocalPath(fileList[of.fileIndex].fileName); - bool success = of.hFile.Open(fullName,FILEACCESS_READ); + bool success = of.hFile.Open(basePath,fileList[of.fileIndex].fileName,FILEACCESS_READ); if (!success) { - ERROR_LOG(FILESYS, "Failed to create file handle for %s.",fullName); + ERROR_LOG(FILESYS, "Failed to create file handle for %s.",fileList[of.fileIndex].fileName.c_str()); } else { if (of.type == VFILETYPE_LBN) { @@ -780,7 +801,6 @@ std::string VirtualDiscFileSystem::GetLocalPath(std::string localpath) { return basePath + localpath; } - int VirtualDiscFileSystem::getFileListIndex(std::string& fileName) { for (int i = 0; i < fileList.size(); i++) @@ -793,7 +813,7 @@ int VirtualDiscFileSystem::getFileListIndex(std::string& fileName) std::string fullName = GetLocalPath(fileName); if (! File::Exists(fullName)) { #if HOST_IS_CASE_SENSITIVE - if (! FixPathCase(fileName, FPC_FILE_MUST_EXIST)) + if (! FixPathCase(basePath,fileName, FPC_FILE_MUST_EXIST)) return -1; fullName = GetLocalPath(fileName); @@ -810,19 +830,7 @@ int VirtualDiscFileSystem::getFileListIndex(std::string& fileName) FileListEntry entry; entry.fileName = fileName; - - struct stat s; - - stat(fullName.c_str(), &s); -#ifdef _WIN32 - WIN32_FILE_ATTRIBUTE_DATA data; - GetFileAttributesEx(fullName.c_str(), GetFileExInfoStandard, &data); - - entry.totalSize = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); -#else - entry.totalSize = s.st_size; -#endif - + entry.totalSize = getFileSize(fullName); entry.firstBlock = currentBlockIndex; currentBlockIndex += (entry.totalSize+2047)/2048; @@ -851,10 +859,15 @@ int VirtualDiscFileSystem::getFileListIndex(u32 accessBlock, u32 accessSize) u32 VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { + OpenFileEntry entry; + entry.curOffset = 0; + entry.size = 0; + entry.startOffset = 0; + if (filename == "") { - OpenFileEntry entry; entry.type = VFILETYPE_ISO; + entry.fileIndex = -1; u32 newHandle = hAlloc->GetNewHandle(); entries[newHandle] = entry; @@ -867,27 +880,28 @@ u32 VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, con u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF; parseLBN(filename, §orStart, &readSize); - OpenFileEntry entry; entry.type = VFILETYPE_LBN; entry.size = readSize; - entry.curOffset = 0; - entry.fileIndex= getFileListIndex(sectorStart,readSize); + entry.fileIndex = getFileListIndex(sectorStart,readSize); if (entry.fileIndex == -1) { - ERROR_LOG(FILESYS, "DiscDirectoryFileSystem: sce_lbn used without calling fileinfo."); + ERROR_LOG(FILESYS, "VirtualDiscFileSystem: sce_lbn used without calling fileinfo."); return 0; } entry.startOffset = (sectorStart-fileList[entry.fileIndex].firstBlock)*2048; // now we just need an actual file handle - std::string fullName = GetLocalPath(fileList[entry.fileIndex].fileName); - bool success = entry.hFile.Open(fullName,FILEACCESS_READ); + bool success = entry.hFile.Open(basePath,fileList[entry.fileIndex].fileName,FILEACCESS_READ); if (!success) { - ERROR_LOG(HLE, "DiscDirectoryFileSystem::OpenFile: FAILED, %i", GetLastError()); +#ifdef _WIN32 + ERROR_LOG(HLE, "VirtualDiscFileSystem::OpenFile: FAILED, %i", GetLastError()); +#else + ERROR_LOG(HLE, "VirtualDiscFileSystem::OpenFile: FAILED"); +#endif return 0; } @@ -899,13 +913,9 @@ u32 VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, con return newHandle; } - - std::string fullName = GetLocalPath(filename); - const char *fullNameC = fullName.c_str(); - - OpenFileEntry entry; + entry.type = VFILETYPE_NORMAL; - bool success = entry.hFile.Open(fullName,access); + bool success = entry.hFile.Open(basePath,filename,access); if (!success) { #ifdef _WIN32 @@ -961,7 +971,7 @@ size_t VirtualDiscFileSystem::SeekFile(u32 handle, s32 position, FileMove type) } } else { //This shouldn't happen... - ERROR_LOG(HLE,"Cannot seek in file that hasn't been opened: %08x", handle); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot seek in file that hasn't been opened: %08x", handle); return 0; } } @@ -982,9 +992,8 @@ size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { return 0; } - std::string fullName = GetLocalPath(fileList[fileIndex].fileName); DirectoryFileHandle hFile; - bool success = hFile.Open(fullName,FILEACCESS_READ); + bool success = hFile.Open(basePath,fileList[fileIndex].fileName,FILEACCESS_READ); if (!success) { @@ -1007,7 +1016,7 @@ size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { return bytesRead; } else { //This shouldn't happen... - ERROR_LOG(HLE,"Cannot read file that hasn't been opened: %08x", handle); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot read file that hasn't been opened: %08x", handle); return 0; } } @@ -1020,7 +1029,7 @@ void VirtualDiscFileSystem::CloseFile(u32 handle) { entries.erase(iter); } else { //This shouldn't happen... - ERROR_LOG(HLE,"Cannot close file that hasn't been opened: %08x", handle); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot close file that hasn't been opened: %08x", handle); } } @@ -1032,11 +1041,11 @@ bool VirtualDiscFileSystem::OwnsHandle(u32 handle) { PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) { PSPFileInfo x; x.name = filename; - + std::string fullName = GetLocalPath(filename); if (! File::Exists(fullName)) { #if HOST_IS_CASE_SENSITIVE - if (! FixPathCase(filename, FPC_FILE_MUST_EXIST)) + if (! FixPathCase(basePath,filename, FPC_FILE_MUST_EXIST)) return x; fullName = GetLocalPath(filename); @@ -1054,14 +1063,8 @@ PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) { { struct stat s; stat(fullName.c_str(), &s); -#ifdef _WIN32 - WIN32_FILE_ATTRIBUTE_DATA data; - GetFileAttributesEx(fullName.c_str(), GetFileExInfoStandard, &data); - x.size = data.nFileSizeLow | ((u64)data.nFileSizeHigh<<32); -#else - x.size = s.st_size; -#endif + x.size = getFileSize(fullName); int fileIndex = getFileListIndex(filename); x.startSector = fileList[fileIndex].firstBlock; @@ -1078,8 +1081,8 @@ PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) { bool VirtualDiscFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) { - outpath = GetLocalPath(inpath); - return true; + ERROR_LOG(HLE,"VirtualDiscFileSystem: Retrieving host path"); + return false; } std::vector VirtualDiscFileSystem::GetDirListing(std::string path) @@ -1091,31 +1094,31 @@ std::vector VirtualDiscFileSystem::GetDirListing(std::string path) size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { - ERROR_LOG(HLE,"Cannot write to file on virtual disc"); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot write to file on virtual disc"); return 0; } bool VirtualDiscFileSystem::MkDir(const std::string &dirname) { - ERROR_LOG(HLE,"Cannot create directory on virtual disc"); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot create directory on virtual disc"); return false; } bool VirtualDiscFileSystem::RmDir(const std::string &dirname) { - ERROR_LOG(HLE,"Cannot remove directory on virtual disc"); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot remove directory on virtual disc"); return false; } int VirtualDiscFileSystem::RenameFile(const std::string &from, const std::string &to) { - ERROR_LOG(HLE,"Cannot rename file on virtual disc"); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot rename file on virtual disc"); return -1; } bool VirtualDiscFileSystem::RemoveFile(const std::string &filename) { - ERROR_LOG(HLE,"Cannot remove file on virtual disc"); + ERROR_LOG(HLE,"VirtualDiscFileSystem: Cannot remove file on virtual disc"); return false; } diff --git a/Core/FileSystems/DirectoryFileSystem.h b/Core/FileSystems/DirectoryFileSystem.h index 8784151c22..2419b90727 100644 --- a/Core/FileSystems/DirectoryFileSystem.h +++ b/Core/FileSystems/DirectoryFileSystem.h @@ -63,7 +63,8 @@ typedef struct sDirectoryFileHandle #endif } - bool Open(std::string& fileName, FileAccess access); + std::string GetLocalPath(std::string& basePath, std::string localpath); + bool Open(std::string& basePath, std::string& fileName, FileAccess access); size_t Read(u8* pointer, s64 size); size_t Write(const u8* pointer, s64 size); size_t Seek(s32 position, FileMove type);