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