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/ISOFileSystem.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 <map>
21
#include <list>
22
#include <memory>
23
24
#include "FileSystem.h"
25
26
#include "BlockDevices.h"
27
28
bool parseLBN(const std::string &filename, u32 *sectorStart, u32 *readSize);
29
30
class ISOFileSystem : public IFileSystem {
31
public:
32
ISOFileSystem(IHandleAllocator *_hAlloc, BlockDevice *_blockDevice);
33
~ISOFileSystem();
34
35
void DoState(PointerWrap &p) override;
36
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
37
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
38
void CloseFile(u32 handle) override;
39
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
40
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
41
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
42
PSPFileInfo GetFileInfo(std::string filename) override;
43
bool OwnsHandle(u32 handle) override;
44
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
45
PSPDevType DevType(u32 handle) override;
46
FileSystemFlags Flags() override;
47
u64 FreeSpace(const std::string &path) override { return 0; }
48
49
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
50
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
51
52
bool MkDir(const std::string &dirname) override {return false;}
53
bool RmDir(const std::string &dirname) override { return false; }
54
int RenameFile(const std::string &from, const std::string &to) override { return -1; }
55
bool RemoveFile(const std::string &filename) override { return false; }
56
57
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
58
59
private:
60
struct TreeEntry {
61
~TreeEntry();
62
63
// Recursive function that reconstructs the path by looking at the parent pointers.
64
std::string BuildPath();
65
66
std::string name;
67
u32 flags = 0;
68
u32 startingPosition = 0;
69
s64 size = 0;
70
bool isDirectory = false;
71
72
u32 startsector = 0;
73
u32 dirsize = 0;
74
75
TreeEntry *parent = nullptr;
76
77
bool valid = false;
78
std::vector<TreeEntry *> children;
79
};
80
81
struct OpenFileEntry {
82
TreeEntry *file;
83
unsigned int seekPos; // TODO: Make 64-bit?
84
bool isRawSector; // "/sce_lbn" mode
85
bool isBlockSectorMode; // "umd:" mode: all sizes and offsets are in 2048 byte chunks
86
u32 sectorStart;
87
u32 openSize;
88
};
89
90
typedef std::map<u32, OpenFileEntry> EntryMap;
91
EntryMap entries;
92
IHandleAllocator *hAlloc;
93
TreeEntry *treeroot;
94
BlockDevice *blockDevice;
95
u32 lastReadBlock_;
96
97
TreeEntry entireISO;
98
99
void ReadDirectory(TreeEntry *root);
100
TreeEntry *GetFromPath(const std::string &path, bool catchError = true);
101
std::string EntryFullPath(TreeEntry *e);
102
};
103
104
// On the "umd0:" device, any file you open is the entire ISO.
105
// Simply wrap around an ISOFileSystem which has all the necessary machinery, while changing
106
// the filenames to "", to achieve this.
107
class ISOBlockSystem : public IFileSystem {
108
public:
109
ISOBlockSystem(std::shared_ptr<IFileSystem> isoFileSystem) : isoFileSystem_(isoFileSystem) {}
110
111
void DoState(PointerWrap &p) override {
112
// This is a bit iffy, as block device savestates already are iffy (loads/saves multiple times for multiple mounts..)
113
isoFileSystem_->DoState(p);
114
}
115
116
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override {
117
if (exists)
118
*exists = true;
119
return std::vector<PSPFileInfo>();
120
}
121
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override {
122
return isoFileSystem_->OpenFile("", access, devicename);
123
}
124
void CloseFile(u32 handle) override {
125
isoFileSystem_->CloseFile(handle);
126
}
127
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override {
128
return isoFileSystem_->ReadFile(handle, pointer, size);
129
}
130
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override {
131
return isoFileSystem_->ReadFile(handle, pointer, size, usec);
132
}
133
size_t SeekFile(u32 handle, s32 position, FileMove type) override {
134
return isoFileSystem_->SeekFile(handle, position, type);
135
}
136
PSPFileInfo GetFileInfo(std::string filename) override {
137
return isoFileSystem_->GetFileInfo("");
138
}
139
bool OwnsHandle(u32 handle) override {
140
return isoFileSystem_->OwnsHandle(handle);
141
}
142
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override {
143
return isoFileSystem_->Ioctl(handle, cmd, indataPtr, inlen, outdataPtr, outlen, usec);
144
}
145
PSPDevType DevType(u32 handle) override {
146
return isoFileSystem_->DevType(handle);
147
}
148
FileSystemFlags Flags() override { return isoFileSystem_->Flags(); }
149
u64 FreeSpace(const std::string &path) override { return isoFileSystem_->FreeSpace(path); }
150
151
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override {
152
return isoFileSystem_->WriteFile(handle, pointer, size);
153
}
154
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override {
155
return isoFileSystem_->WriteFile(handle, pointer, size, usec);
156
}
157
bool MkDir(const std::string &dirname) override { return false; }
158
bool RmDir(const std::string &dirname) override { return false; }
159
int RenameFile(const std::string &from, const std::string &to) override { return -1; }
160
bool RemoveFile(const std::string &filename) override { return false; }
161
162
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
163
164
private:
165
std::shared_ptr<IFileSystem> isoFileSystem_;
166
};
167
168