Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/FileSystem.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
// TODO: Somehow unify FileSystem, VFS and FileLoaders. Actually, maybe FileSystem and VFS have the most in common
21
// but file systems should be able to contain FileLoader as files. Then we can do stuff like playing homebrew directly
22
// out of zip files, and similar tricks.
23
24
#include <vector>
25
#include <string>
26
#include <cstring>
27
#include <cstdint>
28
#include <ctime>
29
30
#include "Common.h"
31
#include "Common/File/Path.h"
32
#include "Core/HLE/sceKernel.h"
33
#include "Core/HLE/ErrorCodes.h"
34
35
enum FileAccess {
36
FILEACCESS_NONE = 0,
37
FILEACCESS_READ = 1,
38
FILEACCESS_WRITE = 2,
39
FILEACCESS_APPEND = 4,
40
FILEACCESS_CREATE = 8,
41
FILEACCESS_TRUNCATE = 16,
42
FILEACCESS_EXCL = 32,
43
44
FILEACCESS_PSP_FLAGS = 63, // Sum of all the above.
45
46
// Non-PSP flags
47
FILEACCESS_PPSSPP_QUIET = 128,
48
};
49
50
enum FileMove {
51
FILEMOVE_BEGIN = 0,
52
FILEMOVE_CURRENT = 1,
53
FILEMOVE_END = 2
54
};
55
56
enum FileType {
57
FILETYPE_NORMAL = 1,
58
FILETYPE_DIRECTORY = 2
59
};
60
61
enum class PSPDevType {
62
INVALID = 0,
63
BLOCK = 0x04,
64
FILE = 0x10,
65
ALIAS = 0x20,
66
EMU_MASK = 0xFF,
67
EMU_LBN = 0x10000,
68
};
69
ENUM_CLASS_BITOPS(PSPDevType);
70
71
enum class FileSystemFlags {
72
NONE = 0,
73
SIMULATE_FAT32 = 1,
74
UMD = 2,
75
CARD = 4,
76
FLASH = 8,
77
STRIP_PSP = 16,
78
CASE_SENSITIVE = 32,
79
};
80
ENUM_CLASS_BITOPS(FileSystemFlags);
81
82
class IHandleAllocator {
83
public:
84
virtual ~IHandleAllocator() {}
85
virtual u32 GetNewHandle() = 0;
86
virtual void FreeHandle(u32 handle) = 0;
87
};
88
89
class SequentialHandleAllocator : public IHandleAllocator {
90
public:
91
SequentialHandleAllocator() : handle_(1) {}
92
93
SequentialHandleAllocator(SequentialHandleAllocator &) = delete;
94
void operator =(SequentialHandleAllocator &) = delete;
95
96
u32 GetNewHandle() override {
97
u32 res = handle_++;
98
if (handle_ < 0) {
99
// Some code assumes it'll never become 0.
100
handle_ = 1;
101
}
102
return res;
103
}
104
void FreeHandle(u32 handle) override {}
105
private:
106
int handle_;
107
};
108
109
struct PSPFileInfo {
110
PSPFileInfo() {
111
}
112
113
void DoState(PointerWrap &p);
114
115
std::string name;
116
s64 size = 0;
117
u32 access = 0; //unix 777
118
bool exists = false;
119
FileType type = FILETYPE_NORMAL;
120
121
tm atime{};
122
tm ctime{};
123
tm mtime{};
124
125
bool isOnSectorSystem = false;
126
u32 startSector = 0;
127
u32 numSectors = 0;
128
u32 sectorSize = 0;
129
};
130
131
132
class IFileSystem {
133
public:
134
virtual ~IFileSystem() {}
135
136
virtual void DoState(PointerWrap &p) = 0;
137
virtual std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) = 0;
138
virtual int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) = 0;
139
virtual void CloseFile(u32 handle) = 0;
140
virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size) = 0;
141
virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) = 0;
142
virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size) = 0;
143
virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) = 0;
144
virtual size_t SeekFile(u32 handle, s32 position, FileMove type) = 0;
145
virtual PSPFileInfo GetFileInfo(std::string filename) = 0;
146
virtual PSPFileInfo GetFileInfoByHandle(u32 handle) = 0; // Mainly used for debugging.
147
virtual bool OwnsHandle(u32 handle) = 0;
148
virtual bool MkDir(const std::string &dirname) = 0;
149
virtual bool RmDir(const std::string &dirname) = 0;
150
virtual int RenameFile(const std::string &from, const std::string &to) = 0;
151
virtual bool RemoveFile(const std::string &filename) = 0;
152
virtual int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) = 0;
153
virtual PSPDevType DevType(u32 handle) = 0;
154
virtual FileSystemFlags Flags() const = 0;
155
virtual u64 FreeDiskSpace(const std::string &path) = 0;
156
virtual bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) = 0;
157
virtual void Describe(char *buf, size_t size) const = 0;
158
};
159
160
161
class EmptyFileSystem : public IFileSystem {
162
public:
163
void DoState(PointerWrap &p) override {}
164
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override {
165
if (exists)
166
*exists = false;
167
std::vector<PSPFileInfo> vec;
168
return vec;
169
}
170
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override {return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;}
171
void CloseFile(u32 handle) override {}
172
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override {return 0;}
173
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override {return 0;}
174
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override {return 0;}
175
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override {return 0;}
176
size_t SeekFile(u32 handle, s32 position, FileMove type) override {return 0;}
177
PSPFileInfo GetFileInfo(std::string filename) override {PSPFileInfo f; return f;}
178
PSPFileInfo GetFileInfoByHandle(u32 handle) override {PSPFileInfo f; return f;}
179
bool OwnsHandle(u32 handle) override {return false;}
180
bool MkDir(const std::string &dirname) override {return false;}
181
bool RmDir(const std::string &dirname) override {return false;}
182
int RenameFile(const std::string &from, const std::string &to) override {return -1;}
183
bool RemoveFile(const std::string &filename) override {return false;}
184
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override { return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED; }
185
PSPDevType DevType(u32 handle) override { return PSPDevType::INVALID; }
186
FileSystemFlags Flags() const override { return FileSystemFlags::NONE; }
187
u64 FreeDiskSpace(const std::string &path) override { return 0; }
188
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
189
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "%s", "Empty"); }
190
};
191
192
193
194