Path: blob/master/Core/FileSystems/DirectoryFileSystem.h
5656 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#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"2526#if defined(_WIN32) && !defined(HAVE_LIBRETRO_VFS)27typedef void * HANDLE;28#endif2930struct DirectoryFileHandle {31enum Flags {32NORMAL,33SKIP_REPLAY,34};3536#ifdef HAVE_LIBRETRO_VFS37FILE *hFile = nullptr;38#elif defined(_WIN32)39HANDLE hFile = (HANDLE)-1;40#else41int hFile = -1;42#endif43s64 needsTrunc_ = -1;44bool replay_ = true;45bool inGameDir_ = false;46FileSystemFlags fileSystemFlags_ = (FileSystemFlags)0;4748DirectoryFileHandle() {}4950DirectoryFileHandle(Flags flags, FileSystemFlags fileSystemFlags)51: replay_(flags != SKIP_REPLAY), fileSystemFlags_(fileSystemFlags) {}5253Path GetLocalPath(const Path &basePath, std::string_view localPath) const;54bool Open(const Path &basePath, std::string &fileName, FileAccess access, u32 &err);55size_t Read(u8* pointer, s64 size);56size_t Write(const u8* pointer, s64 size);57size_t Seek(s32 position, FileMove type);58void Close();59};6061class DirectoryFileSystem : public IFileSystem {62public:63DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath, FileSystemFlags _flags = FileSystemFlags::NONE);64~DirectoryFileSystem();6566void CloseAll();6768void DoState(PointerWrap &p) override;69std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;70int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;71void CloseFile(u32 handle) override;72size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;73size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;74size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;75size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;76size_t SeekFile(u32 handle, s32 position, FileMove type) override;77PSPFileInfo GetFileInfo(std::string filename) override;78PSPFileInfo GetFileInfoByHandle(u32 handle) override;79bool OwnsHandle(u32 handle) override;80int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;81PSPDevType DevType(u32 handle) override;8283bool MkDir(const std::string &dirname) override;84bool RmDir(const std::string &dirname) override;85int RenameFile(const std::string &from, const std::string &to) override;86bool RemoveFile(const std::string &filename) override;87FileSystemFlags Flags() const override { return flags; }88u64 FreeDiskSpace(const std::string &path) override;8990bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;91void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Dir: %s", basePath.c_str()); }9293private:94struct OpenFileEntry {95DirectoryFileHandle hFile;96std::string guestFilename;97FileAccess access = FILEACCESS_NONE;98};99100typedef std::map<u32, OpenFileEntry> EntryMap;101EntryMap entries;102Path basePath;103IHandleAllocator *hAlloc;104FileSystemFlags flags;105106Path GetLocalPath(std::string_view internalPath) const;107};108109// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts.110// Very inefficient - always load the whole file on open.111class VFSFileSystem : public IFileSystem {112public:113VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);114~VFSFileSystem();115116void DoState(PointerWrap &p) override;117std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;118int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;119void CloseFile(u32 handle) override;120size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;121size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;122size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;123size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;124size_t SeekFile(u32 handle, s32 position, FileMove type) override;125PSPFileInfo GetFileInfo(std::string filename) override;126PSPFileInfo GetFileInfoByHandle(u32 handle) override;127bool OwnsHandle(u32 handle) override;128int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;129PSPDevType DevType(u32 handle) override;130131bool MkDir(const std::string &dirname) override;132bool RmDir(const std::string &dirname) override;133int RenameFile(const std::string &from, const std::string &to) override;134bool RemoveFile(const std::string &filename) override;135FileSystemFlags Flags() const override { return FileSystemFlags::FLASH; }136u64 FreeDiskSpace(const std::string &path) override { return 0; }137138bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }139void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VFS: %s", basePath.c_str()); }140141private:142struct OpenFileEntry {143u8 *fileData;144size_t size;145size_t seekPos;146};147148typedef std::map<u32, OpenFileEntry> EntryMap;149EntryMap entries;150std::string basePath;151IHandleAllocator *hAlloc;152153std::string GetLocalPath(std::string_view localpath) const;154};155156157