summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp69
1 files changed, 39 insertions, 30 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 45b750e1e..16c3713e0 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!
77namespace FileUtil { 77namespace 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) {
371bool CreateEmptyFile(const std::string& filename) { 371bool 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
495void CopyDir(const std::string& source_path, const std::string& dest_path) { 495void 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
552bool SetCurrentDir(const std::string& directory) { 559bool 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);
@@ -695,6 +702,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) {
695 paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); 702 paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP);
696 paths.emplace(UserPath::LoadDir, user_path + LOAD_DIR DIR_SEP); 703 paths.emplace(UserPath::LoadDir, user_path + LOAD_DIR DIR_SEP);
697 paths.emplace(UserPath::DumpDir, user_path + DUMP_DIR DIR_SEP); 704 paths.emplace(UserPath::DumpDir, user_path + DUMP_DIR DIR_SEP);
705 paths.emplace(UserPath::ScreenshotsDir, user_path + SCREENSHOTS_DIR DIR_SEP);
698 paths.emplace(UserPath::ShaderDir, user_path + SHADER_DIR DIR_SEP); 706 paths.emplace(UserPath::ShaderDir, user_path + SHADER_DIR DIR_SEP);
699 paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); 707 paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP);
700 paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP); 708 paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP);
@@ -703,7 +711,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) {
703 } 711 }
704 712
705 if (!new_path.empty()) { 713 if (!new_path.empty()) {
706 if (!FileUtil::IsDirectory(new_path)) { 714 if (!IsDirectory(new_path)) {
707 LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); 715 LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path);
708 return paths[path]; 716 return paths[path];
709 } else { 717 } else {
@@ -901,10 +909,10 @@ std::string SanitizePath(std::string_view path_, DirectorySeparator directory_se
901 return std::string(RemoveTrailingSlash(path)); 909 return std::string(RemoveTrailingSlash(path));
902} 910}
903 911
904IOFile::IOFile() {} 912IOFile::IOFile() = default;
905 913
906IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { 914IOFile::IOFile(const std::string& filename, const char openmode[], int flags) {
907 Open(filename, openmode, flags); 915 void(Open(filename, openmode, flags));
908} 916}
909 917
910IOFile::~IOFile() { 918IOFile::~IOFile() {
@@ -945,17 +953,18 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags)
945} 953}
946 954
947bool IOFile::Close() { 955bool IOFile::Close() {
948 if (!IsOpen() || 0 != std::fclose(m_file)) 956 if (!IsOpen() || 0 != std::fclose(m_file)) {
949 return false; 957 return false;
958 }
950 959
951 m_file = nullptr; 960 m_file = nullptr;
952 return true; 961 return true;
953} 962}
954 963
955u64 IOFile::GetSize() const { 964u64 IOFile::GetSize() const {
956 if (IsOpen()) 965 if (IsOpen()) {
957 return FileUtil::GetSize(m_file); 966 return FS::GetSize(m_file);
958 967 }
959 return 0; 968 return 0;
960} 969}
961 970
@@ -964,9 +973,9 @@ bool IOFile::Seek(s64 off, int origin) const {
964} 973}
965 974
966u64 IOFile::Tell() const { 975u64 IOFile::Tell() const {
967 if (IsOpen()) 976 if (IsOpen()) {
968 return ftello(m_file); 977 return ftello(m_file);
969 978 }
970 return std::numeric_limits<u64>::max(); 979 return std::numeric_limits<u64>::max();
971} 980}
972 981
@@ -1015,4 +1024,4 @@ bool IOFile::Resize(u64 size) {
1015 ; 1024 ;
1016} 1025}
1017 1026
1018} // namespace FileUtil 1027} // namespace Common::FS