Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/ISOFileSystem.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 <algorithm>
19
#include <cstring>
20
#include <cstdio>
21
22
#include "Common/CommonTypes.h"
23
#include "Common/Serialize/Serializer.h"
24
#include "Common/Serialize/SerializeFuncs.h"
25
#include "Core/FileSystems/ISOFileSystem.h"
26
#include "Core/HLE/sceKernel.h"
27
#include "Core/MemMap.h"
28
#include "Core/Reporting.h"
29
30
const int sectorSize = 2048;
31
32
bool parseLBN(const std::string &filename, u32 *sectorStart, u32 *readSize) {
33
// The format of this is: "/sce_lbn" "0x"? HEX* ANY* "_size" "0x"? HEX* ANY*
34
// That means that "/sce_lbn/_size1/" is perfectly valid.
35
// Most commonly, it looks like /sce_lbn0x10_size0x100 or /sce_lbn10_size100 (always hex.)
36
37
// If it doesn't starts with /sce_lbn or doesn't have _size, look for a file instead.
38
if (filename.compare(0, sizeof("/sce_lbn") - 1, "/sce_lbn") != 0)
39
return false;
40
size_t size_pos = filename.find("_size");
41
if (size_pos == filename.npos)
42
return false;
43
44
// TODO: Return SCE_KERNEL_ERROR_ERRNO_INVALID_ARGUMENT when >= 32 long but passes above checks.
45
if (filename.size() >= 32)
46
return false;
47
48
const char *filename_c = filename.c_str();
49
size_t pos = strlen("/sce_lbn");
50
51
if (sscanf(filename_c + pos, "%x", sectorStart) != 1)
52
*sectorStart = 0;
53
54
pos = size_pos + strlen("_size");
55
56
if (sscanf(filename_c + pos, "%x", readSize) != 1)
57
*readSize = 0;
58
59
return true;
60
}
61
62
#pragma pack(push)
63
#pragma pack(1)
64
struct u32_le_be_pair {
65
u8 valueLE[4];
66
u8 valueBE[4];
67
operator u32() const {
68
return valueLE[0] + (valueLE[1] << 8) + (valueLE[2] << 16) + (valueLE[3] << 24);
69
}
70
};
71
72
struct u16_le_be_pair {
73
u8 valueLE[2];
74
u8 valueBE[2];
75
operator u16() const {
76
return valueLE[0] + (valueLE[1] << 8);
77
}
78
};
79
80
struct DirectoryEntry {
81
u8 size;
82
u8 sectorsInExtendedRecord;
83
u32_le_be_pair firstDataSector; // LBA
84
u32_le_be_pair dataLength; // Size
85
u8 years;
86
u8 month;
87
u8 day;
88
u8 hour;
89
u8 minute;
90
u8 second;
91
u8 offsetFromGMT;
92
u8 flags; // 2 = directory
93
u8 fileUnitSize;
94
u8 interleaveGap;
95
u16_le_be_pair volSeqNumber;
96
u8 identifierLength; //identifier comes right after
97
u8 firstIdChar;
98
};
99
100
struct DirectorySector {
101
DirectoryEntry entry;
102
char space[2048-sizeof(DirectoryEntry)];
103
};
104
105
struct VolDescriptor {
106
char type;
107
char cd001[6];
108
char version;
109
char sysid[32];
110
char volid[32];
111
char zeros[8];
112
u32_le_be_pair numSectors;
113
char morezeros[32];
114
u16_le_be_pair volSetSize;
115
u16_le_be_pair volSeqNum;
116
u16_le_be_pair sectorSize;
117
u32_le_be_pair pathTableLength;
118
u16_le_be_pair firstLETableSector;
119
u16_le_be_pair secondLETableSector;
120
u16_le_be_pair firstBETableSector;
121
u16_le_be_pair secondBETableSector;
122
DirectoryEntry root;
123
char volumeSetIdentifier[128];
124
char publisherIdentifier[128];
125
char dataPreparerIdentifier[128];
126
char applicationIdentifier[128];
127
char copyrightFileIdentifier[37];
128
char abstractFileIdentifier[37];
129
char bibliographicalFileIdentifier[37];
130
char volCreationDateTime[17];
131
char mreModDateTime[17];
132
char volExpireDateTime[17];
133
char volEffectiveDateTime[17];
134
char one;
135
char zero;
136
char reserved[512];
137
char zeroos[653];
138
};
139
140
#pragma pack(pop)
141
142
ISOFileSystem::ISOFileSystem(IHandleAllocator *_hAlloc, BlockDevice *_blockDevice) {
143
blockDevice = _blockDevice;
144
hAlloc = _hAlloc;
145
146
VolDescriptor desc;
147
if (!blockDevice->ReadBlock(16, (u8*)&desc))
148
blockDevice->NotifyReadError();
149
150
entireISO.name.clear();
151
entireISO.isDirectory = false;
152
entireISO.startingPosition = 0;
153
entireISO.size = _blockDevice->GetNumBlocks();
154
entireISO.flags = 0;
155
entireISO.parent = NULL;
156
157
treeroot = new TreeEntry();
158
treeroot->isDirectory = true;
159
treeroot->startingPosition = 0;
160
treeroot->size = 0;
161
treeroot->flags = 0;
162
treeroot->parent = NULL;
163
treeroot->valid = false;
164
165
if (memcmp(desc.cd001, "CD001", 5)) {
166
ERROR_LOG(Log::FileSystem, "ISO looks bogus, expected CD001 signature not present? Giving up...");
167
return;
168
}
169
170
treeroot->startsector = desc.root.firstDataSector;
171
treeroot->dirsize = desc.root.dataLength;
172
}
173
174
ISOFileSystem::~ISOFileSystem() {
175
delete blockDevice;
176
delete treeroot;
177
}
178
179
std::string ISOFileSystem::TreeEntry::BuildPath() {
180
if (parent) {
181
return parent->BuildPath() + "/" + name;
182
} else {
183
return name;
184
}
185
}
186
187
void ISOFileSystem::ReadDirectory(TreeEntry *root) const {
188
for (u32 secnum = root->startsector, endsector = root->startsector + (root->dirsize + 2047) / 2048; secnum < endsector; ++secnum) {
189
u8 theSector[2048];
190
if (!blockDevice->ReadBlock(secnum, theSector)) {
191
blockDevice->NotifyReadError();
192
ERROR_LOG(Log::FileSystem, "Error reading block for directory '%s' in sector %d - skipping", root->name.c_str(), secnum);
193
root->valid = true; // Prevents re-reading
194
return;
195
}
196
lastReadBlock_ = secnum; // Hm, this could affect timing... but lazy loading is probably more realistic.
197
198
for (int offset = 0; offset < 2048; ) {
199
DirectoryEntry &dir = *(DirectoryEntry *)&theSector[offset];
200
u8 sz = theSector[offset];
201
202
// Nothing left in this sector. There might be more in the next one.
203
if (sz == 0)
204
break;
205
206
const int IDENTIFIER_OFFSET = 33;
207
if (offset + IDENTIFIER_OFFSET + dir.identifierLength > 2048) {
208
blockDevice->NotifyReadError();
209
ERROR_LOG(Log::FileSystem, "Directory entry crosses sectors, corrupt iso?");
210
return;
211
}
212
213
offset += dir.size;
214
215
bool isFile = (dir.flags & 2) ? false : true;
216
bool relative;
217
218
TreeEntry *entry = new TreeEntry();
219
if (dir.identifierLength == 1 && (dir.firstIdChar == '\x00' || dir.firstIdChar == '.')) {
220
entry->name = ".";
221
relative = true;
222
} else if (dir.identifierLength == 1 && dir.firstIdChar == '\x01') {
223
entry->name = "..";
224
relative = true;
225
} else {
226
entry->name = std::string((const char *)&dir.firstIdChar, dir.identifierLength);
227
relative = false;
228
}
229
230
entry->size = dir.dataLength;
231
entry->startingPosition = dir.firstDataSector * 2048;
232
entry->isDirectory = !isFile;
233
entry->flags = dir.flags;
234
entry->parent = root;
235
entry->startsector = dir.firstDataSector;
236
entry->dirsize = dir.dataLength;
237
entry->valid = isFile; // Can pre-mark as valid if file, as we don't recurse into those.
238
VERBOSE_LOG(Log::FileSystem, "%s: %s %08x %08x %d", entry->isDirectory ? "D" : "F", entry->name.c_str(), (u32)dir.firstDataSector, entry->startingPosition, entry->startingPosition);
239
240
// Round down to avoid any false reports.
241
if (isFile && dir.firstDataSector + (dir.dataLength / 2048) > blockDevice->GetNumBlocks()) {
242
blockDevice->NotifyReadError();
243
ERROR_LOG(Log::FileSystem, "File '%s' starts or ends outside ISO. firstDataSector: %d len: %d", entry->BuildPath().c_str(), (int)dir.firstDataSector, (int)dir.dataLength);
244
}
245
246
if (entry->isDirectory && !relative) {
247
if (entry->startsector == root->startsector) {
248
blockDevice->NotifyReadError();
249
ERROR_LOG(Log::FileSystem, "WARNING: Appear to have a recursive file system, breaking recursion. Probably corrupt ISO.");
250
}
251
}
252
root->children.push_back(entry);
253
}
254
}
255
root->valid = true;
256
}
257
258
const ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string_view path, bool catchError) {
259
const size_t pathLength = path.length();
260
261
if (pathLength == 0) {
262
// Ah, the device! "umd0:"
263
return &entireISO;
264
}
265
266
size_t pathIndex = 0;
267
268
// Skip "./"
269
if (pathLength > pathIndex + 1 && path[pathIndex] == '.' && path[pathIndex + 1] == '/')
270
pathIndex += 2;
271
272
// Skip "/"
273
if (pathLength > pathIndex && path[pathIndex] == '/')
274
++pathIndex;
275
276
if (pathLength <= pathIndex)
277
return treeroot;
278
279
TreeEntry *entry = treeroot;
280
while (true) {
281
if (!entry->valid) {
282
ReadDirectory(entry);
283
}
284
TreeEntry *nextEntry = nullptr;
285
std::string name = "";
286
if (pathLength > pathIndex) {
287
size_t nextSlashIndex = path.find_first_of('/', pathIndex);
288
if (nextSlashIndex == std::string::npos)
289
nextSlashIndex = pathLength;
290
291
const std::string_view firstPathComponent = path.substr(pathIndex, nextSlashIndex - pathIndex);
292
for (size_t i = 0; i < entry->children.size(); i++) {
293
const std::string_view n = entry->children[i]->name;
294
if (firstPathComponent == n) {
295
//yay we got it
296
nextEntry = entry->children[i];
297
name = n;
298
break;
299
}
300
}
301
}
302
303
if (nextEntry) {
304
entry = nextEntry;
305
if (!entry->valid) {
306
ReadDirectory(entry);
307
}
308
pathIndex += name.length();
309
if (pathIndex < pathLength && path[pathIndex] == '/')
310
++pathIndex;
311
312
if (pathLength <= pathIndex)
313
return entry;
314
} else {
315
if (catchError) {
316
ERROR_LOG(Log::FileSystem, "File '%.*s' not found", STR_VIEW(path));
317
}
318
return 0;
319
}
320
}
321
}
322
323
int ISOFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename) {
324
OpenFileEntry entry;
325
entry.isRawSector = false;
326
entry.isBlockSectorMode = false;
327
328
if (access & FILEACCESS_WRITE) {
329
ERROR_LOG(Log::FileSystem, "Can't open file '%s' with write access on an ISO partition", filename.c_str());
330
return SCE_KERNEL_ERROR_ERRNO_INVALID_FLAG;
331
}
332
333
if (filename.compare(0, 8, "/sce_lbn") == 0) {
334
// Raw sector read.
335
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
336
parseLBN(filename, &sectorStart, &readSize);
337
if (sectorStart > blockDevice->GetNumBlocks()) {
338
WARN_LOG(Log::FileSystem, "Unable to open raw sector, out of range: '%s', sector %08x, max %08x", filename.c_str(), sectorStart, blockDevice->GetNumBlocks());
339
return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;
340
}
341
else if (sectorStart == blockDevice->GetNumBlocks())
342
{
343
ERROR_LOG(Log::FileSystem, "Should not be able to open the block after the last on disc! %08x", sectorStart);
344
}
345
346
DEBUG_LOG(Log::FileSystem, "Got a raw sector open: '%s', sector %08x, size %08x", filename.c_str(), sectorStart, readSize);
347
u32 newHandle = hAlloc->GetNewHandle();
348
entry.seekPos = 0;
349
entry.file = 0;
350
entry.isRawSector = true;
351
entry.sectorStart = sectorStart;
352
entry.openSize = readSize;
353
// when open as "umd1:/sce_lbn0x0_size0x6B49D200", that mean open umd1 as a block device.
354
// the param in sceIoLseek and sceIoRead is lba mode. we must mark it.
355
if (strncmp(devicename, "umd0:", 5) == 0 || strncmp(devicename, "umd1:", 5) == 0)
356
entry.isBlockSectorMode = true;
357
358
entries[newHandle] = entry;
359
return newHandle;
360
}
361
362
// May return entireISO for "umd0:".
363
entry.file = GetFromPath(filename, false);
364
if (!entry.file) {
365
return SCE_KERNEL_ERROR_ERRNO_FILE_NOT_FOUND;
366
}
367
368
if (entry.file == &entireISO)
369
entry.isBlockSectorMode = true;
370
371
entry.seekPos = 0;
372
373
u32 newHandle = hAlloc->GetNewHandle();
374
entries[newHandle] = entry;
375
return newHandle;
376
}
377
378
void ISOFileSystem::CloseFile(u32 handle) {
379
EntryMap::iterator iter = entries.find(handle);
380
if (iter != entries.end()) {
381
//CloseHandle((*iter).second.hFile);
382
hAlloc->FreeHandle(handle);
383
entries.erase(iter);
384
} else {
385
//This shouldn't happen...
386
ERROR_LOG(Log::FileSystem, "Hey, what are you doing? Closing non-open files?");
387
}
388
}
389
390
bool ISOFileSystem::OwnsHandle(u32 handle) {
391
EntryMap::iterator iter = entries.find(handle);
392
return (iter != entries.end());
393
}
394
395
int ISOFileSystem::Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) {
396
EntryMap::iterator iter = entries.find(handle);
397
if (iter == entries.end()) {
398
ERROR_LOG(Log::FileSystem, "Ioctl on a bad file handle");
399
return SCE_KERNEL_ERROR_BADF;
400
}
401
402
OpenFileEntry &e = iter->second;
403
404
switch (cmd) {
405
// Get ISO9660 volume descriptor (from open ISO9660 file.)
406
case 0x01020001:
407
if (e.isBlockSectorMode) {
408
ERROR_LOG(Log::FileSystem, "Unsupported read volume descriptor command on a umd block device");
409
return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;
410
}
411
412
if (!Memory::IsValidRange(outdataPtr, 0x800) || outlen < 0x800) {
413
WARN_LOG_REPORT(Log::FileSystem, "sceIoIoctl: Invalid out pointer %08x while reading ISO9660 volume descriptor", outdataPtr);
414
return SCE_KERNEL_ERROR_ERRNO_INVALID_ARGUMENT;
415
}
416
417
INFO_LOG(Log::sceIo, "sceIoIoctl: reading ISO9660 volume descriptor read");
418
blockDevice->ReadBlock(16, Memory::GetPointerWriteUnchecked(outdataPtr));
419
return 0;
420
421
// Get ISO9660 path table (from open ISO9660 file.)
422
case 0x01020002:
423
if (e.isBlockSectorMode) {
424
ERROR_LOG(Log::FileSystem, "Unsupported read path table command on a umd block device");
425
return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;
426
}
427
428
VolDescriptor desc;
429
blockDevice->ReadBlock(16, (u8 *)&desc);
430
if (outlen < (u32)desc.pathTableLength) {
431
return SCE_KERNEL_ERROR_ERRNO_INVALID_ARGUMENT;
432
} else {
433
int block = (u16)desc.firstLETableSector;
434
u32 size = Memory::ClampValidSizeAt(outdataPtr, (u32)desc.pathTableLength);
435
u8 *out = Memory::GetPointerWriteRange(outdataPtr, size);
436
437
int blocks = size / blockDevice->GetBlockSize();
438
blockDevice->ReadBlocks(block, blocks, out);
439
size -= blocks * blockDevice->GetBlockSize();
440
out += blocks * blockDevice->GetBlockSize();
441
442
// The remaining (or, usually, only) partial sector.
443
if (size > 0) {
444
u8 temp[2048];
445
blockDevice->ReadBlock(block, temp);
446
memcpy(out, temp, size);
447
}
448
return 0;
449
}
450
}
451
return SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED;
452
}
453
454
PSPDevType ISOFileSystem::DevType(u32 handle) {
455
EntryMap::iterator iter = entries.find(handle);
456
if (iter == entries.end())
457
return PSPDevType::FILE;
458
PSPDevType type = iter->second.isBlockSectorMode ? PSPDevType::BLOCK : PSPDevType::FILE;
459
if (iter->second.isRawSector)
460
type |= PSPDevType::EMU_LBN;
461
return type;
462
}
463
464
FileSystemFlags ISOFileSystem::Flags() const {
465
// TODO: Here may be a good place to force things, in case users recompress games
466
// as PBP or CSO when they were originally the other type.
467
return blockDevice->IsDisc() ? FileSystemFlags::UMD : FileSystemFlags::CARD;
468
}
469
470
size_t ISOFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size)
471
{
472
int ignored;
473
return ReadFile(handle, pointer, size, ignored);
474
}
475
476
size_t ISOFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) {
477
EntryMap::iterator iter = entries.find(handle);
478
if (iter != entries.end()) {
479
OpenFileEntry &e = iter->second;
480
481
if (size < 0) {
482
ERROR_LOG(Log::FileSystem, "Invalid read for %lld bytes from umd %s", size, e.file ? e.file->name.c_str() : "device");
483
return 0;
484
}
485
486
if (e.isBlockSectorMode) {
487
// Whole sectors! Shortcut to this simple code.
488
blockDevice->ReadBlocks(e.seekPos, (int)size, pointer);
489
if (abs((int)lastReadBlock_ - (int)e.seekPos) > 100) {
490
// This is an estimate, sometimes it takes 1+ seconds, but it definitely takes time.
491
usec = 100000;
492
}
493
e.seekPos += (int)size;
494
lastReadBlock_ = e.seekPos;
495
return (int)size;
496
}
497
498
u64 positionOnIso;
499
s64 fileSize;
500
if (e.isRawSector) {
501
positionOnIso = e.sectorStart * 2048ULL + e.seekPos;
502
fileSize = (s64)e.openSize;
503
} else if (e.file == nullptr) {
504
ERROR_LOG(Log::FileSystem, "File no longer exists (loaded savestate with different ISO?)");
505
return 0;
506
} else {
507
positionOnIso = e.file->startingPosition + e.seekPos;
508
fileSize = e.file->size;
509
}
510
511
if ((s64)e.seekPos > fileSize) {
512
WARN_LOG(Log::FileSystem, "Read starting outside of file, at %lld / %lld", (s64)e.seekPos, fileSize);
513
return 0;
514
}
515
if ((s64)e.seekPos + size > fileSize) {
516
// Clamp to the remaining size, but read what we can.
517
const s64 newSize = fileSize - (s64)e.seekPos;
518
// Reading beyond the file is really quite normal behavior (if return value handled correctly), so
519
// not doing WARN here.
520
if (newSize == 0) {
521
DEBUG_LOG(Log::FileSystem, "Attempted read at end of file, 0-size read simulated");
522
} else {
523
DEBUG_LOG(Log::FileSystem, "Reading beyond end of file from seekPos %d, clamping size %lld to %lld", e.seekPos, size, newSize);
524
}
525
size = newSize;
526
}
527
528
// Okay, we have size and position, let's rock.
529
const int firstBlockOffset = positionOnIso & 2047;
530
const int firstBlockSize = firstBlockOffset == 0 ? 0 : (int)std::min(size, 2048LL - firstBlockOffset);
531
const int lastBlockSize = (size - firstBlockSize) & 2047;
532
const s64 middleSize = size - firstBlockSize - lastBlockSize;
533
_dbg_assert_((middleSize & 2047) == 0);
534
535
u32 secNum = (u32)(positionOnIso / 2048);
536
u8 theSector[2048];
537
538
if ((middleSize & 2047) != 0) {
539
ERROR_LOG(Log::FileSystem, "Remaining size should be aligned");
540
}
541
542
const u8 *const start = pointer;
543
if (firstBlockSize > 0) {
544
blockDevice->ReadBlock(secNum++, theSector);
545
memcpy(pointer, theSector + firstBlockOffset, firstBlockSize);
546
pointer += firstBlockSize;
547
}
548
if (middleSize > 0) {
549
const u32 middleSectors = (u32)(middleSize / 2048);
550
blockDevice->ReadBlocks(secNum, middleSectors, pointer);
551
secNum += middleSectors;
552
pointer += middleSize;
553
}
554
if (lastBlockSize > 0) {
555
blockDevice->ReadBlock(secNum++, theSector);
556
memcpy(pointer, theSector, lastBlockSize);
557
pointer += lastBlockSize;
558
}
559
560
size_t totalBytes = pointer - start;
561
if (abs((int)lastReadBlock_ - (int)secNum) > 100) {
562
// This is an estimate, sometimes it takes 1+ seconds, but it definitely takes time.
563
usec = 100000;
564
}
565
lastReadBlock_ = secNum;
566
e.seekPos += (unsigned int)totalBytes;
567
return (size_t)totalBytes;
568
} else {
569
//This shouldn't happen...
570
ERROR_LOG(Log::FileSystem, "Hey, what are you doing? Reading non-open files?");
571
return 0;
572
}
573
}
574
575
size_t ISOFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size) {
576
ERROR_LOG(Log::FileSystem, "Hey, what are you doing? You can't write to an ISO!");
577
return 0;
578
}
579
580
size_t ISOFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) {
581
ERROR_LOG(Log::FileSystem, "Hey, what are you doing? You can't write to an ISO!");
582
return 0;
583
}
584
585
size_t ISOFileSystem::SeekFile(u32 handle, s32 position, FileMove type) {
586
EntryMap::iterator iter = entries.find(handle);
587
if (iter != entries.end()) {
588
OpenFileEntry &e = iter->second;
589
switch (type)
590
{
591
case FILEMOVE_BEGIN:
592
e.seekPos = position;
593
break;
594
case FILEMOVE_CURRENT:
595
e.seekPos += position;
596
break;
597
case FILEMOVE_END:
598
if (e.isRawSector)
599
e.seekPos = e.openSize + position;
600
else
601
e.seekPos = (unsigned int)(e.file->size + position);
602
break;
603
}
604
return (size_t)e.seekPos;
605
} else {
606
//This shouldn't happen...
607
ERROR_LOG(Log::FileSystem, "Hey, what are you doing? Seeking in non-open files?");
608
return 0;
609
}
610
}
611
612
PSPFileInfo ISOFileSystem::GetFileInfo(std::string filename) {
613
if (filename.compare(0,8,"/sce_lbn") == 0) {
614
u32 sectorStart = 0xFFFFFFFF, readSize = 0xFFFFFFFF;
615
parseLBN(filename, &sectorStart, &readSize);
616
617
PSPFileInfo fileInfo;
618
fileInfo.name = filename;
619
fileInfo.exists = true;
620
fileInfo.type = FILETYPE_NORMAL;
621
fileInfo.size = readSize;
622
fileInfo.access = 0444;
623
fileInfo.startSector = sectorStart;
624
fileInfo.isOnSectorSystem = true;
625
fileInfo.numSectors = (readSize + sectorSize - 1) / sectorSize;
626
return fileInfo;
627
}
628
629
const TreeEntry *entry = GetFromPath(filename, false);
630
PSPFileInfo x;
631
if (entry) {
632
x.name = entry->name;
633
// Strangely, it seems to be executable even for files.
634
x.access = 0555;
635
x.size = entry->size;
636
x.exists = true;
637
x.type = entry->isDirectory ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
638
x.isOnSectorSystem = true;
639
x.startSector = entry->startingPosition / 2048;
640
}
641
return x;
642
}
643
644
PSPFileInfo ISOFileSystem::GetFileInfoByHandle(u32 handle) {
645
auto iter = entries.find(handle);
646
PSPFileInfo x;
647
if (iter != entries.end()) {
648
const TreeEntry *entry = iter->second.file;
649
x.name = entry->name;
650
// Strangely, it seems to be executable even for files.
651
x.access = 0555;
652
x.size = entry->size;
653
x.exists = true;
654
x.type = entry->isDirectory ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
655
x.isOnSectorSystem = true;
656
x.startSector = entry->startingPosition / 2048;
657
}
658
return x;
659
}
660
661
std::vector<PSPFileInfo> ISOFileSystem::GetDirListing(const std::string &path, bool *exists) {
662
std::vector<PSPFileInfo> myVector;
663
const TreeEntry *entry = GetFromPath(path);
664
if (!entry) {
665
if (exists)
666
*exists = false;
667
return myVector;
668
}
669
if (entry == &entireISO) {
670
entry = GetFromPath("/");
671
}
672
673
const std::string dot(".");
674
const std::string dotdot("..");
675
676
for (size_t i = 0; i < entry->children.size(); i++) {
677
const TreeEntry *e = entry->children[i];
678
679
// do not include the relative entries in the list
680
if (e->name == dot || e->name == dotdot)
681
continue;
682
683
PSPFileInfo x;
684
x.name = e->name;
685
// Strangely, it seems to be executable even for files.
686
x.access = 0555;
687
x.exists = true;
688
x.size = e->size;
689
x.type = e->isDirectory ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
690
x.isOnSectorSystem = true;
691
x.startSector = e->startingPosition/2048;
692
x.sectorSize = sectorSize;
693
x.numSectors = (u32)((e->size + sectorSize - 1) / sectorSize);
694
myVector.push_back(x);
695
}
696
if (exists)
697
*exists = true;
698
return myVector;
699
}
700
701
std::string ISOFileSystem::EntryFullPath(const TreeEntry *e) {
702
if (e == &entireISO)
703
return "";
704
705
size_t fullLen = 0;
706
const TreeEntry *cur = e;
707
while (cur != NULL && cur != treeroot) {
708
// For the "/".
709
fullLen += 1 + cur->name.size();
710
cur = cur->parent;
711
}
712
713
std::string path;
714
path.resize(fullLen);
715
716
cur = e;
717
while (cur != NULL && cur != treeroot) {
718
path.replace(fullLen - cur->name.size(), cur->name.size(), cur->name);
719
path.replace(fullLen - cur->name.size() - 1, 1, "/");
720
fullLen -= 1 + cur->name.size();
721
cur = cur->parent;
722
}
723
724
return path;
725
}
726
727
ISOFileSystem::TreeEntry::~TreeEntry() {
728
for (size_t i = 0; i < children.size(); ++i)
729
delete children[i];
730
children.clear();
731
}
732
733
void ISOFileSystem::DoState(PointerWrap &p) {
734
auto s = p.Section("ISOFileSystem", 1, 2);
735
if (!s)
736
return;
737
738
int n = (int) entries.size();
739
Do(p, n);
740
741
if (p.mode == p.MODE_READ) {
742
entries.clear();
743
for (int i = 0; i < n; ++i) {
744
u32 fd = 0;
745
OpenFileEntry of;
746
747
Do(p, fd);
748
Do(p, of.seekPos);
749
Do(p, of.isRawSector);
750
Do(p, of.isBlockSectorMode);
751
Do(p, of.sectorStart);
752
Do(p, of.openSize);
753
754
bool hasFile = false;
755
Do(p, hasFile);
756
if (hasFile) {
757
std::string path;
758
Do(p, path);
759
of.file = GetFromPath(path);
760
} else {
761
of.file = NULL;
762
}
763
764
entries[fd] = of;
765
}
766
} else {
767
for (EntryMap::iterator it = entries.begin(), end = entries.end(); it != end; ++it) {
768
OpenFileEntry &of = it->second;
769
Do(p, it->first);
770
Do(p, of.seekPos);
771
Do(p, of.isRawSector);
772
Do(p, of.isBlockSectorMode);
773
Do(p, of.sectorStart);
774
Do(p, of.openSize);
775
776
bool hasFile = of.file != NULL;
777
Do(p, hasFile);
778
if (hasFile) {
779
std::string path = EntryFullPath(of.file);
780
Do(p, path);
781
}
782
}
783
}
784
785
if (s >= 2) {
786
Do(p, lastReadBlock_);
787
} else {
788
lastReadBlock_ = 0;
789
}
790
}
791
792