MCPcopy Create free account
hub / github.com/Kenix3/libultraship / SFileRenameFile

Function SFileRenameFile

extern/StormLib/src/SFileAddFile.cpp:1147–1225  ·  view source on GitHub ↗

Renames the file within the archive.

Source from the content-addressed store, hash-verified

1145
1146// Renames the file within the archive.
1147bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * szNewFileName)
1148{
1149 TMPQArchive * ha = IsValidMpqHandle(hMpq);
1150 TMPQFile * hf;
1151 DWORD dwErrCode = ERROR_SUCCESS;
1152
1153 // Test the valid parameters
1154 if(ha == NULL)
1155 dwErrCode = ERROR_INVALID_HANDLE;
1156 if(szFileName == NULL || *szFileName == 0 || szNewFileName == NULL || *szNewFileName == 0)
1157 dwErrCode = ERROR_INVALID_PARAMETER;
1158 if(IsInternalMpqFileName(szFileName) || IsInternalMpqFileName(szNewFileName))
1159 dwErrCode = ERROR_INTERNAL_FILE;
1160
1161 // Do not allow to rename files in MPQ open for read only
1162 if(dwErrCode == ERROR_SUCCESS)
1163 {
1164 if(ha->dwFlags & MPQ_FLAG_READ_ONLY)
1165 dwErrCode = ERROR_ACCESS_DENIED;
1166 }
1167
1168 // Open the new file. If exists, we don't allow rename operation
1169 if(dwErrCode == ERROR_SUCCESS)
1170 {
1171 if(GetFileEntryLocale(ha, szNewFileName, g_lcFileLocale) != NULL)
1172 dwErrCode = ERROR_ALREADY_EXISTS;
1173 }
1174
1175 // Open the file from the MPQ
1176 if(dwErrCode == ERROR_SUCCESS)
1177 {
1178 // Attempt to open the file
1179 if(SFileOpenFileEx(hMpq, szFileName, SFILE_OPEN_BASE_FILE, (HANDLE *)&hf))
1180 {
1181 ULONGLONG RawDataOffs;
1182 TFileEntry * pFileEntry = hf->pFileEntry;
1183
1184 // Invalidate the entries for internal files
1185 InvalidateInternalFiles(ha);
1186
1187 // Rename the file entry in the table
1188 dwErrCode = RenameFileEntry(ha, hf, szNewFileName);
1189
1190 // If the file is encrypted, we have to re-crypt the file content
1191 // with the new decryption key
1192 if((dwErrCode == ERROR_SUCCESS) && (pFileEntry->dwFlags & MPQ_FILE_ENCRYPTED))
1193 {
1194 // Recrypt the file data in the MPQ
1195 dwErrCode = RecryptFileData(ha, hf, szFileName, szNewFileName);
1196
1197 // Update the MD5 of the raw block
1198 if(dwErrCode == ERROR_SUCCESS && ha->pHeader->dwRawChunkSize != 0)
1199 {
1200 RawDataOffs = ha->MpqPos + pFileEntry->ByteOffset;
1201 WriteMpqDataMD5(ha->pStream,
1202 RawDataOffs,
1203 pFileEntry->dwCmpSize,
1204 ha->pHeader->dwRawChunkSize);

Callers 3

RenameMpqFileFunction · 0.85
RenameFileMethod · 0.85

Calls 12

IsValidMpqHandleFunction · 0.85
IsInternalMpqFileNameFunction · 0.85
GetFileEntryLocaleFunction · 0.85
SFileOpenFileExFunction · 0.85
InvalidateInternalFilesFunction · 0.85
RenameFileEntryFunction · 0.85
RecryptFileDataFunction · 0.85
WriteMpqDataMD5Function · 0.85
FreeFileHandleFunction · 0.85
GetLastErrorFunction · 0.85
RebuildHetTableFunction · 0.85
SetLastErrorFunction · 0.85

Tested by 2

RenameMpqFileFunction · 0.68