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/VirtualDiscFileSystem.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
// TODO: Remove the Windows-specific code, FILE is fine there too.
21
22
#include <map>
23
24
#include "Common/File/Path.h"
25
#include "Core/FileSystems/FileSystem.h"
26
#include "Core/FileSystems/DirectoryFileSystem.h"
27
28
extern const std::string INDEX_FILENAME;
29
30
class VirtualDiscFileSystem: public IFileSystem {
31
public:
32
VirtualDiscFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath);
33
~VirtualDiscFileSystem();
34
35
void DoState(PointerWrap &p) override;
36
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
37
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
38
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
39
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
40
void CloseFile(u32 handle) override;
41
PSPFileInfo GetFileInfo(std::string filename) override;
42
bool OwnsHandle(u32 handle) override;
43
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
44
PSPDevType DevType(u32 handle) override;
45
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
46
FileSystemFlags Flags() override { return FileSystemFlags::UMD; }
47
u64 FreeSpace(const std::string &path) override { return 0; }
48
49
// unsupported operations
50
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
51
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
52
bool MkDir(const std::string &dirname) override;
53
bool RmDir(const std::string &dirname) override;
54
int RenameFile(const std::string &from, const std::string &to) override;
55
bool RemoveFile(const std::string &filename) override;
56
57
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
58
59
private:
60
void LoadFileListIndex();
61
// Warning: modifies input string.
62
int getFileListIndex(std::string &fileName);
63
int getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode = false);
64
Path GetLocalPath(std::string localpath);
65
66
typedef void *HandlerLibrary;
67
typedef int HandlerHandle;
68
typedef s64 HandlerOffset;
69
typedef void (*HandlerLogFunc)(void *arg, HandlerHandle handle, LogLevel level, const char *msg);
70
71
static void HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg);
72
73
// The primary purpose of handlers is to make it easier to work with large archives.
74
// However, they have other uses as well, such as patching individual files.
75
struct Handler {
76
Handler(const char *filename, VirtualDiscFileSystem *const sys);
77
~Handler();
78
79
typedef bool (*InitFunc)(HandlerLogFunc logger, void *loggerArg);
80
typedef void (*ShutdownFunc)();
81
typedef void (*ShutdownV2Func)(void *loggerArg);
82
typedef HandlerHandle (*OpenFunc)(const char *basePath, const char *filename);
83
typedef HandlerOffset (*SeekFunc)(HandlerHandle handle, HandlerOffset offset, FileMove origin);
84
typedef HandlerOffset (*ReadFunc)(HandlerHandle handle, void *data, HandlerOffset size);
85
typedef void (*CloseFunc)(HandlerHandle handle);
86
typedef int (*VersionFunc)();
87
88
HandlerLibrary library;
89
VirtualDiscFileSystem *const sys_;
90
InitFunc Init;
91
ShutdownFunc Shutdown;
92
ShutdownV2Func ShutdownV2;
93
OpenFunc Open;
94
SeekFunc Seek;
95
ReadFunc Read;
96
CloseFunc Close;
97
98
bool IsValid() const { return library != nullptr; }
99
};
100
101
struct HandlerFileHandle {
102
Handler *handler;
103
HandlerHandle handle;
104
105
HandlerFileHandle() : handler(nullptr), handle(0) {}
106
HandlerFileHandle(Handler *handler_) : handler(handler_), handle(-1) {}
107
108
bool Open(const std::string& basePath, const std::string& fileName, FileAccess access) {
109
// Ignore access, read only.
110
handle = handler->Open(basePath.c_str(), fileName.c_str());
111
return handle > 0;
112
}
113
size_t Read(u8 *data, s64 size) {
114
return (size_t)handler->Read(handle, data, size);
115
}
116
size_t Seek(s32 position, FileMove type) {
117
return (size_t)handler->Seek(handle, position, type);
118
}
119
void Close() {
120
handler->Close(handle);
121
}
122
123
bool IsValid() {
124
return handler != nullptr && handler->IsValid();
125
}
126
127
HandlerFileHandle &operator =(Handler *_handler) {
128
handler = _handler;
129
return *this;
130
}
131
};
132
133
typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType;
134
135
struct OpenFileEntry {
136
OpenFileEntry() {}
137
OpenFileEntry(FileSystemFlags fileSystemFlags) {
138
hFile = DirectoryFileHandle(DirectoryFileHandle::SKIP_REPLAY, fileSystemFlags);
139
}
140
141
DirectoryFileHandle hFile;
142
HandlerFileHandle handler;
143
VirtualFileType type = VFILETYPE_NORMAL;
144
u32 fileIndex = 0;
145
u64 curOffset = 0;
146
u64 startOffset = 0; // only used by lbn files
147
u64 size = 0; // only used by lbn files
148
149
bool Open(const Path &basePath, std::string& fileName, FileAccess access) {
150
// Ignored, we're read only.
151
u32 err;
152
if (handler.IsValid()) {
153
return handler.Open(basePath.ToString(), fileName, access);
154
} else {
155
return hFile.Open(basePath, fileName, access, err);
156
}
157
}
158
size_t Read(u8 *data, s64 size) {
159
if (handler.IsValid()) {
160
return handler.Read(data, size);
161
} else {
162
return hFile.Read(data, size);
163
}
164
}
165
size_t Seek(s32 position, FileMove type) {
166
if (handler.IsValid()) {
167
return handler.Seek(position, type);
168
} else {
169
return hFile.Seek(position, type);
170
}
171
}
172
void Close() {
173
if (handler.IsValid()) {
174
return handler.Close();
175
} else {
176
return hFile.Close();
177
}
178
}
179
};
180
181
typedef std::map<u32, OpenFileEntry> EntryMap;
182
183
EntryMap entries;
184
IHandleAllocator *hAlloc;
185
Path basePath;
186
187
struct FileListEntry {
188
std::string fileName;
189
u32 firstBlock;
190
u32 totalSize;
191
Handler *handler;
192
};
193
194
std::vector<FileListEntry> fileList;
195
u32 currentBlockIndex;
196
u32 lastReadBlock_;
197
198
std::map<std::string, Handler *> handlers;
199
};
200
201