diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/file_util.h | 69 |
1 files changed, 46 insertions, 23 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..840cde2a6 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -6,7 +6,6 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstdio> | 8 | #include <cstdio> |
| 9 | #include <filesystem> | ||
| 10 | #include <fstream> | 9 | #include <fstream> |
| 11 | #include <functional> | 10 | #include <functional> |
| 12 | #include <limits> | 11 | #include <limits> |
| @@ -39,34 +38,48 @@ enum class UserPath { | |||
| 39 | UserDir, | 38 | UserDir, |
| 40 | }; | 39 | }; |
| 41 | 40 | ||
| 42 | // Returns true if the path exists | 41 | // FileSystem tree node/ |
| 43 | [[nodiscard]] bool Exists(const std::filesystem::path& path); | 42 | struct FSTEntry { |
| 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); | ||
| 44 | 52 | ||
| 45 | // Returns true if path is a directory | 53 | // Returns true if filename is a directory |
| 46 | [[nodiscard]] bool IsDirectory(const std::filesystem::path& path); | 54 | [[nodiscard]] bool IsDirectory(const std::string& filename); |
| 47 | 55 | ||
| 48 | // Returns the size of filename (64bit) | 56 | // Returns the size of filename (64bit) |
| 49 | [[nodiscard]] u64 GetSize(const std::filesystem::path& path); | 57 | [[nodiscard]] u64 GetSize(const std::string& filename); |
| 58 | |||
| 59 | // Overloaded GetSize, accepts file descriptor | ||
| 60 | [[nodiscard]] u64 GetSize(int fd); | ||
| 50 | 61 | ||
| 51 | // Overloaded GetSize, accepts FILE* | 62 | // Overloaded GetSize, accepts FILE* |
| 52 | [[nodiscard]] u64 GetSize(FILE* f); | 63 | [[nodiscard]] u64 GetSize(FILE* f); |
| 53 | 64 | ||
| 54 | // Returns true if successful, or path already exists. | 65 | // Returns true if successful, or path already exists. |
| 55 | bool CreateDir(const std::filesystem::path& path); | 66 | bool CreateDir(const std::string& filename); |
| 67 | |||
| 68 | // Creates the full path of fullPath returns true on success | ||
| 69 | bool CreateFullPath(const std::string& fullPath); | ||
| 56 | 70 | ||
| 57 | // Creates the full path of path. Returns true on success | 71 | // Deletes a given filename, return true on success |
| 58 | bool CreateFullPath(const std::filesystem::path& path); | 72 | // Doesn't supports deleting a directory |
| 73 | bool Delete(const std::string& filename); | ||
| 59 | 74 | ||
| 60 | // Deletes a given file at the path. | 75 | // Deletes a directory filename, returns true on success |
| 61 | // This will also delete empty directories. | 76 | bool DeleteDir(const std::string& filename); |
| 62 | // Return true on success | ||
| 63 | bool Delete(const std::filesystem::path& path); | ||
| 64 | 77 | ||
| 65 | // Renames file src to dst, returns true on success | 78 | // renames file srcFilename to destFilename, returns true on success |
| 66 | bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); | 79 | bool Rename(const std::string& srcFilename, const std::string& destFilename); |
| 67 | 80 | ||
| 68 | // copies file src to dst, returns true on success | 81 | // copies file srcFilename to destFilename, returns true on success |
| 69 | bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); | 82 | bool Copy(const std::string& srcFilename, const std::string& destFilename); |
| 70 | 83 | ||
| 71 | // creates an empty file filename, returns true on success | 84 | // creates an empty file filename, returns true on success |
| 72 | bool CreateEmptyFile(const std::string& filename); | 85 | bool CreateEmptyFile(const std::string& filename); |
| @@ -93,17 +106,27 @@ using DirectoryEntryCallable = std::function<bool( | |||
| 93 | bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, | 106 | bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, |
| 94 | DirectoryEntryCallable callback); | 107 | DirectoryEntryCallable callback); |
| 95 | 108 | ||
| 96 | // Deletes the given path and anything under it. Returns true on success. | 109 | /** |
| 97 | bool DeleteDirRecursively(const std::filesystem::path& path); | 110 | * Scans the directory tree, storing the results. |
| 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); | ||
| 98 | 121 | ||
| 99 | // Returns the current directory | 122 | // Returns the current directory |
| 100 | [[nodiscard]] std::optional<std::filesystem::path> GetCurrentDir(); | 123 | [[nodiscard]] std::optional<std::string> GetCurrentDir(); |
| 101 | 124 | ||
| 102 | // Create directory and copy contents (does not overwrite existing files) | 125 | // Create directory and copy contents (does not overwrite existing files) |
| 103 | void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); | 126 | void CopyDir(const std::string& source_path, const std::string& dest_path); |
| 104 | 127 | ||
| 105 | // Set the current directory to given path | 128 | // Set the current directory to given directory |
| 106 | bool SetCurrentDir(const std::filesystem::path& path); | 129 | bool SetCurrentDir(const std::string& directory); |
| 107 | 130 | ||
| 108 | // Returns a pointer to a string with a yuzu data dir in the user's home | 131 | // Returns a pointer to a string with a yuzu data dir in the user's home |
| 109 | // directory. To be used in "multi-user" mode (that is, installed). | 132 | // directory. To be used in "multi-user" mode (that is, installed). |