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/MetaFileSystem.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#include <string>20#include <vector>21#include <mutex>22#include <memory>2324#include "Core/FileSystems/FileSystem.h"2526class MetaFileSystem : public IHandleAllocator, public IFileSystem {27private:28s32 current;29struct MountPoint {30std::string prefix;31std::shared_ptr<IFileSystem> system;3233bool operator == (const MountPoint &other) const {34return prefix == other.prefix && system == other.system;35}36};3738// The order of this vector is meaningful - lookups are always a linear search from the start.39std::vector<MountPoint> fileSystems;4041typedef std::map<int, std::string> currentDir_t;42currentDir_t currentDir;4344std::string startingDirectory;45std::recursive_mutex lock; // must be recursive. TODO: fix that4647// Assumes the lock is held48void Reset() {49// This used to be 6, probably an attempt to replicate PSP handles.50// However, that's an artifact of using psplink anyway...51current = 1;52startingDirectory.clear();53}5455public:56MetaFileSystem() {57Reset();58}5960void Mount(const std::string &prefix, std::shared_ptr<IFileSystem> system);61// Fails if there's not already a file system at prefix.62bool Remount(const std::string &prefix, std::shared_ptr<IFileSystem> system);6364void UnmountAll();65void Unmount(const std::string &prefix);6667// The pointer returned from these are for temporary usage only. Do not store.68IFileSystem *GetSystem(const std::string &prefix);69IFileSystem *GetSystemFromFilename(const std::string &filename);70IFileSystem *GetHandleOwner(u32 handle);71FileSystemFlags FlagsFromFilename(const std::string &filename) {72IFileSystem *sys = GetSystemFromFilename(filename);73return sys ? sys->Flags() : FileSystemFlags::NONE;74}7576void ThreadEnded(int threadID);77void Shutdown();7879u32 GetNewHandle() override {80u32 res = current++;81if (current < 0) {82// Some code assumes it'll never become 0.83current = 1;84}85return res;86}87void FreeHandle(u32 handle) override {}8889void DoState(PointerWrap &p) override;9091int MapFilePath(const std::string &inpath, std::string &outpath, MountPoint **system);9293inline int MapFilePath(const std::string &_inpath, std::string &outpath, IFileSystem **system) {94MountPoint *mountPoint = nullptr;95int error = MapFilePath(_inpath, outpath, &mountPoint);96if (error == 0) {97*system = mountPoint->system.get();98return error;99}100101return error;102}103104std::string NormalizePrefix(std::string prefix) const;105106std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;107int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;108void CloseFile(u32 handle) override;109size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;110size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;111size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;112size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;113size_t SeekFile(u32 handle, s32 position, FileMove type) override;114PSPFileInfo GetFileInfo(std::string filename) override;115bool OwnsHandle(u32 handle) override { return false; }116inline size_t GetSeekPos(u32 handle) {117return SeekFile(handle, 0, FILEMOVE_CURRENT);118}119120virtual int ChDir(const std::string &dir);121122bool MkDir(const std::string &dirname) override;123bool RmDir(const std::string &dirname) override;124int RenameFile(const std::string &from, const std::string &to) override;125bool RemoveFile(const std::string &filename) override;126int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;127PSPDevType DevType(u32 handle) override;128FileSystemFlags Flags() override { return FileSystemFlags::NONE; }129u64 FreeSpace(const std::string &path) override;130131// Convenience helper - returns < 0 on failure.132int ReadEntireFile(const std::string &filename, std::vector<u8> &data, bool quiet = false);133134void SetStartingDirectory(const std::string &dir) {135std::lock_guard<std::recursive_mutex> guard(lock);136startingDirectory = dir;137}138139int64_t ComputeRecursiveDirectorySize(const std::string &dirPath);140141// Shouldn't ever be called, but meh.142bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override {143int64_t sizeTemp = ComputeRecursiveDirectorySize(path);144if (sizeTemp >= 0) {145*size = sizeTemp;146return true;147} else {148return false;149}150}151152private:153int64_t RecursiveSize(const std::string &dirPath);154};155156157