From fe826cd64fe2c9ef0e7a321f4de9aa29dd617ebb Mon Sep 17 00:00:00 2001 From: janisozaur Date: Thu, 5 May 2016 23:59:27 +0200 Subject: [PATCH] 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. --- src/platform/posix.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/platform/posix.c b/src/platform/posix.c index 9637a6cb8c..310c873185 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -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);