diff options
| author | 2020-12-09 03:47:21 -0300 | |
|---|---|---|
| committer | 2020-12-09 03:47:21 -0300 | |
| commit | ce5fcb6bb2c358b0251a2ce87945bda52789a76d (patch) | |
| tree | 9946623b410e4400c4c418a12a3052111042ac6d /src/common/file_util.h | |
| parent | Merge pull request #5166 from lioncash/log-cast (diff) | |
| parent | file_util: Migrate remaining file handling functions over to std::filesystem (diff) | |
| download | yuzu-ce5fcb6bb2c358b0251a2ce87945bda52789a76d.tar.gz yuzu-ce5fcb6bb2c358b0251a2ce87945bda52789a76d.tar.xz yuzu-ce5fcb6bb2c358b0251a2ce87945bda52789a76d.zip | |
Merge pull request #5173 from lioncash/common-fs
common/file_util: Make use of std::filesystem
Diffstat (limited to 'src/common/file_util.h')
| -rw-r--r-- | src/common/file_util.h | 69 |
1 files changed, 23 insertions, 46 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index 840cde2a6..c2ee7ca27 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstdio> | 8 | #include <cstdio> |
| 9 | #include <filesystem> | ||
| 9 | #include <fstream> | 10 | #include <fstream> |
| 10 | #include <functional> | 11 | #include <functional> |
| 11 | #include <limits> | 12 | #include <limits> |
| @@ -38,48 +39,34 @@ enum class UserPath { | |||
| 38 | UserDir, | 39 | UserDir, |
| 39 | }; | 40 | }; |
| 40 | 41 | ||
| 41 | // FileSystem tree node/ | 42 | // Returns true if the path exists |
| 42 | struct FSTEntry { | 43 | [[nodiscard]] bool Exists(const std::filesystem::path& path); |
| 43 | bool isDirectory; | ||
| 44 | u64 size; // file length or number of entries from children | ||
| 45 | std::string physicalName; // name on disk | ||
| 46 | std::string virtualName; // name in FST names table | ||
| 47 | std::vector<FSTEntry> children; | ||
| 48 | }; | ||
| 49 | |||
| 50 | // Returns true if file filename exists | ||
| 51 | [[nodiscard]] bool Exists(const std::string& filename); | ||
| 52 | 44 | ||
| 53 | // Returns true if filename is a directory | 45 | // Returns true if path is a directory |
| 54 | [[nodiscard]] bool IsDirectory(const std::string& filename); | 46 | [[nodiscard]] bool IsDirectory(const std::filesystem::path& path); |
| 55 | 47 | ||
| 56 | // Returns the size of filename (64bit) | 48 | // Returns the size of filename (64bit) |
| 57 | [[nodiscard]] u64 GetSize(const std::string& filename); | 49 | [[nodiscard]] u64 GetSize(const std::filesystem::path& path); |
| 58 | |||
| 59 | // Overloaded GetSize, accepts file descriptor | ||
| 60 | [[nodiscard]] u64 GetSize(int fd); | ||
| 61 | 50 | ||
| 62 | // Overloaded GetSize, accepts FILE* | 51 | // Overloaded GetSize, accepts FILE* |
| 63 | [[nodiscard]] u64 GetSize(FILE* f); | 52 | [[nodiscard]] u64 GetSize(FILE* f); |
| 64 | 53 | ||
| 65 | // Returns true if successful, or path already exists. | 54 | // Returns true if successful, or path already exists. |
| 66 | bool CreateDir(const std::string& filename); | 55 | bool CreateDir(const std::filesystem::path& path); |
| 67 | |||
| 68 | // Creates the full path of fullPath returns true on success | ||
| 69 | bool CreateFullPath(const std::string& fullPath); | ||
| 70 | 56 | ||
| 71 | // Deletes a given filename, return true on success | 57 | // Creates the full path of path. Returns true on success |
| 72 | // Doesn't supports deleting a directory | 58 | bool CreateFullPath(const std::filesystem::path& path); |
| 73 | bool Delete(const std::string& filename); | ||
| 74 | 59 | ||
| 75 | // Deletes a directory filename, returns true on success | 60 | // Deletes a given file at the path. |
| 76 | bool DeleteDir(const std::string& filename); | 61 | // This will also delete empty directories. |
| 62 | // Return true on success | ||
| 63 | bool Delete(const std::filesystem::path& path); | ||
| 77 | 64 | ||
| 78 | // renames file srcFilename to destFilename, returns true on success | 65 | // Renames file src to dst, returns true on success |
| 79 | bool Rename(const std::string& srcFilename, const std::string& destFilename); | 66 | bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); |
| 80 | 67 | ||
| 81 | // copies file srcFilename to destFilename, returns true on success | 68 | // copies file src to dst, returns true on success |
| 82 | bool Copy(const std::string& srcFilename, const std::string& destFilename); | 69 | bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); |
| 83 | 70 | ||
| 84 | // creates an empty file filename, returns true on success | 71 | // creates an empty file filename, returns true on success |
| 85 | bool CreateEmptyFile(const std::string& filename); | 72 | bool CreateEmptyFile(const std::string& filename); |
| @@ -106,27 +93,17 @@ using DirectoryEntryCallable = std::function<bool( | |||
| 106 | bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, | 93 | bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, |
| 107 | DirectoryEntryCallable callback); | 94 | DirectoryEntryCallable callback); |
| 108 | 95 | ||
| 109 | /** | 96 | // Deletes the given path and anything under it. Returns true on success. |
| 110 | * Scans the directory tree, storing the results. | 97 | bool DeleteDirRecursively(const std::filesystem::path& path); |
| 111 | * @param directory the parent directory to start scanning from | ||
| 112 | * @param parent_entry FSTEntry where the filesystem tree results will be stored. | ||
| 113 | * @param recursion Number of children directories to read before giving up. | ||
| 114 | * @return the total number of files/directories found | ||
| 115 | */ | ||
| 116 | u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, | ||
| 117 | unsigned int recursion = 0); | ||
| 118 | |||
| 119 | // deletes the given directory and anything under it. Returns true on success. | ||
| 120 | bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); | ||
| 121 | 98 | ||
| 122 | // Returns the current directory | 99 | // Returns the current directory |
| 123 | [[nodiscard]] std::optional<std::string> GetCurrentDir(); | 100 | [[nodiscard]] std::optional<std::filesystem::path> GetCurrentDir(); |
| 124 | 101 | ||
| 125 | // Create directory and copy contents (does not overwrite existing files) | 102 | // Create directory and copy contents (does not overwrite existing files) |
| 126 | void CopyDir(const std::string& source_path, const std::string& dest_path); | 103 | void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); |
| 127 | 104 | ||
| 128 | // Set the current directory to given directory | 105 | // Set the current directory to given path |
| 129 | bool SetCurrentDir(const std::string& directory); | 106 | bool SetCurrentDir(const std::filesystem::path& path); |
| 130 | 107 | ||
| 131 | // Returns a pointer to a string with a yuzu data dir in the user's home | 108 | // Returns a pointer to a string with a yuzu data dir in the user's home |
| 132 | // directory. To be used in "multi-user" mode (that is, installed). | 109 | // directory. To be used in "multi-user" mode (that is, installed). |