Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/VirtualDiscFileSystem.cpp
5654 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_view localpath) const {
240
if (localpath.empty())
241
return basePath;
242
243
if (localpath[0] == '/')
244
localpath.remove_prefix(1);
245
return basePath / localpath;
246
}
247
248
int VirtualDiscFileSystem::getFileListIndex(std::string &fileName)
249
{
250
std::string normalized;
251
if (fileName.length() >= 1 && fileName[0] == '/') {
252
normalized = fileName.substr(1);
253
} else {
254
normalized = fileName;
255
}
256
257
for (size_t i = 0; i < fileList.size(); i++)
258
{
259
if (fileList[i].fileName == normalized)
260
return (int)i;
261
}
262
263
// unknown file - add it
264
Path fullName = GetLocalPath(fileName);
265
if (! File::Exists(fullName)) {
266
#if HOST_IS_CASE_SENSITIVE
267
if (! FixPathCase(basePath, fileName, FPC_FILE_MUST_EXIST))
268
return -1;
269
fullName = GetLocalPath(fileName);
270
271
if (! File::Exists(fullName))
272
return -1;
273
#else
274
return -1;
275
#endif
276
}
277
278
if (File::IsDirectory(fullName))
279
return -1;
280
281
FileListEntry entry = {""};
282
entry.fileName = normalized;
283
entry.totalSize = File::GetFileSize(fullName);
284
entry.firstBlock = currentBlockIndex;
285
currentBlockIndex += (entry.totalSize+2047)/2048;
286
287
fileList.push_back(entry);
288
289
return (int)fileList.size()-1;
290
}
291
292
int VirtualDiscFileSystem::getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode) const {
293
for (size_t i = 0; i < fileList.size(); i++) {
294
if (fileList[i].firstBlock <= accessBlock) {
295
u32 sectorOffset = (accessBlock-fileList[i].firstBlock)*2048;
296
u32 totalFileSize = blockMode ? (fileList[i].totalSize+2047) & ~2047 : fileList[i].totalSize;
297
298
u32 endOffset = sectorOffset+accessSize;
299
if (endOffset <= totalFileSize) {
300
return (int)i;
301
}
302
}
303
}
304
305
return -1;
306
}
307
308
int VirtualDiscFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename)
309
{
310
OpenFileEntry entry(Flags());
311
entry.curOffset = 0;
312
entry.size = 0;
313
entry.startOffset = 0;
314
315
if (filename.empty())
316
{
317
entry.type = VFILETYPE_ISO;
318
entry.fileIndex = -1;
319
320
u32 newHandle = hAlloc->GetNewHandle();
321
entries[newHandle] = entry;
322
323
return newHandle;
324
}
325
326
if (filename.compare(0, 8, "/sce_lbn") == 0)
327
{
328
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
329
parseLBN(filename, &sectorStart, &readSize);
330
331
entry.type = VFILETYPE_LBN;
332
entry.size = readSize;
333
334
int fileIndex = getFileListIndex(sectorStart,readSize);
335
if (fileIndex == -1)
336
{
337
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem: sce_lbn used without calling fileinfo.");
338
return 0;
339
}
340
entry.fileIndex = (u32)fileIndex;
341
342
entry.startOffset = (sectorStart-fileList[entry.fileIndex].firstBlock)*2048;
343
344
// now we just need an actual file handle
345
if (fileList[entry.fileIndex].handler != NULL) {
346
entry.handler = fileList[entry.fileIndex].handler;
347
}
348
bool success = entry.Open(basePath, fileList[entry.fileIndex].fileName, FILEACCESS_READ);
349
350
if (!success) {
351
if (!(access & FILEACCESS_PPSSPP_QUIET)) {
352
#ifdef _WIN32
353
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i", (int)GetLastError());
354
#else
355
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED");
356
#endif
357
}
358
return 0;
359
}
360
361
// seek to start
362
entry.Seek(entry.startOffset, FILEMOVE_BEGIN);
363
364
u32 newHandle = hAlloc->GetNewHandle();
365
entries[newHandle] = entry;
366
367
return newHandle;
368
}
369
370
entry.type = VFILETYPE_NORMAL;
371
entry.fileIndex = getFileListIndex(filename);
372
373
if (entry.fileIndex != (u32)-1 && fileList[entry.fileIndex].handler != NULL) {
374
entry.handler = fileList[entry.fileIndex].handler;
375
}
376
bool success = entry.Open(basePath, filename, (FileAccess)(access & FILEACCESS_PSP_FLAGS));
377
378
if (!success) {
379
if (!(access & FILEACCESS_PPSSPP_QUIET)) {
380
#ifdef _WIN32
381
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, %i - access = %i", (int)GetLastError(), (int)access);
382
#else
383
ERROR_LOG(Log::FileSystem, "VirtualDiscFileSystem::OpenFile: FAILED, access = %i", (int)access);
384
#endif
385
}
386
return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;
387
} else {
388
u32 newHandle = hAlloc->GetNewHandle();
389
entries[newHandle] = entry;
390
391
return newHandle;
392
}
393
}
394
395
size_t VirtualDiscFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {
396
EntryMap::iterator iter = entries.find(handle);
397
if (iter != entries.end()) {
398
auto &entry = iter->second;
399
switch (entry.type)
400
{
401
case VFILETYPE_NORMAL:
402
{
403
return entry.Seek(position, type);
404
}
405
case VFILETYPE_LBN:
406
{
407
switch (type)
408
{
409
case FILEMOVE_BEGIN: entry.curOffset = position; break;
410
case FILEMOVE_CURRENT: entry.curOffset += position; break;
411
case FILEMOVE_END: entry.curOffset = entry.size + position; break;
412
}
413
414
u32 off = entry.startOffset + entry.curOffset;
415
entry.Seek(off, FILEMOVE_BEGIN);
416
return entry.curOffset;
417
}
418
case VFILETYPE_ISO:
419
{
420
switch (type)
421
{
422
case FILEMOVE_BEGIN: entry.curOffset = position; break;
423
case FILEMOVE_CURRENT: entry.curOffset += position; break;
424
case FILEMOVE_END: entry.curOffset = currentBlockIndex + position; break;
425
}
426
427
return entry.curOffset;
428
}
429
}
430
return 0;
431
} else {
432
//This shouldn't happen...
433
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot seek in file that hasn't been opened: %08x", handle);
434
return 0;
435
}
436
}
437
438
size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size) {
439
int ignored;
440
return ReadFile(handle, pointer, size, ignored);
441
}
442
443
size_t VirtualDiscFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) {
444
EntryMap::iterator iter = entries.find(handle);
445
if (iter != entries.end()) {
446
if (size < 0) {
447
ERROR_LOG_REPORT(Log::FileSystem, "Invalid read for %lld bytes from virtual umd", size);
448
return 0;
449
}
450
451
// it's the whole iso... it could reference any of the files on the disc.
452
// For now let's just open and close the files on demand. Can certainly be done
453
// better though
454
if (iter->second.type == VFILETYPE_ISO)
455
{
456
int fileIndex = getFileListIndex(iter->second.curOffset,size*2048,true);
457
if (fileIndex == -1)
458
{
459
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Reading from unknown address in %08x at %08llx", handle, iter->second.curOffset);
460
return 0;
461
}
462
463
OpenFileEntry temp(Flags());
464
if (fileList[fileIndex].handler != NULL) {
465
temp.handler = fileList[fileIndex].handler;
466
}
467
bool success = temp.Open(basePath, fileList[fileIndex].fileName, FILEACCESS_READ);
468
469
if (!success)
470
{
471
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Error opening file %s", fileList[fileIndex].fileName.c_str());
472
return 0;
473
}
474
475
u32 startOffset = (iter->second.curOffset-fileList[fileIndex].firstBlock)*2048;
476
size_t bytesRead;
477
478
temp.Seek(startOffset, FILEMOVE_BEGIN);
479
480
u32 remainingSize = fileList[fileIndex].totalSize-startOffset;
481
if (remainingSize < size * 2048)
482
{
483
// the file doesn't fill the whole last sector
484
// read what's there and zero fill the rest like on a real disc
485
bytesRead = temp.Read(pointer, remainingSize);
486
memset(&pointer[bytesRead], 0, size * 2048 - bytesRead);
487
} else {
488
bytesRead = temp.Read(pointer, size * 2048);
489
}
490
491
temp.Close();
492
493
iter->second.curOffset += size;
494
// TODO: This probably isn't enough...
495
if (abs((int)lastReadBlock_ - (int)iter->second.curOffset) > 100) {
496
// This is an estimate, sometimes it takes 1+ seconds, but it definitely takes time.
497
usec = 100000;
498
}
499
lastReadBlock_ = iter->second.curOffset;
500
return size;
501
}
502
503
if (iter->second.type == VFILETYPE_LBN && iter->second.curOffset + size > iter->second.size) {
504
// Clamp to the remaining size, but read what we can.
505
const s64 newSize = iter->second.size - iter->second.curOffset;
506
WARN_LOG(Log::FileSystem, "VirtualDiscFileSystem: Reading beyond end of file, clamping size %lld to %lld", size, newSize);
507
size = newSize;
508
}
509
510
size_t bytesRead = iter->second.Read(pointer, size);
511
iter->second.curOffset += bytesRead;
512
return bytesRead;
513
} else {
514
//This shouldn't happen...
515
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot read file that hasn't been opened: %08x", handle);
516
return 0;
517
}
518
}
519
520
void VirtualDiscFileSystem::CloseFile(u32 handle) {
521
EntryMap::iterator iter = entries.find(handle);
522
if (iter != entries.end()) {
523
hAlloc->FreeHandle(handle);
524
iter->second.Close();
525
entries.erase(iter);
526
} else {
527
//This shouldn't happen...
528
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot close file that hasn't been opened: %08x", handle);
529
}
530
}
531
532
bool VirtualDiscFileSystem::OwnsHandle(u32 handle) {
533
EntryMap::iterator iter = entries.find(handle);
534
return (iter != entries.end());
535
}
536
537
int VirtualDiscFileSystem::Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) {
538
// TODO: How to support these?
539
return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;
540
}
541
542
PSPDevType VirtualDiscFileSystem::DevType(u32 handle) {
543
EntryMap::iterator iter = entries.find(handle);
544
if (iter == entries.end())
545
return PSPDevType::FILE;
546
PSPDevType type = iter->second.type == VFILETYPE_ISO ? PSPDevType::BLOCK : PSPDevType::FILE;
547
if (iter->second.type == VFILETYPE_LBN)
548
type |= PSPDevType::EMU_LBN;
549
return type;
550
}
551
552
PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) {
553
PSPFileInfo x;
554
x.name = filename;
555
x.access = FILEACCESS_READ;
556
557
if (filename.compare(0,8,"/sce_lbn") == 0) {
558
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
559
parseLBN(filename, &sectorStart, &readSize);
560
561
PSPFileInfo fileInfo;
562
fileInfo.name = filename;
563
fileInfo.exists = true;
564
fileInfo.type = FILETYPE_NORMAL;
565
fileInfo.size = readSize;
566
fileInfo.access = 0444;
567
fileInfo.startSector = sectorStart;
568
fileInfo.isOnSectorSystem = true;
569
fileInfo.numSectors = (readSize + 2047) / 2048;
570
return fileInfo;
571
}
572
573
int fileIndex = getFileListIndex(filename);
574
if (fileIndex != -1 && fileList[fileIndex].handler != NULL) {
575
x.type = FILETYPE_NORMAL;
576
x.isOnSectorSystem = true;
577
x.startSector = fileList[fileIndex].firstBlock;
578
x.access = 0555;
579
580
HandlerFileHandle temp = fileList[fileIndex].handler;
581
if (temp.Open(basePath.ToString(), filename, FILEACCESS_READ)) {
582
x.exists = true;
583
x.size = temp.Seek(0, FILEMOVE_END);
584
temp.Close();
585
}
586
587
// TODO: Probably should include dates or something...
588
return x;
589
}
590
591
Path fullName = GetLocalPath(filename);
592
if (!File::Exists(fullName)) {
593
#if HOST_IS_CASE_SENSITIVE
594
if (! FixPathCase(basePath, filename, FPC_FILE_MUST_EXIST))
595
return x;
596
fullName = GetLocalPath(filename);
597
598
if (! File::Exists(fullName))
599
return x;
600
#else
601
return x;
602
#endif
603
}
604
605
x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
606
x.exists = true;
607
x.access = 0555;
608
if (fileIndex != -1) {
609
x.isOnSectorSystem = true;
610
x.startSector = fileList[fileIndex].firstBlock;
611
}
612
613
if (x.type != FILETYPE_DIRECTORY) {
614
File::FileInfo details;
615
if (!File::GetFileInfo(fullName, &details)) {
616
ERROR_LOG(Log::FileSystem, "DirectoryFileSystem::GetFileInfo: GetFileInfo failed: %s", fullName.c_str());
617
x.size = 0;
618
x.access = 0;
619
} else {
620
x.size = details.size;
621
time_t atime = details.atime;
622
time_t ctime = details.ctime;
623
time_t mtime = details.mtime;
624
625
localtime_r((time_t*)&atime, &x.atime);
626
localtime_r((time_t*)&ctime, &x.ctime);
627
localtime_r((time_t*)&mtime, &x.mtime);
628
}
629
630
// x.startSector was set above in "if (fileIndex != -1)".
631
x.numSectors = (x.size+2047)/2048;
632
}
633
634
return x;
635
}
636
637
PSPFileInfo VirtualDiscFileSystem::GetFileInfoByHandle(u32 handle) {
638
WARN_LOG(Log::FileSystem, "GetFileInfoByHandle not yet implemented for VirtualDiscFileSystem");
639
return PSPFileInfo();
640
}
641
642
#ifdef _WIN32
643
#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL
644
645
static void tmFromFiletime(tm &dest, const FILETIME &src)
646
{
647
u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL;
648
u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US;
649
650
time_t t = (time_t) (from_1970_us / 1000000UL);
651
localtime_s(&dest, &t);
652
}
653
#endif
654
655
std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(const std::string &path, bool *exists) {
656
std::vector<PSPFileInfo> myVector;
657
658
// TODO(scoped): Switch this over to GetFilesInDir!
659
660
#ifdef _WIN32
661
WIN32_FIND_DATA findData;
662
HANDLE hFind;
663
664
// TODO: Handler files that are virtual might not be listed.
665
666
std::wstring w32path = GetLocalPath(path).ToWString() + L"\\*.*";
667
668
#if PPSSPP_PLATFORM(UWP)
669
hFind = FindFirstFileExFromAppW(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
670
#else
671
hFind = FindFirstFileEx(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
672
#endif
673
if (hFind == INVALID_HANDLE_VALUE) {
674
if (exists)
675
*exists = false;
676
return myVector; //the empty list
677
}
678
679
if (exists)
680
*exists = true;
681
682
for (BOOL retval = 1; retval; retval = FindNextFile(hFind, &findData)) {
683
if (!wcscmp(findData.cFileName, L"..") || !wcscmp(findData.cFileName, L".")) {
684
continue;
685
}
686
687
PSPFileInfo entry;
688
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
689
entry.type = FILETYPE_DIRECTORY;
690
} else {
691
entry.type = FILETYPE_NORMAL;
692
}
693
694
entry.access = 0555;
695
entry.exists = true;
696
entry.size = findData.nFileSizeLow | ((u64)findData.nFileSizeHigh<<32);
697
entry.name = ConvertWStringToUTF8(findData.cFileName);
698
tmFromFiletime(entry.atime, findData.ftLastAccessTime);
699
tmFromFiletime(entry.ctime, findData.ftCreationTime);
700
tmFromFiletime(entry.mtime, findData.ftLastWriteTime);
701
entry.isOnSectorSystem = true;
702
703
std::string fullRelativePath = path + "/" + entry.name;
704
int fileIndex = getFileListIndex(fullRelativePath);
705
if (fileIndex != -1)
706
entry.startSector = fileList[fileIndex].firstBlock;
707
myVector.push_back(entry);
708
}
709
FindClose(hFind);
710
#else
711
dirent *dirp;
712
Path localPath = GetLocalPath(path);
713
DIR *dp = opendir(localPath.c_str());
714
715
#if HOST_IS_CASE_SENSITIVE
716
std::string fixedPath = path;
717
if(dp == NULL && FixPathCase(basePath, fixedPath, FPC_FILE_MUST_EXIST)) {
718
// May have failed due to case sensitivity, try again
719
localPath = GetLocalPath(fixedPath);
720
dp = opendir(localPath.c_str());
721
}
722
#endif
723
724
if (dp == NULL) {
725
ERROR_LOG(Log::FileSystem,"Error opening directory %s\n", path.c_str());
726
if (exists)
727
*exists = false;
728
return myVector;
729
}
730
731
if (exists)
732
*exists = true;
733
734
while ((dirp = readdir(dp)) != NULL) {
735
if (!strcmp(dirp->d_name, "..") || !strcmp(dirp->d_name, ".")) {
736
continue;
737
}
738
739
PSPFileInfo entry;
740
struct stat s;
741
std::string fullName = (localPath / std::string(dirp->d_name)).ToString();
742
stat(fullName.c_str(), &s);
743
if (S_ISDIR(s.st_mode))
744
entry.type = FILETYPE_DIRECTORY;
745
else
746
entry.type = FILETYPE_NORMAL;
747
entry.access = 0555;
748
entry.exists = true;
749
entry.name = dirp->d_name;
750
entry.size = s.st_size;
751
localtime_r((time_t*)&s.st_atime,&entry.atime);
752
localtime_r((time_t*)&s.st_ctime,&entry.ctime);
753
localtime_r((time_t*)&s.st_mtime,&entry.mtime);
754
entry.isOnSectorSystem = true;
755
756
std::string fullRelativePath = path + "/" + entry.name;
757
int fileIndex = getFileListIndex(fullRelativePath);
758
if (fileIndex != -1)
759
entry.startSector = fileList[fileIndex].firstBlock;
760
myVector.push_back(entry);
761
}
762
closedir(dp);
763
#endif
764
return myVector;
765
}
766
767
size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)
768
{
769
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");
770
return 0;
771
}
772
773
size_t VirtualDiscFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec)
774
{
775
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot write to file on virtual disc");
776
return 0;
777
}
778
779
bool VirtualDiscFileSystem::MkDir(const std::string &dirname)
780
{
781
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot create directory on virtual disc");
782
return false;
783
}
784
785
bool VirtualDiscFileSystem::RmDir(const std::string &dirname)
786
{
787
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove directory on virtual disc");
788
return false;
789
}
790
791
int VirtualDiscFileSystem::RenameFile(const std::string &from, const std::string &to)
792
{
793
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot rename file on virtual disc");
794
return -1;
795
}
796
797
bool VirtualDiscFileSystem::RemoveFile(const std::string &filename)
798
{
799
ERROR_LOG(Log::FileSystem,"VirtualDiscFileSystem: Cannot remove file on virtual disc");
800
return false;
801
}
802
803
void VirtualDiscFileSystem::HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg) {
804
VirtualDiscFileSystem *sys = static_cast<VirtualDiscFileSystem *>(arg);
805
806
// TODO: Probably could do this smarter / use a lookup.
807
const char *filename = NULL;
808
for (auto it = sys->entries.begin(), end = sys->entries.end(); it != end; ++it) {
809
if (it->second.fileIndex != (u32)-1 && it->second.handler.handle == handle) {
810
filename = sys->fileList[it->second.fileIndex].fileName.c_str();
811
break;
812
}
813
}
814
815
if (filename != NULL) {
816
GENERIC_LOG(Log::FileSystem, level, "%s: %s", filename, msg);
817
} else {
818
GENERIC_LOG(Log::FileSystem, level, "%s", msg);
819
}
820
}
821
822
VirtualDiscFileSystem::Handler::Handler(const char *filename, VirtualDiscFileSystem *const sys)
823
: sys_(sys) {
824
#if !PPSSPP_PLATFORM(SWITCH)
825
#ifdef _WIN32
826
#if PPSSPP_PLATFORM(UWP)
827
#define dlopen(name, ignore) (void *)LoadPackagedLibrary(ConvertUTF8ToWString(name).c_str(), 0)
828
#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)
829
#define dlclose(mod) FreeLibrary((HMODULE)mod)
830
#else
831
#define dlopen(name, ignore) (void *)LoadLibrary(ConvertUTF8ToWString(name).c_str())
832
#define dlsym(mod, name) GetProcAddress((HMODULE)mod, name)
833
#define dlclose(mod) FreeLibrary((HMODULE)mod)
834
#endif
835
#endif
836
837
library = dlopen(filename, RTLD_LOCAL | RTLD_NOW);
838
if (library != NULL) {
839
Init = (InitFunc)dlsym(library, "Init");
840
Shutdown = (ShutdownFunc)dlsym(library, "Shutdown");
841
Open = (OpenFunc)dlsym(library, "Open");
842
Seek = (SeekFunc)dlsym(library, "Seek");
843
Read = (ReadFunc)dlsym(library, "Read");
844
Close = (CloseFunc)dlsym(library, "Close");
845
846
VersionFunc Version = (VersionFunc)dlsym(library, "Version");
847
if (Version && Version() >= 2) {
848
ShutdownV2 = (ShutdownV2Func)Shutdown;
849
}
850
851
if (!Init || !Shutdown || !Open || !Seek || !Read || !Close) {
852
ERROR_LOG(Log::FileSystem, "Unable to find all handler functions: %s", filename);
853
dlclose(library);
854
library = NULL;
855
} else if (!Init(&HandlerLogger, sys)) {
856
ERROR_LOG(Log::FileSystem, "Unable to initialize handler: %s", filename);
857
dlclose(library);
858
library = NULL;
859
}
860
} else {
861
ERROR_LOG(Log::FileSystem, "Unable to load handler '%s': %s", filename, GetLastErrorMsg().c_str());
862
}
863
#ifdef _WIN32
864
#undef dlopen
865
#undef dlsym
866
#undef dlclose
867
#endif
868
#endif
869
}
870
871
VirtualDiscFileSystem::Handler::~Handler() {
872
if (library != NULL) {
873
if (ShutdownV2)
874
ShutdownV2(sys_);
875
else
876
Shutdown();
877
878
#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(SWITCH)
879
#ifdef _WIN32
880
FreeLibrary((HMODULE)library);
881
#else
882
dlclose(library);
883
#endif
884
#endif
885
}
886
}
887
888
889