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