mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Fix posix's platform_file_copy (#3500)
The method may fail to properly copy data for files of sizes not being a multiple of FILE_BUFFER_SIZE.
This commit is contained in:
@@ -595,12 +595,21 @@ bool platform_file_copy(const utf8 *srcPath, const utf8 *dstPath, bool overwrite
|
||||
}
|
||||
|
||||
size_t amount_read = 0;
|
||||
size_t file_offset = 0;
|
||||
|
||||
// Copy file in FILE_BUFFER_SIZE-d chunks
|
||||
char* buffer = (char*) malloc(FILE_BUFFER_SIZE);
|
||||
while ((amount_read = fread(buffer, FILE_BUFFER_SIZE, 1, srcFile))) {
|
||||
fwrite(buffer, amount_read, 1, dstFile);
|
||||
file_offset += amount_read;
|
||||
}
|
||||
|
||||
// Finish the left-over data from file, which may not be a full
|
||||
// FILE_BUFFER_SIZE-d chunk.
|
||||
fseek(srcFile, file_offset, SEEK_SET);
|
||||
amount_read = fread(buffer, 1, FILE_BUFFER_SIZE, srcFile);
|
||||
fwrite(buffer, amount_read, 1, dstFile);
|
||||
|
||||
fclose(srcFile);
|
||||
fclose(dstFile);
|
||||
free(buffer);
|
||||
|
||||
Reference in New Issue
Block a user