diff options
| author | 2020-12-09 05:22:16 -0300 | |
|---|---|---|
| committer | 2020-12-09 05:42:03 -0300 | |
| commit | 532983437616e15539b448594a360c138995e282 (patch) | |
| tree | 2537197bb02dfce45dbc55933cabfa06a1b5bc25 | |
| parent | common/file_util: Succeed on CreateDir when the directory exists (diff) | |
| download | yuzu-532983437616e15539b448594a360c138995e282.tar.gz yuzu-532983437616e15539b448594a360c138995e282.tar.xz yuzu-532983437616e15539b448594a360c138995e282.zip | |
common/file_util: Fix and deprecate CreateFullPath, add CreateDirs
Fix CreateFullPath to have its intended previous behavior (whatever
that was), and deprecate it in favor of the new CreateDirs function.
Unlike CreateDir, CreateDirs is marked as [[nodiscard]] to avoid new
code ignoring its result value.
| -rw-r--r-- | src/common/file_util.cpp | 25 | ||||
| -rw-r--r-- | src/common/file_util.h | 10 |
2 files changed, 31 insertions, 4 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index c4d738bb6..7752c0421 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -114,20 +114,41 @@ bool CreateDir(const fs::path& path) { | |||
| 114 | return true; | 114 | return true; |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | bool CreateFullPath(const fs::path& path) { | 117 | bool CreateDirs(const fs::path& path) { |
| 118 | LOG_TRACE(Common_Filesystem, "path {}", path.string()); | 118 | LOG_TRACE(Common_Filesystem, "path {}", path.string()); |
| 119 | 119 | ||
| 120 | if (Exists(path)) { | ||
| 121 | LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); | ||
| 122 | return true; | ||
| 123 | } | ||
| 124 | |||
| 120 | std::error_code ec; | 125 | std::error_code ec; |
| 121 | const bool success = fs::create_directories(path, ec); | 126 | const bool success = fs::create_directories(path, ec); |
| 122 | 127 | ||
| 123 | if (!success) { | 128 | if (!success) { |
| 124 | LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); | 129 | LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message()); |
| 125 | return false; | 130 | return false; |
| 126 | } | 131 | } |
| 127 | 132 | ||
| 128 | return true; | 133 | return true; |
| 129 | } | 134 | } |
| 130 | 135 | ||
| 136 | bool CreateFullPath(const fs::path& path) { | ||
| 137 | LOG_TRACE(Common_Filesystem, "path {}", path); | ||
| 138 | |||
| 139 | // Removes trailing slashes and turns any '\' into '/' | ||
| 140 | const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); | ||
| 141 | |||
| 142 | if (new_path.rfind('.') == std::string::npos) { | ||
| 143 | // The path is a directory | ||
| 144 | return CreateDirs(new_path); | ||
| 145 | } else { | ||
| 146 | // The path is a file | ||
| 147 | // Creates directory preceding the last '/' | ||
| 148 | return CreateDirs(new_path.substr(0, new_path.rfind('/'))); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 131 | bool Rename(const fs::path& src, const fs::path& dst) { | 152 | bool Rename(const fs::path& src, const fs::path& dst) { |
| 132 | LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); | 153 | LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); |
| 133 | 154 | ||
diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..cf006cc9d 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -54,8 +54,14 @@ enum class UserPath { | |||
| 54 | // Returns true if successful, or path already exists. | 54 | // Returns true if successful, or path already exists. |
| 55 | bool CreateDir(const std::filesystem::path& path); | 55 | bool CreateDir(const std::filesystem::path& path); |
| 56 | 56 | ||
| 57 | // Creates the full path of path. Returns true on success | 57 | // Create all directories in path |
| 58 | bool CreateFullPath(const std::filesystem::path& path); | 58 | // Returns true if successful, or path already exists. |
| 59 | [[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs( | ||
| 60 | const std::filesystem::path& path); | ||
| 61 | |||
| 62 | // Creates directories in path. Returns true on success. | ||
| 63 | [[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath( | ||
| 64 | const std::filesystem::path& path); | ||
| 59 | 65 | ||
| 60 | // Deletes a given file at the path. | 66 | // Deletes a given file at the path. |
| 61 | // This will also delete empty directories. | 67 | // This will also delete empty directories. |