CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/FileSystems/VirtualDiscFileSystem.h
Views: 1401
// 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#pragma once1819// TODO: Remove the Windows-specific code, FILE is fine there too.2021#include <map>2223#include "Common/File/Path.h"24#include "Core/FileSystems/FileSystem.h"25#include "Core/FileSystems/DirectoryFileSystem.h"2627extern const std::string INDEX_FILENAME;2829class VirtualDiscFileSystem: public IFileSystem {30public:31VirtualDiscFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath);32~VirtualDiscFileSystem();3334void DoState(PointerWrap &p) override;35int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;36size_t SeekFile(u32 handle, s32 position, FileMove type) override;37size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;38size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;39void CloseFile(u32 handle) override;40PSPFileInfo GetFileInfo(std::string filename) override;41bool OwnsHandle(u32 handle) override;42int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;43PSPDevType DevType(u32 handle) override;44std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;45FileSystemFlags Flags() override { return FileSystemFlags::UMD; }46u64 FreeSpace(const std::string &path) override { return 0; }4748// unsupported operations49size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;50size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;51bool MkDir(const std::string &dirname) override;52bool RmDir(const std::string &dirname) override;53int RenameFile(const std::string &from, const std::string &to) override;54bool RemoveFile(const std::string &filename) override;5556bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }5758private:59void LoadFileListIndex();60// Warning: modifies input string.61int getFileListIndex(std::string &fileName);62int getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode = false);63Path GetLocalPath(std::string localpath);6465typedef void *HandlerLibrary;66typedef int HandlerHandle;67typedef s64 HandlerOffset;68typedef void (*HandlerLogFunc)(void *arg, HandlerHandle handle, LogLevel level, const char *msg);6970static void HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg);7172// The primary purpose of handlers is to make it easier to work with large archives.73// However, they have other uses as well, such as patching individual files.74struct Handler {75Handler(const char *filename, VirtualDiscFileSystem *const sys);76~Handler();7778typedef bool (*InitFunc)(HandlerLogFunc logger, void *loggerArg);79typedef void (*ShutdownFunc)();80typedef void (*ShutdownV2Func)(void *loggerArg);81typedef HandlerHandle (*OpenFunc)(const char *basePath, const char *filename);82typedef HandlerOffset (*SeekFunc)(HandlerHandle handle, HandlerOffset offset, FileMove origin);83typedef HandlerOffset (*ReadFunc)(HandlerHandle handle, void *data, HandlerOffset size);84typedef void (*CloseFunc)(HandlerHandle handle);85typedef int (*VersionFunc)();8687HandlerLibrary library;88VirtualDiscFileSystem *const sys_;89InitFunc Init;90ShutdownFunc Shutdown;91ShutdownV2Func ShutdownV2;92OpenFunc Open;93SeekFunc Seek;94ReadFunc Read;95CloseFunc Close;9697bool IsValid() const { return library != nullptr; }98};99100struct HandlerFileHandle {101Handler *handler;102HandlerHandle handle;103104HandlerFileHandle() : handler(nullptr), handle(0) {}105HandlerFileHandle(Handler *handler_) : handler(handler_), handle(-1) {}106107bool Open(const std::string& basePath, const std::string& fileName, FileAccess access) {108// Ignore access, read only.109handle = handler->Open(basePath.c_str(), fileName.c_str());110return handle > 0;111}112size_t Read(u8 *data, s64 size) {113return (size_t)handler->Read(handle, data, size);114}115size_t Seek(s32 position, FileMove type) {116return (size_t)handler->Seek(handle, position, type);117}118void Close() {119handler->Close(handle);120}121122bool IsValid() {123return handler != nullptr && handler->IsValid();124}125126HandlerFileHandle &operator =(Handler *_handler) {127handler = _handler;128return *this;129}130};131132typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType;133134struct OpenFileEntry {135OpenFileEntry() {}136OpenFileEntry(FileSystemFlags fileSystemFlags) {137hFile = DirectoryFileHandle(DirectoryFileHandle::SKIP_REPLAY, fileSystemFlags);138}139140DirectoryFileHandle hFile;141HandlerFileHandle handler;142VirtualFileType type = VFILETYPE_NORMAL;143u32 fileIndex = 0;144u64 curOffset = 0;145u64 startOffset = 0; // only used by lbn files146u64 size = 0; // only used by lbn files147148bool Open(const Path &basePath, std::string& fileName, FileAccess access) {149// Ignored, we're read only.150u32 err;151if (handler.IsValid()) {152return handler.Open(basePath.ToString(), fileName, access);153} else {154return hFile.Open(basePath, fileName, access, err);155}156}157size_t Read(u8 *data, s64 size) {158if (handler.IsValid()) {159return handler.Read(data, size);160} else {161return hFile.Read(data, size);162}163}164size_t Seek(s32 position, FileMove type) {165if (handler.IsValid()) {166return handler.Seek(position, type);167} else {168return hFile.Seek(position, type);169}170}171void Close() {172if (handler.IsValid()) {173return handler.Close();174} else {175return hFile.Close();176}177}178};179180typedef std::map<u32, OpenFileEntry> EntryMap;181182EntryMap entries;183IHandleAllocator *hAlloc;184Path basePath;185186struct FileListEntry {187std::string fileName;188u32 firstBlock;189u32 totalSize;190Handler *handler;191};192193std::vector<FileListEntry> fileList;194u32 currentBlockIndex;195u32 lastReadBlock_;196197std::map<std::string, Handler *> handlers;198};199200201