From b53723bf38d30887e4f56574a22145b167b207cc Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 18 Jun 2022 10:44:04 +0300 Subject: [PATCH] Provide rename compatibility layer --- src/automap.cc | 2 +- src/loadsave.cc | 10 +++++----- src/platform_compat.cc | 13 +++++++++++++ src/platform_compat.h | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/automap.cc b/src/automap.cc index 152b777..9d36cdd 100644 --- a/src/automap.cc +++ b/src/automap.cc @@ -782,7 +782,7 @@ int automapSaveCurrent() // NOTE: Not sure about the size. char automapTmpPath[512]; sprintf(automapTmpPath, "%s\\%s\\%s", masterPatchesPath, "MAPS", AUTOMAP_TMP); - if (rename(automapTmpPath, automapDbPath) != 0) { + if (compat_rename(automapTmpPath, automapDbPath) != 0) { debugPrint("\nAUTOMAP: Error renaming database!\n"); return -1; } diff --git a/src/loadsave.cc b/src/loadsave.cc index ab11cf2..dda3761 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -2498,7 +2498,7 @@ int _SaveBackup() File* stream1 = fileOpen(_str0, "rb"); if (stream1 != NULL) { fileClose(stream1); - if (rename(_str0, _str1) != 0) { + if (compat_rename(_str0, _str1) != 0) { return -1; } } @@ -2520,7 +2520,7 @@ int _SaveBackup() strcat(_str0, fileList[index]); _strmfe(_str1, _str0, "BAK"); - if (rename(_str0, _str1) != 0) { + if (compat_rename(_str0, _str1) != 0) { fileNameListFree(&fileList, 0); return -1; } @@ -2567,7 +2567,7 @@ int _RestoreSave() _strmfe(_str1, _str0, "BAK"); compat_remove(_str0); - if (rename(_str1, _str0) != 0) { + if (compat_rename(_str1, _str0) != 0) { _EraseSave(); return -1; } @@ -2594,7 +2594,7 @@ int _RestoreSave() strcat(_str0, fileList[index]); _strmfe(_str1, _str0, "SAV"); compat_remove(_str1); - if (rename(_str0, _str1) != 0) { + if (compat_rename(_str0, _str1) != 0) { // FIXME: Probably leaks fileList. _EraseSave(); return -1; @@ -2616,7 +2616,7 @@ int _RestoreSave() strcpy(_str1, _gmpath); strcat(_str1, v2); - if (rename(_str0, _str1) != 0) { + if (compat_rename(_str0, _str1) != 0) { _EraseSave(); return -1; } diff --git a/src/platform_compat.cc b/src/platform_compat.cc index 5af4851..4239d45 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -179,6 +179,19 @@ int compat_remove(const char* path) return remove(nativePath); } +int compat_rename(const char* oldFileName, const char* newFileName) +{ + char nativeOldFileName[COMPAT_MAX_PATH]; + strcpy(nativeOldFileName, oldFileName); + compat_windows_path_to_native(nativeOldFileName); + + char nativeNewFileName[COMPAT_MAX_PATH]; + strcpy(nativeNewFileName, newFileName); + compat_windows_path_to_native(nativeNewFileName); + + return rename(nativeOldFileName, nativeNewFileName); +} + void compat_windows_path_to_native(char* path) { #ifndef _WIN32 diff --git a/src/platform_compat.h b/src/platform_compat.h index 6807ad0..1516a09 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -37,6 +37,7 @@ unsigned int compat_timeGetTime(); FILE* compat_fopen(const char* path, const char* mode); gzFile compat_gzopen(const char* path, const char* mode); int compat_remove(const char* path); +int compat_rename(const char* oldFileName, const char* newFileName); void compat_windows_path_to_native(char* path); #endif /* PLATFORM_COMPAT_H */