aboutsummaryrefslogtreecommitdiff
path: root/src/common/fs/fs.cpp
blob: 6d66c926dc5319d111e27beaebb68a3b0c8412d1 (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/fs/file.h"
#include "common/fs/fs.h"
#ifdef ANDROID
#include "common/fs/fs_android.h"
#endif
#include "common/fs/path_util.h"
#include "common/logging/log.h"

namespace Common::FS {

namespace fs = std::filesystem;

// File Operations

bool NewFile(const fs::path& path, u64 size) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path.parent_path())) {
        LOG_ERROR(Common_Filesystem, "Parent directory of path={} does not exist",
                  PathToUTF8String(path));
        return false;
    }

    if (Exists(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} exists", PathToUTF8String(path));
        return false;
    }

    IOFile io_file{path, FileAccessMode::Write};

    if (!io_file.IsOpen()) {
        LOG_ERROR(Common_Filesystem, "Failed to create a file at path={}", PathToUTF8String(path));
        return false;
    }

    if (!io_file.SetSize(size)) {
        LOG_ERROR(Common_Filesystem, "Failed to resize the file at path={} to size={}",
                  PathToUTF8String(path), size);
        return false;
    }

    io_file.Close();

    LOG_DEBUG(Common_Filesystem, "Successfully created a file at path={} with size={}",
              PathToUTF8String(path), size);

    return true;
}

bool RemoveFile(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return true;
    }

    if (!IsFile(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a file",
                  PathToUTF8String(path));
        return false;
    }

    std::error_code ec;

    fs::remove(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to remove the file at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully removed the file at path={}",
              PathToUTF8String(path));

    return true;
}

bool RenameFile(const fs::path& old_path, const fs::path& new_path) {
    if (!ValidatePath(old_path) || !ValidatePath(new_path)) {
        LOG_ERROR(Common_Filesystem,
                  "One or both input path(s) is not valid, old_path={}, new_path={}",
                  PathToUTF8String(old_path), PathToUTF8String(new_path));
        return false;
    }

    if (!Exists(old_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at old_path={} does not exist",
                  PathToUTF8String(old_path));
        return false;
    }

    if (!IsFile(old_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at old_path={} is not a file",
                  PathToUTF8String(old_path));
        return false;
    }

    if (Exists(new_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at new_path={} exists",
                  PathToUTF8String(new_path));
        return false;
    }

    std::error_code ec;

    fs::rename(old_path, new_path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to rename the file from old_path={} to new_path={}, ec_message={}",
                  PathToUTF8String(old_path), PathToUTF8String(new_path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully renamed the file from old_path={} to new_path={}",
              PathToUTF8String(old_path), PathToUTF8String(new_path));

    return true;
}

std::shared_ptr<IOFile> FileOpen(const fs::path& path, FileAccessMode mode, FileType type,
                                 FileShareFlag flag) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return nullptr;
    }

    if (Exists(path) && !IsFile(path)) {
        LOG_ERROR(Common_Filesystem,
                  "Filesystem object at path={} exists and is not a regular file",
                  PathToUTF8String(path));
        return nullptr;
    }

    auto io_file = std::make_shared<IOFile>(path, mode, type, flag);

    if (!io_file->IsOpen()) {
        io_file.reset();

        LOG_ERROR(Common_Filesystem,
                  "Failed to open the file at path={} with mode={}, type={}, flag={}",
                  PathToUTF8String(path), mode, type, flag);

        return nullptr;
    }

    LOG_DEBUG(Common_Filesystem,
              "Successfully opened the file at path={} with mode={}, type={}, flag={}",
              PathToUTF8String(path), mode, type, flag);

    return io_file;
}

// Directory Operations

bool CreateDir(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path.parent_path())) {
        LOG_ERROR(Common_Filesystem, "Parent directory of path={} does not exist",
                  PathToUTF8String(path));
        return false;
    }

    if (IsDir(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} exists and is a directory",
                  PathToUTF8String(path));
        return true;
    }

    std::error_code ec;

    fs::create_directory(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to create the directory at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully created the directory at path={}",
              PathToUTF8String(path));

    return true;
}

bool CreateDirs(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (IsDir(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} exists and is a directory",
                  PathToUTF8String(path));
        return true;
    }

    std::error_code ec;

    fs::create_directories(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to create the directories at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully created the directories at path={}",
              PathToUTF8String(path));

    return true;
}

bool CreateParentDir(const fs::path& path) {
    return CreateDir(path.parent_path());
}

bool CreateParentDirs(const fs::path& path) {
    return CreateDirs(path.parent_path());
}

bool RemoveDir(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return true;
    }

    if (!IsDir(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a directory",
                  PathToUTF8String(path));
        return false;
    }

    std::error_code ec;

    fs::remove(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to remove the directory at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully removed the directory at path={}",
              PathToUTF8String(path));

    return true;
}

bool RemoveDirRecursively(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return true;
    }

    if (!IsDir(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a directory",
                  PathToUTF8String(path));
        return false;
    }

    std::error_code ec;

    fs::remove_all(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to remove the directory and its contents at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully removed the directory and its contents at path={}",
              PathToUTF8String(path));

    return true;
}

bool RemoveDirContentsRecursively(const fs::path& path) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return false;
    }

    if (!Exists(path)) {
        LOG_DEBUG(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return true;
    }

    if (!IsDir(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a directory",
                  PathToUTF8String(path));
        return false;
    }

    std::error_code ec;

    // TODO (Morph): Replace this with recursive_directory_iterator once it's fixed in MSVC.
    for (const auto& entry : fs::directory_iterator(path, ec)) {
        if (ec) {
            LOG_ERROR(Common_Filesystem,
                      "Failed to completely enumerate the directory at path={}, ec_message={}",
                      PathToUTF8String(path), ec.message());
            break;
        }

        fs::remove(entry.path(), ec);

        if (ec) {
            LOG_ERROR(Common_Filesystem,
                      "Failed to remove the filesystem object at path={}, ec_message={}",
                      PathToUTF8String(entry.path()), ec.message());
            break;
        }

        // TODO (Morph): Remove this when MSVC fixes recursive_directory_iterator.
        // recursive_directory_iterator throws an exception despite passing in a std::error_code.
        if (entry.status().type() == fs::file_type::directory) {
            return RemoveDirContentsRecursively(entry.path());
        }
    }

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to remove all the contents of the directory at path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem,
              "Successfully removed all the contents of the directory at path={}",
              PathToUTF8String(path));

    return true;
}

bool RenameDir(const fs::path& old_path, const fs::path& new_path) {
    if (!ValidatePath(old_path) || !ValidatePath(new_path)) {
        LOG_ERROR(Common_Filesystem,
                  "One or both input path(s) is not valid, old_path={}, new_path={}",
                  PathToUTF8String(old_path), PathToUTF8String(new_path));
        return false;
    }

    if (!Exists(old_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at old_path={} does not exist",
                  PathToUTF8String(old_path));
        return false;
    }

    if (!IsDir(old_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at old_path={} is not a directory",
                  PathToUTF8String(old_path));
        return false;
    }

    if (Exists(new_path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at new_path={} exists",
                  PathToUTF8String(new_path));
        return false;
    }

    std::error_code ec;

    fs::rename(old_path, new_path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to rename the file from old_path={} to new_path={}, ec_message={}",
                  PathToUTF8String(old_path), PathToUTF8String(new_path), ec.message());
        return false;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully renamed the file from old_path={} to new_path={}",
              PathToUTF8String(old_path), PathToUTF8String(new_path));

    return true;
}

void IterateDirEntries(const std::filesystem::path& path, const DirEntryCallable& callback,
                       DirEntryFilter filter) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return;
    }

    if (!Exists(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return;
    }

    if (!IsDir(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a directory",
                  PathToUTF8String(path));
        return;
    }

    bool callback_error = false;

    std::error_code ec;

    for (const auto& entry : fs::directory_iterator(path, ec)) {
        if (ec) {
            break;
        }

        if (True(filter & DirEntryFilter::File) &&
            entry.status().type() == fs::file_type::regular) {
            if (!callback(entry.path())) {
                callback_error = true;
                break;
            }
        }

        if (True(filter & DirEntryFilter::Directory) &&
            entry.status().type() == fs::file_type::directory) {
            if (!callback(entry.path())) {
                callback_error = true;
                break;
            }
        }
    }

    if (callback_error || ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to visit all the directory entries of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully visited all the directory entries of path={}",
              PathToUTF8String(path));
}

void IterateDirEntriesRecursively(const std::filesystem::path& path,
                                  const DirEntryCallable& callback, DirEntryFilter filter) {
    if (!ValidatePath(path)) {
        LOG_ERROR(Common_Filesystem, "Input path is not valid, path={}", PathToUTF8String(path));
        return;
    }

    if (!Exists(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} does not exist",
                  PathToUTF8String(path));
        return;
    }

    if (!IsDir(path)) {
        LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a directory",
                  PathToUTF8String(path));
        return;
    }

    bool callback_error = false;

    std::error_code ec;

    // TODO (Morph): Replace this with recursive_directory_iterator once it's fixed in MSVC.
    for (const auto& entry : fs::directory_iterator(path, ec)) {
        if (ec) {
            break;
        }

        if (True(filter & DirEntryFilter::File) &&
            entry.status().type() == fs::file_type::regular) {
            if (!callback(entry.path())) {
                callback_error = true;
                break;
            }
        }

        if (True(filter & DirEntryFilter::Directory) &&
            entry.status().type() == fs::file_type::directory) {
            if (!callback(entry.path())) {
                callback_error = true;
                break;
            }
        }

        // TODO (Morph): Remove this when MSVC fixes recursive_directory_iterator.
        // recursive_directory_iterator throws an exception despite passing in a std::error_code.
        if (entry.status().type() == fs::file_type::directory) {
            IterateDirEntriesRecursively(entry.path(), callback, filter);
        }
    }

    if (callback_error || ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to visit all the directory entries of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return;
    }

    LOG_DEBUG(Common_Filesystem, "Successfully visited all the directory entries of path={}",
              PathToUTF8String(path));
}

// Generic Filesystem Operations

bool Exists(const fs::path& path) {
#ifdef ANDROID
    if (Android::IsContentUri(path)) {
        return Android::Exists(path);
    } else {
        return fs::exists(path);
    }
#else
    return fs::exists(path);
#endif
}

bool IsFile(const fs::path& path) {
#ifdef ANDROID
    if (Android::IsContentUri(path)) {
        return !Android::IsDirectory(path);
    } else {
        return fs::is_regular_file(path);
    }
#else
    return fs::is_regular_file(path);
#endif
}

bool IsDir(const fs::path& path) {
#ifdef ANDROID
    if (Android::IsContentUri(path)) {
        return Android::IsDirectory(path);
    } else {
        return fs::is_directory(path);
    }
#else
    return fs::is_directory(path);
#endif
}

fs::path GetCurrentDir() {
    std::error_code ec;

    const auto current_path = fs::current_path(ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to get the current path, ec_message={}", ec.message());
        return {};
    }

    return current_path;
}

bool SetCurrentDir(const fs::path& path) {
    std::error_code ec;

    fs::current_path(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to set the current path to path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return false;
    }

    return true;
}

fs::file_type GetEntryType(const fs::path& path) {
    std::error_code ec;

    const auto file_status = fs::status(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to retrieve the entry type of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return fs::file_type::not_found;
    }

    return file_status.type();
}

u64 GetSize(const fs::path& path) {
    std::error_code ec;

    const auto file_size = fs::file_size(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem, "Failed to retrieve the file size of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return 0;
    }

    return file_size;
}

u64 GetFreeSpaceSize(const fs::path& path) {
    std::error_code ec;

    const auto space_info = fs::space(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to retrieve the available free space of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return 0;
    }

    return space_info.free;
}

u64 GetTotalSpaceSize(const fs::path& path) {
    std::error_code ec;

    const auto space_info = fs::space(path, ec);

    if (ec) {
        LOG_ERROR(Common_Filesystem,
                  "Failed to retrieve the total capacity of path={}, ec_message={}",
                  PathToUTF8String(path), ec.message());
        return 0;
    }

    return space_info.capacity;
}

} // namespace Common::FS