diff options
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 49 |
1 files changed, 13 insertions, 36 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index aecb66c32..2d9374783 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -78,16 +78,17 @@ namespace FileUtil { | |||
| 78 | // Remove any ending forward slashes from directory paths | 78 | // Remove any ending forward slashes from directory paths |
| 79 | // Modifies argument. | 79 | // Modifies argument. |
| 80 | static void StripTailDirSlashes(std::string& fname) { | 80 | static void StripTailDirSlashes(std::string& fname) { |
| 81 | if (fname.length() > 1) { | 81 | if (fname.length() <= 1) { |
| 82 | std::size_t i = fname.length(); | 82 | return; |
| 83 | while (i > 0 && fname[i - 1] == DIR_SEP_CHR) | 83 | } |
| 84 | --i; | 84 | |
| 85 | fname.resize(i); | 85 | std::size_t i = fname.length(); |
| 86 | while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { | ||
| 87 | --i; | ||
| 86 | } | 88 | } |
| 87 | return; | 89 | fname.resize(i); |
| 88 | } | 90 | } |
| 89 | 91 | ||
| 90 | // Returns true if file filename exists | ||
| 91 | bool Exists(const std::string& filename) { | 92 | bool Exists(const std::string& filename) { |
| 92 | struct stat file_info; | 93 | struct stat file_info; |
| 93 | 94 | ||
| @@ -107,7 +108,6 @@ bool Exists(const std::string& filename) { | |||
| 107 | return (result == 0); | 108 | return (result == 0); |
| 108 | } | 109 | } |
| 109 | 110 | ||
| 110 | // Returns true if filename is a directory | ||
| 111 | bool IsDirectory(const std::string& filename) { | 111 | bool IsDirectory(const std::string& filename) { |
| 112 | struct stat file_info; | 112 | struct stat file_info; |
| 113 | 113 | ||
| @@ -132,8 +132,6 @@ bool IsDirectory(const std::string& filename) { | |||
| 132 | return S_ISDIR(file_info.st_mode); | 132 | return S_ISDIR(file_info.st_mode); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | // Deletes a given filename, return true on success | ||
| 136 | // Doesn't supports deleting a directory | ||
| 137 | bool Delete(const std::string& filename) { | 135 | bool Delete(const std::string& filename) { |
| 138 | LOG_TRACE(Common_Filesystem, "file {}", filename); | 136 | LOG_TRACE(Common_Filesystem, "file {}", filename); |
| 139 | 137 | ||
| @@ -165,7 +163,6 @@ bool Delete(const std::string& filename) { | |||
| 165 | return true; | 163 | return true; |
| 166 | } | 164 | } |
| 167 | 165 | ||
| 168 | // Returns true if successful, or path already exists. | ||
| 169 | bool CreateDir(const std::string& path) { | 166 | bool CreateDir(const std::string& path) { |
| 170 | LOG_TRACE(Common_Filesystem, "directory {}", path); | 167 | LOG_TRACE(Common_Filesystem, "directory {}", path); |
| 171 | #ifdef _WIN32 | 168 | #ifdef _WIN32 |
| @@ -194,7 +191,6 @@ bool CreateDir(const std::string& path) { | |||
| 194 | #endif | 191 | #endif |
| 195 | } | 192 | } |
| 196 | 193 | ||
| 197 | // Creates the full path of fullPath returns true on success | ||
| 198 | bool CreateFullPath(const std::string& fullPath) { | 194 | bool CreateFullPath(const std::string& fullPath) { |
| 199 | int panicCounter = 100; | 195 | int panicCounter = 100; |
| 200 | LOG_TRACE(Common_Filesystem, "path {}", fullPath); | 196 | LOG_TRACE(Common_Filesystem, "path {}", fullPath); |
| @@ -230,7 +226,6 @@ bool CreateFullPath(const std::string& fullPath) { | |||
| 230 | } | 226 | } |
| 231 | } | 227 | } |
| 232 | 228 | ||
| 233 | // Deletes a directory filename, returns true on success | ||
| 234 | bool DeleteDir(const std::string& filename) { | 229 | bool DeleteDir(const std::string& filename) { |
| 235 | LOG_TRACE(Common_Filesystem, "directory {}", filename); | 230 | LOG_TRACE(Common_Filesystem, "directory {}", filename); |
| 236 | 231 | ||
| @@ -252,7 +247,6 @@ bool DeleteDir(const std::string& filename) { | |||
| 252 | return false; | 247 | return false; |
| 253 | } | 248 | } |
| 254 | 249 | ||
| 255 | // renames file srcFilename to destFilename, returns true on success | ||
| 256 | bool Rename(const std::string& srcFilename, const std::string& destFilename) { | 250 | bool Rename(const std::string& srcFilename, const std::string& destFilename) { |
| 257 | LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); | 251 | LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); |
| 258 | #ifdef _WIN32 | 252 | #ifdef _WIN32 |
| @@ -268,7 +262,6 @@ bool Rename(const std::string& srcFilename, const std::string& destFilename) { | |||
| 268 | return false; | 262 | return false; |
| 269 | } | 263 | } |
| 270 | 264 | ||
| 271 | // copies file srcFilename to destFilename, returns true on success | ||
| 272 | bool Copy(const std::string& srcFilename, const std::string& destFilename) { | 265 | bool Copy(const std::string& srcFilename, const std::string& destFilename) { |
| 273 | LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); | 266 | LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); |
| 274 | #ifdef _WIN32 | 267 | #ifdef _WIN32 |
| @@ -324,7 +317,6 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename) { | |||
| 324 | #endif | 317 | #endif |
| 325 | } | 318 | } |
| 326 | 319 | ||
| 327 | // Returns the size of filename (64bit) | ||
| 328 | u64 GetSize(const std::string& filename) { | 320 | u64 GetSize(const std::string& filename) { |
| 329 | if (!Exists(filename)) { | 321 | if (!Exists(filename)) { |
| 330 | LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); | 322 | LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); |
| @@ -351,7 +343,6 @@ u64 GetSize(const std::string& filename) { | |||
| 351 | return 0; | 343 | return 0; |
| 352 | } | 344 | } |
| 353 | 345 | ||
| 354 | // Overloaded GetSize, accepts file descriptor | ||
| 355 | u64 GetSize(const int fd) { | 346 | u64 GetSize(const int fd) { |
| 356 | struct stat buf; | 347 | struct stat buf; |
| 357 | if (fstat(fd, &buf) != 0) { | 348 | if (fstat(fd, &buf) != 0) { |
| @@ -361,7 +352,6 @@ u64 GetSize(const int fd) { | |||
| 361 | return buf.st_size; | 352 | return buf.st_size; |
| 362 | } | 353 | } |
| 363 | 354 | ||
| 364 | // Overloaded GetSize, accepts FILE* | ||
| 365 | u64 GetSize(FILE* f) { | 355 | u64 GetSize(FILE* f) { |
| 366 | // can't use off_t here because it can be 32-bit | 356 | // can't use off_t here because it can be 32-bit |
| 367 | u64 pos = ftello(f); | 357 | u64 pos = ftello(f); |
| @@ -377,7 +367,6 @@ u64 GetSize(FILE* f) { | |||
| 377 | return size; | 367 | return size; |
| 378 | } | 368 | } |
| 379 | 369 | ||
| 380 | // creates an empty file filename, returns true on success | ||
| 381 | bool CreateEmptyFile(const std::string& filename) { | 370 | bool CreateEmptyFile(const std::string& filename) { |
| 382 | LOG_TRACE(Common_Filesystem, "{}", filename); | 371 | LOG_TRACE(Common_Filesystem, "{}", filename); |
| 383 | 372 | ||
| @@ -502,7 +491,6 @@ bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) | |||
| 502 | return true; | 491 | return true; |
| 503 | } | 492 | } |
| 504 | 493 | ||
| 505 | // Create directory and copy contents (does not overwrite existing files) | ||
| 506 | void CopyDir(const std::string& source_path, const std::string& dest_path) { | 494 | void CopyDir(const std::string& source_path, const std::string& dest_path) { |
| 507 | #ifndef _WIN32 | 495 | #ifndef _WIN32 |
| 508 | if (source_path == dest_path) | 496 | if (source_path == dest_path) |
| @@ -539,8 +527,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { | |||
| 539 | #endif | 527 | #endif |
| 540 | } | 528 | } |
| 541 | 529 | ||
| 542 | // Returns the current directory | 530 | std::optional<std::string> GetCurrentDir() { |
| 543 | std::string GetCurrentDir() { | ||
| 544 | // Get the current working directory (getcwd uses malloc) | 531 | // Get the current working directory (getcwd uses malloc) |
| 545 | #ifdef _WIN32 | 532 | #ifdef _WIN32 |
| 546 | wchar_t* dir; | 533 | wchar_t* dir; |
| @@ -550,7 +537,7 @@ std::string GetCurrentDir() { | |||
| 550 | if (!(dir = getcwd(nullptr, 0))) { | 537 | if (!(dir = getcwd(nullptr, 0))) { |
| 551 | #endif | 538 | #endif |
| 552 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); | 539 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); |
| 553 | return nullptr; | 540 | return {}; |
| 554 | } | 541 | } |
| 555 | #ifdef _WIN32 | 542 | #ifdef _WIN32 |
| 556 | std::string strDir = Common::UTF16ToUTF8(dir); | 543 | std::string strDir = Common::UTF16ToUTF8(dir); |
| @@ -561,7 +548,6 @@ std::string GetCurrentDir() { | |||
| 561 | return strDir; | 548 | return strDir; |
| 562 | } | 549 | } |
| 563 | 550 | ||
| 564 | // Sets the current directory to the given directory | ||
| 565 | bool SetCurrentDir(const std::string& directory) { | 551 | bool SetCurrentDir(const std::string& directory) { |
| 566 | #ifdef _WIN32 | 552 | #ifdef _WIN32 |
| 567 | return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; | 553 | return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; |
| @@ -673,8 +659,6 @@ std::string GetSysDirectory() { | |||
| 673 | return sysDir; | 659 | return sysDir; |
| 674 | } | 660 | } |
| 675 | 661 | ||
| 676 | // Returns a string with a yuzu data dir or file in the user's home | ||
| 677 | // directory. To be used in "multi-user" mode (that is, installed). | ||
| 678 | const std::string& GetUserPath(UserPath path, const std::string& new_path) { | 662 | const std::string& GetUserPath(UserPath path, const std::string& new_path) { |
| 679 | static std::unordered_map<UserPath, std::string> paths; | 663 | static std::unordered_map<UserPath, std::string> paths; |
| 680 | auto& user_path = paths[UserPath::UserDir]; | 664 | auto& user_path = paths[UserPath::UserDir]; |
| @@ -762,11 +746,11 @@ std::string GetNANDRegistrationDir(bool system) { | |||
| 762 | return GetUserPath(UserPath::NANDDir) + "user/Contents/registered/"; | 746 | return GetUserPath(UserPath::NANDDir) + "user/Contents/registered/"; |
| 763 | } | 747 | } |
| 764 | 748 | ||
| 765 | std::size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { | 749 | std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str) { |
| 766 | return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); | 750 | return IOFile(filename, text_file ? "w" : "wb").WriteString(str); |
| 767 | } | 751 | } |
| 768 | 752 | ||
| 769 | std::size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { | 753 | std::size_t ReadFileToString(bool text_file, const std::string& filename, std::string& str) { |
| 770 | IOFile file(filename, text_file ? "r" : "rb"); | 754 | IOFile file(filename, text_file ? "r" : "rb"); |
| 771 | 755 | ||
| 772 | if (!file.IsOpen()) | 756 | if (!file.IsOpen()) |
| @@ -776,13 +760,6 @@ std::size_t ReadFileToString(bool text_file, const char* filename, std::string& | |||
| 776 | return file.ReadArray(&str[0], str.size()); | 760 | return file.ReadArray(&str[0], str.size()); |
| 777 | } | 761 | } |
| 778 | 762 | ||
| 779 | /** | ||
| 780 | * Splits the filename into 8.3 format | ||
| 781 | * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename | ||
| 782 | * @param filename The normal filename to use | ||
| 783 | * @param short_name A 9-char array in which the short name will be written | ||
| 784 | * @param extension A 4-char array in which the extension will be written | ||
| 785 | */ | ||
| 786 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, | 763 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, |
| 787 | std::array<char, 4>& extension) { | 764 | std::array<char, 4>& extension) { |
| 788 | const std::string forbidden_characters = ".\"/\\[]:;=, "; | 765 | const std::string forbidden_characters = ".\"/\\[]:;=, "; |