1
// Copyright (c) 2012- PPSSPP Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
20
// TODO: Remove the Windows-specific code, FILE is fine there too.
24
#include "Core/FileSystems/FileSystem.h"
25
#include "Core/FileSystems/DirectoryFileSystem.h"
27
class VirtualDiscFileSystem: public IFileSystem {
29
VirtualDiscFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
30
~VirtualDiscFileSystem();
32
void DoState(PointerWrap &p) override;
33
u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) override;
34
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
35
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
36
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
37
void CloseFile(u32 handle) override;
38
PSPFileInfo GetFileInfo(std::string filename) override;
39
bool OwnsHandle(u32 handle) override;
40
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
41
int DevType(u32 handle) override;
42
bool GetHostPath(const std::string &inpath, std::string &outpath) override;
43
std::vector<PSPFileInfo> GetDirListing(std::string path) override;
44
int Flags() override { return 0; }
45
u64 FreeSpace(const std::string &path) override { return 0; }
47
// unsupported operations
48
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
49
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
50
bool MkDir(const std::string &dirname) override;
51
bool RmDir(const std::string &dirname) override;
52
int RenameFile(const std::string &from, const std::string &to) override;
53
bool RemoveFile(const std::string &filename) override;
56
void LoadFileListIndex();
57
// Warning: modifies input string.
58
int getFileListIndex(std::string &fileName);
59
int getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode = false);
60
std::string GetLocalPath(std::string localpath);
62
typedef void *HandlerLibrary;
63
typedef int HandlerHandle;
64
typedef s64 HandlerOffset;
65
typedef void (*HandlerLogFunc)(void *arg, HandlerHandle handle, LogTypes::LOG_LEVELS level, const char *msg);
67
static void HandlerLogger(void *arg, HandlerHandle handle, LogTypes::LOG_LEVELS level, const char *msg);
69
// The primary purpose of handlers is to make it easier to work with large archives.
70
// However, they have other uses as well, such as patching individual files.
72
Handler(const char *filename, VirtualDiscFileSystem *const sys);
75
typedef bool (*InitFunc)(HandlerLogFunc logger, void *loggerArg);
76
typedef void (*ShutdownFunc)();
77
typedef HandlerHandle (*OpenFunc)(const char *basePath, const char *filename);
78
typedef HandlerOffset (*SeekFunc)(HandlerHandle handle, HandlerOffset offset, FileMove origin);
79
typedef HandlerOffset (*ReadFunc)(HandlerHandle handle, void *data, HandlerOffset size);
80
typedef void (*CloseFunc)(HandlerHandle handle);
82
HandlerLibrary library;
84
ShutdownFunc Shutdown;
90
bool IsValid() const { return library != NULL; }
93
struct HandlerFileHandle {
97
HandlerFileHandle() : handler(NULL), handle(0) {
99
HandlerFileHandle(Handler *handler_) : handler(handler_), handle(-1) {
102
bool Open(std::string& basePath, std::string& fileName, FileAccess access) {
103
// Ignore access, read only.
104
handle = handler->Open(basePath.c_str(), fileName.c_str());
107
size_t Read(u8 *data, s64 size) {
108
return (size_t)handler->Read(handle, data, size);
110
size_t Seek(s32 position, FileMove type) {
111
return (size_t)handler->Seek(handle, position, type);
114
handler->Close(handle);
118
return handler != NULL && handler->IsValid();
121
HandlerFileHandle &operator =(Handler *_handler) {
127
typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType;
129
struct OpenFileEntry {
130
DirectoryFileHandle hFile;
131
HandlerFileHandle handler;
132
VirtualFileType type;
135
u64 startOffset; // only used by lbn files
136
u64 size; // only used by lbn files
138
bool Open(std::string& basePath, std::string& fileName, FileAccess access) {
139
// Ignored, we're read only.
141
if (handler.IsValid()) {
142
return handler.Open(basePath, fileName, access);
144
return hFile.Open(basePath, fileName, access, err);
147
size_t Read(u8 *data, s64 size) {
148
if (handler.IsValid()) {
149
return handler.Read(data, size);
151
return hFile.Read(data, size);
154
size_t Seek(s32 position, FileMove type) {
155
if (handler.IsValid()) {
156
return handler.Seek(position, type);
158
return hFile.Seek(position, type);
162
if (handler.IsValid()) {
163
return handler.Close();
165
return hFile.Close();
170
typedef std::map<u32, OpenFileEntry> EntryMap;
172
IHandleAllocator *hAlloc;
173
std::string basePath;
175
struct FileListEntry {
176
std::string fileName;
182
std::vector<FileListEntry> fileList;
183
u32 currentBlockIndex;
186
std::map<std::string, Handler *> handlers;