Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/MetaFileSystem.h
3187 views
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
mutable 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
// Will replace the existing mount if already exists.
62
void Mount(const std::string &prefix, std::shared_ptr<IFileSystem> system);
63
64
void UnmountAll();
65
void Unmount(const std::string &prefix);
66
67
// Would like to make this const, but...
68
std::vector<MountPoint> &GetMounts() {
69
return fileSystems;
70
}
71
72
// The pointer returned from these are for temporary usage only. Do not store.
73
IFileSystem *GetSystem(const std::string &prefix);
74
IFileSystem *GetSystemFromFilename(const std::string &filename);
75
IFileSystem *GetHandleOwner(u32 handle) const;
76
FileSystemFlags FlagsFromFilename(const std::string &filename) {
77
IFileSystem *sys = GetSystemFromFilename(filename);
78
return sys ? sys->Flags() : FileSystemFlags::NONE;
79
}
80
81
void ThreadEnded(int threadID);
82
void Shutdown();
83
84
u32 GetNewHandle() override {
85
u32 res = current++;
86
if (current < 0) {
87
// Some code assumes it'll never become 0.
88
current = 1;
89
}
90
return res;
91
}
92
void FreeHandle(u32 handle) override {}
93
94
void DoState(PointerWrap &p) override;
95
96
int MapFilePath(const std::string &inpath, std::string &outpath, MountPoint **system);
97
98
inline int MapFilePath(const std::string &_inpath, std::string &outpath, IFileSystem **system) {
99
MountPoint *mountPoint = nullptr;
100
int error = MapFilePath(_inpath, outpath, &mountPoint);
101
if (error == 0) {
102
*system = mountPoint->system.get();
103
return error;
104
}
105
106
return error;
107
}
108
109
std::string NormalizePrefix(std::string prefix) const;
110
111
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
112
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
113
void CloseFile(u32 handle) override;
114
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
115
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
116
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
117
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
118
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
119
PSPFileInfo GetFileInfo(std::string filename) override;
120
PSPFileInfo GetFileInfoByHandle(u32 handle) override;
121
bool OwnsHandle(u32 handle) override { return false; }
122
inline size_t GetSeekPos(u32 handle) {
123
return SeekFile(handle, 0, FILEMOVE_CURRENT);
124
}
125
126
virtual int ChDir(const std::string &dir);
127
128
bool MkDir(const std::string &dirname) override;
129
bool RmDir(const std::string &dirname) override;
130
int RenameFile(const std::string &from, const std::string &to) override;
131
bool RemoveFile(const std::string &filename) override;
132
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
133
PSPDevType DevType(u32 handle) override;
134
FileSystemFlags Flags() const override { return FileSystemFlags::NONE; }
135
u64 FreeDiskSpace(const std::string &path) override;
136
137
// Convenience helper - returns < 0 on failure.
138
int ReadEntireFile(const std::string &filename, std::vector<u8> &data, bool quiet = false);
139
140
void SetStartingDirectory(const std::string &dir) {
141
std::lock_guard<std::recursive_mutex> guard(lock);
142
startingDirectory = dir;
143
}
144
145
int64_t ComputeRecursiveDirectorySize(const std::string &dirPath);
146
147
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;
148
149
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Meta"); }
150
151
private:
152
int64_t RecursiveSize(const std::string &dirPath);
153
};
154
155