diff options
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 64 |
1 files changed, 36 insertions, 28 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 4ede9f72c..c869e7b82 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -74,7 +74,7 @@ | |||
| 74 | // This namespace has various generic functions related to files and paths. | 74 | // This namespace has various generic functions related to files and paths. |
| 75 | // The code still needs a ton of cleanup. | 75 | // The code still needs a ton of cleanup. |
| 76 | // REMEMBER: strdup considered harmful! | 76 | // REMEMBER: strdup considered harmful! |
| 77 | namespace FileUtil { | 77 | namespace Common::FS { |
| 78 | 78 | ||
| 79 | // Remove any ending forward slashes from directory paths | 79 | // Remove any ending forward slashes from directory paths |
| 80 | // Modifies argument. | 80 | // Modifies argument. |
| @@ -196,7 +196,7 @@ bool CreateFullPath(const std::string& fullPath) { | |||
| 196 | int panicCounter = 100; | 196 | int panicCounter = 100; |
| 197 | LOG_TRACE(Common_Filesystem, "path {}", fullPath); | 197 | LOG_TRACE(Common_Filesystem, "path {}", fullPath); |
| 198 | 198 | ||
| 199 | if (FileUtil::Exists(fullPath)) { | 199 | if (Exists(fullPath)) { |
| 200 | LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); | 200 | LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); |
| 201 | return true; | 201 | return true; |
| 202 | } | 202 | } |
| @@ -212,7 +212,7 @@ bool CreateFullPath(const std::string& fullPath) { | |||
| 212 | 212 | ||
| 213 | // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") | 213 | // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") |
| 214 | std::string const subPath(fullPath.substr(0, position + 1)); | 214 | std::string const subPath(fullPath.substr(0, position + 1)); |
| 215 | if (!FileUtil::IsDirectory(subPath) && !FileUtil::CreateDir(subPath)) { | 215 | if (!IsDirectory(subPath) && !CreateDir(subPath)) { |
| 216 | LOG_ERROR(Common, "CreateFullPath: directory creation failed"); | 216 | LOG_ERROR(Common, "CreateFullPath: directory creation failed"); |
| 217 | return false; | 217 | return false; |
| 218 | } | 218 | } |
| @@ -231,7 +231,7 @@ bool DeleteDir(const std::string& filename) { | |||
| 231 | LOG_TRACE(Common_Filesystem, "directory {}", filename); | 231 | LOG_TRACE(Common_Filesystem, "directory {}", filename); |
| 232 | 232 | ||
| 233 | // check if a directory | 233 | // check if a directory |
| 234 | if (!FileUtil::IsDirectory(filename)) { | 234 | if (!IsDirectory(filename)) { |
| 235 | LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); | 235 | LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); |
| 236 | return false; | 236 | return false; |
| 237 | } | 237 | } |
| @@ -371,7 +371,7 @@ u64 GetSize(FILE* f) { | |||
| 371 | bool CreateEmptyFile(const std::string& filename) { | 371 | bool CreateEmptyFile(const std::string& filename) { |
| 372 | LOG_TRACE(Common_Filesystem, "{}", filename); | 372 | LOG_TRACE(Common_Filesystem, "{}", filename); |
| 373 | 373 | ||
| 374 | if (!FileUtil::IOFile(filename, "wb").IsOpen()) { | 374 | if (!IOFile(filename, "wb").IsOpen()) { |
| 375 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); | 375 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); |
| 376 | return false; | 376 | return false; |
| 377 | } | 377 | } |
| @@ -488,29 +488,34 @@ bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) | |||
| 488 | return false; | 488 | return false; |
| 489 | 489 | ||
| 490 | // Delete the outermost directory | 490 | // Delete the outermost directory |
| 491 | FileUtil::DeleteDir(directory); | 491 | DeleteDir(directory); |
| 492 | return true; | 492 | return true; |
| 493 | } | 493 | } |
| 494 | 494 | ||
| 495 | void CopyDir(const std::string& source_path, const std::string& dest_path) { | 495 | void CopyDir(const std::string& source_path, const std::string& dest_path) { |
| 496 | #ifndef _WIN32 | 496 | #ifndef _WIN32 |
| 497 | if (source_path == dest_path) | 497 | if (source_path == dest_path) { |
| 498 | return; | 498 | return; |
| 499 | if (!FileUtil::Exists(source_path)) | 499 | } |
| 500 | if (!Exists(source_path)) { | ||
| 500 | return; | 501 | return; |
| 501 | if (!FileUtil::Exists(dest_path)) | 502 | } |
| 502 | FileUtil::CreateFullPath(dest_path); | 503 | if (!Exists(dest_path)) { |
| 504 | CreateFullPath(dest_path); | ||
| 505 | } | ||
| 503 | 506 | ||
| 504 | DIR* dirp = opendir(source_path.c_str()); | 507 | DIR* dirp = opendir(source_path.c_str()); |
| 505 | if (!dirp) | 508 | if (!dirp) { |
| 506 | return; | 509 | return; |
| 510 | } | ||
| 507 | 511 | ||
| 508 | while (struct dirent* result = readdir(dirp)) { | 512 | while (struct dirent* result = readdir(dirp)) { |
| 509 | const std::string virtualName(result->d_name); | 513 | const std::string virtualName(result->d_name); |
| 510 | // check for "." and ".." | 514 | // check for "." and ".." |
| 511 | if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 515 | if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || |
| 512 | ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) | 516 | ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { |
| 513 | continue; | 517 | continue; |
| 518 | } | ||
| 514 | 519 | ||
| 515 | std::string source, dest; | 520 | std::string source, dest; |
| 516 | source = source_path + virtualName; | 521 | source = source_path + virtualName; |
| @@ -518,11 +523,13 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { | |||
| 518 | if (IsDirectory(source)) { | 523 | if (IsDirectory(source)) { |
| 519 | source += '/'; | 524 | source += '/'; |
| 520 | dest += '/'; | 525 | dest += '/'; |
| 521 | if (!FileUtil::Exists(dest)) | 526 | if (!Exists(dest)) { |
| 522 | FileUtil::CreateFullPath(dest); | 527 | CreateFullPath(dest); |
| 528 | } | ||
| 523 | CopyDir(source, dest); | 529 | CopyDir(source, dest); |
| 524 | } else if (!FileUtil::Exists(dest)) | 530 | } else if (!Exists(dest)) { |
| 525 | FileUtil::Copy(source, dest); | 531 | Copy(source, dest); |
| 532 | } | ||
| 526 | } | 533 | } |
| 527 | closedir(dirp); | 534 | closedir(dirp); |
| 528 | #endif | 535 | #endif |
| @@ -538,7 +545,7 @@ std::optional<std::string> GetCurrentDir() { | |||
| 538 | if (!dir) { | 545 | if (!dir) { |
| 539 | #endif | 546 | #endif |
| 540 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); | 547 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); |
| 541 | return {}; | 548 | return std::nullopt; |
| 542 | } | 549 | } |
| 543 | #ifdef _WIN32 | 550 | #ifdef _WIN32 |
| 544 | std::string strDir = Common::UTF16ToUTF8(dir); | 551 | std::string strDir = Common::UTF16ToUTF8(dir); |
| @@ -546,7 +553,7 @@ std::optional<std::string> GetCurrentDir() { | |||
| 546 | std::string strDir = dir; | 553 | std::string strDir = dir; |
| 547 | #endif | 554 | #endif |
| 548 | free(dir); | 555 | free(dir); |
| 549 | return strDir; | 556 | return std::move(strDir); |
| 550 | } | 557 | } |
| 551 | 558 | ||
| 552 | bool SetCurrentDir(const std::string& directory) { | 559 | bool SetCurrentDir(const std::string& directory) { |
| @@ -668,7 +675,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { | |||
| 668 | if (user_path.empty()) { | 675 | if (user_path.empty()) { |
| 669 | #ifdef _WIN32 | 676 | #ifdef _WIN32 |
| 670 | user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; | 677 | user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; |
| 671 | if (!FileUtil::IsDirectory(user_path)) { | 678 | if (!IsDirectory(user_path)) { |
| 672 | user_path = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; | 679 | user_path = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; |
| 673 | } else { | 680 | } else { |
| 674 | LOG_INFO(Common_Filesystem, "Using the local user directory"); | 681 | LOG_INFO(Common_Filesystem, "Using the local user directory"); |
| @@ -677,7 +684,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { | |||
| 677 | paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); | 684 | paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); |
| 678 | paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); | 685 | paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); |
| 679 | #else | 686 | #else |
| 680 | if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { | 687 | if (Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { |
| 681 | user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; | 688 | user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; |
| 682 | paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); | 689 | paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); |
| 683 | paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); | 690 | paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); |
| @@ -704,7 +711,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { | |||
| 704 | } | 711 | } |
| 705 | 712 | ||
| 706 | if (!new_path.empty()) { | 713 | if (!new_path.empty()) { |
| 707 | if (!FileUtil::IsDirectory(new_path)) { | 714 | if (!IsDirectory(new_path)) { |
| 708 | LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); | 715 | LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); |
| 709 | return paths[path]; | 716 | return paths[path]; |
| 710 | } else { | 717 | } else { |
| @@ -946,17 +953,18 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags) | |||
| 946 | } | 953 | } |
| 947 | 954 | ||
| 948 | bool IOFile::Close() { | 955 | bool IOFile::Close() { |
| 949 | if (!IsOpen() || 0 != std::fclose(m_file)) | 956 | if (!IsOpen() || 0 != std::fclose(m_file)) { |
| 950 | return false; | 957 | return false; |
| 958 | } | ||
| 951 | 959 | ||
| 952 | m_file = nullptr; | 960 | m_file = nullptr; |
| 953 | return true; | 961 | return true; |
| 954 | } | 962 | } |
| 955 | 963 | ||
| 956 | u64 IOFile::GetSize() const { | 964 | u64 IOFile::GetSize() const { |
| 957 | if (IsOpen()) | 965 | if (IsOpen()) { |
| 958 | return FileUtil::GetSize(m_file); | 966 | return FS::GetSize(m_file); |
| 959 | 967 | } | |
| 960 | return 0; | 968 | return 0; |
| 961 | } | 969 | } |
| 962 | 970 | ||
| @@ -965,9 +973,9 @@ bool IOFile::Seek(s64 off, int origin) const { | |||
| 965 | } | 973 | } |
| 966 | 974 | ||
| 967 | u64 IOFile::Tell() const { | 975 | u64 IOFile::Tell() const { |
| 968 | if (IsOpen()) | 976 | if (IsOpen()) { |
| 969 | return ftello(m_file); | 977 | return ftello(m_file); |
| 970 | 978 | } | |
| 971 | return std::numeric_limits<u64>::max(); | 979 | return std::numeric_limits<u64>::max(); |
| 972 | } | 980 | } |
| 973 | 981 | ||
| @@ -1016,4 +1024,4 @@ bool IOFile::Resize(u64 size) { | |||
| 1016 | ; | 1024 | ; |
| 1017 | } | 1025 | } |
| 1018 | 1026 | ||
| 1019 | } // namespace FileUtil | 1027 | } // namespace Common::FS |