From 0cc3e85685496f1dd5b742c42d0b2903b9f0c735 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 25 Jun 2014 17:32:09 -0400 Subject: Core: Removed unused directory_file_system and meta_file_system modules. Core: Updated CMakeLists.txt to remove directory_file_system and meta_file_system modules. --- src/core/file_sys/directory_file_system.cpp | 669 ---------------------------- src/core/file_sys/directory_file_system.h | 155 ------- src/core/file_sys/meta_file_system.cpp | 519 --------------------- src/core/file_sys/meta_file_system.h | 110 ----- 4 files changed, 1453 deletions(-) delete mode 100644 src/core/file_sys/directory_file_system.cpp delete mode 100644 src/core/file_sys/directory_file_system.h delete mode 100644 src/core/file_sys/meta_file_system.cpp delete mode 100644 src/core/file_sys/meta_file_system.h (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/directory_file_system.cpp b/src/core/file_sys/directory_file_system.cpp deleted file mode 100644 index 6c6f33c2b..000000000 --- a/src/core/file_sys/directory_file_system.cpp +++ /dev/null @@ -1,669 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#include "common/chunk_file.h" -#include "common/file_util.h" -#include "common/utf8.h" - -#include "core/file_sys/directory_file_system.h" - -#if EMU_PLATFORM == PLATFORM_WINDOWS -#include -#include -#else -#include -#include -#include -#include -#endif - -#if HOST_IS_CASE_SENSITIVE -static bool FixFilenameCase(const std::string &path, std::string &filename) -{ - // Are we lucky? - if (File::Exists(path + filename)) - return true; - - size_t filenameSize = filename.size(); // size in bytes, not characters - for (size_t i = 0; i < filenameSize; i++) - { - filename[i] = tolower(filename[i]); - } - - //TODO: lookup filename in cache for "path" - - struct dirent_large { struct dirent entry; char padding[FILENAME_MAX+1]; } diren; - struct dirent_large; - struct dirent *result = NULL; - - DIR *dirp = opendir(path.c_str()); - if (!dirp) - return false; - - bool retValue = false; - - while (!readdir_r(dirp, (dirent*) &diren, &result) && result) - { - if (strlen(result->d_name) != filenameSize) - continue; - - size_t i; - for (i = 0; i < filenameSize; i++) - { - if (filename[i] != tolower(result->d_name[i])) - break; - } - - if (i < filenameSize) - continue; - - filename = result->d_name; - retValue = true; - } - - closedir(dirp); - - return retValue; -} - -bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior) -{ - size_t len = path.size(); - - if (len == 0) - return true; - - if (path[len - 1] == '/') - { - len--; - - if (len == 0) - return true; - } - - std::string fullPath; - fullPath.reserve(basePath.size() + len + 1); - fullPath.append(basePath); - - size_t start = 0; - while (start < len) - { - size_t i = path.find('/', start); - if (i == std::string::npos) - i = len; - - if (i > start) - { - std::string component = path.substr(start, i - start); - - // Fix case and stop on nonexistant path component - if (FixFilenameCase(fullPath, component) == false) { - // Still counts as success if partial matches allowed or if this - // is the last component and only the ones before it are required - return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len)); - } - - path.replace(start, i - start, component); - - fullPath.append(component); - fullPath.append(1, '/'); - } - - start = i + 1; - } - - return true; -} - -#endif - -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(FILESYS, "Checking case for path %s", fileName.c_str()); - 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 - - std::string fullName = GetLocalPath(basePath,fileName); - INFO_LOG(FILESYS,"Actually opening %s", fullName.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(ConvertUTF8ToWString(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 - 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(fullName.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(basePath,fileName, FPC_PATH_MUST_EXIST) ) - return 0; // or go on and attempt (for a better error code than just 0?) - fullName = GetLocalPath(basePath,fileName); - const char* fullNameC = fullName.c_str(); - - DEBUG_LOG(FILESYS, "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 = 0; -#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 = 0; -#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) { - iter->second.hFile.Close(); - } -} - -std::string DirectoryFileSystem::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; -} - -bool DirectoryFileSystem::MkDir(const std::string &dirname) { -#if HOST_IS_CASE_SENSITIVE - // Must fix case BEFORE attempting, because MkDir would create - // duplicate (different case) directories - - std::string fixedCase = dirname; - if ( ! FixPathCase(basePath,fixedCase, FPC_PARTIAL_ALLOWED) ) - return false; - - return File::CreateFullPath(GetLocalPath(fixedCase)); -#else - return File::CreateFullPath(GetLocalPath(dirname)); -#endif -} - -bool DirectoryFileSystem::RmDir(const std::string &dirname) { - std::string fullName = GetLocalPath(dirname); - -#if HOST_IS_CASE_SENSITIVE - // Maybe we're lucky? - if (File::DeleteDirRecursively(fullName)) - return true; - - // Nope, fix case and try again - fullName = dirname; - 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); -#endif - -/*#ifdef _WIN32 - return RemoveDirectory(fullName.c_str()) == TRUE; -#else - return 0 == rmdir(fullName.c_str()); -#endif*/ - return File::DeleteDirRecursively(fullName); -} - -int DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) { - std::string fullTo = to; - - // Rename ignores the path (even if specified) on to. - size_t chop_at = to.find_last_of('/'); - if (chop_at != to.npos) - fullTo = to.substr(chop_at + 1); - - // Now put it in the same directory as from. - size_t dirname_end = from.find_last_of('/'); - if (dirname_end != from.npos) - fullTo = from.substr(0, dirname_end + 1) + fullTo; - - // At this point, we should check if the paths match and give an already exists error. - if (from == fullTo) - return -1;//SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS; - - std::string fullFrom = GetLocalPath(from); - -#if HOST_IS_CASE_SENSITIVE - // In case TO should overwrite a file with different case - if ( ! FixPathCase(basePath,fullTo, FPC_PATH_MUST_EXIST) ) - return -1; // or go on and attempt (for a better error code than just false?) -#endif - - fullTo = GetLocalPath(fullTo); - const char * fullToC = fullTo.c_str(); - -#ifdef _WIN32 - bool retValue = (MoveFile(ConvertUTF8ToWString(fullFrom).c_str(), ConvertUTF8ToWString(fullToC).c_str()) == TRUE); -#else - bool retValue = (0 == rename(fullFrom.c_str(), fullToC)); -#endif - -#if HOST_IS_CASE_SENSITIVE - if (! retValue) - { - // May have failed due to case sensitivity on FROM, so try again - fullFrom = from; - 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); - -#ifdef _WIN32 - retValue = (MoveFile(fullFrom.c_str(), fullToC) == TRUE); -#else - retValue = (0 == rename(fullFrom.c_str(), fullToC)); -#endif - } -#endif - - // TODO: Better error codes. - return retValue ? 0 : -1;//SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS; -} - -bool DirectoryFileSystem::RemoveFile(const std::string &filename) { - std::string fullName = GetLocalPath(filename); -#ifdef _WIN32 - bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - bool retValue = (0 == unlink(fullName.c_str())); -#endif - -#if HOST_IS_CASE_SENSITIVE - if (! retValue) - { - // May have failed due to case sensitivity, so try again - fullName = filename; - 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); - -#ifdef _WIN32 - retValue = (::DeleteFileA(fullName.c_str()) == TRUE); -#else - retValue = (0 == unlink(fullName.c_str())); -#endif - } -#endif - - return retValue; -} - -u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) { - OpenFileEntry entry; - bool success = entry.hFile.Open(basePath,filename,access); - - if (!success) { -#ifdef _WIN32 - ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, %i - access = %i", GetLastError(), (int)access); -#else - ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, access = %i", (int)access); -#endif - //wwwwaaaaahh!! - return 0; - } else { -#ifdef _WIN32 - if (access & FILEACCESS_APPEND) - entry.hFile.Seek(0,FILEMOVE_END); -#endif - - u32 newHandle = hAlloc->GetNewHandle(); - entries[newHandle] = entry; - - return newHandle; - } -} - -void DirectoryFileSystem::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(FILESYS,"Cannot close file that hasn't been opened: %08x", handle); - } -} - -bool DirectoryFileSystem::OwnsHandle(u32 handle) { - EntryMap::iterator iter = entries.find(handle); - return (iter != entries.end()); -} - -size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) - { - size_t bytesRead = iter->second.hFile.Read(pointer,size); - return bytesRead; - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot read file that hasn't been opened: %08x", handle); - return 0; - } -} - -size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) - { - size_t bytesWritten = iter->second.hFile.Write(pointer,size); - return bytesWritten; - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot write to file that hasn't been opened: %08x", handle); - return 0; - } -} - -size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) { - EntryMap::iterator iter = entries.find(handle); - if (iter != entries.end()) { - return iter->second.hFile.Seek(position,type); - } else { - //This shouldn't happen... - ERROR_LOG(FILESYS,"Cannot seek in file that hasn't been opened: %08x", handle); - return 0; - } -} - -FileInfo DirectoryFileSystem::GetFileInfo(std::string filename) { - FileInfo x; - x.name = filename; - - std::string fullName = GetLocalPath(filename); - if (! File::Exists(fullName)) { -#if HOST_IS_CASE_SENSITIVE - if (! FixPathCase(basePath,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) - { -#ifdef _WIN32 - struct _stat64i32 s; - _wstat64i32(ConvertUTF8ToWString(fullName).c_str(), &s); -#else - struct stat s; - stat(fullName.c_str(), &s); -#endif - - x.size = File::GetSize(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); - localtime_r((time_t*)&s.st_mtime,&x.mtime); - } - - return x; -} - -bool DirectoryFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) { - outpath = GetLocalPath(inpath); - return true; -} - -#ifdef _WIN32 -#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL - -static void tmFromFiletime(tm &dest, FILETIME &src) -{ - u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL; - u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US; - - time_t t = (time_t) (from_1970_us / 1000000UL); - localtime_r(&t, &dest); -} -#endif - -std::vector DirectoryFileSystem::GetDirListing(std::string path) { - std::vector myVector; -#ifdef _WIN32 - WIN32_FIND_DATA findData; - HANDLE hFind; - - std::string w32path = GetLocalPath(path) + "\\*.*"; - - hFind = FindFirstFile(ConvertUTF8ToWString(w32path).c_str(), &findData); - - if (hFind == INVALID_HANDLE_VALUE) { - return myVector; //the empty list - } - - while (true) { - FileInfo entry; - if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - entry.type = FILETYPE_DIRECTORY; - else - entry.type = FILETYPE_NORMAL; - - // TODO: Make this more correct? - entry.access = entry.type == FILETYPE_NORMAL ? 0666 : 0777; - // TODO: is this just for .. or all subdirectories? Need to add a directory to the test - // to find out. Also why so different than the old test results? - if (!wcscmp(findData.cFileName, L"..") ) - entry.size = 4096; - else - entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32); - entry.name = ConvertWStringToUTF8(findData.cFileName); - tmFromFiletime(entry.atime, findData.ftLastAccessTime); - tmFromFiletime(entry.ctime, findData.ftCreationTime); - tmFromFiletime(entry.mtime, findData.ftLastWriteTime); - myVector.push_back(entry); - - int retval = FindNextFile(hFind, &findData); - if (!retval) - break; - } -#else - dirent *dirp; - std::string localPath = GetLocalPath(path); - DIR *dp = opendir(localPath.c_str()); - -#if HOST_IS_CASE_SENSITIVE - 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()); - } -#endif - - if (dp == NULL) { - ERROR_LOG(FILESYS,"Error opening directory %s\n",path.c_str()); - return myVector; - } - - while ((dirp = readdir(dp)) != NULL) { - FileInfo entry; - struct stat s; - std::string fullName = GetLocalPath(path) + "/"+dirp->d_name; - stat(fullName.c_str(), &s); - if (S_ISDIR(s.st_mode)) - entry.type = FILETYPE_DIRECTORY; - else - entry.type = FILETYPE_NORMAL; - entry.access = s.st_mode & 0x1FF; - entry.name = dirp->d_name; - entry.size = s.st_size; - localtime_r((time_t*)&s.st_atime,&entry.atime); - localtime_r((time_t*)&s.st_ctime,&entry.ctime); - localtime_r((time_t*)&s.st_mtime,&entry.mtime); - myVector.push_back(entry); - } - closedir(dp); -#endif - return myVector; -} - -void DirectoryFileSystem::DoState(PointerWrap &p) { - if (!entries.empty()) { - p.SetError(p.ERROR_WARNING); - ERROR_LOG(FILESYS, "FIXME: Open files during savestate, could go badly."); - } -} diff --git a/src/core/file_sys/directory_file_system.h b/src/core/file_sys/directory_file_system.h deleted file mode 100644 index 9af2854a2..000000000 --- a/src/core/file_sys/directory_file_system.h +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -// TODO: Remove the Windows-specific code, FILE is fine there too. - -#include - -#include "core/file_sys/file_sys.h" - -#ifdef _WIN32 -typedef void * HANDLE; -#endif - -#if defined(__APPLE__) - -#if TARGET_OS_IPHONE -#define HOST_IS_CASE_SENSITIVE 1 -#elif TARGET_IPHONE_SIMULATOR -#define HOST_IS_CASE_SENSITIVE 0 -#else -// Mac OSX case sensitivity defaults off, but is user configurable (when -// creating a filesytem), so assume the worst: -#define HOST_IS_CASE_SENSITIVE 1 -#endif - -#elif defined(_WIN32) || defined(__SYMBIAN32__) -#define HOST_IS_CASE_SENSITIVE 0 - -#else // Android, Linux, BSD (and the rest?) -#define HOST_IS_CASE_SENSITIVE 1 - -#endif - -#if HOST_IS_CASE_SENSITIVE -enum FixPathCaseBehavior { - 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) -}; - -bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior); -#endif - -struct DirectoryFileHandle -{ -#ifdef _WIN32 - HANDLE hFile; -#else - FILE* hFile; -#endif - DirectoryFileHandle() - { -#ifdef _WIN32 - hFile = (HANDLE)-1; -#else - hFile = 0; -#endif - } - - 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); - void Close(); -}; - -class DirectoryFileSystem : public IFileSystem { -public: - DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); - ~DirectoryFileSystem(); - - void DoState(PointerWrap &p); - std::vector GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle); - - 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); - bool GetHostPath(const std::string &inpath, std::string &outpath); - -private: - struct OpenFileEntry { - DirectoryFileHandle hFile; - }; - - typedef std::map EntryMap; - EntryMap entries; - std::string basePath; - IHandleAllocator *hAlloc; - - // In case of Windows: Translate slashes, etc. - std::string GetLocalPath(std::string localpath); -}; - -// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts. -// Very inefficient - always load the whole file on open. -class VFSFileSystem : public IFileSystem { -public: - VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath); - ~VFSFileSystem(); - - void DoState(PointerWrap &p); - std::vector GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle); - - 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); - bool GetHostPath(const std::string &inpath, std::string &outpath); - -private: - struct OpenFileEntry { - u8 *fileData; - size_t size; - size_t seekPos; - }; - - typedef std::map EntryMap; - EntryMap entries; - std::string basePath; - IHandleAllocator *hAlloc; - - std::string GetLocalPath(std::string localpath); -}; diff --git a/src/core/file_sys/meta_file_system.cpp b/src/core/file_sys/meta_file_system.cpp deleted file mode 100644 index 4347ff451..000000000 --- a/src/core/file_sys/meta_file_system.cpp +++ /dev/null @@ -1,519 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#include -#include - -#include "common/string_util.h" -#include "core/file_sys/meta_file_system.h" - -static bool ApplyPathStringToComponentsVector(std::vector &vector, const std::string &pathString) -{ - size_t len = pathString.length(); - size_t start = 0; - - while (start < len) - { - size_t i = pathString.find('/', start); - if (i == std::string::npos) - i = len; - - if (i > start) - { - std::string component = pathString.substr(start, i - start); - if (component != ".") - { - if (component == "..") - { - if (vector.size() != 0) - { - vector.pop_back(); - } - else - { - // The PSP silently ignores attempts to .. to parent of root directory - WARN_LOG(FILESYS, "RealPath: ignoring .. beyond root - root directory is its own parent: \"%s\"", pathString.c_str()); - } - } - else - { - vector.push_back(component); - } - } - } - - start = i + 1; - } - - return true; -} - -/* - * Changes relative paths to absolute, removes ".", "..", and trailing "/" - * "drive:./blah" is absolute (ignore the dot) and "/blah" is relative (because it's missing "drive:") - * babel (and possibly other games) use "/directoryThatDoesNotExist/../directoryThatExists/filename" - */ -static bool RealPath(const std::string ¤tDirectory, const std::string &inPath, std::string &outPath) -{ - size_t inLen = inPath.length(); - if (inLen == 0) - { - WARN_LOG(FILESYS, "RealPath: inPath is empty"); - outPath = currentDirectory; - return true; - } - - size_t inColon = inPath.find(':'); - if (inColon + 1 == inLen) - { - // There's nothing after the colon, e.g. umd0: - this is perfectly valid. - outPath = inPath; - return true; - } - - bool relative = (inColon == std::string::npos); - - std::string prefix, inAfterColon; - std::vector cmpnts; // path components - size_t outPathCapacityGuess = inPath.length(); - - if (relative) - { - size_t curDirLen = currentDirectory.length(); - if (curDirLen == 0) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory is empty", inPath.c_str()); - return false; - } - - size_t curDirColon = currentDirectory.find(':'); - if (curDirColon == std::string::npos) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" has no prefix", inPath.c_str(), currentDirectory.c_str()); - return false; - } - if (curDirColon + 1 == curDirLen) - { - ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" is all prefix and no path. Using \"/\" as path for current directory.", inPath.c_str(), currentDirectory.c_str()); - } - else - { - const std::string curDirAfter = currentDirectory.substr(curDirColon + 1); - if (! ApplyPathStringToComponentsVector(cmpnts, curDirAfter) ) - { - ERROR_LOG(FILESYS,"RealPath: currentDirectory is not a valid path: \"%s\"", currentDirectory.c_str()); - return false; - } - - outPathCapacityGuess += curDirLen; - } - - prefix = currentDirectory.substr(0, curDirColon + 1); - inAfterColon = inPath; - } - else - { - prefix = inPath.substr(0, inColon + 1); - inAfterColon = inPath.substr(inColon + 1); - } - - // Special case: "disc0:" is different from "disc0:/", so keep track of the single slash. - if (inAfterColon == "/") - { - outPath = prefix + inAfterColon; - return true; - } - - if (! ApplyPathStringToComponentsVector(cmpnts, inAfterColon) ) - { - WARN_LOG(FILESYS, "RealPath: inPath is not a valid path: \"%s\"", inPath.c_str()); - return false; - } - - outPath.clear(); - outPath.reserve(outPathCapacityGuess); - - outPath.append(prefix); - - size_t numCmpnts = cmpnts.size(); - for (size_t i = 0; i < numCmpnts; i++) - { - outPath.append(1, '/'); - outPath.append(cmpnts[i]); - } - - return true; -} - -IFileSystem *MetaFileSystem::GetHandleOwner(u32 handle) -{ - std::lock_guard guard(lock); - for (size_t i = 0; i < fileSystems.size(); i++) - { - if (fileSystems[i].system->OwnsHandle(handle)) - return fileSystems[i].system; //got it! - } - //none found? - return 0; -} - -bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpath, MountPoint **system) -{ - std::lock_guard guard(lock); - std::string realpath; - - // Special handling: host0:command.txt (as seen in Super Monkey Ball Adventures, for example) - // appears to mean the current directory on the UMD. Let's just assume the current directory. - std::string inpath = _inpath; - if (strncasecmp(inpath.c_str(), "host0:", strlen("host0:")) == 0) { - INFO_LOG(FILESYS, "Host0 path detected, stripping: %s", inpath.c_str()); - inpath = inpath.substr(strlen("host0:")); - } - - const std::string *currentDirectory = &startingDirectory; - - _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread"); - - int currentThread = 0;//__KernelGetCurThread(); - currentDir_t::iterator it = currentDir.find(currentThread); - if (it == currentDir.end()) - { - //Attempt to emulate SCE_KERNEL_ERROR_NOCWD / 8002032C: may break things requiring fixes elsewhere - if (inpath.find(':') == std::string::npos /* means path is relative */) - { - lastOpenError = -1;//SCE_KERNEL_ERROR_NOCWD; - WARN_LOG(FILESYS, "Path is relative, but current directory not set for thread %i. returning 8002032C(SCE_KERNEL_ERROR_NOCWD) instead.", currentThread); - } - } - else - { - currentDirectory = &(it->second); - } - - if ( RealPath(*currentDirectory, inpath, realpath) ) - { - for (size_t i = 0; i < fileSystems.size(); i++) - { - size_t prefLen = fileSystems[i].prefix.size(); - if (strncasecmp(fileSystems[i].prefix.c_str(), realpath.c_str(), prefLen) == 0) - { - outpath = realpath.substr(prefLen); - *system = &(fileSystems[i]); - - INFO_LOG(FILESYS, "MapFilePath: mapped \"%s\" to prefix: \"%s\", path: \"%s\"", inpath.c_str(), fileSystems[i].prefix.c_str(), outpath.c_str()); - - return true; - } - } - } - - DEBUG_LOG(FILESYS, "MapFilePath: failed mapping \"%s\", returning false", inpath.c_str()); - return false; -} - -void MetaFileSystem::Mount(std::string prefix, IFileSystem *system) -{ - std::lock_guard guard(lock); - MountPoint x; - x.prefix = prefix; - x.system = system; - fileSystems.push_back(x); -} - -void MetaFileSystem::Unmount(std::string prefix, IFileSystem *system) -{ - std::lock_guard guard(lock); - MountPoint x; - x.prefix = prefix; - x.system = system; - fileSystems.erase(std::remove(fileSystems.begin(), fileSystems.end(), x), fileSystems.end()); -} - -void MetaFileSystem::Shutdown() -{ - std::lock_guard guard(lock); - current = 6; - - // Ownership is a bit convoluted. Let's just delete everything once. - - std::set toDelete; - for (size_t i = 0; i < fileSystems.size(); i++) { - toDelete.insert(fileSystems[i].system); - } - - for (auto iter = toDelete.begin(); iter != toDelete.end(); ++iter) - { - delete *iter; - } - - fileSystems.clear(); - currentDir.clear(); - startingDirectory = ""; -} - -u32 MetaFileSystem::OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename) -{ - std::lock_guard guard(lock); - u32 h = OpenFile(filename, access, devicename); - error = lastOpenError; - return h; -} - -u32 MetaFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) -{ - std::lock_guard guard(lock); - lastOpenError = 0; - std::string of; - MountPoint *mount; - if (MapFilePath(filename, of, &mount)) - { - return mount->system->OpenFile(of, access, mount->prefix.c_str()); - } - else - { - return 0; - } -} - -FileInfo MetaFileSystem::GetFileInfo(std::string filename) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(filename, of, &system)) - { - return system->GetFileInfo(of); - } - else - { - FileInfo bogus; // TODO - return bogus; - } -} - -bool MetaFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(inpath, of, &system)) { - return system->GetHostPath(of, outpath); - } else { - return false; - } -} - -std::vector MetaFileSystem::GetDirListing(std::string path) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(path, of, &system)) - { - return system->GetDirListing(of); - } - else - { - std::vector empty; - return empty; - } -} - -void MetaFileSystem::ThreadEnded(int threadID) -{ - std::lock_guard guard(lock); - currentDir.erase(threadID); -} - -int MetaFileSystem::ChDir(const std::string &dir) -{ - std::lock_guard guard(lock); - // Retain the old path and fail if the arg is 1023 bytes or longer. - if (dir.size() >= 1023) - return -1;//SCE_KERNEL_ERROR_NAMETOOLONG; - - _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread"); - - int curThread = 0; //__KernelGetCurThread(); - - std::string of; - MountPoint *mountPoint; - if (MapFilePath(dir, of, &mountPoint)) - { - currentDir[curThread] = mountPoint->prefix + of; - return 0; - } - else - { - for (size_t i = 0; i < fileSystems.size(); i++) - { - const std::string &prefix = fileSystems[i].prefix; - if (strncasecmp(prefix.c_str(), dir.c_str(), prefix.size()) == 0) - { - // The PSP is completely happy with invalid current dirs as long as they have a valid device. - WARN_LOG(FILESYS, "ChDir failed to map path \"%s\", saving as current directory anyway", dir.c_str()); - currentDir[curThread] = dir; - return 0; - } - } - - WARN_LOG(FILESYS, "ChDir failed to map device for \"%s\", failing", dir.c_str()); - return -1;//SCE_KERNEL_ERROR_NODEV; - } -} - -bool MetaFileSystem::MkDir(const std::string &dirname) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(dirname, of, &system)) - { - return system->MkDir(of); - } - else - { - return false; - } -} - -bool MetaFileSystem::RmDir(const std::string &dirname) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(dirname, of, &system)) - { - return system->RmDir(of); - } - else - { - return false; - } -} - -int MetaFileSystem::RenameFile(const std::string &from, const std::string &to) -{ - std::lock_guard guard(lock); - std::string of; - std::string rf; - IFileSystem *osystem; - IFileSystem *rsystem = NULL; - if (MapFilePath(from, of, &osystem)) - { - // If it's a relative path, it seems to always use from's filesystem. - if (to.find(":/") != to.npos) - { - if (!MapFilePath(to, rf, &rsystem)) - return -1; - } - else - { - rf = to; - rsystem = osystem; - } - - if (osystem != rsystem) - return -1;//SCE_KERNEL_ERROR_XDEV; - - return osystem->RenameFile(of, rf); - } - else - { - return -1; - } -} - -bool MetaFileSystem::RemoveFile(const std::string &filename) -{ - std::lock_guard guard(lock); - std::string of; - IFileSystem *system; - if (MapFilePath(filename, of, &system)) - { - return system->RemoveFile(of); - } - else - { - return false; - } -} - -void MetaFileSystem::CloseFile(u32 handle) -{ - std::lock_guard guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - sys->CloseFile(handle); -} - -size_t MetaFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) -{ - std::lock_guard guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->ReadFile(handle,pointer,size); - else - return 0; -} - -size_t MetaFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) -{ - std::lock_guard guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->WriteFile(handle,pointer,size); - else - return 0; -} - -size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type) -{ - std::lock_guard guard(lock); - IFileSystem *sys = GetHandleOwner(handle); - if (sys) - return sys->SeekFile(handle,position,type); - else - return 0; -} - -void MetaFileSystem::DoState(PointerWrap &p) -{ - std::lock_guard guard(lock); - - auto s = p.Section("MetaFileSystem", 1); - if (!s) - return; - - p.Do(current); - - // Save/load per-thread current directory map - p.Do(currentDir); - - u32 n = (u32) fileSystems.size(); - p.Do(n); - if (n != (u32) fileSystems.size()) - { - p.SetError(p.ERROR_FAILURE); - ERROR_LOG(FILESYS, "Savestate failure: number of filesystems doesn't match."); - return; - } - - for (u32 i = 0; i < n; ++i) - fileSystems[i].system->DoState(p); -} - diff --git a/src/core/file_sys/meta_file_system.h b/src/core/file_sys/meta_file_system.h deleted file mode 100644 index f358d8d5c..000000000 --- a/src/core/file_sys/meta_file_system.h +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -#include "common/std_mutex.h" - -#include "core/file_sys/file_sys.h" - -class MetaFileSystem : public IHandleAllocator, public IFileSystem -{ -private: - u32 current; - struct MountPoint - { - std::string prefix; - IFileSystem *system; - - bool operator == (const MountPoint &other) const - { - return prefix == other.prefix && system == other.system; - } - }; - std::vector fileSystems; - - typedef std::map currentDir_t; - currentDir_t currentDir; - - std::string startingDirectory; - int lastOpenError; - std::recursive_mutex lock; - -public: - MetaFileSystem() - { - current = 6; // what? - } - - void Mount(std::string prefix, IFileSystem *system); - void Unmount(std::string prefix, IFileSystem *system); - - void ThreadEnded(int threadID); - - void Shutdown(); - - u32 GetNewHandle() {return current++;} - void FreeHandle(u32 handle) {} - - virtual void DoState(PointerWrap &p); - - IFileSystem *GetHandleOwner(u32 handle); - bool MapFilePath(const std::string &inpath, std::string &outpath, MountPoint **system); - - inline bool MapFilePath(const std::string &_inpath, std::string &outpath, IFileSystem **system) - { - MountPoint *mountPoint; - if (MapFilePath(_inpath, outpath, &mountPoint)) - { - *system = mountPoint->system; - return true; - } - - return false; - } - - // Only possible if a file system is a DirectoryFileSystem or similar. - bool GetHostPath(const std::string &inpath, std::string &outpath); - - std::vector GetDirListing(std::string path); - u32 OpenFile(std::string filename, FileAccess access, const char *devicename = NULL); - u32 OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename = NULL); - void CloseFile(u32 handle); - size_t ReadFile(u32 handle, u8 *pointer, s64 size); - size_t WriteFile(u32 handle, const u8 *pointer, s64 size); - size_t SeekFile(u32 handle, s32 position, FileMove type); - FileInfo GetFileInfo(std::string filename); - bool OwnsHandle(u32 handle) {return false;} - inline size_t GetSeekPos(u32 handle) - { - return SeekFile(handle, 0, FILEMOVE_CURRENT); - } - - virtual int ChDir(const std::string &dir); - - virtual bool MkDir(const std::string &dirname); - virtual bool RmDir(const std::string &dirname); - virtual int RenameFile(const std::string &from, const std::string &to); - virtual bool RemoveFile(const std::string &filename); - - // TODO: void IoCtl(...) - - void SetStartingDirectory(const std::string &dir) { - std::lock_guard guard(lock); - startingDirectory = dir; - } -}; -- cgit v1.2.3 From 17a6148f9df406a6ca4bdca98777e0aaf21f582a Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 27 Jun 2014 16:18:56 -0400 Subject: FileSys: Added preliminary support for applications reading the RomFS archive. Archive: Fixed brace ugliness for neobrain :) FS: Commented out unused local variables to prevent warnings. ...But keeping them here for future use. archive_romfs: Removed unused #include. --- src/core/CMakeLists.txt | 4 +- src/core/core.vcxproj | 4 +- src/core/core.vcxproj.filters | 12 +++- src/core/file_sys/archive.h | 54 ++++++++++++++ src/core/file_sys/archive_romfs.cpp | 46 ++++++++++++ src/core/file_sys/archive_romfs.h | 50 +++++++++++++ src/core/file_sys/file_sys.h | 138 ------------------------------------ src/core/hle/kernel/archive.cpp | 98 +++++++++++++++++++++++-- src/core/hle/kernel/archive.h | 17 +++-- src/core/hle/service/fs.cpp | 33 ++++++++- src/core/loader/loader.cpp | 15 ++-- 11 files changed, 311 insertions(+), 160 deletions(-) create mode 100644 src/core/file_sys/archive.h create mode 100644 src/core/file_sys/archive_romfs.cpp create mode 100644 src/core/file_sys/archive_romfs.h delete mode 100644 src/core/file_sys/file_sys.h (limited to 'src/core/file_sys') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 978e956dc..9ee803fda 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -8,6 +8,7 @@ set(SRCS core.cpp system.cpp arm/disassembler/arm_disasm.cpp arm/disassembler/load_symbol_map.cpp + file_sys/archive_romfs.cpp arm/interpreter/arm_interpreter.cpp arm/interpreter/armcopro.cpp arm/interpreter/armemu.cpp @@ -75,7 +76,8 @@ set(HEADERS core.h arm/interpreter/vfp/asm_vfp.h arm/interpreter/vfp/vfp.h arm/interpreter/vfp/vfp_helper.h - file_sys/file_sys.h + file_sys/archive.h + file_sys/archive_romfs.h hle/config_mem.h hle/coprocessor.h hle/hle.h diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj index 63efe7c4d..4e521903c 100644 --- a/src/core/core.vcxproj +++ b/src/core/core.vcxproj @@ -162,6 +162,7 @@ + @@ -211,7 +212,8 @@ - + + diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters index 39a3cdc4b..17829b8b1 100644 --- a/src/core/core.vcxproj.filters +++ b/src/core/core.vcxproj.filters @@ -176,6 +176,9 @@ hle\service + + file_sys + @@ -205,9 +208,6 @@ arm\interpreter - - file_sys - hw @@ -314,6 +314,12 @@ hle\service + + file_sys + + + file_sys + diff --git a/src/core/file_sys/archive.h b/src/core/file_sys/archive.h new file mode 100644 index 000000000..ed2d83640 --- /dev/null +++ b/src/core/file_sys/archive.h @@ -0,0 +1,54 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +class Archive : NonCopyable { +public: + /// Supported archive types + enum class IdCode : u32 { + RomFS = 0x00000003, + SaveData = 0x00000004, + ExtSaveData = 0x00000006, + SharedExtSaveData = 0x00000007, + SystemSaveData = 0x00000008, + SDMC = 0x00000009, + SDMCWriteOnly = 0x0000000A, + }; + + Archive() { } + virtual ~Archive() { } + + /** + * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.) + * @return IdCode of the archive + */ + virtual IdCode GetIdCode() const = 0; + + /** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ + virtual size_t Read(const u64 offset, const u32 length, u8* buffer) const = 0; + + /** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ + virtual size_t GetSize() const = 0; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp new file mode 100644 index 000000000..6fdb768d6 --- /dev/null +++ b/src/core/file_sys/archive_romfs.cpp @@ -0,0 +1,46 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/file_sys/archive_romfs.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +Archive_RomFS::Archive_RomFS(Loader::AppLoader& app_loader) { + // Load the RomFS from the app + if (Loader::ResultStatus::Success != app_loader.ReadRomFS(raw_data)) { + WARN_LOG(FILESYS, "Unable to read RomFS!"); + } +} + +Archive_RomFS::~Archive_RomFS() { +} + +/** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ +size_t Archive_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const { + DEBUG_LOG(FILESYS, "called offset=%d, length=%d", offset, length); + memcpy(buffer, &raw_data[(u32)offset], length); + return length; +} + +/** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ +size_t Archive_RomFS::GetSize() const { + ERROR_LOG(FILESYS, "(UNIMPLEMENTED)"); + return 0; +} + +} // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h new file mode 100644 index 000000000..60af8ff0d --- /dev/null +++ b/src/core/file_sys/archive_romfs.h @@ -0,0 +1,50 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/common_types.h" + +#include "core/file_sys/archive.h" +#include "core/loader/loader.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +/// File system interface to the RomFS archive +class Archive_RomFS : public Archive { +public: + Archive_RomFS(Loader::AppLoader& app_loader); + ~Archive_RomFS(); + + /** + * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.) + * @return IdCode of the archive + */ + IdCode GetIdCode() const { return IdCode::RomFS; }; + + /** + * Read data from the archive + * @param offset Offset in bytes to start reading archive from + * @param length Length in bytes to read data from archive + * @param buffer Buffer to read data into + * @return Number of bytes read + */ + size_t Read(const u64 offset, const u32 length, u8* buffer) const; + + /** + * Get the size of the archive in bytes + * @return Size of the archive in bytes + */ + size_t GetSize() const; + +private: + std::vector raw_data; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/file_sys.h b/src/core/file_sys/file_sys.h deleted file mode 100644 index bb8503e62..000000000 --- a/src/core/file_sys/file_sys.h +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -#include "common/common.h" -#include "common/chunk_file.h" - -enum FileAccess { - FILEACCESS_NONE=0, - FILEACCESS_READ=1, - FILEACCESS_WRITE=2, - FILEACCESS_APPEND=4, - FILEACCESS_CREATE=8 -}; - -enum FileMove { - FILEMOVE_BEGIN=0, - FILEMOVE_CURRENT=1, - FILEMOVE_END=2 -}; - -enum FileType { - FILETYPE_NORMAL=1, - FILETYPE_DIRECTORY=2 -}; - - -class IHandleAllocator { -public: - virtual ~IHandleAllocator() {} - virtual u32 GetNewHandle() = 0; - virtual void FreeHandle(u32 handle) = 0; -}; - -class SequentialHandleAllocator : public IHandleAllocator { -public: - SequentialHandleAllocator() : handle_(1) {} - virtual u32 GetNewHandle() { return handle_++; } - virtual void FreeHandle(u32 handle) {} -private: - int handle_; -}; - -struct FileInfo { - FileInfo() - : size(0), access(0), exists(false), type(FILETYPE_NORMAL), isOnSectorSystem(false), startSector(0), numSectors(0) {} - - void DoState(PointerWrap &p) { - auto s = p.Section("FileInfo", 1); - if (!s) - return; - - p.Do(name); - p.Do(size); - p.Do(access); - p.Do(exists); - p.Do(type); - p.Do(atime); - p.Do(ctime); - p.Do(mtime); - p.Do(isOnSectorSystem); - p.Do(startSector); - p.Do(numSectors); - p.Do(sectorSize); - } - - std::string name; - s64 size; - u32 access; //unix 777 - bool exists; - FileType type; - - tm atime; - tm ctime; - tm mtime; - - bool isOnSectorSystem; - u32 startSector; - u32 numSectors; - u32 sectorSize; -}; - - -class IFileSystem { -public: - virtual ~IFileSystem() {} - - virtual void DoState(PointerWrap &p) = 0; - virtual std::vector GetDirListing(std::string path) = 0; - virtual u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) = 0; - virtual void CloseFile(u32 handle) = 0; - virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size) = 0; - virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size) = 0; - virtual size_t SeekFile(u32 handle, s32 position, FileMove type) = 0; - virtual FileInfo GetFileInfo(std::string filename) = 0; - virtual bool OwnsHandle(u32 handle) = 0; - virtual bool MkDir(const std::string &dirname) = 0; - virtual bool RmDir(const std::string &dirname) = 0; - virtual int RenameFile(const std::string &from, const std::string &to) = 0; - virtual bool RemoveFile(const std::string &filename) = 0; - virtual bool GetHostPath(const std::string &inpath, std::string &outpath) = 0; -}; - - -class EmptyFileSystem : public IFileSystem { -public: - virtual void DoState(PointerWrap &p) {} - std::vector GetDirListing(std::string path) {std::vector vec; return vec;} - u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) {return 0;} - void CloseFile(u32 handle) {} - size_t ReadFile(u32 handle, u8 *pointer, s64 size) {return 0;} - size_t WriteFile(u32 handle, const u8 *pointer, s64 size) {return 0;} - size_t SeekFile(u32 handle, s32 position, FileMove type) {return 0;} - FileInfo GetFileInfo(std::string filename) {FileInfo f; return f;} - 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 int RenameFile(const std::string &from, const std::string &to) {return -1;} - virtual bool RemoveFile(const std::string &filename) {return false;} - virtual bool GetHostPath(const std::string &inpath, std::string &outpath) {return false;} -}; - - diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp index d7351e702..f31c02ea9 100644 --- a/src/core/hle/kernel/archive.cpp +++ b/src/core/hle/kernel/archive.cpp @@ -4,6 +4,8 @@ #include "common/common_types.h" +#include "core/file_sys/archive.h" +#include "core/hle/service/service.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/archive.h" @@ -12,6 +14,21 @@ namespace Kernel { +// Command to access archive file +enum class FileCommand : u32 { + Dummy1 = 0x000100C6, + Control = 0x040100C4, + OpenSubFile = 0x08010100, + Read = 0x080200C2, + Write = 0x08030102, + GetSize = 0x08040000, + SetSize = 0x08050080, + GetAttributes = 0x08060000, + SetAttributes = 0x08070040, + Close = 0x08080000, + Flush = 0x08090000, +}; + class Archive : public Object { public: const char* GetTypeName() const { return "Archive"; } @@ -20,7 +37,37 @@ public: static Kernel::HandleType GetStaticHandleType() { return HandleType::Archive; } Kernel::HandleType GetHandleType() const { return HandleType::Archive; } - std::string name; ///< Name of archive (optional) + std::string name; ///< Name of archive (optional) + FileSys::Archive* backend; ///< Archive backend interface + + /** + * Synchronize kernel object + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result SyncRequest(bool* wait) { + u32* cmd_buff = Service::GetCommandBuffer(); + FileCommand cmd = static_cast(cmd_buff[0]); + switch (cmd) { + + // Read from archive... + case FileCommand::Read: + { + u64 offset = cmd_buff[1] | ((u64) cmd_buff[2]) << 32; + u32 length = cmd_buff[3]; + u32 address = cmd_buff[5]; + cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address)); + break; + } + + // Unknown command... + default: + ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd); + return -1; + } + cmd_buff[1] = 0; // No error + return 0; + } /** * Wait for kernel object to synchronize @@ -29,32 +76,71 @@ public: */ Result WaitSynchronization(bool* wait) { // TODO(bunnei): ImplementMe - ERROR_LOG(OSHLE, "unimplemented function"); + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); return 0; } }; +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::map g_archive_map; ///< Map of file archives by IdCode + +/** + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) + */ +Handle OpenArchive(FileSys::Archive::IdCode id_code) { + auto itr = g_archive_map.find(id_code); + if (itr == g_archive_map.end()) { + return 0; + } + return itr->second; +} + +/** + * Mounts an archive + * @param archive Pointer to the archive to mount + * @return Result of operation, 0 on success, otherwise error code + */ +Result MountArchive(Archive* archive) { + FileSys::Archive::IdCode id_code = archive->backend->GetIdCode(); + if (0 != OpenArchive(id_code)) { + ERROR_LOG(KERNEL, "Cannot mount two archives with the same ID code! (%d)", (int) id_code); + return -1; + } + g_archive_map[id_code] = archive->GetHandle(); + INFO_LOG(KERNEL, "Mounted archive %s", archive->GetName()); + return 0; +} + /** * Creates an Archive - * @param name Optional name of Archive * @param handle Handle to newly created archive object + * @param backend File system backend interface to the archive + * @param name Optional name of Archive * @return Newly created Archive object */ -Archive* CreateArchive(Handle& handle, const std::string& name) { +Archive* CreateArchive(Handle& handle, FileSys::Archive* backend, const std::string& name) { Archive* archive = new Archive; handle = Kernel::g_object_pool.Create(archive); archive->name = name; + archive->backend = backend; + + MountArchive(archive); + return archive; } /** * Creates an Archive + * @param backend File system backend interface to the archive * @param name Optional name of Archive * @return Handle to newly created Archive object */ -Handle CreateArchive(const std::string& name) { +Handle CreateArchive(FileSys::Archive* backend, const std::string& name) { Handle handle; - Archive* archive = CreateArchive(handle, name); + Archive* archive = CreateArchive(handle, backend, name); return handle; } diff --git a/src/core/hle/kernel/archive.h b/src/core/hle/kernel/archive.h index 98670f06c..07fb37ae7 100644 --- a/src/core/hle/kernel/archive.h +++ b/src/core/hle/kernel/archive.h @@ -7,6 +7,7 @@ #include "common/common_types.h" #include "core/hle/kernel/kernel.h" +#include "core/file_sys/archive.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Kernel namespace @@ -14,10 +15,18 @@ namespace Kernel { /** - * Creates an archive - * @param name Optional name of archive - * @return Handle to newly created archive object + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) */ -Handle CreateArchive(const std::string& name="Unknown"); +Handle OpenArchive(FileSys::Archive::IdCode id_code); + +/** + * Creates an Archive + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Handle to newly created Archive object + */ +Handle CreateArchive(FileSys::Archive* backend, const std::string& name); } // namespace FileSys diff --git a/src/core/hle/service/fs.cpp b/src/core/hle/service/fs.cpp index 3a5afaa3c..5eabf36ad 100644 --- a/src/core/hle/service/fs.cpp +++ b/src/core/hle/service/fs.cpp @@ -2,11 +2,12 @@ // Licensed under GPLv2 // Refer to the license.txt file included. - #include "common/common.h" +#include "core/loader/loader.h" #include "core/hle/hle.h" #include "core/hle/service/fs.h" +#include "core/hle/kernel/archive.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace FS_User @@ -15,8 +16,34 @@ namespace FS_User { void Initialize(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); - DEBUG_LOG(KERNEL, "called"); cmd_buff[1] = 0; // No error + DEBUG_LOG(KERNEL, "called"); +} + +void OpenFileDirectly(Service::Interface* self) { + u32* cmd_buff = Service::GetCommandBuffer(); + + FileSys::Archive::IdCode arch_id = static_cast(cmd_buff[2]); + + // TODO(bunnei): Properly implement use of these... + //u32 transaction = cmd_buff[1]; + //u32 arch_lowpath_type = cmd_buff[3]; + //u32 arch_lowpath_sz = cmd_buff[4]; + //u32 file_lowpath_type = cmd_buff[5]; + //u32 file_lowpath_sz = cmd_buff[6]; + //u32 flags = cmd_buff[7]; + //u32 attr = cmd_buff[8]; + //u32 arch_lowpath_desc = cmd_buff[9]; + //u32 arch_lowpath_ptr = cmd_buff[10]; + //u32 file_lowpath_desc = cmd_buff[11]; + //u32 file_lowpath_ptr = cmd_buff[12]; + + Handle handle = Kernel::OpenArchive(arch_id); + if (0 != handle) { + cmd_buff[1] = 0; // No error + cmd_buff[3] = handle; + } + DEBUG_LOG(KERNEL, "called"); } const Interface::FunctionInfo FunctionTable[] = { @@ -24,7 +51,7 @@ const Interface::FunctionInfo FunctionTable[] = { {0x040100C4, nullptr, "Control"}, {0x08010002, Initialize, "Initialize"}, {0x080201C2, nullptr, "OpenFile"}, - {0x08030204, nullptr, "OpenFileDirectly"}, + {0x08030204, OpenFileDirectly, "OpenFileDirectly"}, {0x08040142, nullptr, "DeleteFile"}, {0x08050244, nullptr, "RenameFile"}, {0x08060142, nullptr, "DeleteDirectory"}, diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 96cb81de0..2b42e3c64 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -4,9 +4,11 @@ #include +#include "core/file_sys/archive_romfs.h" #include "core/loader/loader.h" #include "core/loader/elf.h" #include "core/loader/ncch.h" +#include "core/hle/kernel/archive.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -51,14 +53,20 @@ ResultStatus LoadFile(const std::string& filename) { switch (IdentifyFile(filename)) { // Standard ELF file format... - case FileType::ELF: { + case FileType::ELF: return AppLoader_ELF(filename).Load(); - } // NCCH/NCSD container formats... case FileType::CXI: case FileType::CCI: { - return AppLoader_NCCH(filename).Load(); + AppLoader_NCCH app_loader(filename); + + // Load application and RomFS + if (ResultStatus::Success == app_loader.Load()) { + Kernel::CreateArchive(new FileSys::Archive_RomFS(app_loader), "RomFS"); + return ResultStatus::Success; + } + break; } // Error occurred durring IdentifyFile... @@ -70,7 +78,6 @@ ResultStatus LoadFile(const std::string& filename) { default: return ResultStatus::ErrorInvalidFormat; } - return ResultStatus::Error; } -- cgit v1.2.3 From 2c62d9255160b7302942d536c9c990b86c7fb907 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 4 Jul 2014 13:11:09 -0400 Subject: Loader: Updated read methods to be const - Required "file" handle to be made local and explicitly opened/closed as needed --- src/core/file_sys/archive_romfs.cpp | 2 +- src/core/file_sys/archive_romfs.h | 2 +- src/core/loader/loader.h | 10 +-- src/core/loader/ncch.cpp | 118 ++++++++++++++++++++---------------- src/core/loader/ncch.h | 15 +++-- 5 files changed, 79 insertions(+), 68 deletions(-) (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp index 6fdb768d6..fd84b9c8c 100644 --- a/src/core/file_sys/archive_romfs.cpp +++ b/src/core/file_sys/archive_romfs.cpp @@ -11,7 +11,7 @@ namespace FileSys { -Archive_RomFS::Archive_RomFS(Loader::AppLoader& app_loader) { +Archive_RomFS::Archive_RomFS(const Loader::AppLoader& app_loader) { // Load the RomFS from the app if (Loader::ResultStatus::Success != app_loader.ReadRomFS(raw_data)) { WARN_LOG(FILESYS, "Unable to read RomFS!"); diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index 60af8ff0d..5ef4fed50 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -19,7 +19,7 @@ namespace FileSys { /// File system interface to the RomFS archive class Archive_RomFS : public Archive { public: - Archive_RomFS(Loader::AppLoader& app_loader); + Archive_RomFS(const Loader::AppLoader& app_loader); ~Archive_RomFS(); /** diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 7891c1ed7..c27b5b4b6 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -51,7 +51,7 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - virtual ResultStatus ReadCode(std::vector& buffer) { + virtual ResultStatus ReadCode(std::vector& buffer) const { return ResultStatus::ErrorNotImplemented; } @@ -60,7 +60,7 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - virtual ResultStatus ReadIcon(std::vector& buffer) { + virtual ResultStatus ReadIcon(std::vector& buffer) const { return ResultStatus::ErrorNotImplemented; } @@ -69,7 +69,7 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - virtual ResultStatus ReadBanner(std::vector& buffer) { + virtual ResultStatus ReadBanner(std::vector& buffer) const { return ResultStatus::ErrorNotImplemented; } @@ -78,7 +78,7 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - virtual ResultStatus ReadLogo(std::vector& buffer) { + virtual ResultStatus ReadLogo(std::vector& buffer) const { return ResultStatus::ErrorNotImplemented; } @@ -87,7 +87,7 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - virtual ResultStatus ReadRomFS(std::vector& buffer) { + virtual ResultStatus ReadRomFS(std::vector& buffer) const { return ResultStatus::ErrorNotImplemented; } }; diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 0e3f768ce..a82338904 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -113,15 +113,13 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { /// AppLoader_NCCH destructor AppLoader_NCCH::~AppLoader_NCCH() { - if (file.IsOpen()) - file.Close(); } /** * Loads .code section into memory for booting * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::LoadExec() { +ResultStatus AppLoader_NCCH::LoadExec() const { if (!is_loaded) return ResultStatus::ErrorNotLoaded; @@ -140,41 +138,48 @@ ResultStatus AppLoader_NCCH::LoadExec() { * @param buffer Vector to read data into * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) { +ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) const { // Iterate through the ExeFs archive until we find the .code file... - for (int i = 0; i < kMaxSections; i++) { - // Load the specified section... - if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { - INFO_LOG(LOADER, "ExeFS section %d:", i); - INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name); - INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset); - INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); - - s64 section_offset = (exefs_header.section[i].offset + exefs_offset + - sizeof(ExeFs_Header) + ncch_offset); - file.Seek(section_offset, 0); - - // Section is compressed... - if (i == 0 && is_compressed) { - // Read compressed .code section... - std::unique_ptr temp_buffer(new u8[exefs_header.section[i].size]); - file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); - - // Decompress .code section... - u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], - exefs_header.section[i].size); - buffer.resize(decompressed_size); - if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], - decompressed_size)) { - return ResultStatus::ErrorInvalidFormat; + File::IOFile file(filename, "rb"); + if (file.IsOpen()) { + for (int i = 0; i < kMaxSections; i++) { + // Load the specified section... + if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { + INFO_LOG(LOADER, "ExeFS section %d:", i); + INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name); + INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset); + INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size); + + s64 section_offset = (exefs_header.section[i].offset + exefs_offset + + sizeof(ExeFs_Header)+ncch_offset); + file.Seek(section_offset, 0); + + // Section is compressed... + if (i == 0 && is_compressed) { + // Read compressed .code section... + std::unique_ptr temp_buffer(new u8[exefs_header.section[i].size]); + file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); + + // Decompress .code section... + u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], + exefs_header.section[i].size); + buffer.resize(decompressed_size); + if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], + decompressed_size)) { + return ResultStatus::ErrorInvalidFormat; + } + // Section is uncompressed... } - // Section is uncompressed... - } else { - buffer.resize(exefs_header.section[i].size); - file.ReadBytes(&buffer[0], exefs_header.section[i].size); + else { + buffer.resize(exefs_header.section[i].size); + file.ReadBytes(&buffer[0], exefs_header.section[i].size); + } + return ResultStatus::Success; } - return ResultStatus::Success; } + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); + return ResultStatus::Error; } return ResultStatus::ErrorNotUsed; } @@ -191,8 +196,7 @@ ResultStatus AppLoader_NCCH::Load() { if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - file = File::IOFile(filename, "rb"); - + File::IOFile file(filename, "rb"); if (file.IsOpen()) { file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); @@ -235,6 +239,8 @@ ResultStatus AppLoader_NCCH::Load() { LoadExec(); // Load the executable into memory for booting return ResultStatus::Success; + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); } return ResultStatus::Error; } @@ -244,7 +250,7 @@ ResultStatus AppLoader_NCCH::Load() { * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) { +ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) const { return LoadSectionExeFS(".code", buffer); } @@ -253,7 +259,7 @@ ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) { * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::ReadIcon(std::vector& buffer) { +ResultStatus AppLoader_NCCH::ReadIcon(std::vector& buffer) const { return LoadSectionExeFS("icon", buffer); } @@ -262,7 +268,7 @@ ResultStatus AppLoader_NCCH::ReadIcon(std::vector& buffer) { * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::ReadBanner(std::vector& buffer) { +ResultStatus AppLoader_NCCH::ReadBanner(std::vector& buffer) const { return LoadSectionExeFS("banner", buffer); } @@ -271,7 +277,7 @@ ResultStatus AppLoader_NCCH::ReadBanner(std::vector& buffer) { * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) { +ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) const { return LoadSectionExeFS("logo", buffer); } @@ -280,24 +286,30 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) { * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ -ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) { - // Check if the NCCH has a RomFS... - if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { - u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; - u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; +ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) const { + File::IOFile file(filename, "rb"); + if (file.IsOpen()) { + // Check if the NCCH has a RomFS... + if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { + u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; + u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; - INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset); - INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size); + INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset); + INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size); - buffer.resize(romfs_size); + buffer.resize(romfs_size); - file.Seek(romfs_offset, 0); - file.ReadBytes(&buffer[0], romfs_size); + file.Seek(romfs_offset, 0); + file.ReadBytes(&buffer[0], romfs_size); - return ResultStatus::Success; + return ResultStatus::Success; + } + NOTICE_LOG(LOADER, "RomFS unused"); + return ResultStatus::ErrorNotUsed; + } else { + ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str()); } - NOTICE_LOG(LOADER, "RomFS unused"); - return ResultStatus::ErrorNotUsed; + return ResultStatus::Error; } } // namespace Loader diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index 99753b244..c9a35228e 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -161,35 +161,35 @@ public: * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadCode(std::vector& buffer); + ResultStatus ReadCode(std::vector& buffer) const; /** * Get the icon (typically icon section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadIcon(std::vector& buffer); + ResultStatus ReadIcon(std::vector& buffer) const; /** * Get the banner (typically banner section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadBanner(std::vector& buffer); + ResultStatus ReadBanner(std::vector& buffer) const; /** * Get the logo (typically logo section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadLogo(std::vector& buffer); + ResultStatus ReadLogo(std::vector& buffer) const; /** * Get the RomFS of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadRomFS(std::vector& buffer); + ResultStatus ReadRomFS(std::vector& buffer) const; private: @@ -199,15 +199,14 @@ private: * @param buffer Vector to read data into * @return ResultStatus result of function */ - ResultStatus LoadSectionExeFS(const char* name, std::vector& buffer); + ResultStatus LoadSectionExeFS(const char* name, std::vector& buffer) const; /** * Loads .code section into memory for booting * @return ResultStatus result of function */ - ResultStatus LoadExec(); + ResultStatus LoadExec() const; - File::IOFile file; std::string filename; bool is_loaded; -- cgit v1.2.3 From 2d734bb6c5cf1a428f8ce2a6d13955ee4ff872eb Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 4 Jul 2014 13:20:40 -0400 Subject: Marked AppLoader_ELF, AppLoader_NCCH, and Archive_RomFS classes as "final" --- src/core/file_sys/archive_romfs.h | 2 +- src/core/loader/elf.h | 2 +- src/core/loader/ncch.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index 5ef4fed50..e5664ba16 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -17,7 +17,7 @@ namespace FileSys { /// File system interface to the RomFS archive -class Archive_RomFS : public Archive { +class Archive_RomFS final : public Archive { public: Archive_RomFS(const Loader::AppLoader& app_loader); ~Archive_RomFS(); diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index d3cbf414d..cec1167ca 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -13,7 +13,7 @@ namespace Loader { /// Loads an ELF/AXF file -class AppLoader_ELF : public AppLoader { +class AppLoader_ELF final : public AppLoader { public: AppLoader_ELF(const std::string& filename); ~AppLoader_ELF(); diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index c9a35228e..de89280ea 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -145,7 +145,7 @@ struct ExHeader_Header{ namespace Loader { /// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) -class AppLoader_NCCH : public AppLoader { +class AppLoader_NCCH final : public AppLoader { public: AppLoader_NCCH(const std::string& filename); ~AppLoader_NCCH(); -- cgit v1.2.3 From 1099d83455153f9d81b10b54a788bc9fe91f33e9 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 4 Jul 2014 13:25:30 -0400 Subject: Marked AppLoader_ELF, AppLoader_NCCH, and Archive_RomFS virtual functions as "override". --- src/core/file_sys/archive_romfs.h | 8 ++++---- src/core/loader/elf.h | 4 ++-- src/core/loader/ncch.h | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index e5664ba16..8a31190a9 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -20,13 +20,13 @@ namespace FileSys { class Archive_RomFS final : public Archive { public: Archive_RomFS(const Loader::AppLoader& app_loader); - ~Archive_RomFS(); + ~Archive_RomFS() override; /** * Get the IdCode of the archive (e.g. RomFS, SaveData, etc.) * @return IdCode of the archive */ - IdCode GetIdCode() const { return IdCode::RomFS; }; + IdCode GetIdCode() const override { return IdCode::RomFS; }; /** * Read data from the archive @@ -35,13 +35,13 @@ public: * @param buffer Buffer to read data into * @return Number of bytes read */ - size_t Read(const u64 offset, const u32 length, u8* buffer) const; + size_t Read(const u64 offset, const u32 length, u8* buffer) const override; /** * Get the size of the archive in bytes * @return Size of the archive in bytes */ - size_t GetSize() const; + size_t GetSize() const override; private: std::vector raw_data; diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index cec1167ca..5ae88439a 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -16,13 +16,13 @@ namespace Loader { class AppLoader_ELF final : public AppLoader { public: AppLoader_ELF(const std::string& filename); - ~AppLoader_ELF(); + ~AppLoader_ELF() override; /** * Load the bootable file * @return ResultStatus result of function */ - ResultStatus Load(); + ResultStatus Load() override; private: std::string filename; diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h index de89280ea..29b59aa11 100644 --- a/src/core/loader/ncch.h +++ b/src/core/loader/ncch.h @@ -148,48 +148,48 @@ namespace Loader { class AppLoader_NCCH final : public AppLoader { public: AppLoader_NCCH(const std::string& filename); - ~AppLoader_NCCH(); + ~AppLoader_NCCH() override; /** * Load the application * @return ResultStatus result of function */ - ResultStatus Load(); + ResultStatus Load() override; /** * Get the code (typically .code section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadCode(std::vector& buffer) const; + ResultStatus ReadCode(std::vector& buffer) const override; /** * Get the icon (typically icon section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadIcon(std::vector& buffer) const; + ResultStatus ReadIcon(std::vector& buffer) const override; /** * Get the banner (typically banner section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadBanner(std::vector& buffer) const; + ResultStatus ReadBanner(std::vector& buffer) const override; /** * Get the logo (typically logo section) of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadLogo(std::vector& buffer) const; + ResultStatus ReadLogo(std::vector& buffer) const override; /** * Get the RomFS of the application * @param buffer Reference to buffer to store data * @return ResultStatus result of function */ - ResultStatus ReadRomFS(std::vector& buffer) const; + ResultStatus ReadRomFS(std::vector& buffer) const override; private: -- cgit v1.2.3