CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/MetaFileSystem.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <string>
21
#include <vector>
22
#include <mutex>
23
#include <memory>
24
25
#include "Core/FileSystems/FileSystem.h"
26
27
class MetaFileSystem : public IHandleAllocator, public IFileSystem {
28
private:
29
s32 current;
30
struct MountPoint {
31
std::string prefix;
32
std::shared_ptr<IFileSystem> system;
33
34
bool operator == (const MountPoint &other) const {
35
return prefix == other.prefix && system == other.system;
36
}
37
};
38
39
// The order of this vector is meaningful - lookups are always a linear search from the start.
40
std::vector<MountPoint> fileSystems;
41
42
typedef std::map<int, std::string> currentDir_t;
43
currentDir_t currentDir;
44
45
std::string startingDirectory;
46
std::recursive_mutex lock; // must be recursive. TODO: fix that
47
48
// Assumes the lock is held
49
void Reset() {
50
// This used to be 6, probably an attempt to replicate PSP handles.
51
// However, that's an artifact of using psplink anyway...
52
current = 1;
53
startingDirectory.clear();
54
}
55
56
public:
57
MetaFileSystem() {
58
Reset();
59
}
60
61
void Mount(const std::string &prefix, std::shared_ptr<IFileSystem> system);
62
// Fails if there's not already a file system at prefix.
63
bool Remount(const std::string &prefix, std::shared_ptr<IFileSystem> system);
64
65
void UnmountAll();
66
void Unmount(const std::string &prefix);
67
68
// The pointer returned from these are for temporary usage only. Do not store.
69
IFileSystem *GetSystem(const std::string &prefix);
70
IFileSystem *GetSystemFromFilename(const std::string &filename);
71
IFileSystem *GetHandleOwner(u32 handle);
72
FileSystemFlags FlagsFromFilename(const std::string &filename) {
73
IFileSystem *sys = GetSystemFromFilename(filename);
74
return sys ? sys->Flags() : FileSystemFlags::NONE;
75
}
76
77
void ThreadEnded(int threadID);
78
void Shutdown();
79
80
u32 GetNewHandle() override {
81
u32 res = current++;
82
if (current < 0) {
83
// Some code assumes it'll never become 0.
84
current = 1;
85
}
86
return res;
87
}
88
void FreeHandle(u32 handle) override {}
89
90
void DoState(PointerWrap &p) override;
91
92
int MapFilePath(const std::string &inpath, std::string &outpath, MountPoint **system);
93
94
inline int MapFilePath(const std::string &_inpath, std::string &outpath, IFileSystem **system) {
95
MountPoint *mountPoint = nullptr;
96
int error = MapFilePath(_inpath, outpath, &mountPoint);
97
if (error == 0) {
98
*system = mountPoint->system.get();
99
return error;
100
}
101
102
return error;
103
}
104
105
std::string NormalizePrefix(std::string prefix) const;
106
107
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
108
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
109
void CloseFile(u32 handle) override;
110
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
111
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
112
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
113
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
114
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
115
PSPFileInfo GetFileInfo(std::string filename) override;
116
bool OwnsHandle(u32 handle) override { return false; }
117
inline size_t GetSeekPos(u32 handle) {
118
return SeekFile(handle, 0, FILEMOVE_CURRENT);
119
}
120
121
virtual int ChDir(const std::string &dir);
122
123
bool MkDir(const std::string &dirname) override;
124
bool RmDir(const std::string &dirname) override;
125
int RenameFile(const std::string &from, const std::string &to) override;
126
bool RemoveFile(const std::string &filename) override;
127
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
128
PSPDevType DevType(u32 handle) override;
129
FileSystemFlags Flags() override { return FileSystemFlags::NONE; }
130
u64 FreeSpace(const std::string &path) override;
131
132
// Convenience helper - returns < 0 on failure.
133
int ReadEntireFile(const std::string &filename, std::vector<u8> &data, bool quiet = false);
134
135
void SetStartingDirectory(const std::string &dir) {
136
std::lock_guard<std::recursive_mutex> guard(lock);
137
startingDirectory = dir;
138
}
139
140
int64_t ComputeRecursiveDirectorySize(const std::string &dirPath);
141
142
// Shouldn't ever be called, but meh.
143
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override {
144
int64_t sizeTemp = ComputeRecursiveDirectorySize(path);
145
if (sizeTemp >= 0) {
146
*size = sizeTemp;
147
return true;
148
} else {
149
return false;
150
}
151
}
152
153
private:
154
int64_t RecursiveSize(const std::string &dirPath);
155
};
156
157