-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFSWrapper.cpp
More file actions
772 lines (651 loc) · 28.1 KB
/
FSWrapper.cpp
File metadata and controls
772 lines (651 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#include "FSWrapper.h"
#include "FileUtils.h"
#include "utils/StringTools.h"
#include "utils/logger.h"
#include "utils/utils.h"
#include <coreinit/cache.h>
#include <coreinit/debug.h>
#include <algorithm>
#include <filesystem>
#include <cstdio>
#include <sys/dirent.h>
#include <sys/fcntl.h>
#include <sys/unistd.h>
FSError FSWrapper::FSOpenDirWrapper(const char *path, FSDirectoryHandle *handle) {
if (path == nullptr) {
return FS_ERROR_INVALID_PARAM;
}
if (!IsPathToReplace(path)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (handle == nullptr) {
DEBUG_FUNCTION_LINE_ERR("[%s] handle was nullptr", getName().c_str());
return FS_ERROR_INVALID_PARAM;
}
FSError result = FS_ERROR_OK;
const auto dirHandle = getNewDirInfoHandle();
if (dirHandle) {
DIR *dir;
auto newPath = GetNewPath(path);
if ((dir = opendir(newPath.c_str()))) {
dirHandle->dir = dir;
dirHandle->handle = (((uint32_t) dirHandle.get()) & 0x0FFFFFFF) | 0x30000000;
*handle = dirHandle->handle;
dirHandle->path[0] = '\0';
strncat(dirHandle->path, newPath.c_str(), sizeof(dirHandle->path) - 1);
addDirHandle(dirHandle);
} else {
auto err = errno;
if (err == ENOENT) {
DEBUG_FUNCTION_LINE("[%s] Open dir %s (%s) failed. FS_ERROR_NOT_FOUND", getName().c_str(), path, newPath.c_str());
return FS_ERROR_NOT_FOUND;
}
DEBUG_FUNCTION_LINE_ERR("[%s] Open dir %s (%s) failed. errno %d", getName().c_str(), path, newPath.c_str(), err);
if (err == EACCES) {
return FS_ERROR_PERMISSION_ERROR;
} else if (err == ENOTDIR) {
return FS_ERROR_NOT_DIR;
} else if (err == ENFILE || err == EMFILE) {
return FS_ERROR_MAX_DIRS;
}
return FS_ERROR_MEDIA_ERROR;
}
} else {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to alloc dir handle", getName().c_str());
result = FS_ERROR_MAX_DIRS;
}
return result;
}
FSError FSWrapper::FSReadDirWrapper(const FSDirectoryHandle handle, FSDirectoryEntry *entry) {
if (!isValidDirHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
const auto dirHandle = getDirInfoFromHandle(handle);
DIR *dir = dirHandle->dir;
FSError result = FS_ERROR_END_OF_DIR;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] readdir %p (handle %08X)", getName().c_str(), dir, handle);
do {
errno = 0;
struct dirent *entry_ = readdir(dir);
if (entry_) {
if (SkipDeletedFilesInReadDir() && starts_with_case_insensitive(entry_->d_name, deletePrefix)) {
DEBUG_FUNCTION_LINE_ERR("Skip file file name %s because of the prefix", entry_->d_name);
continue;
}
entry->name[0] = '\0';
strlcpy(entry->name, entry_->d_name, sizeof(entry->name));
entry->info.mode = (FSMode) FS_MODE_READ_OWNER;
if (entry_->d_type == DT_DIR) {
entry->info.flags = (FSStatFlags) ((uint32_t) FS_STAT_DIRECTORY);
entry->info.size = 0;
} else {
entry->info.flags = (FSStatFlags) 0;
if (strcmp(entry_->d_name, ".") == 0 || strcmp(entry_->d_name, "..") == 0) {
entry->info.size = 0;
} else {
#ifdef _DIRENT_HAVE_D_STAT
translate_stat(&entry_->d_stat, &entry->info);
#else
struct stat sb {};
auto path = string_format("%s/%s", dirHandle->path, entry_->d_name);
std::replace(path.begin(), path.end(), '\\', '/');
uint32_t length = path.size();
//! clear path of double slashes
for (uint32_t i = 1; i < length; ++i) {
if (path[i - 1] == '/' && path[i] == '/') {
path.erase(i, 1);
i--;
length--;
}
}
if (stat(path.c_str(), &sb) >= 0) {
translate_stat(&sb, &entry->info);
} else {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to stat file (%s) in read dir %08X (dir handle %08X)", getName().c_str(), path.c_str(), dir, handle);
result = FS_ERROR_MEDIA_ERROR;
break;
}
#endif
}
}
result = FS_ERROR_OK;
} else {
auto err = errno;
if (err != 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to read dir %p (handle %08X). errno %d (%s)", getName().c_str(), dir, handle, err, strerror(err));
result = FS_ERROR_MEDIA_ERROR;
}
}
break;
} while (true);
return result;
}
FSError FSWrapper::FSCloseDirWrapper(const FSDirectoryHandle handle) {
if (!isValidDirHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
const auto dirHandle = getDirInfoFromHandle(handle);
DIR *dir = dirHandle->dir;
FSError result = FS_ERROR_OK;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] closedir %p (handle %08X)", getName().c_str(), dir, handle);
if (closedir(dir) < 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to close dir %p (handle %08X)", getName().c_str(), dir, handle);
result = FS_ERROR_MEDIA_ERROR;
}
dirHandle->dir = nullptr;
return result;
}
FSError FSWrapper::FSRewindDirWrapper(const FSDirectoryHandle handle) {
if (!isValidDirHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
const auto dirHandle = getDirInfoFromHandle(handle);
DIR *dir = dirHandle->dir;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] rewinddir %p (handle %08X)", getName().c_str(), dir, handle);
rewinddir(dir);
return FS_ERROR_OK;
}
FSError FSWrapper::FSMakeDirWrapper(const char *path) {
if (!IsPathToReplace(path)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (!pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to create dir %s but layer is not writeable", getName().c_str(), path);
return FS_ERROR_PERMISSION_ERROR;
}
auto newPath = GetNewPath(path);
auto res = mkdir(newPath.c_str(), 0000660);
if (res < 0) {
auto err = errno;
if (err == EACCES) {
return FS_ERROR_PERMISSION_ERROR;
} else if (err == EEXIST) {
return FS_ERROR_ALREADY_EXISTS;
} else if (err == ENOTDIR) {
return FS_ERROR_NOT_DIR;
} else if (err == ENOENT) {
return FS_ERROR_NOT_FOUND;
}
return FS_ERROR_MEDIA_ERROR;
}
return FS_ERROR_OK;
}
FSError FSWrapper::FSOpenFileWrapper(const char *path, const char *mode, FSFileHandle *handle) {
if (!IsPathToReplace(path)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (path == nullptr) {
DEBUG_FUNCTION_LINE_ERR("[%s] path was nullptr", getName().c_str());
return FS_ERROR_INVALID_PARAM;
}
if (mode == nullptr || handle == nullptr) {
DEBUG_FUNCTION_LINE_ERR("[%s] mode or handle was nullptr", getName().c_str());
return FS_ERROR_INVALID_PARAM;
}
auto newPath = GetNewPath(path);
if (pCheckIfDeleted && CheckFileShouldBeIgnored(newPath)) {
return static_cast<FSError>((FS_ERROR_NOT_FOUND & FS_ERROR_REAL_MASK) | FS_ERROR_FORCE_NO_FALLBACK);
}
auto result = FS_ERROR_OK;
int _mode;
// Map flags to open modes
if (!IsFileModeAllowed(mode)) {
OSReport("## WARN ## [%s] Given mode is not allowed %s", getName().c_str(), mode);
DEBUG_FUNCTION_LINE("[%s] Given mode is not allowed %s", getName().c_str(), mode);
return FS_ERROR_ACCESS_ERROR;
}
if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) {
_mode = O_RDONLY;
} else if (strcmp(mode, "r+") == 0) {
_mode = O_RDWR;
} else if (strcmp(mode, "w") == 0 || strcmp(mode, "wb") == 0) {
_mode = O_WRONLY | O_CREAT | O_TRUNC;
} else if (strcmp(mode, "w+") == 0) {
_mode = O_RDWR | O_CREAT | O_TRUNC;
} else if (strcmp(mode, "a") == 0) {
_mode = O_WRONLY | O_CREAT | O_APPEND;
} else if (strcmp(mode, "a+") == 0) {
_mode = O_RDWR | O_CREAT | O_APPEND;
} else {
DEBUG_FUNCTION_LINE_ERR("[%s] mode \"%s\" was allowed but is unsupported", getName().c_str(), mode);
return FS_ERROR_ACCESS_ERROR;
}
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Open %s (as %s) mode %s,", getName().c_str(), path, newPath.c_str(), mode);
int32_t fd = open(newPath.c_str(), _mode);
if (fd >= 0) {
auto fileHandle = getNewFileHandle();
if (fileHandle) {
std::lock_guard lock(openFilesMutex);
fileHandle->handle = (((uint32_t) fileHandle.get()) & 0x0FFFFFFF) | 0x30000000;
*handle = fileHandle->handle;
fileHandle->fd = fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Opened %s (as %s) mode %s (%08X), fd %d (%08X)", getName().c_str(), path, newPath.c_str(), mode, _mode, fd, fileHandle->handle);
openFiles.push_back(fileHandle);
OSMemoryBarrier();
} else {
close(fd);
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to alloc new fileHandle", getName().c_str());
result = FS_ERROR_MAX_FILES;
}
} else {
auto err = errno;
if (err == ENOENT) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] File not found %s (%s)", getName().c_str(), path, newPath.c_str());
result = FS_ERROR_NOT_FOUND;
} else {
DEBUG_FUNCTION_LINE("[%s] Open file %s (%s) failed. errno %d ", getName().c_str(), path, newPath.c_str(), err);
if (err == EACCES) {
return FS_ERROR_PERMISSION_ERROR;
} else if (err == ENOENT) {
result = FS_ERROR_NOT_FOUND;
} else if (err == EEXIST) {
result = FS_ERROR_ALREADY_EXISTS;
} else if (err == EISDIR) {
result = FS_ERROR_NOT_FILE;
} else if (err == ENOTDIR) {
result = FS_ERROR_NOT_DIR;
} else if (err == ENFILE || err == EMFILE) {
result = FS_ERROR_MAX_FILES;
} else {
result = FS_ERROR_MEDIA_ERROR;
}
}
}
return result;
}
FSError FSWrapper::FSCloseFileWrapper(const FSFileHandle handle) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto fileHandle = getFileFromHandle(handle);
int real_fd = fileHandle->fd;
FSError result = FS_ERROR_OK;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Close %d (handle %08X)", getName().c_str(), real_fd, handle);
if (close(real_fd) != 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to close %d (handle %08X) ", getName().c_str(), real_fd, handle);
result = FS_ERROR_MEDIA_ERROR;
}
fileHandle->fd = -1;
return result;
}
bool FSWrapper::CheckFileShouldBeIgnored(std::string &path) {
auto asPath = std::filesystem::path(path);
if (starts_with_case_insensitive(asPath.filename().c_str(), deletePrefix)) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Ignore %s, filename starts with %s", getName().c_str(), path.c_str(), deletePrefix.c_str());
return true;
}
auto newDelPath = asPath.replace_filename(deletePrefix + asPath.filename().c_str());
struct stat buf {};
if (stat(newDelPath.c_str(), &buf) == 0) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Ignore %s, file %s exists", getName().c_str(), path.c_str(), newDelPath.c_str());
return true;
}
return false;
}
FSError FSWrapper::FSGetStatWrapper(const char *path, FSStat *stats) {
if (path == nullptr || stats == nullptr) {
DEBUG_FUNCTION_LINE_ERR("[%s] path was or stats nullptr", getName().c_str());
return FS_ERROR_INVALID_PARAM;
}
if (!IsPathToReplace(path)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto newPath = GetNewPath(path);
struct stat path_stat {};
if (pCheckIfDeleted && CheckFileShouldBeIgnored(newPath)) {
return static_cast<FSError>((FS_ERROR_NOT_FOUND & FS_ERROR_REAL_MASK) | FS_ERROR_FORCE_NO_FALLBACK);
}
FSError result = FS_ERROR_OK;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] stat of %s (%s)", getName().c_str(), path, newPath.c_str());
if (stat(newPath.c_str(), &path_stat) < 0) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Path %s (%s) not found ", getName().c_str(), path, newPath.c_str());
result = FS_ERROR_NOT_FOUND;
} else {
translate_stat(&path_stat, stats);
}
return result;
}
FSError FSWrapper::FSGetStatFileWrapper(const FSFileHandle handle, FSStat *stats) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto fileHandle = getFileFromHandle(handle);
int real_fd = fileHandle->fd;
struct stat path_stat {};
FSError result = FS_ERROR_OK;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] fstat of fd %d (FSFileHandle %08X)", getName().c_str(), real_fd, handle);
if (fstat(real_fd, &path_stat) < 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] fstat of fd %d (FSFileHandle %08X) failed", getName().c_str(), real_fd, handle);
result = FS_ERROR_MEDIA_ERROR;
} else {
translate_stat(&path_stat, stats);
}
return result;
}
FSError FSWrapper::FSReadFileWrapper(void *buffer, const uint32_t size, const uint32_t count, const FSFileHandle handle, [[maybe_unused]] uint32_t unk1) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (size * count == 0) {
return FS_ERROR_OK;
}
if (buffer == nullptr) {
DEBUG_FUNCTION_LINE_ERR("[%s] buffer is null but size * count is not 0 (It's: %d)", getName().c_str(), size * count);
return FS_ERROR_INVALID_BUFFER;
}
auto fileHandle = getFileFromHandle(handle);
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Read %u bytes of fd %08X (FSFileHandle %08X) to buffer %p", getName().c_str(), size * count, real_fd, handle, buffer);
int64_t read = readIntoBuffer(real_fd, buffer, size, count);
FSError result;
if (read < 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] Read %u bytes of fd %d (FSFileHandle %08X) failed", getName().c_str(), size * count, real_fd, handle);
auto err = errno;
if (err == EBADF || err == EROFS) {
return FS_ERROR_ACCESS_ERROR;
}
result = FS_ERROR_MEDIA_ERROR;
} else {
result = static_cast<FSError>(((uint32_t) read) / size);
}
return result;
}
FSError FSWrapper::FSReadFileWithPosWrapper(void *buffer, const uint32_t size, const uint32_t count, const uint32_t pos, const FSFileHandle handle, const int32_t unk1) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Read from with position.", getName().c_str());
FSError result;
if ((result = this->FSSetPosFileWrapper(handle, pos)) != FS_ERROR_OK) {
return result;
}
result = this->FSReadFileWrapper(buffer, size, count, handle, unk1);
return result;
}
FSError FSWrapper::FSSetPosFileWrapper(const FSFileHandle handle, const uint32_t pos) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto fileHandle = getFileFromHandle(handle);
FSError result = FS_ERROR_OK;
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] lseek fd %d (FSFileHandle %08X) SEEK_SET to position %08X", getName().c_str(), real_fd, handle, pos);
off_t newPos;
if ((newPos = lseek(real_fd, (off_t) pos, SEEK_SET)) != pos) {
DEBUG_FUNCTION_LINE_ERR("[%s] lseek fd %d (FSFileHandle %08X) to position %08X failed", getName().c_str(), real_fd, handle, pos);
if (newPos < 0) {
// TODO: read errno
}
result = FS_ERROR_MEDIA_ERROR;
} else {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] pos set to %u for fd %d (FSFileHandle %08X)", getName().c_str(), pos, real_fd, handle);
}
return result;
}
FSError FSWrapper::FSGetPosFileWrapper(const FSFileHandle handle, uint32_t *pos) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto fileHandle = getFileFromHandle(handle);
FSError result = FS_ERROR_OK;
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] lseek fd %08X (FSFileHandle %08X) to get current position for truncation", getName().c_str(), real_fd, handle);
off_t currentPos = lseek(real_fd, (off_t) 0, SEEK_CUR);
if (currentPos == -1) {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to get current position (res: %lld) of fd %08X (handle %08X) to check EoF", getName().c_str(), currentPos, real_fd, handle);
result = FS_ERROR_MEDIA_ERROR;
} else {
*pos = currentPos;
}
return result;
}
FSError FSWrapper::FSIsEofWrapper(const FSFileHandle handle) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
auto fileHandle = getFileFromHandle(handle);
FSError result;
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] lseek fd %08X (FSFileHandle %08X) to get current position for EOF detection", getName().c_str(), real_fd, handle);
off_t currentPos = lseek(real_fd, (off_t) 0, SEEK_CUR);
DEBUG_FUNCTION_LINE_VERBOSE("[%s] lseek fd %08X (FSFileHandle %08X) to get end position for EOF detection", getName().c_str(), real_fd, handle);
off_t endPos = lseek(real_fd, (off_t) 0, SEEK_END);
if (currentPos == -1 || endPos == -1) {
// TODO: check errno
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to get current position (res: %lld) or endPos (res: %lld) of fd %08X (handle %08X) to check EoF", getName().c_str(), currentPos, endPos, real_fd, handle);
result = FS_ERROR_MEDIA_ERROR;
} else if (currentPos == endPos) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] FSIsEof END for %d\n", getName().c_str(), real_fd);
result = FS_ERROR_END_OF_FILE;
} else {
lseek(real_fd, currentPos, SEEK_CUR);
DEBUG_FUNCTION_LINE_VERBOSE("[%s] FSIsEof OK for %d\n", getName().c_str(), real_fd);
result = FS_ERROR_OK;
}
return result;
}
FSError FSWrapper::FSTruncateFileWrapper(const FSFileHandle handle) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to truncate fd %d (handle %08X) but layer is not writeable", getName().c_str(), getFileFromHandle(handle)->fd, handle);
return FS_ERROR_ACCESS_ERROR;
}
auto fileHandle = getFileFromHandle(handle);
FSError result = FS_ERROR_OK;
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] lseek fd %08X (FSFileHandle %08X) to get current position for truncation", getName().c_str(), real_fd, handle);
off_t currentPos = lseek(real_fd, (off_t) 0, SEEK_CUR);
if (currentPos == -1) {
// TODO check errno
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to get current position of fd %08X (handle %08X) to truncate file", getName().c_str(), real_fd, handle);
result = FS_ERROR_MEDIA_ERROR;
} else {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Truncate fd %08X (FSFileHandle %08X) to %lld bytes ", getName().c_str(), real_fd, handle, currentPos);
if (ftruncate(real_fd, currentPos) < 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] ftruncate failed for fd %08X (FSFileHandle %08X) errno %d", getName().c_str(), real_fd, handle, errno);
result = FS_ERROR_MEDIA_ERROR;
}
}
return result;
}
FSError FSWrapper::FSWriteFileWrapper(const uint8_t *buffer, const uint32_t size, const uint32_t count, const FSFileHandle handle, [[maybe_unused]] uint32_t unk1) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (!pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to write to fd %d (handle %08X) but layer is not writeable", getName().c_str(), getFileFromHandle(handle)->fd, handle);
return FS_ERROR_ACCESS_ERROR;
}
auto fileHandle = getFileFromHandle(handle);
FSError result;
int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Write %u bytes to fd %08X (FSFileHandle %08X) from buffer %p", getName().c_str(), count * size, real_fd, handle, buffer);
auto writeRes = writeFromBuffer(real_fd, buffer, size, count);
if (writeRes < 0) {
auto err = errno;
DEBUG_FUNCTION_LINE_ERR("[%s] Write failed %u bytes to fd %08X (FSFileHandle %08X) from buffer %p errno %d", getName().c_str(), count * size, real_fd, handle, buffer, err);
if (err == EFBIG) {
result = FS_ERROR_FILE_TOO_BIG;
} else if (err == EACCES) {
result = FS_ERROR_ACCESS_ERROR;
} else {
result = FS_ERROR_MEDIA_ERROR;
}
} else {
result = static_cast<FSError>(static_cast<uint32_t>(writeRes) / size);
}
return result;
}
FSError FSWrapper::FSRemoveWrapper(const char *path) {
if (!IsPathToReplace(path)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (!pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to remove %s but layer is not writeable", getName().c_str(), path);
return FS_ERROR_PERMISSION_ERROR;
}
auto newPath = GetNewPath(path);
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Remove %s (%s)", getName().c_str(), path, newPath.c_str());
if (remove(newPath.c_str()) < 0) {
auto err = errno;
DEBUG_FUNCTION_LINE_ERR("[%s] Rename failed %s (%s) errno %d", getName().c_str(), path, newPath.c_str(), err);
if (err == ENOTDIR) {
return FS_ERROR_NOT_DIR;
} else if (err == EACCES) {
return FS_ERROR_ACCESS_ERROR;
} else if (err == EISDIR) {
return FS_ERROR_NOT_FILE;
} else if (err == EPERM) {
return FS_ERROR_PERMISSION_ERROR;
}
return FS_ERROR_MEDIA_ERROR;
}
return FS_ERROR_OK;
}
FSError FSWrapper::FSRenameWrapper(const char *oldPath, const char *newPath) {
if (!IsPathToReplace(oldPath) || !IsPathToReplace(newPath)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (!pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to rename %s to %s but layer is not writeable", getName().c_str(), oldPath, newPath);
return FS_ERROR_PERMISSION_ERROR;
}
auto oldPathRedirect = GetNewPath(oldPath);
auto newPathRedirect = GetNewPath(newPath);
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Rename %s (%s) -> %s (%s)", getName().c_str(), oldPath, oldPathRedirect.c_str(), newPath, newPathRedirect.c_str());
if (rename(oldPathRedirect.c_str(), newPathRedirect.c_str()) < 0) {
auto err = errno;
DEBUG_FUNCTION_LINE_ERR("[%s] Rename failed %s (%s) -> %s (%s). errno %d", getName().c_str(), oldPath, oldPathRedirect.c_str(), newPath, newPathRedirect.c_str(), err);
if (err == ENOTDIR) {
return FS_ERROR_NOT_DIR;
} else if (err == EACCES) {
return FS_ERROR_ACCESS_ERROR;
} else if (err == EISDIR) {
return FS_ERROR_NOT_FILE;
} else if (err == EPERM) {
return FS_ERROR_PERMISSION_ERROR;
}
return FS_ERROR_MEDIA_ERROR;
}
return FS_ERROR_OK;
}
FSError FSWrapper::FSFlushFileWrapper(const FSFileHandle handle) {
if (!isValidFileHandle(handle)) {
return FS_ERROR_FORCE_PARENT_LAYER;
}
if (!pIsWriteable) {
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Tried to fsync fd %d (handle %08X)) but layer is not writeable", getName().c_str(), getFileFromHandle(handle)->fd, handle);
return FS_ERROR_ACCESS_ERROR;
}
const auto fileHandle = getFileFromHandle(handle);
const int real_fd = fileHandle->fd;
DEBUG_FUNCTION_LINE_VERBOSE("[%s] fsync fd %08X (FSFileHandle %08X)", getName().c_str(), real_fd, handle);
FSError result = FS_ERROR_OK;
if (fsync(real_fd) < 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] fsync failed for fd %08X (FSFileHandle %08X)", getName().c_str(), real_fd, handle);
auto err = errno;
if (err == EBADF) {
result = FS_ERROR_INVALID_FILEHANDLE;
} else {
result = FS_ERROR_MEDIA_ERROR;
}
}
return result;
}
bool FSWrapper::IsFileModeAllowed(const char *mode) {
if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) {
return true;
}
if (pIsWriteable && (strcmp(mode, "r+") == 0 ||
strcmp(mode, "w") == 0 ||
strcmp(mode, "wb") == 0 ||
strcmp(mode, "w+") == 0 ||
strcmp(mode, "a") == 0 ||
strcmp(mode, "a+") == 0)) {
return true;
}
return false;
}
bool FSWrapper::IsPathToReplace(const std::string_view &path) {
return starts_with_case_insensitive(path, pPathToReplace);
}
std::string FSWrapper::GetNewPath(const std::string_view &path) const {
auto res = std::string(path);
SafeReplaceInString(res, this->pPathToReplace, this->pReplacePathWith);
std::ranges::replace(res, '\\', '/');
uint32_t length = res.size();
//! clear path of double slashes
for (uint32_t i = 1; i < length; ++i) {
if (res[i - 1] == '/' && res[i] == '/') {
res.erase(i, 1);
i--;
length--;
}
}
DEBUG_FUNCTION_LINE_VERBOSE("[%s] Redirect %.*s -> %s", getName().c_str(), int(path.length()), path.data(), res.c_str());
return res;
}
bool FSWrapper::isValidFileHandle(FSFileHandle handle) {
std::lock_guard lock(openFilesMutex);
return std::ranges::any_of(openFiles, [handle](auto &cur) { return cur->handle == handle; });
}
bool FSWrapper::isValidDirHandle(FSDirectoryHandle handle) {
std::lock_guard lock(openDirsMutex);
return std::ranges::any_of(openDirs, [handle](auto &cur) { return cur->handle == handle; });
}
std::shared_ptr<FileInfo> FSWrapper::getNewFileHandle() {
return make_shared_nothrow<FileInfo>();
}
std::shared_ptr<DirInfo> FSWrapper::getNewDirInfoHandle() {
return make_shared_nothrow<DirInfo>();
}
std::shared_ptr<FileInfo> FSWrapper::getFileFromHandle(const FSFileHandle handle) {
std::lock_guard lock(openFilesMutex);
for (auto &file : openFiles) {
if (file->handle == handle) {
return file;
}
}
DEBUG_FUNCTION_LINE_ERR("[%s] FileInfo for handle %08X was not found. isValidFileHandle check missing?", getName().c_str(), handle);
OSFatal("ContentRedirectionModule: Failed to find file handle");
return nullptr;
}
std::shared_ptr<DirInfoBase> FSWrapper::getDirFromHandle(const FSDirectoryHandle handle) {
std::lock_guard lock(openDirsMutex);
for (auto &dir : openDirs) {
if (dir->handle == handle) {
return dir;
}
}
DEBUG_FUNCTION_LINE_ERR("[%s] DirInfo for handle %08X was not found. isValidDirHandle check missing?", getName().c_str(), handle);
OSFatal("ContentRedirectionModule: Failed to find dir handle");
return nullptr;
}
void FSWrapper::addDirHandle(const std::shared_ptr<DirInfoBase> &dirHandle) {
std::lock_guard lock(openDirsMutex);
openDirs.push_back(dirHandle);
OSMemoryBarrier();
}
void FSWrapper::deleteDirHandle(FSDirectoryHandle handle) {
if (!remove_locked_first_if(openDirsMutex, openDirs, [handle](auto &cur) { return static_cast<FSDirectoryHandle>(cur->handle) == handle; })) {
DEBUG_FUNCTION_LINE_ERR("[%s] Delete failed because the handle %08X was not found", getName().c_str(), handle);
}
}
void FSWrapper::deleteFileHandle(FSFileHandle handle) {
if (!remove_locked_first_if(openFilesMutex, openFiles, [handle](auto &cur) { return static_cast<FSFileHandle>(cur->handle) == handle; })) {
DEBUG_FUNCTION_LINE_ERR("[%s] Delete failed because the handle %08X was not found", getName().c_str(), handle);
}
}
bool FSWrapper::SkipDeletedFilesInReadDir() {
return true;
}
std::shared_ptr<DirInfo> FSWrapper::getDirInfoFromHandle(const FSDirectoryHandle handle) {
auto dir = std::dynamic_pointer_cast<DirInfo>(getDirFromHandle(handle));
if (!dir) {
DEBUG_FUNCTION_LINE_ERR("[%s] dynamic_pointer_cast<DirInfoEx *>(%08X) failed", getName().c_str(), handle);
OSFatal("ContentRedirectionModule: dynamic_pointer_cast<DirInfoEx *> failed");
}
return dir;
}