Path: blob/master/Core/FileSystems/DirectoryFileSystem.h
3187 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#ifdef _WIN3227typedef void * HANDLE;28#endif2930struct DirectoryFileHandle {31enum Flags {32NORMAL,33SKIP_REPLAY,34};3536#ifdef _WIN3237HANDLE hFile = (HANDLE)-1;38#else39int hFile = -1;40#endif41s64 needsTrunc_ = -1;42bool replay_ = true;43bool inGameDir_ = false;44FileSystemFlags fileSystemFlags_ = (FileSystemFlags)0;4546DirectoryFileHandle() {}4748DirectoryFileHandle(Flags flags, FileSystemFlags fileSystemFlags)49: replay_(flags != SKIP_REPLAY), fileSystemFlags_(fileSystemFlags) {}5051Path GetLocalPath(const Path &basePath, std::string localpath) const;52bool Open(const Path &basePath, std::string &fileName, FileAccess access, u32 &err);53size_t Read(u8* pointer, s64 size);54size_t Write(const u8* pointer, s64 size);55size_t Seek(s32 position, FileMove type);56void Close();57};5859class DirectoryFileSystem : public IFileSystem {60public:61DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath, FileSystemFlags _flags = FileSystemFlags::NONE);62~DirectoryFileSystem();6364void CloseAll();6566void DoState(PointerWrap &p) override;67std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;68int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;69void CloseFile(u32 handle) override;70size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;71size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;72size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;73size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;74size_t SeekFile(u32 handle, s32 position, FileMove type) override;75PSPFileInfo GetFileInfo(std::string filename) override;76PSPFileInfo GetFileInfoByHandle(u32 handle) override;77bool OwnsHandle(u32 handle) override;78int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;79PSPDevType DevType(u32 handle) override;8081bool MkDir(const std::string &dirname) override;82bool RmDir(const std::string &dirname) override;83int RenameFile(const std::string &from, const std::string &to) override;84bool RemoveFile(const std::string &filename) override;85FileSystemFlags Flags() const override { return flags; }86u64 FreeDiskSpace(const std::string &path) override;8788bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;89void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Dir: %s", basePath.c_str()); }9091private:92struct OpenFileEntry {93DirectoryFileHandle hFile;94std::string guestFilename;95FileAccess access = FILEACCESS_NONE;96};9798typedef std::map<u32, OpenFileEntry> EntryMap;99EntryMap entries;100Path basePath;101IHandleAllocator *hAlloc;102FileSystemFlags flags;103104Path GetLocalPath(std::string internalPath) const;105};106107// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts.108// Very inefficient - always load the whole file on open.109class VFSFileSystem : public IFileSystem {110public:111VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);112~VFSFileSystem();113114void DoState(PointerWrap &p) override;115std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;116int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;117void CloseFile(u32 handle) override;118size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;119size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;120size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;121size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;122size_t SeekFile(u32 handle, s32 position, FileMove type) override;123PSPFileInfo GetFileInfo(std::string filename) override;124PSPFileInfo GetFileInfoByHandle(u32 handle) override;125bool OwnsHandle(u32 handle) override;126int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;127PSPDevType DevType(u32 handle) override;128129bool MkDir(const std::string &dirname) override;130bool RmDir(const std::string &dirname) override;131int RenameFile(const std::string &from, const std::string &to) override;132bool RemoveFile(const std::string &filename) override;133FileSystemFlags Flags() const override { return FileSystemFlags::FLASH; }134u64 FreeDiskSpace(const std::string &path) override { return 0; }135136bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }137void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VFS: %s", basePath.c_str()); }138139private:140struct OpenFileEntry {141u8 *fileData;142size_t size;143size_t seekPos;144};145146typedef std::map<u32, OpenFileEntry> EntryMap;147EntryMap entries;148std::string basePath;149IHandleAllocator *hAlloc;150151std::string GetLocalPath(const std::string &localpath) const;152};153154155