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/DirectoryFileSystem.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
27
#ifdef _WIN32
28
typedef void * HANDLE;
29
#endif
30
31
struct DirectoryFileHandle {
32
enum Flags {
33
NORMAL,
34
SKIP_REPLAY,
35
};
36
37
#ifdef _WIN32
38
HANDLE hFile = (HANDLE)-1;
39
#else
40
int hFile = -1;
41
#endif
42
s64 needsTrunc_ = -1;
43
bool replay_ = true;
44
bool inGameDir_ = false;
45
FileSystemFlags fileSystemFlags_ = (FileSystemFlags)0;
46
47
DirectoryFileHandle() {}
48
49
DirectoryFileHandle(Flags flags, FileSystemFlags fileSystemFlags)
50
: replay_(flags != SKIP_REPLAY), fileSystemFlags_(fileSystemFlags) {}
51
52
Path GetLocalPath(const Path &basePath, std::string localpath) const;
53
bool Open(const Path &basePath, std::string &fileName, FileAccess access, u32 &err);
54
size_t Read(u8* pointer, s64 size);
55
size_t Write(const u8* pointer, s64 size);
56
size_t Seek(s32 position, FileMove type);
57
void Close();
58
};
59
60
class DirectoryFileSystem : public IFileSystem {
61
public:
62
DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath, FileSystemFlags _flags = FileSystemFlags::NONE);
63
~DirectoryFileSystem();
64
65
void CloseAll();
66
67
void DoState(PointerWrap &p) override;
68
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
69
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
70
void CloseFile(u32 handle) override;
71
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
72
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
73
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
74
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
75
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
76
PSPFileInfo GetFileInfo(std::string filename) override;
77
bool OwnsHandle(u32 handle) override;
78
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
79
PSPDevType DevType(u32 handle) override;
80
81
bool MkDir(const std::string &dirname) override;
82
bool RmDir(const std::string &dirname) override;
83
int RenameFile(const std::string &from, const std::string &to) override;
84
bool RemoveFile(const std::string &filename) override;
85
FileSystemFlags Flags() override { return flags; }
86
u64 FreeSpace(const std::string &path) override;
87
88
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;
89
90
private:
91
struct OpenFileEntry {
92
DirectoryFileHandle hFile;
93
std::string guestFilename;
94
FileAccess access = FILEACCESS_NONE;
95
};
96
97
typedef std::map<u32, OpenFileEntry> EntryMap;
98
EntryMap entries;
99
Path basePath;
100
IHandleAllocator *hAlloc;
101
FileSystemFlags flags;
102
103
Path GetLocalPath(std::string internalPath) const;
104
};
105
106
// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts.
107
// Very inefficient - always load the whole file on open.
108
class VFSFileSystem : public IFileSystem {
109
public:
110
VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
111
~VFSFileSystem();
112
113
void DoState(PointerWrap &p) override;
114
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
115
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
116
void CloseFile(u32 handle) override;
117
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
118
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
119
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
120
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
121
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
122
PSPFileInfo GetFileInfo(std::string filename) override;
123
bool OwnsHandle(u32 handle) override;
124
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
125
PSPDevType DevType(u32 handle) override;
126
127
bool MkDir(const std::string &dirname) override;
128
bool RmDir(const std::string &dirname) override;
129
int RenameFile(const std::string &from, const std::string &to) override;
130
bool RemoveFile(const std::string &filename) override;
131
FileSystemFlags Flags() override { return FileSystemFlags::FLASH; }
132
u64 FreeSpace(const std::string &path) override { return 0; }
133
134
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
135
136
private:
137
struct OpenFileEntry {
138
u8 *fileData;
139
size_t size;
140
size_t seekPos;
141
};
142
143
typedef std::map<u32, OpenFileEntry> EntryMap;
144
EntryMap entries;
145
std::string basePath;
146
IHandleAllocator *hAlloc;
147
148
std::string GetLocalPath(const std::string &localpath);
149
};
150
151