From db410fd564af358cb7c1e2e72cdbaeaeef510e09 Mon Sep 17 00:00:00 2001 From: Ladislav Zezula Date: Wed, 6 Aug 2025 09:34:58 +0200 Subject: Fixed renaming files vs file locale --- src/SFileAddFile.cpp | 33 ++++++++++++++++++++++++++++----- src/StormCommon.h | 2 +- src/lzma/C/Threads.c | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/SFileAddFile.cpp b/src/SFileAddFile.cpp index e04e876..f281d94 100644 --- a/src/SFileAddFile.cpp +++ b/src/SFileAddFile.cpp @@ -1163,8 +1163,11 @@ bool WINAPI SFileRemoveFile(HANDLE hMpq, const char * szFileName, DWORD dwSearch bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * szNewFileName) { TMPQArchive * ha = IsValidMpqHandle(hMpq); + TFileEntry * pFileEntry; TMPQFile * hf; + DWORD dwHashIndex = 0; DWORD dwErrCode = ERROR_SUCCESS; + LCID lcFileLocale = 0; // Test the valid parameters if(ha == NULL) @@ -1181,11 +1184,25 @@ bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * s dwErrCode = ERROR_ACCESS_DENIED; } - // Open the new file. If exists, we don't allow rename operation + // Retrieve the locale of the existing file + // Could be the preferred one or neutral one + if(dwErrCode == ERROR_SUCCESS && ha->pHashTable != NULL) + { + if((pFileEntry = GetFileEntryLocale(ha, szFileName, g_lcFileLocale, &dwHashIndex)) != NULL) + { + lcFileLocale = ha->pHashTable[dwHashIndex].Locale; + } + else + dwErrCode = ERROR_FILE_NOT_FOUND; + } + + // The target file entry must not be there if(dwErrCode == ERROR_SUCCESS) { - if(GetFileEntryLocale(ha, szNewFileName, g_lcFileLocale) != NULL) + if(GetFileEntryExact(ha, szNewFileName, lcFileLocale) != NULL) + { dwErrCode = ERROR_ALREADY_EXISTS; + } } // Open the file from the MPQ @@ -1195,9 +1212,9 @@ bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * s if(SFileOpenFileEx(hMpq, szFileName, SFILE_OPEN_BASE_FILE, (HANDLE *)&hf)) { ULONGLONG RawDataOffs; - TFileEntry * pFileEntry = hf->pFileEntry; // Invalidate the entries for internal files + pFileEntry = hf->pFileEntry; InvalidateInternalFiles(ha); // Rename the file entry in the table @@ -1266,15 +1283,21 @@ bool WINAPI SFileSetFileLocale(HANDLE hFile, LCID lcNewLocale) TFileEntry * pFileEntry; TMPQFile * hf = IsValidFileHandle(hFile); - // Invalid handle => do nothing + // Invalid file handle => return error if(hf == NULL) { SErrSetLastError(ERROR_INVALID_HANDLE); return false; } + // Invalid archive handle => return error + if((ha = IsValidMpqHandle(hf->ha)) == NULL) + { + SErrSetLastError(ERROR_INVALID_HANDLE); + return false; + } + // Do not allow to rename files in MPQ open for read only - ha = hf->ha; if(ha->dwFlags & MPQ_FLAG_READ_ONLY) { SErrSetLastError(ERROR_ACCESS_DENIED); diff --git a/src/StormCommon.h b/src/StormCommon.h index ed67fe1..7f71452 100644 --- a/src/StormCommon.h +++ b/src/StormCommon.h @@ -366,7 +366,7 @@ void FreeBetTable(TMPQBetTable * pBetTable); // Functions for finding files in the file table TFileEntry * GetFileEntryLocale(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex = NULL); -TFileEntry * GetFileEntryExact(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex); +TFileEntry * GetFileEntryExact(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex = NULL); // Allocates file name in the file entry void AllocateFileName(TMPQArchive * ha, TFileEntry * pFileEntry, const char * szFileName); diff --git a/src/lzma/C/Threads.c b/src/lzma/C/Threads.c index 17f70be..7af1da2 100644 --- a/src/lzma/C/Threads.c +++ b/src/lzma/C/Threads.c @@ -9,7 +9,7 @@ static WRes GetError() { - DWORD res = SErrGetLastError(); + DWORD res = GetLastError(); return (res) ? (WRes)(res) : 1; } -- cgit v1.2.3