Path: blob/master/Core/FileSystems/VirtualDiscFileSystem.cpp
5654 views
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "ppsspp_config.h"18#include <ctime>1920#include "Common/File/FileUtil.h"21#include "Common/File/DirListing.h"22#include "Common/Serialize/Serializer.h"23#include "Common/Serialize/SerializeFuncs.h"24#include "Common/SysError.h"25#include "Core/FileSystems/VirtualDiscFileSystem.h"26#include "Core/FileSystems/ISOFileSystem.h"27#include "Core/HLE/sceKernel.h"28#include "Core/Reporting.h"29#include "Common/Data/Encoding/Utf8.h"3031#ifdef _WIN3232#include "Common/CommonWindows.h"33#include <sys/stat.h>34#if PPSSPP_PLATFORM(UWP)35#include <fileapifromapp.h>36#endif37#else38#include <dirent.h>39#include <unistd.h>40#include <sys/stat.h>41#include <ctype.h>42#if !PPSSPP_PLATFORM(SWITCH)43#include <dlfcn.h>44#endif45#endif4647const std::string INDEX_FILENAME = ".ppsspp-index.lst";4849VirtualDiscFileSystem::VirtualDiscFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath)50: basePath(_basePath), currentBlockIndex(0) {51hAlloc = _hAlloc;52LoadFileListIndex();53}5455VirtualDiscFileSystem::~VirtualDiscFileSystem() {56for (auto iter = entries.begin(), end = entries.end(); iter != end; ++iter) {57if (iter->second.type != VFILETYPE_ISO) {58iter->second.Close();59}60}61for (auto iter = handlers.begin(), end = handlers.end(); iter != end; ++iter) {62delete iter->second;63}64}6566void VirtualDiscFileSystem::LoadFileListIndex() {67const Path filename = basePath / INDEX_FILENAME;68if (!File::Exists(filename)) {69return;70}7172FILE *f = File::OpenCFile(filename, "r");73if (!f) {74return;75}7677static const int MAX_LINE_SIZE = 2048;78char linebuf[MAX_LINE_SIZE]{};79while (fgets(linebuf, MAX_LINE_SIZE, f)) {80std::string line = linebuf;81// Strip newline from fgets.82if (!line.empty() && line.back() == '\n')83line.resize(line.size() - 1);8485// Ignore any UTF-8 BOM.86if (line.substr(0, 3) == "\xEF\xBB\xBF") {87line = line.substr(3);88}8990if (line.empty() || line[0] == ';') {91continue;92}9394FileListEntry entry = {""};9596// Syntax: HEXPOS filename or HEXPOS filename:handler97size_t filename_pos = line.find(' ');98if (filename_pos == line.npos) {99ERROR_LOG(Log::FileSystem, "Unexpected line in %s: %s", INDEX_FILENAME.c_str(), line.c_str());100continue;101}102103filename_pos++;104// Strip any slash prefix.105while (filename_pos < line.length() && line[filename_pos] == '/') {106filename_pos++;107}108109// Check if there's a handler specified.110size_t handler_pos = line.find(':', filename_pos);111if (handler_pos != line.npos) {112entry.fileName = line.substr(filename_pos, handler_pos - filename_pos);113114std::string handler = line.substr(handler_pos + 1);115size_t trunc = handler.find_last_not_of("\r\n");116if (trunc != handler.npos && trunc != handler.size())117handler.resize(trunc + 1);118119if (handlers.find(handler) == handlers.end())120handlers[handler] = new Handler(handler.c_str(), this);121if (handlers[handler]->IsValid())122entry.handler = handlers[handler];123} else {124entry.fileName = line.substr(filename_pos);125}126size_t trunc = entry.fileName.find_last_not_of("\r\n");127if (trunc != entry.fileName.npos && trunc != entry.fileName.size())128entry.fileName.resize(trunc + 1);129130entry.firstBlock = (u32)strtol(line.c_str(), NULL, 16);131if (entry.handler != NULL && entry.handler->IsValid()) {132HandlerFileHandle temp = entry.handler;133if (temp.Open(basePath.ToString(), entry.fileName, FILEACCESS_READ)) {134entry.totalSize = (u32)temp.Seek(0, FILEMOVE_END);135temp.Close();136} else {137ERROR_LOG(Log::FileSystem, "Unable to open virtual file: %s", entry.fileName.c_str());138}139} else {140entry.totalSize = File::GetFileSize(GetLocalPath(entry.fileName));141}142143// Try to keep currentBlockIndex sane, in case there are other files.144u32 nextBlock = entry.firstBlock + (entry.totalSize + 2047) / 2048;145if (nextBlock > currentBlockIndex) {146currentBlockIndex = nextBlock;147}148149fileList.push_back(entry);150}151152fclose(f);153}154155void VirtualDiscFileSystem::DoState(PointerWrap &p)156{157auto s = p.Section("VirtualDiscFileSystem", 1, 2);158if (!s)159return;160161int fileListSize = (int)fileList.size();162int entryCount = (int)entries.size();163164Do(p, fileListSize);165Do(p, entryCount);166Do(p, currentBlockIndex);167168FileListEntry dummy = {""};169fileList.resize(fileListSize, dummy);170171for (int i = 0; i < fileListSize; i++)172{173Do(p, fileList[i].fileName);174Do(p, fileList[i].firstBlock);175Do(p, fileList[i].totalSize);176}177178if (p.mode == p.MODE_READ)179{180entries.clear();181182for (int i = 0; i < entryCount; i++)183{184u32 fd = 0;185OpenFileEntry of(Flags());186187Do(p, fd);188Do(p, of.fileIndex);189Do(p, of.type);190Do(p, of.curOffset);191Do(p, of.startOffset);192Do(p, of.size);193194// open file195if (of.type != VFILETYPE_ISO) {196if (fileList[of.fileIndex].handler != NULL) {197of.handler = fileList[of.fileIndex].handler;198}199200bool success = of.Open(basePath, fileList[of.fileIndex].fileName, FILEACCESS_READ);201if (!success) {202ERROR_LOG(Log::FileSystem, "Failed to create file handle for %s.", fileList[of.fileIndex].fileName.c_str());203} else {204if (of.type == VFILETYPE_LBN) {205of.Seek(of.curOffset + of.startOffset, FILEMOVE_BEGIN);206} else {207of.Seek(of.curOffset, FILEMOVE_BEGIN);208}209}210}211212// TODO: I think we only need to write to the map on load?213entries[fd] = of;214}215} else {216for (EntryMap::iterator it = entries.begin(), end = entries.end(); it != end; ++it)217{218OpenFileEntry &of = it->second;219220Do(p, it->first);221Do(p, of.fileIndex);222Do(p, of.type);223Do(p, of.curOffset);224Do(p, of.startOffset);225Do(p, of.size);226}227}228229if (s >= 2) {230Do(p, lastReadBlock_);231} else {232lastReadBlock_ = 0;233}234235// We don't savestate handlers (loaded on fs load), but if they change, it may not load properly.236}237238Path VirtualDiscFileSystem::GetLocalPath(std::string_view localpath) const {239if (localpath.empty())240return basePath;241242if (localpath[0] == '/')243localpath.remove_prefix(1);244return basePath / localpath;245}246247int VirtualDiscFileSystem::getFileListIndex(std::string &fileName)248{249std::string normalized;250if (fileName.length() >= 1 && fileName[0] == '/') {251normalized = fileName.substr(1);252} else {253normalized = fileName;254}255256for (size_t i = 0; i < fileList.size(); i++)257{258if (fileList[i].fileName == normalized)259return (int)i;260}261262// unknown file - add it263Path fullName = GetLocalPath(fileName);264if (! File::Exists(fullName)) {265#if HOST_IS_CASE_SENSITIVE266if (! FixPathCase(basePath, fileName, FPC_FILE_MUST_EXIST))267return -1;268fullName = GetLocalPath(fileName);269270if (! File::Exists(fullName))271return -1;272#else273return -1;274#endif275}276277if (File::IsDirectory(fullName))278return -1;279280FileListEntry entry = {""};281entry.fileName = normalized;282entry.totalSize = File::GetFileSize(fullName);283entry.firstBlock = currentBlockIndex;284currentBlockIndex += (entry.totalSize+2047)/2048;285286fileList.push_back(entry);287288return (int)fileList.size()-1;289}290291int VirtualDiscFileSystem::getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode) const {292for (size_t i = 0; i < fileList.size(); i++) {293if (fileList[i].firstBlock <= accessBlock) {294u32 sectorOffset = (accessBlock-fileList[i].firstBlock)*2048;295u32 totalFileSize = blockMode ? (fileList[i].totalSize+2047) & ~2047 : fileList[i].totalSize;296297u32 endOffset = sectorOffset+accessSize;298if (endOffset <= totalFileSize) {299return (int)i;300}301}302}303304return -1;305}306307int VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename)308{309OpenFileEntry entry(Flags());310entry.curOffset = 0;311entry.size = 0;312entry.startOffset = 0;313314if (filename.empty())315{316entry.type = VFILETYPE_ISO;317entry.fileIndex = -1;318319u32 newHandle = hAlloc->GetNewHandle();320entries[newHandle] = entry;321322return newHandle;323}324325if (filename.compare(0, 8, "/sce_lbn") == 0)326{327u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;328parseLBN(filename, §orStart, &readSize);329330entry.type = VFILETYPE_LBN;331entry.size = readSize;332333int fileIndex = getFileListIndex(sectorStart,readSize);334if (fileIndex == -1)335{336ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem: sce_lbn used without calling fileinfo.");337return 0;338}339entry.fileIndex = (u32)fileIndex;340341entry.startOffset = (sectorStart-fileList[entry.fileIndex].firstBlock)*2048;342343// now we just need an actual file handle344if (fileList[entry.fileIndex].handler != NULL) {345entry.handler = fileList[entry.fileIndex].handler;346}347bool success = entry.Open(basePath, fileList[entry.fileIndex].fileName, FILEACCESS_READ);348349if (!success) {350if (!(access & FILEACCESS_PPSSPP_QUIET)) {351#ifdef _WIN32352ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i", (int)GetLastError());353#else354ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED");355#endif356}357return 0;358}359360// seek to start361entry.Seek(entry.startOffset, FILEMOVE_BEGIN);362363u32 newHandle = hAlloc->GetNewHandle();364entries[newHandle] = entry;365366return newHandle;367}368369entry.type = VFILETYPE_NORMAL;370entry.fileIndex = getFileListIndex(filename);371372if (entry.fileIndex != (u32)-1 && fileList[entry.fileIndex].handler != NULL) {373entry.handler = fileList[entry.fileIndex].handler;374}375bool success = entry.Open(basePath, filename, (FileAccess)(access & FILEACCESS_PSP_FLAGS));376377if (!success) {378if (!(access & FILEACCESS_PPSSPP_QUIET)) {379#ifdef _WIN32380ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i - access = %i", (int)GetLastError(), (int)access);381#else382ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, access = %i", (int)access);383#endif384}385return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;386} else {387u32 newHandle = hAlloc->GetNewHandle();388entries[newHandle] = entry;389390return newHandle;391}392}393394size_t VirtualDiscFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {395EntryMap::iterator iter = entries.find(handle);396if (iter != entries.end()) {397auto &entry = iter->second;398switch (entry.type)399{400case VFILETYPE_NORMAL:401{402return entry.Seek(position, type);403}404case VFILETYPE_LBN:405{406switch (type)407{408case FILEMOVE_BEGIN: entry.curOffset = position; break;409case FILEMOVE_CURRENT: entry.curOffset += position; break;410case FILEMOVE_END: entry.curOffset = entry.size + position; break;411}412413u32 off = entry.startOffset + entry.curOffset;414entry.Seek(off, FILEMOVE_BEGIN);415return entry.curOffset;416}417case VFILETYPE_ISO:418{419switch (type)420{421case FILEMOVE_BEGIN: entry.curOffset = position; break;422case FILEMOVE_CURRENT: entry.curOffset += position; break;423case FILEMOVE_END: entry.curOffset = currentBlockIndex + position; break;424}425426return entry.curOffset;427}428}429return 0;430} else {431//This shouldn't happen...432ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot seek in file that hasn't been opened: %08x", handle);433return 0;434}435}436437size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) {438int ignored;439return ReadFile(handle, pointer, size, ignored);440}441442size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) {443EntryMap::iterator iter = entries.find(handle);444if (iter != entries.end()) {445if (size < 0) {446ERROR_LOG_REPORT(Log::FileSystem, "Invalid read for %lld bytes from virtual umd", size);447return 0;448}449450// it's the whole iso... it could reference any of the files on the disc.451// For now let's just open and close the files on demand. Can certainly be done452// better though453if (iter->second.type == VFILETYPE_ISO)454{455int fileIndex = getFileListIndex(iter->second.curOffset,size*2048,true);456if (fileIndex == -1)457{458ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Reading from unknown address in %08x at %08llx", handle, iter->second.curOffset);459return 0;460}461462OpenFileEntry temp(Flags());463if (fileList[fileIndex].handler != NULL) {464temp.handler = fileList[fileIndex].handler;465}466bool success = temp.Open(basePath, fileList[fileIndex].fileName, FILEACCESS_READ);467468if (!success)469{470ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Error opening file %s", fileList[fileIndex].fileName.c_str());471return 0;472}473474u32 startOffset = (iter->second.curOffset-fileList[fileIndex].firstBlock)*2048;475size_t bytesRead;476477temp.Seek(startOffset, FILEMOVE_BEGIN);478479u32 remainingSize = fileList[fileIndex].totalSize-startOffset;480if (remainingSize < size * 2048)481{482// the file doesn't fill the whole last sector483// read what's there and zero fill the rest like on a real disc484bytesRead = temp.Read(pointer, remainingSize);485memset(&pointer[bytesRead], 0, size * 2048 - bytesRead);486} else {487bytesRead = temp.Read(pointer, size * 2048);488}489490temp.Close();491492iter->second.curOffset += size;493// TODO: This probably isn't enough...494if (abs((int)lastReadBlock_ - (int)iter->second.curOffset) > 100) {495// This is an estimate, sometimes it takes 1+ seconds, but it definitely takes time.496usec = 100000;497}498lastReadBlock_ = iter->second.curOffset;499return size;500}501502if (iter->second.type == VFILETYPE_LBN && iter->second.curOffset + size > iter->second.size) {503// Clamp to the remaining size, but read what we can.504const s64 newSize = iter->second.size - iter->second.curOffset;505WARN_LOG(Log::FileSystem, "VirtualDiscFileSystem: Reading beyond end of file, clamping size %lld to %lld", size, newSize);506size = newSize;507}508509size_t bytesRead = iter->second.Read(pointer, size);510iter->second.curOffset += bytesRead;511return bytesRead;512} else {513//This shouldn't happen...514ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot read file that hasn't been opened: %08x", handle);515return 0;516}517}518519void VirtualDiscFileSystem::CloseFile(u32 handle) {520EntryMap::iterator iter = entries.find(handle);521if (iter != entries.end()) {522hAlloc->FreeHandle(handle);523iter->second.Close();524entries.erase(iter);525} else {526//This shouldn't happen...527ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot close file that hasn't been opened: %08x", handle);528}529}530531bool VirtualDiscFileSystem::OwnsHandle(u32 handle) {532EntryMap::iterator iter = entries.find(handle);533return (iter != entries.end());534}535536int VirtualDiscFileSystem::Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) {537// TODO: How to support these?538return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;539}540541PSPDevType VirtualDiscFileSystem::DevType(u32 handle) {542EntryMap::iterator iter = entries.find(handle);543if (iter == entries.end())544return PSPDevType::FILE;545PSPDevType type = iter->second.type == VFILETYPE_ISO ? PSPDevType::BLOCK : PSPDevType::FILE;546if (iter->second.type == VFILETYPE_LBN)547type |= PSPDevType::EMU_LBN;548return type;549}550551PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) {552PSPFileInfo x;553x.name = filename;554x.access = FILEACCESS_READ;555556if (filename.compare(0,8,"/sce_lbn") == 0) {557u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;558parseLBN(filename, §orStart, &readSize);559560PSPFileInfo fileInfo;561fileInfo.name = filename;562fileInfo.exists = true;563fileInfo.type = FILETYPE_NORMAL;564fileInfo.size = readSize;565fileInfo.access = 0444;566fileInfo.startSector = sectorStart;567fileInfo.isOnSectorSystem = true;568fileInfo.numSectors = (readSize + 2047) / 2048;569return fileInfo;570}571572int fileIndex = getFileListIndex(filename);573if (fileIndex != -1 && fileList[fileIndex].handler != NULL) {574x.type = FILETYPE_NORMAL;575x.isOnSectorSystem = true;576x.startSector = fileList[fileIndex].firstBlock;577x.access = 0555;578579HandlerFileHandle temp = fileList[fileIndex].handler;580if (temp.Open(basePath.ToString(), filename, FILEACCESS_READ)) {581x.exists = true;582x.size = temp.Seek(0, FILEMOVE_END);583temp.Close();584}585586// TODO: Probably should include dates or something...587return x;588}589590Path fullName = GetLocalPath(filename);591if (!File::Exists(fullName)) {592#if HOST_IS_CASE_SENSITIVE593if (! FixPathCase(basePath, filename, FPC_FILE_MUST_EXIST))594return x;595fullName = GetLocalPath(filename);596597if (! File::Exists(fullName))598return x;599#else600return x;601#endif602}603604x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;605x.exists = true;606x.access = 0555;607if (fileIndex != -1) {608x.isOnSectorSystem = true;609x.startSector = fileList[fileIndex].firstBlock;610}611612if (x.type != FILETYPE_DIRECTORY) {613File::FileInfo details;614if (!File::GetFileInfo(fullName, &details)) {615ERROR_LOG(Log::FileSystem, "DirectoryFileSystem::GetFileInfo: GetFileInfo failed: %s", fullName.c_str());616x.size = 0;617x.access = 0;618} else {619x.size = details.size;620time_t atime = details.atime;621time_t ctime = details.ctime;622time_t mtime = details.mtime;623624localtime_r((time_t*)&atime, &x.atime);625localtime_r((time_t*)&ctime, &x.ctime);626localtime_r((time_t*)&mtime, &x.mtime);627}628629// x.startSector was set above in "if (fileIndex != -1)".630x.numSectors = (x.size+2047)/2048;631}632633return x;634}635636PSPFileInfo VirtualDiscFileSystem::GetFileInfoByHandle(u32 handle) {637WARN_LOG(Log::FileSystem, "GetFileInfoByHandle not yet implemented for VirtualDiscFileSystem");638return PSPFileInfo();639}640641#ifdef _WIN32642#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL643644static void tmFromFiletime(tm &dest, const FILETIME &src)645{646u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL;647u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US;648649time_t t = (time_t) (from_1970_us / 1000000UL);650localtime_s(&dest, &t);651}652#endif653654std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(const std::string &path, bool *exists) {655std::vector<PSPFileInfo> myVector;656657// TODO(scoped): Switch this over to GetFilesInDir!658659#ifdef _WIN32660WIN32_FIND_DATA findData;661HANDLE hFind;662663// TODO: Handler files that are virtual might not be listed.664665std::wstring w32path = GetLocalPath(path).ToWString() + L"\\*.*";666667#if PPSSPP_PLATFORM(UWP)668hFind = FindFirstFileExFromAppW(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);669#else670hFind = FindFirstFileEx(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);671#endif672if (hFind == INVALID_HANDLE_VALUE) {673if (exists)674*exists = false;675return myVector; //the empty list676}677678if (exists)679*exists = true;680681for (BOOL retval = 1; retval; retval = FindNextFile(hFind, &findData)) {682if (!wcscmp(findData.cFileName, L"..") || !wcscmp(findData.cFileName, L".")) {683continue;684}685686PSPFileInfo entry;687if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {688entry.type = FILETYPE_DIRECTORY;689} else {690entry.type = FILETYPE_NORMAL;691}692693entry.access = 0555;694entry.exists = true;695entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32);696entry.name = ConvertWStringToUTF8(findData.cFileName);697tmFromFiletime(entry.atime, findData.ftLastAccessTime);698tmFromFiletime(entry.ctime, findData.ftCreationTime);699tmFromFiletime(entry.mtime, findData.ftLastWriteTime);700entry.isOnSectorSystem = true;701702std::string fullRelativePath = path + "/" + entry.name;703int fileIndex = getFileListIndex(fullRelativePath);704if (fileIndex != -1)705entry.startSector = fileList[fileIndex].firstBlock;706myVector.push_back(entry);707}708FindClose(hFind);709#else710dirent *dirp;711Path localPath = GetLocalPath(path);712DIR *dp = opendir(localPath.c_str());713714#if HOST_IS_CASE_SENSITIVE715std::string fixedPath = path;716if(dp == NULL && FixPathCase(basePath, fixedPath, FPC_FILE_MUST_EXIST)) {717// May have failed due to case sensitivity, try again718localPath = GetLocalPath(fixedPath);719dp = opendir(localPath.c_str());720}721#endif722723if (dp == NULL) {724ERROR_LOG(Log::FileSystem,"Error opening directory %s\n", path.c_str());725if (exists)726*exists = false;727return myVector;728}729730if (exists)731*exists = true;732733while ((dirp = readdir(dp)) != NULL) {734if (!strcmp(dirp->d_name, "..") || !strcmp(dirp->d_name, ".")) {735continue;736}737738PSPFileInfo entry;739struct stat s;740std::string fullName = (localPath / std::string(dirp->d_name)).ToString();741stat(fullName.c_str(), &s);742if (S_ISDIR(s.st_mode))743entry.type = FILETYPE_DIRECTORY;744else745entry.type = FILETYPE_NORMAL;746entry.access = 0555;747entry.exists = true;748entry.name = dirp->d_name;749entry.size = s.st_size;750localtime_r((time_t*)&s.st_atime,&entry.atime);751localtime_r((time_t*)&s.st_ctime,&entry.ctime);752localtime_r((time_t*)&s.st_mtime,&entry.mtime);753entry.isOnSectorSystem = true;754755std::string fullRelativePath = path + "/" + entry.name;756int fileIndex = getFileListIndex(fullRelativePath);757if (fileIndex != -1)758entry.startSector = fileList[fileIndex].firstBlock;759myVector.push_back(entry);760}761closedir(dp);762#endif763return myVector;764}765766size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)767{768ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");769return 0;770}771772size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec)773{774ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");775return 0;776}777778bool VirtualDiscFileSystem::MkDir(const std::string &dirname)779{780ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot create directory on virtual disc");781return false;782}783784bool VirtualDiscFileSystem::RmDir(const std::string &dirname)785{786ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove directory on virtual disc");787return false;788}789790int VirtualDiscFileSystem::RenameFile(const std::string &from, const std::string &to)791{792ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot rename file on virtual disc");793return -1;794}795796bool VirtualDiscFileSystem::RemoveFile(const std::string &filename)797{798ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove file on virtual disc");799return false;800}801802void VirtualDiscFileSystem::HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg) {803VirtualDiscFileSystem *sys = static_cast<VirtualDiscFileSystem *>(arg);804805// TODO: Probably could do this smarter / use a lookup.806const char *filename = NULL;807for (auto it = sys->entries.begin(), end = sys->entries.end(); it != end; ++it) {808if (it->second.fileIndex != (u32)-1 && it->second.handler.handle == handle) {809filename = sys->fileList[it->second.fileIndex].fileName.c_str();810break;811}812}813814if (filename != NULL) {815GENERIC_LOG(Log::FileSystem, level, "%s: %s", filename, msg);816} else {817GENERIC_LOG(Log::FileSystem, level, "%s", msg);818}819}820821VirtualDiscFileSystem::Handler::Handler(const char *filename, VirtualDiscFileSystem *const sys)822: sys_(sys) {823#if !PPSSPP_PLATFORM(SWITCH)824#ifdef _WIN32825#if PPSSPP_PLATFORM(UWP)826#define dlopen(name, ignore) (void *)LoadPackagedLibrary(ConvertUTF8ToWString(name).c_str(), 0)827#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)828#define dlclose(mod) FreeLibrary((HMODULE)mod)829#else830#define dlopen(name, ignore) (void *)LoadLibrary(ConvertUTF8ToWString(name).c_str())831#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)832#define dlclose(mod) FreeLibrary((HMODULE)mod)833#endif834#endif835836library = dlopen(filename, RTLD_LOCAL | RTLD_NOW);837if (library != NULL) {838Init = (InitFunc)dlsym(library, "Init");839Shutdown = (ShutdownFunc)dlsym(library, "Shutdown");840Open = (OpenFunc)dlsym(library, "Open");841Seek = (SeekFunc)dlsym(library, "Seek");842Read = (ReadFunc)dlsym(library, "Read");843Close = (CloseFunc)dlsym(library, "Close");844845VersionFunc Version = (VersionFunc)dlsym(library, "Version");846if (Version && Version() >= 2) {847ShutdownV2 = (ShutdownV2Func)Shutdown;848}849850if (!Init || !Shutdown || !Open || !Seek || !Read || !Close) {851ERROR_LOG(Log::FileSystem, "Unable to find all handler functions: %s", filename);852dlclose(library);853library = NULL;854} else if (!Init(&HandlerLogger, sys)) {855ERROR_LOG(Log::FileSystem, "Unable to initialize handler: %s", filename);856dlclose(library);857library = NULL;858}859} else {860ERROR_LOG(Log::FileSystem, "Unable to load handler '%s': %s", filename, GetLastErrorMsg().c_str());861}862#ifdef _WIN32863#undef dlopen864#undef dlsym865#undef dlclose866#endif867#endif868}869870VirtualDiscFileSystem::Handler::~Handler() {871if (library != NULL) {872if (ShutdownV2)873ShutdownV2(sys_);874else875Shutdown();876877#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(SWITCH)878#ifdef _WIN32879FreeLibrary((HMODULE)library);880#else881dlclose(library);882#endif883#endif884}885}886887888889