summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ShizZy2013-09-19 23:28:34 -0400
committerGravatar ShizZy2013-09-19 23:28:34 -0400
commit80b85ee7e6ca605820dfe3d983cf80c071330527 (patch)
treec163727b50e2a42f1d4b4dc01c7b47412fb6f941 /src
parentupdated to chunk_file module from ppsspp (diff)
downloadyuzu-80b85ee7e6ca605820dfe3d983cf80c071330527.tar.gz
yuzu-80b85ee7e6ca605820dfe3d983cf80c071330527.tar.xz
yuzu-80b85ee7e6ca605820dfe3d983cf80c071330527.zip
ppsspp file system module - currently unused
Diffstat (limited to 'src')
-rw-r--r--src/core/src/file_sys/file_sys.h138
-rw-r--r--src/core/src/file_sys/file_sys_directory.cpp712
-rw-r--r--src/core/src/file_sys/file_sys_directory.h158
3 files changed, 1008 insertions, 0 deletions
diff --git a/src/core/src/file_sys/file_sys.h b/src/core/src/file_sys/file_sys.h
new file mode 100644
index 000000000..8611d743f
--- /dev/null
+++ b/src/core/src/file_sys/file_sys.h
@@ -0,0 +1,138 @@
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 "common.h"
21#include "chunk_file.h"
22
23enum FileAccess {
24 FILEACCESS_NONE=0,
25 FILEACCESS_READ=1,
26 FILEACCESS_WRITE=2,
27 FILEACCESS_APPEND=4,
28 FILEACCESS_CREATE=8
29};
30
31enum FileMove {
32 FILEMOVE_BEGIN=0,
33 FILEMOVE_CURRENT=1,
34 FILEMOVE_END=2
35};
36
37enum FileType {
38 FILETYPE_NORMAL=1,
39 FILETYPE_DIRECTORY=2
40};
41
42
43class IHandleAllocator {
44public:
45 virtual ~IHandleAllocator() {}
46 virtual u32 GetNewHandle() = 0;
47 virtual void FreeHandle(u32 handle) = 0;
48};
49
50class SequentialHandleAllocator : public IHandleAllocator {
51public:
52 SequentialHandleAllocator() : handle_(1) {}
53 virtual u32 GetNewHandle() { return handle_++; }
54 virtual void FreeHandle(u32 handle) {}
55private:
56 int handle_;
57};
58
59struct PSPFileInfo {
60 PSPFileInfo()
61 : size(0), access(0), exists(false), type(FILETYPE_NORMAL), isOnSectorSystem(false), startSector(0), numSectors(0) {}
62
63 void DoState(PointerWrap &p) {
64 auto s = p.Section("PSPFileInfo", 1);
65 if (!s)
66 return;
67
68 p.Do(name);
69 p.Do(size);
70 p.Do(access);
71 p.Do(exists);
72 p.Do(type);
73 p.Do(atime);
74 p.Do(ctime);
75 p.Do(mtime);
76 p.Do(isOnSectorSystem);
77 p.Do(startSector);
78 p.Do(numSectors);
79 p.Do(sectorSize);
80 }
81
82 std::string name;
83 s64 size;
84 u32 access; //unix 777
85 bool exists;
86 FileType type;
87
88 tm atime;
89 tm ctime;
90 tm mtime;
91
92 bool isOnSectorSystem;
93 u32 startSector;
94 u32 numSectors;
95 u32 sectorSize;
96};
97
98
99class IFileSystem {
100public:
101 virtual ~IFileSystem() {}
102
103 virtual void DoState(PointerWrap &p) = 0;
104 virtual std::vector<PSPFileInfo> GetDirListing(std::string path) = 0;
105 virtual u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) = 0;
106 virtual void CloseFile(u32 handle) = 0;
107 virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size) = 0;
108 virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size) = 0;
109 virtual size_t SeekFile(u32 handle, s32 position, FileMove type) = 0;
110 virtual PSPFileInfo GetFileInfo(std::string filename) = 0;
111 virtual bool OwnsHandle(u32 handle) = 0;
112 virtual bool MkDir(const std::string &dirname) = 0;
113 virtual bool RmDir(const std::string &dirname) = 0;
114 virtual int RenameFile(const std::string &from, const std::string &to) = 0;
115 virtual bool RemoveFile(const std::string &filename) = 0;
116 virtual bool GetHostPath(const std::string &inpath, std::string &outpath) = 0;
117};
118
119
120class EmptyFileSystem : public IFileSystem {
121public:
122 virtual void DoState(PointerWrap &p) {}
123 std::vector<PSPFileInfo> GetDirListing(std::string path) {std::vector<PSPFileInfo> vec; return vec;}
124 u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) {return 0;}
125 void CloseFile(u32 handle) {}
126 size_t ReadFile(u32 handle, u8 *pointer, s64 size) {return 0;}
127 size_t WriteFile(u32 handle, const u8 *pointer, s64 size) {return 0;}
128 size_t SeekFile(u32 handle, s32 position, FileMove type) {return 0;}
129 PSPFileInfo GetFileInfo(std::string filename) {PSPFileInfo f; return f;}
130 bool OwnsHandle(u32 handle) {return false;}
131 virtual bool MkDir(const std::string &dirname) {return false;}
132 virtual bool RmDir(const std::string &dirname) {return false;}
133 virtual int RenameFile(const std::string &from, const std::string &to) {return -1;}
134 virtual bool RemoveFile(const std::string &filename) {return false;}
135 virtual bool GetHostPath(const std::string &inpath, std::string &outpath) {return false;}
136};
137
138
diff --git a/src/core/src/file_sys/file_sys_directory.cpp b/src/core/src/file_sys/file_sys_directory.cpp
new file mode 100644
index 000000000..255369cb6
--- /dev/null
+++ b/src/core/src/file_sys/file_sys_directory.cpp
@@ -0,0 +1,712 @@
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#include "chunk_file.h"
19#include "file_util.h"
20#include "file_sys_directory.h"
21//#include "ISOFileSystem.h"
22//#include "Core/HLE/sceKernel.h"
23//#include "file/zip_read.h"
24//#include "util/text/utf8.h"
25
26#ifdef _WIN32
27//#include "Common/CommonWindows.h"
28#include <sys/stat.h>
29#else
30#include <dirent.h>
31#include <unistd.h>
32#include <sys/stat.h>
33#include <ctype.h>
34#endif
35
36std::string DirectoryFileHandle::GetLocalPath(std::string& basePath, std::string localpath)
37{
38 if (localpath.empty())
39 return basePath;
40
41 if (localpath[0] == '/')
42 localpath.erase(0,1);
43 //Convert slashes
44#ifdef _WIN32
45 for (size_t i = 0; i < localpath.size(); i++) {
46 if (localpath[i] == '/')
47 localpath[i] = '\\';
48 }
49#endif
50 return basePath + localpath;
51}
52
53bool DirectoryFileHandle::Open(std::string& basePath, std::string& fileName, FileAccess access) {
54 std::string fullName = GetLocalPath(basePath,fileName);
55 INFO_LOG(FILESYS, "Actually opening %s", fullName.c_str());
56
57 //TODO: tests, should append seek to end of file? seeking in a file opened for append?
58#ifdef _WIN32
59 // Convert parameters to Windows permissions and access
60 DWORD desired = 0;
61 DWORD sharemode = 0;
62 DWORD openmode = 0;
63 if (access & FILEACCESS_READ) {
64 desired |= GENERIC_READ;
65 sharemode |= FILE_SHARE_READ;
66 }
67 if (access & FILEACCESS_WRITE) {
68 desired |= GENERIC_WRITE;
69 sharemode |= FILE_SHARE_WRITE;
70 }
71 if (access & FILEACCESS_CREATE) {
72 openmode = OPEN_ALWAYS;
73 } else {
74 openmode = OPEN_EXISTING;
75 }
76 //Let's do it!
77 hFile = CreateFile(fullName.c_str(), desired, sharemode, 0, openmode, 0, 0);
78 bool success = hFile != INVALID_HANDLE_VALUE;
79#else
80 // Convert flags in access parameter to fopen access mode
81 const char *mode = NULL;
82 if (access & FILEACCESS_APPEND) {
83 if (access & FILEACCESS_READ)
84 mode = "ab+"; // append+read, create if needed
85 else
86 mode = "ab"; // append only, create if needed
87 } else if (access & FILEACCESS_WRITE) {
88 if (access & FILEACCESS_READ) {
89 // FILEACCESS_CREATE is ignored for read only, write only, and append
90 // because C++ standard fopen's nonexistant file creation can only be
91 // customized for files opened read+write
92 if (access & FILEACCESS_CREATE)
93 mode = "wb+"; // read+write, create if needed
94 else
95 mode = "rb+"; // read+write, but don't create
96 } else {
97 mode = "wb"; // write only, create if needed
98 }
99 } else { // neither write nor append, so default to read only
100 mode = "rb"; // read only, don't create
101 }
102
103 hFile = fopen(fullName.c_str(), mode);
104 bool success = hFile != 0;
105#endif
106
107#if HOST_IS_CASE_SENSITIVE
108 if (!success &&
109 !(access & FILEACCESS_APPEND) &&
110 !(access & FILEACCESS_CREATE) &&
111 !(access & FILEACCESS_WRITE))
112 {
113 if ( ! FixPathCase(basePath,fileName, FPC_PATH_MUST_EXIST) )
114 return 0; // or go on and attempt (for a better error code than just 0?)
115 fullName = GetLocalPath(basePath,fileName);
116 const char* fullNameC = fullName.c_str();
117
118 DEBUG_LOG(FILESYS, "Case may have been incorrect, second try opening %s (%s)", fullNameC, fileName.c_str());
119
120 // And try again with the correct case this time
121#ifdef _WIN32
122 hFile = CreateFile(fullNameC, desired, sharemode, 0, openmode, 0, 0);
123 success = hFile != INVALID_HANDLE_VALUE;
124#else
125 hFile = fopen(fullNameC, mode);
126 success = hFile != 0;
127#endif
128 }
129#endif
130
131 return success;
132}
133
134size_t DirectoryFileHandle::Read(u8* pointer, s64 size)
135{
136 size_t bytesRead = 0;
137#ifdef _WIN32
138 ::ReadFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesRead, 0);
139#else
140 bytesRead = fread(pointer, 1, size, hFile);
141#endif
142 return bytesRead;
143}
144
145size_t DirectoryFileHandle::Write(const u8* pointer, s64 size)
146{
147 size_t bytesWritten = 0;
148#ifdef _WIN32
149 ::WriteFile(hFile, (LPVOID)pointer, (DWORD)size, (LPDWORD)&bytesWritten, 0);
150#else
151 bytesWritten = fwrite(pointer, 1, size, hFile);
152#endif
153 return bytesWritten;
154}
155
156size_t DirectoryFileHandle::Seek(s32 position, FileMove type)
157{
158#ifdef _WIN32
159 DWORD moveMethod = 0;
160 switch (type) {
161 case FILEMOVE_BEGIN: moveMethod = FILE_BEGIN; break;
162 case FILEMOVE_CURRENT: moveMethod = FILE_CURRENT; break;
163 case FILEMOVE_END: moveMethod = FILE_END; break;
164 }
165 DWORD newPos = SetFilePointer(hFile, (LONG)position, 0, moveMethod);
166 return newPos;
167#else
168 int moveMethod = 0;
169 switch (type) {
170 case FILEMOVE_BEGIN: moveMethod = SEEK_SET; break;
171 case FILEMOVE_CURRENT: moveMethod = SEEK_CUR; break;
172 case FILEMOVE_END: moveMethod = SEEK_END; break;
173 }
174 fseek(hFile, position, moveMethod);
175 return ftell(hFile);
176#endif
177}
178
179void DirectoryFileHandle::Close()
180{
181#ifdef _WIN32
182 if (hFile != (HANDLE)-1)
183 CloseHandle(hFile);
184#else
185 if (hFile != 0)
186 fclose(hFile);
187#endif
188}
189
190DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) {
191 File::CreateFullPath(basePath);
192 hAlloc = _hAlloc;
193}
194
195DirectoryFileSystem::~DirectoryFileSystem() {
196 for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
197 iter->second.hFile.Close();
198 }
199}
200
201std::string DirectoryFileSystem::GetLocalPath(std::string localpath) {
202 if (localpath.empty())
203 return basePath;
204
205 if (localpath[0] == '/')
206 localpath.erase(0,1);
207 //Convert slashes
208#ifdef _WIN32
209 for (size_t i = 0; i < localpath.size(); i++) {
210 if (localpath[i] == '/')
211 localpath[i] = '\\';
212 }
213#endif
214 return basePath + localpath;
215}
216
217bool DirectoryFileSystem::MkDir(const std::string &dirname) {
218#if HOST_IS_CASE_SENSITIVE
219 // Must fix case BEFORE attempting, because MkDir would create
220 // duplicate (different case) directories
221
222 std::string fixedCase = dirname;
223 if ( ! FixPathCase(basePath,fixedCase, FPC_PARTIAL_ALLOWED) )
224 return false;
225
226 return File::CreateFullPath(GetLocalPath(fixedCase));
227#else
228 return File::CreateFullPath(GetLocalPath(dirname));
229#endif
230}
231
232bool DirectoryFileSystem::RmDir(const std::string &dirname) {
233 std::string fullName = GetLocalPath(dirname);
234
235#if HOST_IS_CASE_SENSITIVE
236 // Maybe we're lucky?
237 if (File::DeleteDirRecursively(fullName))
238 return true;
239
240 // Nope, fix case and try again
241 fullName = dirname;
242 if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) )
243 return false; // or go on and attempt (for a better error code than just false?)
244
245 fullName = GetLocalPath(fullName);
246#endif
247
248/*#ifdef _WIN32
249 return RemoveDirectory(fullName.c_str()) == TRUE;
250#else
251 return 0 == rmdir(fullName.c_str());
252#endif*/
253 return File::DeleteDirRecursively(fullName);
254}
255
256int DirectoryFileSystem::RenameFile(const std::string &from, const std::string &to) {
257 std::string fullTo = to;
258
259 // Rename ignores the path (even if specified) on to.
260 size_t chop_at = to.find_last_of('/');
261 if (chop_at != to.npos)
262 fullTo = to.substr(chop_at + 1);
263
264 // Now put it in the same directory as from.
265 size_t dirname_end = from.find_last_of('/');
266 if (dirname_end != from.npos)
267 fullTo = from.substr(0, dirname_end + 1) + fullTo;
268
269 // At this point, we should check if the paths match and give an already exists error.
270 if (from == fullTo)
271 return SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS;
272
273 std::string fullFrom = GetLocalPath(from);
274
275#if HOST_IS_CASE_SENSITIVE
276 // In case TO should overwrite a file with different case
277 if ( ! FixPathCase(basePath,fullTo, FPC_PATH_MUST_EXIST) )
278 return -1; // or go on and attempt (for a better error code than just false?)
279#endif
280
281 fullTo = GetLocalPath(fullTo);
282 const char * fullToC = fullTo.c_str();
283
284#ifdef _WIN32
285 bool retValue = (MoveFile(fullFrom.c_str(), fullToC) == TRUE);
286#else
287 bool retValue = (0 == rename(fullFrom.c_str(), fullToC));
288#endif
289
290#if HOST_IS_CASE_SENSITIVE
291 if (! retValue)
292 {
293 // May have failed due to case sensitivity on FROM, so try again
294 fullFrom = from;
295 if ( ! FixPathCase(basePath,fullFrom, FPC_FILE_MUST_EXIST) )
296 return -1; // or go on and attempt (for a better error code than just false?)
297 fullFrom = GetLocalPath(fullFrom);
298
299#ifdef _WIN32
300 retValue = (MoveFile(fullFrom.c_str(), fullToC) == TRUE);
301#else
302 retValue = (0 == rename(fullFrom.c_str(), fullToC));
303#endif
304 }
305#endif
306
307 // TODO: Better error codes.
308 return retValue ? 0 : SCE_KERNEL_ERROR_ERRNO_FILE_ALREADY_EXISTS;
309}
310
311bool DirectoryFileSystem::RemoveFile(const std::string &filename) {
312 std::string fullName = GetLocalPath(filename);
313#ifdef _WIN32
314 bool retValue = (::DeleteFileA(fullName.c_str()) == TRUE);
315#else
316 bool retValue = (0 == unlink(fullName.c_str()));
317#endif
318
319#if HOST_IS_CASE_SENSITIVE
320 if (! retValue)
321 {
322 // May have failed due to case sensitivity, so try again
323 fullName = filename;
324 if ( ! FixPathCase(basePath,fullName, FPC_FILE_MUST_EXIST) )
325 return false; // or go on and attempt (for a better error code than just false?)
326 fullName = GetLocalPath(fullName);
327
328#ifdef _WIN32
329 retValue = (::DeleteFileA(fullName.c_str()) == TRUE);
330#else
331 retValue = (0 == unlink(fullName.c_str()));
332#endif
333 }
334#endif
335
336 return retValue;
337}
338
339u32 DirectoryFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) {
340 OpenFileEntry entry;
341 bool success = entry.hFile.Open(basePath,filename,access);
342
343 if (!success) {
344#ifdef _WIN32
345 ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, %i - access = %i", GetLastError(), (int)access);
346#else
347 ERROR_LOG(FILESYS, "DirectoryFileSystem::OpenFile: FAILED, access = %i", (int)access);
348#endif
349 //wwwwaaaaahh!!
350 return 0;
351 } else {
352#ifdef _WIN32
353 if (access & FILEACCESS_APPEND)
354 entry.hFile.Seek(0,FILEMOVE_END);
355#endif
356
357 u32 newHandle = hAlloc->GetNewHandle();
358 entries[newHandle] = entry;
359
360 return newHandle;
361 }
362}
363
364void DirectoryFileSystem::CloseFile(u32 handle) {
365 EntryMap::iterator iter = entries.find(handle);
366 if (iter != entries.end()) {
367 hAlloc->FreeHandle(handle);
368 iter->second.hFile.Close();
369 entries.erase(iter);
370 } else {
371 //This shouldn't happen...
372 ERROR_LOG(FILESYS,"Cannot close file that hasn't been opened: %08x", handle);
373 }
374}
375
376bool DirectoryFileSystem::OwnsHandle(u32 handle) {
377 EntryMap::iterator iter = entries.find(handle);
378 return (iter != entries.end());
379}
380
381size_t DirectoryFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) {
382 EntryMap::iterator iter = entries.find(handle);
383 if (iter != entries.end())
384 {
385 size_t bytesRead = iter->second.hFile.Read(pointer,size);
386 return bytesRead;
387 } else {
388 //This shouldn't happen...
389 ERROR_LOG(FILESYS,"Cannot read file that hasn't been opened: %08x", handle);
390 return 0;
391 }
392}
393
394size_t DirectoryFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) {
395 EntryMap::iterator iter = entries.find(handle);
396 if (iter != entries.end())
397 {
398 size_t bytesWritten = iter->second.hFile.Write(pointer,size);
399 return bytesWritten;
400 } else {
401 //This shouldn't happen...
402 ERROR_LOG(FILESYS,"Cannot write to file that hasn't been opened: %08x", handle);
403 return 0;
404 }
405}
406
407size_t DirectoryFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {
408 EntryMap::iterator iter = entries.find(handle);
409 if (iter != entries.end()) {
410 return iter->second.hFile.Seek(position,type);
411 } else {
412 //This shouldn't happen...
413 ERROR_LOG(FILESYS,"Cannot seek in file that hasn't been opened: %08x", handle);
414 return 0;
415 }
416}
417
418PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) {
419 PSPFileInfo x;
420 x.name = filename;
421
422 std::string fullName = GetLocalPath(filename);
423 if (! File::Exists(fullName)) {
424#if HOST_IS_CASE_SENSITIVE
425 if (! FixPathCase(basePath,filename, FPC_FILE_MUST_EXIST))
426 return x;
427 fullName = GetLocalPath(filename);
428
429 if (! File::Exists(fullName))
430 return x;
431#else
432 return x;
433#endif
434 }
435 x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
436 x.exists = true;
437
438 if (x.type != FILETYPE_DIRECTORY)
439 {
440#ifdef _WIN32
441 struct _stat64i32 s;
442 _wstat64i32(fullName.c_str(), &s);
443#else
444 struct stat s;
445 stat(fullName.c_str(), &s);
446#endif
447
448 x.size = File::GetSize(fullName);
449 x.access = s.st_mode & 0x1FF;
450 localtime_r((time_t*)&s.st_atime,&x.atime);
451 localtime_r((time_t*)&s.st_ctime,&x.ctime);
452 localtime_r((time_t*)&s.st_mtime,&x.mtime);
453 }
454
455 return x;
456}
457
458bool DirectoryFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) {
459 outpath = GetLocalPath(inpath);
460 return true;
461}
462
463#ifdef _WIN32
464#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL
465
466static void tmFromFiletime(tm &dest, FILETIME &src)
467{
468 u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL;
469 u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US;
470
471 time_t t = (time_t) (from_1970_us / 1000000UL);
472 localtime_r(&t, &dest);
473}
474#endif
475
476std::vector<PSPFileInfo> DirectoryFileSystem::GetDirListing(std::string path) {
477 std::vector<PSPFileInfo> myVector;
478#ifdef _WIN32
479 WIN32_FIND_DATA findData;
480 HANDLE hFind;
481
482 std::string w32path = GetLocalPath(path) + "\\*.*";
483
484 hFind = FindFirstFile(ConvertUTF8ToWString(w32path).c_str(), &findData);
485
486 if (hFind == INVALID_HANDLE_VALUE) {
487 return myVector; //the empty list
488 }
489
490 while (true) {
491 PSPFileInfo entry;
492 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
493 entry.type = FILETYPE_DIRECTORY;
494 else
495 entry.type = FILETYPE_NORMAL;
496
497 // TODO: Make this more correct?
498 entry.access = entry.type == FILETYPE_NORMAL ? 0666 : 0777;
499 // TODO: is this just for .. or all subdirectories? Need to add a directory to the test
500 // to find out. Also why so different than the old test results?
501 if (!wcscmp(findData.cFileName, L"..") )
502 entry.size = 4096;
503 else
504 entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32);
505 entry.name = ConvertWStringToUTF8(findData.cFileName);
506 tmFromFiletime(entry.atime, findData.ftLastAccessTime);
507 tmFromFiletime(entry.ctime, findData.ftCreationTime);
508 tmFromFiletime(entry.mtime, findData.ftLastWriteTime);
509 myVector.push_back(entry);
510
511 int retval = FindNextFile(hFind, &findData);
512 if (!retval)
513 break;
514 }
515#else
516 dirent *dirp;
517 std::string localPath = GetLocalPath(path);
518 DIR *dp = opendir(localPath.c_str());
519
520#if HOST_IS_CASE_SENSITIVE
521 if(dp == NULL && FixPathCase(basePath,path, FPC_FILE_MUST_EXIST)) {
522 // May have failed due to case sensitivity, try again
523 localPath = GetLocalPath(path);
524 dp = opendir(localPath.c_str());
525 }
526#endif
527
528 if (dp == NULL) {
529 ERROR_LOG(FILESYS,"Error opening directory %s\n",path.c_str());
530 return myVector;
531 }
532
533 while ((dirp = readdir(dp)) != NULL) {
534 PSPFileInfo entry;
535 struct stat s;
536 std::string fullName = GetLocalPath(path) + "/"+dirp->d_name;
537 stat(fullName.c_str(), &s);
538 if (S_ISDIR(s.st_mode))
539 entry.type = FILETYPE_DIRECTORY;
540 else
541 entry.type = FILETYPE_NORMAL;
542 entry.access = s.st_mode & 0x1FF;
543 entry.name = dirp->d_name;
544 entry.size = s.st_size;
545 localtime_r((time_t*)&s.st_atime,&entry.atime);
546 localtime_r((time_t*)&s.st_ctime,&entry.ctime);
547 localtime_r((time_t*)&s.st_mtime,&entry.mtime);
548 myVector.push_back(entry);
549 }
550 closedir(dp);
551#endif
552 return myVector;
553}
554
555void DirectoryFileSystem::DoState(PointerWrap &p) {
556 if (!entries.empty()) {
557 p.SetError(p.ERROR_WARNING);
558 ERROR_LOG(FILESYS, "FIXME: Open files during savestate, could go badly.");
559 }
560}
561
562
563
564VFSFileSystem::VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath) : basePath(_basePath) {
565 INFO_LOG(FILESYS, "Creating VFS file system");
566 hAlloc = _hAlloc;
567}
568
569VFSFileSystem::~VFSFileSystem() {
570 for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
571 delete [] iter->second.fileData;
572 }
573 entries.clear();
574}
575
576std::string VFSFileSystem::GetLocalPath(std::string localPath) {
577 return basePath + localPath;
578}
579
580bool VFSFileSystem::MkDir(const std::string &dirname) {
581 // NOT SUPPORTED - READ ONLY
582 return false;
583}
584
585bool VFSFileSystem::RmDir(const std::string &dirname) {
586 // NOT SUPPORTED - READ ONLY
587 return false;
588}
589
590int VFSFileSystem::RenameFile(const std::string &from, const std::string &to) {
591 // NOT SUPPORTED - READ ONLY
592 return -1;
593}
594
595bool VFSFileSystem::RemoveFile(const std::string &filename) {
596 // NOT SUPPORTED - READ ONLY
597 return false;
598}
599
600u32 VFSFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) {
601 if (access != FILEACCESS_READ) {
602 ERROR_LOG(FILESYS, "VFSFileSystem only supports plain reading");
603 return 0;
604 }
605
606 std::string fullName = GetLocalPath(filename);
607 const char *fullNameC = fullName.c_str();
608 INFO_LOG(FILESYS,"VFSFileSystem actually opening %s (%s)", fullNameC, filename.c_str());
609
610 size_t size;
611 u8 *data = VFSReadFile(fullNameC, &size);
612 if (!data) {
613 ERROR_LOG(FILESYS, "VFSFileSystem failed to open %s", filename.c_str());
614 return 0;
615 }
616
617 OpenFileEntry entry;
618 entry.fileData = data;
619 entry.size = size;
620 entry.seekPos = 0;
621 u32 newHandle = hAlloc->GetNewHandle();
622 entries[newHandle] = entry;
623 return newHandle;
624}
625
626PSPFileInfo VFSFileSystem::GetFileInfo(std::string filename) {
627 PSPFileInfo x;
628 x.name = filename;
629
630 std::string fullName = GetLocalPath(filename);
631 INFO_LOG(FILESYS,"Getting VFS file info %s (%s)", fullName.c_str(), filename.c_str());
632 FileInfo fo;
633 VFSGetFileInfo(fullName.c_str(), &fo);
634 x.exists = fo.exists;
635 if (x.exists) {
636 x.size = fo.size;
637 x.type = fo.isDirectory ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
638 }
639 INFO_LOG(FILESYS,"Got VFS file info: size = %i", (int)x.size);
640 return x;
641}
642
643void VFSFileSystem::CloseFile(u32 handle) {
644 EntryMap::iterator iter = entries.find(handle);
645 if (iter != entries.end()) {
646 delete [] iter->second.fileData;
647 entries.erase(iter);
648 } else {
649 //This shouldn't happen...
650 ERROR_LOG(FILESYS,"Cannot close file that hasn't been opened: %08x", handle);
651 }
652}
653
654bool VFSFileSystem::OwnsHandle(u32 handle) {
655 EntryMap::iterator iter = entries.find(handle);
656 return (iter != entries.end());
657}
658
659size_t VFSFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) {
660 INFO_LOG(FILESYS,"VFSFileSystem::ReadFile %08x %p %i", handle, pointer, (u32)size);
661 EntryMap::iterator iter = entries.find(handle);
662 if (iter != entries.end())
663 {
664 size_t bytesRead = size;
665 memcpy(pointer, iter->second.fileData + iter->second.seekPos, size);
666 iter->second.seekPos += size;
667 return bytesRead;
668 } else {
669 ERROR_LOG(FILESYS,"Cannot read file that hasn't been opened: %08x", handle);
670 return 0;
671 }
672}
673
674size_t VFSFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) {
675 // NOT SUPPORTED - READ ONLY
676 return 0;
677}
678
679size_t VFSFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {
680 EntryMap::iterator iter = entries.find(handle);
681 if (iter != entries.end()) {
682 switch (type) {
683 case FILEMOVE_BEGIN: iter->second.seekPos = position; break;
684 case FILEMOVE_CURRENT: iter->second.seekPos += position; break;
685 case FILEMOVE_END: iter->second.seekPos = iter->second.size + position; break;
686 }
687 return iter->second.seekPos;
688 } else {
689 //This shouldn't happen...
690 ERROR_LOG(FILESYS,"Cannot seek in file that hasn't been opened: %08x", handle);
691 return 0;
692 }
693}
694
695
696bool VFSFileSystem::GetHostPath(const std::string &inpath, std::string &outpath) {
697 // NOT SUPPORTED
698 return false;
699}
700
701std::vector<PSPFileInfo> VFSFileSystem::GetDirListing(std::string path) {
702 std::vector<PSPFileInfo> myVector;
703 // TODO
704 return myVector;
705}
706
707void VFSFileSystem::DoState(PointerWrap &p) {
708 if (!entries.empty()) {
709 p.SetError(p.ERROR_WARNING);
710 ERROR_LOG(FILESYS, "FIXME: Open files during savestate, could go badly.");
711 }
712}
diff --git a/src/core/src/file_sys/file_sys_directory.h b/src/core/src/file_sys/file_sys_directory.h
new file mode 100644
index 000000000..5591cd7d9
--- /dev/null
+++ b/src/core/src/file_sys/file_sys_directory.h
@@ -0,0 +1,158 @@
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#ifndef CORE_FILE_SYS_DIRECTORY_H_
19#define CORE_FILE_SYS_DIRECTORY_H_
20
21// TODO: Remove the Windows-specific code, FILE is fine there too.
22
23#include <map>
24
25#include "file_sys.h"
26
27#ifdef _WIN32
28typedef void * HANDLE;
29#endif
30
31#if defined(__APPLE__)
32
33#if TARGET_OS_IPHONE
34#define HOST_IS_CASE_SENSITIVE 1
35#elif TARGET_IPHONE_SIMULATOR
36#define HOST_IS_CASE_SENSITIVE 0
37#else
38// Mac OSX case sensitivity defaults off, but is user configurable (when
39// creating a filesytem), so assume the worst:
40#define HOST_IS_CASE_SENSITIVE 1
41#endif
42
43#elif defined(_WIN32) || defined(__SYMBIAN32__)
44#define HOST_IS_CASE_SENSITIVE 0
45
46#else // Android, Linux, BSD (and the rest?)
47#define HOST_IS_CASE_SENSITIVE 1
48
49#endif
50
51#if HOST_IS_CASE_SENSITIVE
52enum FixPathCaseBehavior {
53 FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from)
54 FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to)
55 FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive)
56};
57
58bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior);
59#endif
60
61struct DirectoryFileHandle
62{
63#ifdef _WIN32
64 HANDLE hFile;
65#else
66 FILE* hFile;
67#endif
68 DirectoryFileHandle()
69 {
70#ifdef _WIN32
71 hFile = (HANDLE)-1;
72#else
73 hFile = 0;
74#endif
75 }
76
77 std::string GetLocalPath(std::string& basePath, std::string localpath);
78 bool Open(std::string& basePath, std::string& fileName, FileAccess access);
79 size_t Read(u8* pointer, s64 size);
80 size_t Write(const u8* pointer, s64 size);
81 size_t Seek(s32 position, FileMove type);
82 void Close();
83};
84
85class DirectoryFileSystem : public IFileSystem {
86public:
87 DirectoryFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
88 ~DirectoryFileSystem();
89
90 void DoState(PointerWrap &p);
91 std::vector<PSPFileInfo> GetDirListing(std::string path);
92 u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL);
93 void CloseFile(u32 handle);
94 size_t ReadFile(u32 handle, u8 *pointer, s64 size);
95 size_t WriteFile(u32 handle, const u8 *pointer, s64 size);
96 size_t SeekFile(u32 handle, s32 position, FileMove type);
97 PSPFileInfo GetFileInfo(std::string filename);
98 bool OwnsHandle(u32 handle);
99
100 bool MkDir(const std::string &dirname);
101 bool RmDir(const std::string &dirname);
102 int RenameFile(const std::string &from, const std::string &to);
103 bool RemoveFile(const std::string &filename);
104 bool GetHostPath(const std::string &inpath, std::string &outpath);
105
106private:
107 struct OpenFileEntry {
108 DirectoryFileHandle hFile;
109 };
110
111 typedef std::map<u32, OpenFileEntry> EntryMap;
112 EntryMap entries;
113 std::string basePath;
114 IHandleAllocator *hAlloc;
115
116 // In case of Windows: Translate slashes, etc.
117 std::string GetLocalPath(std::string localpath);
118};
119
120// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts.
121// Very inefficient - always load the whole file on open.
122class VFSFileSystem : public IFileSystem {
123public:
124 VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
125 ~VFSFileSystem();
126
127 void DoState(PointerWrap &p);
128 std::vector<PSPFileInfo> GetDirListing(std::string path);
129 u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL);
130 void CloseFile(u32 handle);
131 size_t ReadFile(u32 handle, u8 *pointer, s64 size);
132 size_t WriteFile(u32 handle, const u8 *pointer, s64 size);
133 size_t SeekFile(u32 handle, s32 position, FileMove type);
134 PSPFileInfo GetFileInfo(std::string filename);
135 bool OwnsHandle(u32 handle);
136
137 bool MkDir(const std::string &dirname);
138 bool RmDir(const std::string &dirname);
139 int RenameFile(const std::string &from, const std::string &to);
140 bool RemoveFile(const std::string &filename);
141 bool GetHostPath(const std::string &inpath, std::string &outpath);
142
143private:
144 struct OpenFileEntry {
145 u8 *fileData;
146 size_t size;
147 size_t seekPos;
148 };
149
150 typedef std::map<u32, OpenFileEntry> EntryMap;
151 EntryMap entries;
152 std::string basePath;
153 IHandleAllocator *hAlloc;
154
155 std::string GetLocalPath(std::string localpath);
156};
157
158#endif // CORE_FILE_SYS_DIRECTORY_H_