Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/VirtualDiscFileSystem.cpp
3186 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
#include "ppsspp_config.h"
19
#include <ctime>
20
21
#include "Common/File/FileUtil.h"
22
#include "Common/File/DirListing.h"
23
#include "Common/Serialize/Serializer.h"
24
#include "Common/Serialize/SerializeFuncs.h"
25
#include "Common/SysError.h"
26
#include "Core/FileSystems/VirtualDiscFileSystem.h"
27
#include "Core/FileSystems/ISOFileSystem.h"
28
#include "Core/HLE/sceKernel.h"
29
#include "Core/Reporting.h"
30
#include "Common/Data/Encoding/Utf8.h"
31
32
#ifdef _WIN32
33
#include "Common/CommonWindows.h"
34
#include <sys/stat.h>
35
#if PPSSPP_PLATFORM(UWP)
36
#include <fileapifromapp.h>
37
#endif
38
#else
39
#include <dirent.h>
40
#include <unistd.h>
41
#include <sys/stat.h>
42
#include <ctype.h>
43
#if !PPSSPP_PLATFORM(SWITCH)
44
#include <dlfcn.h>
45
#endif
46
#endif
47
48
const std::string INDEX_FILENAME = ".ppsspp-index.lst";
49
50
VirtualDiscFileSystem::VirtualDiscFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath)
51
: basePath(_basePath), currentBlockIndex(0) {
52
hAlloc = _hAlloc;
53
LoadFileListIndex();
54
}
55
56
VirtualDiscFileSystem::~VirtualDiscFileSystem() {
57
for (auto iter = entries.begin(), end = entries.end(); iter != end; ++iter) {
58
if (iter->second.type != VFILETYPE_ISO) {
59
iter->second.Close();
60
}
61
}
62
for (auto iter = handlers.begin(), end = handlers.end(); iter != end; ++iter) {
63
delete iter->second;
64
}
65
}
66
67
void VirtualDiscFileSystem::LoadFileListIndex() {
68
const Path filename = basePath / INDEX_FILENAME;
69
if (!File::Exists(filename)) {
70
return;
71
}
72
73
FILE *f = File::OpenCFile(filename, "r");
74
if (!f) {
75
return;
76
}
77
78
static const int MAX_LINE_SIZE = 2048;
79
char linebuf[MAX_LINE_SIZE]{};
80
while (fgets(linebuf, MAX_LINE_SIZE, f)) {
81
std::string line = linebuf;
82
// Strip newline from fgets.
83
if (!line.empty() && line.back() == '\n')
84
line.resize(line.size() - 1);
85
86
// Ignore any UTF-8 BOM.
87
if (line.substr(0, 3) == "\xEF\xBB\xBF") {
88
line = line.substr(3);
89
}
90
91
if (line.empty() || line[0] == ';') {
92
continue;
93
}
94
95
FileListEntry entry = {""};
96
97
// Syntax: HEXPOS filename or HEXPOS filename:handler
98
size_t filename_pos = line.find(' ');
99
if (filename_pos == line.npos) {
100
ERROR_LOG(Log::FileSystem, "Unexpected line in %s: %s", INDEX_FILENAME.c_str(), line.c_str());
101
continue;
102
}
103
104
filename_pos++;
105
// Strip any slash prefix.
106
while (filename_pos < line.length() && line[filename_pos] == '/') {
107
filename_pos++;
108
}
109
110
// Check if there's a handler specified.
111
size_t handler_pos = line.find(':', filename_pos);
112
if (handler_pos != line.npos) {
113
entry.fileName = line.substr(filename_pos, handler_pos - filename_pos);
114
115
std::string handler = line.substr(handler_pos + 1);
116
size_t trunc = handler.find_last_not_of("\r\n");
117
if (trunc != handler.npos && trunc != handler.size())
118
handler.resize(trunc + 1);
119
120
if (handlers.find(handler) == handlers.end())
121
handlers[handler] = new Handler(handler.c_str(), this);
122
if (handlers[handler]->IsValid())
123
entry.handler = handlers[handler];
124
} else {
125
entry.fileName = line.substr(filename_pos);
126
}
127
size_t trunc = entry.fileName.find_last_not_of("\r\n");
128
if (trunc != entry.fileName.npos && trunc != entry.fileName.size())
129
entry.fileName.resize(trunc + 1);
130
131
entry.firstBlock = (u32)strtol(line.c_str(), NULL, 16);
132
if (entry.handler != NULL && entry.handler->IsValid()) {
133
HandlerFileHandle temp = entry.handler;
134
if (temp.Open(basePath.ToString(), entry.fileName, FILEACCESS_READ)) {
135
entry.totalSize = (u32)temp.Seek(0, FILEMOVE_END);
136
temp.Close();
137
} else {
138
ERROR_LOG(Log::FileSystem, "Unable to open virtual file: %s", entry.fileName.c_str());
139
}
140
} else {
141
entry.totalSize = File::GetFileSize(GetLocalPath(entry.fileName));
142
}
143
144
// Try to keep currentBlockIndex sane, in case there are other files.
145
u32 nextBlock = entry.firstBlock + (entry.totalSize + 2047) / 2048;
146
if (nextBlock > currentBlockIndex) {
147
currentBlockIndex = nextBlock;
148
}
149
150
fileList.push_back(entry);
151
}
152
153
fclose(f);
154
}
155
156
void VirtualDiscFileSystem::DoState(PointerWrap &p)
157
{
158
auto s = p.Section("VirtualDiscFileSystem", 1, 2);
159
if (!s)
160
return;
161
162
int fileListSize = (int)fileList.size();
163
int entryCount = (int)entries.size();
164
165
Do(p, fileListSize);
166
Do(p, entryCount);
167
Do(p, currentBlockIndex);
168
169
FileListEntry dummy = {""};
170
fileList.resize(fileListSize, dummy);
171
172
for (int i = 0; i < fileListSize; i++)
173
{
174
Do(p, fileList[i].fileName);
175
Do(p, fileList[i].firstBlock);
176
Do(p, fileList[i].totalSize);
177
}
178
179
if (p.mode == p.MODE_READ)
180
{
181
entries.clear();
182
183
for (int i = 0; i < entryCount; i++)
184
{
185
u32 fd = 0;
186
OpenFileEntry of(Flags());
187
188
Do(p, fd);
189
Do(p, of.fileIndex);
190
Do(p, of.type);
191
Do(p, of.curOffset);
192
Do(p, of.startOffset);
193
Do(p, of.size);
194
195
// open file
196
if (of.type != VFILETYPE_ISO) {
197
if (fileList[of.fileIndex].handler != NULL) {
198
of.handler = fileList[of.fileIndex].handler;
199
}
200
201
bool success = of.Open(basePath, fileList[of.fileIndex].fileName, FILEACCESS_READ);
202
if (!success) {
203
ERROR_LOG(Log::FileSystem, "Failed to create file handle for %s.", fileList[of.fileIndex].fileName.c_str());
204
} else {
205
if (of.type == VFILETYPE_LBN) {
206
of.Seek(of.curOffset + of.startOffset, FILEMOVE_BEGIN);
207
} else {
208
of.Seek(of.curOffset, FILEMOVE_BEGIN);
209
}
210
}
211
}
212
213
// TODO: I think we only need to write to the map on load?
214
entries[fd] = of;
215
}
216
} else {
217
for (EntryMap::iterator it = entries.begin(), end = entries.end(); it != end; ++it)
218
{
219
OpenFileEntry &of = it->second;
220
221
Do(p, it->first);
222
Do(p, of.fileIndex);
223
Do(p, of.type);
224
Do(p, of.curOffset);
225
Do(p, of.startOffset);
226
Do(p, of.size);
227
}
228
}
229
230
if (s >= 2) {
231
Do(p, lastReadBlock_);
232
} else {
233
lastReadBlock_ = 0;
234
}
235
236
// We don't savestate handlers (loaded on fs load), but if they change, it may not load properly.
237
}
238
239
Path VirtualDiscFileSystem::GetLocalPath(std::string localpath) const {
240
if (localpath.empty())
241
return basePath;
242
243
if (localpath[0] == '/')
244
localpath.erase(0,1);
245
//Convert slashes
246
#ifdef _WIN32
247
for (size_t i = 0; i < localpath.size(); i++) {
248
if (localpath[i] == '/')
249
localpath[i] = '\\';
250
}
251
#endif
252
return basePath / localpath;
253
}
254
255
int VirtualDiscFileSystem::getFileListIndex(std::string &fileName)
256
{
257
std::string normalized;
258
if (fileName.length() >= 1 && fileName[0] == '/') {
259
normalized = fileName.substr(1);
260
} else {
261
normalized = fileName;
262
}
263
264
for (size_t i = 0; i < fileList.size(); i++)
265
{
266
if (fileList[i].fileName == normalized)
267
return (int)i;
268
}
269
270
// unknown file - add it
271
Path fullName = GetLocalPath(fileName);
272
if (! File::Exists(fullName)) {
273
#if HOST_IS_CASE_SENSITIVE
274
if (! FixPathCase(basePath, fileName, FPC_FILE_MUST_EXIST))
275
return -1;
276
fullName = GetLocalPath(fileName);
277
278
if (! File::Exists(fullName))
279
return -1;
280
#else
281
return -1;
282
#endif
283
}
284
285
if (File::IsDirectory(fullName))
286
return -1;
287
288
FileListEntry entry = {""};
289
entry.fileName = normalized;
290
entry.totalSize = File::GetFileSize(fullName);
291
entry.firstBlock = currentBlockIndex;
292
currentBlockIndex += (entry.totalSize+2047)/2048;
293
294
fileList.push_back(entry);
295
296
return (int)fileList.size()-1;
297
}
298
299
int VirtualDiscFileSystem::getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode) const {
300
for (size_t i = 0; i < fileList.size(); i++) {
301
if (fileList[i].firstBlock <= accessBlock) {
302
u32 sectorOffset = (accessBlock-fileList[i].firstBlock)*2048;
303
u32 totalFileSize = blockMode ? (fileList[i].totalSize+2047) & ~2047 : fileList[i].totalSize;
304
305
u32 endOffset = sectorOffset+accessSize;
306
if (endOffset <= totalFileSize) {
307
return (int)i;
308
}
309
}
310
}
311
312
return -1;
313
}
314
315
int VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename)
316
{
317
OpenFileEntry entry(Flags());
318
entry.curOffset = 0;
319
entry.size = 0;
320
entry.startOffset = 0;
321
322
if (filename.empty())
323
{
324
entry.type = VFILETYPE_ISO;
325
entry.fileIndex = -1;
326
327
u32 newHandle = hAlloc->GetNewHandle();
328
entries[newHandle] = entry;
329
330
return newHandle;
331
}
332
333
if (filename.compare(0, 8, "/sce_lbn") == 0)
334
{
335
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
336
parseLBN(filename, &sectorStart, &readSize);
337
338
entry.type = VFILETYPE_LBN;
339
entry.size = readSize;
340
341
int fileIndex = getFileListIndex(sectorStart,readSize);
342
if (fileIndex == -1)
343
{
344
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem: sce_lbn used without calling fileinfo.");
345
return 0;
346
}
347
entry.fileIndex = (u32)fileIndex;
348
349
entry.startOffset = (sectorStart-fileList[entry.fileIndex].firstBlock)*2048;
350
351
// now we just need an actual file handle
352
if (fileList[entry.fileIndex].handler != NULL) {
353
entry.handler = fileList[entry.fileIndex].handler;
354
}
355
bool success = entry.Open(basePath, fileList[entry.fileIndex].fileName, FILEACCESS_READ);
356
357
if (!success) {
358
if (!(access & FILEACCESS_PPSSPP_QUIET)) {
359
#ifdef _WIN32
360
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i", (int)GetLastError());
361
#else
362
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED");
363
#endif
364
}
365
return 0;
366
}
367
368
// seek to start
369
entry.Seek(entry.startOffset, FILEMOVE_BEGIN);
370
371
u32 newHandle = hAlloc->GetNewHandle();
372
entries[newHandle] = entry;
373
374
return newHandle;
375
}
376
377
entry.type = VFILETYPE_NORMAL;
378
entry.fileIndex = getFileListIndex(filename);
379
380
if (entry.fileIndex != (u32)-1 && fileList[entry.fileIndex].handler != NULL) {
381
entry.handler = fileList[entry.fileIndex].handler;
382
}
383
bool success = entry.Open(basePath, filename, (FileAccess)(access & FILEACCESS_PSP_FLAGS));
384
385
if (!success) {
386
if (!(access & FILEACCESS_PPSSPP_QUIET)) {
387
#ifdef _WIN32
388
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i - access = %i", (int)GetLastError(), (int)access);
389
#else
390
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, access = %i", (int)access);
391
#endif
392
}
393
return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;
394
} else {
395
u32 newHandle = hAlloc->GetNewHandle();
396
entries[newHandle] = entry;
397
398
return newHandle;
399
}
400
}
401
402
size_t VirtualDiscFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {
403
EntryMap::iterator iter = entries.find(handle);
404
if (iter != entries.end()) {
405
auto &entry = iter->second;
406
switch (entry.type)
407
{
408
case VFILETYPE_NORMAL:
409
{
410
return entry.Seek(position, type);
411
}
412
case VFILETYPE_LBN:
413
{
414
switch (type)
415
{
416
case FILEMOVE_BEGIN: entry.curOffset = position; break;
417
case FILEMOVE_CURRENT: entry.curOffset += position; break;
418
case FILEMOVE_END: entry.curOffset = entry.size + position; break;
419
}
420
421
u32 off = entry.startOffset + entry.curOffset;
422
entry.Seek(off, FILEMOVE_BEGIN);
423
return entry.curOffset;
424
}
425
case VFILETYPE_ISO:
426
{
427
switch (type)
428
{
429
case FILEMOVE_BEGIN: entry.curOffset = position; break;
430
case FILEMOVE_CURRENT: entry.curOffset += position; break;
431
case FILEMOVE_END: entry.curOffset = currentBlockIndex + position; break;
432
}
433
434
return entry.curOffset;
435
}
436
}
437
return 0;
438
} else {
439
//This shouldn't happen...
440
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot seek in file that hasn't been opened: %08x", handle);
441
return 0;
442
}
443
}
444
445
size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) {
446
int ignored;
447
return ReadFile(handle, pointer, size, ignored);
448
}
449
450
size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) {
451
EntryMap::iterator iter = entries.find(handle);
452
if (iter != entries.end()) {
453
if (size < 0) {
454
ERROR_LOG_REPORT(Log::FileSystem, "Invalid read for %lld bytes from virtual umd", size);
455
return 0;
456
}
457
458
// it's the whole iso... it could reference any of the files on the disc.
459
// For now let's just open and close the files on demand. Can certainly be done
460
// better though
461
if (iter->second.type == VFILETYPE_ISO)
462
{
463
int fileIndex = getFileListIndex(iter->second.curOffset,size*2048,true);
464
if (fileIndex == -1)
465
{
466
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Reading from unknown address in %08x at %08llx", handle, iter->second.curOffset);
467
return 0;
468
}
469
470
OpenFileEntry temp(Flags());
471
if (fileList[fileIndex].handler != NULL) {
472
temp.handler = fileList[fileIndex].handler;
473
}
474
bool success = temp.Open(basePath, fileList[fileIndex].fileName, FILEACCESS_READ);
475
476
if (!success)
477
{
478
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Error opening file %s", fileList[fileIndex].fileName.c_str());
479
return 0;
480
}
481
482
u32 startOffset = (iter->second.curOffset-fileList[fileIndex].firstBlock)*2048;
483
size_t bytesRead;
484
485
temp.Seek(startOffset, FILEMOVE_BEGIN);
486
487
u32 remainingSize = fileList[fileIndex].totalSize-startOffset;
488
if (remainingSize < size * 2048)
489
{
490
// the file doesn't fill the whole last sector
491
// read what's there and zero fill the rest like on a real disc
492
bytesRead = temp.Read(pointer, remainingSize);
493
memset(&pointer[bytesRead], 0, size * 2048 - bytesRead);
494
} else {
495
bytesRead = temp.Read(pointer, size * 2048);
496
}
497
498
temp.Close();
499
500
iter->second.curOffset += size;
501
// TODO: This probably isn't enough...
502
if (abs((int)lastReadBlock_ - (int)iter->second.curOffset) > 100) {
503
// This is an estimate, sometimes it takes 1+ seconds, but it definitely takes time.
504
usec = 100000;
505
}
506
lastReadBlock_ = iter->second.curOffset;
507
return size;
508
}
509
510
if (iter->second.type == VFILETYPE_LBN && iter->second.curOffset + size > iter->second.size) {
511
// Clamp to the remaining size, but read what we can.
512
const s64 newSize = iter->second.size - iter->second.curOffset;
513
WARN_LOG(Log::FileSystem, "VirtualDiscFileSystem: Reading beyond end of file, clamping size %lld to %lld", size, newSize);
514
size = newSize;
515
}
516
517
size_t bytesRead = iter->second.Read(pointer, size);
518
iter->second.curOffset += bytesRead;
519
return bytesRead;
520
} else {
521
//This shouldn't happen...
522
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot read file that hasn't been opened: %08x", handle);
523
return 0;
524
}
525
}
526
527
void VirtualDiscFileSystem::CloseFile(u32 handle) {
528
EntryMap::iterator iter = entries.find(handle);
529
if (iter != entries.end()) {
530
hAlloc->FreeHandle(handle);
531
iter->second.Close();
532
entries.erase(iter);
533
} else {
534
//This shouldn't happen...
535
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot close file that hasn't been opened: %08x", handle);
536
}
537
}
538
539
bool VirtualDiscFileSystem::OwnsHandle(u32 handle) {
540
EntryMap::iterator iter = entries.find(handle);
541
return (iter != entries.end());
542
}
543
544
int VirtualDiscFileSystem::Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) {
545
// TODO: How to support these?
546
return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;
547
}
548
549
PSPDevType VirtualDiscFileSystem::DevType(u32 handle) {
550
EntryMap::iterator iter = entries.find(handle);
551
if (iter == entries.end())
552
return PSPDevType::FILE;
553
PSPDevType type = iter->second.type == VFILETYPE_ISO ? PSPDevType::BLOCK : PSPDevType::FILE;
554
if (iter->second.type == VFILETYPE_LBN)
555
type |= PSPDevType::EMU_LBN;
556
return type;
557
}
558
559
PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) {
560
PSPFileInfo x;
561
x.name = filename;
562
x.access = FILEACCESS_READ;
563
564
if (filename.compare(0,8,"/sce_lbn") == 0) {
565
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
566
parseLBN(filename, &sectorStart, &readSize);
567
568
PSPFileInfo fileInfo;
569
fileInfo.name = filename;
570
fileInfo.exists = true;
571
fileInfo.type = FILETYPE_NORMAL;
572
fileInfo.size = readSize;
573
fileInfo.access = 0444;
574
fileInfo.startSector = sectorStart;
575
fileInfo.isOnSectorSystem = true;
576
fileInfo.numSectors = (readSize + 2047) / 2048;
577
return fileInfo;
578
}
579
580
int fileIndex = getFileListIndex(filename);
581
if (fileIndex != -1 && fileList[fileIndex].handler != NULL) {
582
x.type = FILETYPE_NORMAL;
583
x.isOnSectorSystem = true;
584
x.startSector = fileList[fileIndex].firstBlock;
585
x.access = 0555;
586
587
HandlerFileHandle temp = fileList[fileIndex].handler;
588
if (temp.Open(basePath.ToString(), filename, FILEACCESS_READ)) {
589
x.exists = true;
590
x.size = temp.Seek(0, FILEMOVE_END);
591
temp.Close();
592
}
593
594
// TODO: Probably should include dates or something...
595
return x;
596
}
597
598
Path fullName = GetLocalPath(filename);
599
if (!File::Exists(fullName)) {
600
#if HOST_IS_CASE_SENSITIVE
601
if (! FixPathCase(basePath, filename, FPC_FILE_MUST_EXIST))
602
return x;
603
fullName = GetLocalPath(filename);
604
605
if (! File::Exists(fullName))
606
return x;
607
#else
608
return x;
609
#endif
610
}
611
612
x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
613
x.exists = true;
614
x.access = 0555;
615
if (fileIndex != -1) {
616
x.isOnSectorSystem = true;
617
x.startSector = fileList[fileIndex].firstBlock;
618
}
619
620
if (x.type != FILETYPE_DIRECTORY) {
621
File::FileInfo details;
622
if (!File::GetFileInfo(fullName, &details)) {
623
ERROR_LOG(Log::FileSystem, "DirectoryFileSystem::GetFileInfo: GetFileInfo failed: %s", fullName.c_str());
624
x.size = 0;
625
x.access = 0;
626
} else {
627
x.size = details.size;
628
time_t atime = details.atime;
629
time_t ctime = details.ctime;
630
time_t mtime = details.mtime;
631
632
localtime_r((time_t*)&atime, &x.atime);
633
localtime_r((time_t*)&ctime, &x.ctime);
634
localtime_r((time_t*)&mtime, &x.mtime);
635
}
636
637
// x.startSector was set above in "if (fileIndex != -1)".
638
x.numSectors = (x.size+2047)/2048;
639
}
640
641
return x;
642
}
643
644
PSPFileInfo VirtualDiscFileSystem::GetFileInfoByHandle(u32 handle) {
645
WARN_LOG(Log::FileSystem, "GetFileInfoByHandle not yet implemented for VirtualDiscFileSystem");
646
return PSPFileInfo();
647
}
648
649
#ifdef _WIN32
650
#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL
651
652
static void tmFromFiletime(tm &dest, const FILETIME &src)
653
{
654
u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL;
655
u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US;
656
657
time_t t = (time_t) (from_1970_us / 1000000UL);
658
localtime_s(&dest, &t);
659
}
660
#endif
661
662
std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(const std::string &path, bool *exists) {
663
std::vector<PSPFileInfo> myVector;
664
665
// TODO(scoped): Switch this over to GetFilesInDir!
666
667
#ifdef _WIN32
668
WIN32_FIND_DATA findData;
669
HANDLE hFind;
670
671
// TODO: Handler files that are virtual might not be listed.
672
673
std::wstring w32path = GetLocalPath(path).ToWString() + L"\\*.*";
674
675
#if PPSSPP_PLATFORM(UWP)
676
hFind = FindFirstFileExFromAppW(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
677
#else
678
hFind = FindFirstFileEx(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
679
#endif
680
if (hFind == INVALID_HANDLE_VALUE) {
681
if (exists)
682
*exists = false;
683
return myVector; //the empty list
684
}
685
686
if (exists)
687
*exists = true;
688
689
for (BOOL retval = 1; retval; retval = FindNextFile(hFind, &findData)) {
690
if (!wcscmp(findData.cFileName, L"..") || !wcscmp(findData.cFileName, L".")) {
691
continue;
692
}
693
694
PSPFileInfo entry;
695
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
696
entry.type = FILETYPE_DIRECTORY;
697
} else {
698
entry.type = FILETYPE_NORMAL;
699
}
700
701
entry.access = 0555;
702
entry.exists = true;
703
entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32);
704
entry.name = ConvertWStringToUTF8(findData.cFileName);
705
tmFromFiletime(entry.atime, findData.ftLastAccessTime);
706
tmFromFiletime(entry.ctime, findData.ftCreationTime);
707
tmFromFiletime(entry.mtime, findData.ftLastWriteTime);
708
entry.isOnSectorSystem = true;
709
710
std::string fullRelativePath = path + "/" + entry.name;
711
int fileIndex = getFileListIndex(fullRelativePath);
712
if (fileIndex != -1)
713
entry.startSector = fileList[fileIndex].firstBlock;
714
myVector.push_back(entry);
715
}
716
FindClose(hFind);
717
#else
718
dirent *dirp;
719
Path localPath = GetLocalPath(path);
720
DIR *dp = opendir(localPath.c_str());
721
722
#if HOST_IS_CASE_SENSITIVE
723
std::string fixedPath = path;
724
if(dp == NULL && FixPathCase(basePath, fixedPath, FPC_FILE_MUST_EXIST)) {
725
// May have failed due to case sensitivity, try again
726
localPath = GetLocalPath(fixedPath);
727
dp = opendir(localPath.c_str());
728
}
729
#endif
730
731
if (dp == NULL) {
732
ERROR_LOG(Log::FileSystem,"Error opening directory %s\n", path.c_str());
733
if (exists)
734
*exists = false;
735
return myVector;
736
}
737
738
if (exists)
739
*exists = true;
740
741
while ((dirp = readdir(dp)) != NULL) {
742
if (!strcmp(dirp->d_name, "..") || !strcmp(dirp->d_name, ".")) {
743
continue;
744
}
745
746
PSPFileInfo entry;
747
struct stat s;
748
std::string fullName = (localPath / std::string(dirp->d_name)).ToString();
749
stat(fullName.c_str(), &s);
750
if (S_ISDIR(s.st_mode))
751
entry.type = FILETYPE_DIRECTORY;
752
else
753
entry.type = FILETYPE_NORMAL;
754
entry.access = 0555;
755
entry.exists = true;
756
entry.name = dirp->d_name;
757
entry.size = s.st_size;
758
localtime_r((time_t*)&s.st_atime,&entry.atime);
759
localtime_r((time_t*)&s.st_ctime,&entry.ctime);
760
localtime_r((time_t*)&s.st_mtime,&entry.mtime);
761
entry.isOnSectorSystem = true;
762
763
std::string fullRelativePath = path + "/" + entry.name;
764
int fileIndex = getFileListIndex(fullRelativePath);
765
if (fileIndex != -1)
766
entry.startSector = fileList[fileIndex].firstBlock;
767
myVector.push_back(entry);
768
}
769
closedir(dp);
770
#endif
771
return myVector;
772
}
773
774
size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)
775
{
776
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");
777
return 0;
778
}
779
780
size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec)
781
{
782
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");
783
return 0;
784
}
785
786
bool VirtualDiscFileSystem::MkDir(const std::string &dirname)
787
{
788
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot create directory on virtual disc");
789
return false;
790
}
791
792
bool VirtualDiscFileSystem::RmDir(const std::string &dirname)
793
{
794
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove directory on virtual disc");
795
return false;
796
}
797
798
int VirtualDiscFileSystem::RenameFile(const std::string &from, const std::string &to)
799
{
800
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot rename file on virtual disc");
801
return -1;
802
}
803
804
bool VirtualDiscFileSystem::RemoveFile(const std::string &filename)
805
{
806
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove file on virtual disc");
807
return false;
808
}
809
810
void VirtualDiscFileSystem::HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg) {
811
VirtualDiscFileSystem *sys = static_cast<VirtualDiscFileSystem *>(arg);
812
813
// TODO: Probably could do this smarter / use a lookup.
814
const char *filename = NULL;
815
for (auto it = sys->entries.begin(), end = sys->entries.end(); it != end; ++it) {
816
if (it->second.fileIndex != (u32)-1 && it->second.handler.handle == handle) {
817
filename = sys->fileList[it->second.fileIndex].fileName.c_str();
818
break;
819
}
820
}
821
822
if (filename != NULL) {
823
GENERIC_LOG(Log::FileSystem, level, "%s: %s", filename, msg);
824
} else {
825
GENERIC_LOG(Log::FileSystem, level, "%s", msg);
826
}
827
}
828
829
VirtualDiscFileSystem::Handler::Handler(const char *filename, VirtualDiscFileSystem *const sys)
830
: sys_(sys) {
831
#if !PPSSPP_PLATFORM(SWITCH)
832
#ifdef _WIN32
833
#if PPSSPP_PLATFORM(UWP)
834
#define dlopen(name, ignore) (void *)LoadPackagedLibrary(ConvertUTF8ToWString(name).c_str(), 0)
835
#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)
836
#define dlclose(mod) FreeLibrary((HMODULE)mod)
837
#else
838
#define dlopen(name, ignore) (void *)LoadLibrary(ConvertUTF8ToWString(name).c_str())
839
#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)
840
#define dlclose(mod) FreeLibrary((HMODULE)mod)
841
#endif
842
#endif
843
844
library = dlopen(filename, RTLD_LOCAL | RTLD_NOW);
845
if (library != NULL) {
846
Init = (InitFunc)dlsym(library, "Init");
847
Shutdown = (ShutdownFunc)dlsym(library, "Shutdown");
848
Open = (OpenFunc)dlsym(library, "Open");
849
Seek = (SeekFunc)dlsym(library, "Seek");
850
Read = (ReadFunc)dlsym(library, "Read");
851
Close = (CloseFunc)dlsym(library, "Close");
852
853
VersionFunc Version = (VersionFunc)dlsym(library, "Version");
854
if (Version && Version() >= 2) {
855
ShutdownV2 = (ShutdownV2Func)Shutdown;
856
}
857
858
if (!Init || !Shutdown || !Open || !Seek || !Read || !Close) {
859
ERROR_LOG(Log::FileSystem, "Unable to find all handler functions: %s", filename);
860
dlclose(library);
861
library = NULL;
862
} else if (!Init(&HandlerLogger, sys)) {
863
ERROR_LOG(Log::FileSystem, "Unable to initialize handler: %s", filename);
864
dlclose(library);
865
library = NULL;
866
}
867
} else {
868
ERROR_LOG(Log::FileSystem, "Unable to load handler '%s': %s", filename, GetLastErrorMsg().c_str());
869
}
870
#ifdef _WIN32
871
#undef dlopen
872
#undef dlsym
873
#undef dlclose
874
#endif
875
#endif
876
}
877
878
VirtualDiscFileSystem::Handler::~Handler() {
879
if (library != NULL) {
880
if (ShutdownV2)
881
ShutdownV2(sys_);
882
else
883
Shutdown();
884
885
#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(SWITCH)
886
#ifdef _WIN32
887
FreeLibrary((HMODULE)library);
888
#else
889
dlclose(library);
890
#endif
891
#endif
892
}
893
}
894
895
896