From 3062de063a718891a405f5690d35fe3df41e8732 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 8 Nov 2012 16:57:49 -0500 Subject: [PATCH] Bug 801437. Reimplement ensure_copy. r=ehsan The previous implementation would copy the entire file into memory which would cause a huge amount of memory usage on when copying large files. This version copies a block at a time to avoid this. --- toolkit/mozapps/update/updater/updater.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/toolkit/mozapps/update/updater/updater.cpp b/toolkit/mozapps/update/updater/updater.cpp index 88c564b60d4..e026f027852 100644 --- a/toolkit/mozapps/update/updater/updater.cpp +++ b/toolkit/mozapps/update/updater/updater.cpp @@ -643,13 +643,16 @@ static int ensure_copy(const NS_tchar *path, const NS_tchar *dest) return WRITE_ERROR; } - void* buffer = malloc(ss.st_size); + // This block size was chosen pretty arbitrarily but seems like a reasonable + // compromise. For example, the optimal block size on a modern OS X machine + // is 100k */ + const int blockSize = 32 * 1024; + void* buffer = malloc(blockSize); if (!buffer) return UPDATER_MEM_ERROR; - size_t left = ss.st_size; - while (left) { - size_t read = fread(buffer, 1, left, infile); + while (!feof(infile)) { + size_t read = fread(buffer, 1, blockSize, infile); if (ferror(infile.get())) { LOG(("ensure_copy: failed to read the file: " LOG_S ", err: %d", path, errno)); @@ -657,7 +660,6 @@ static int ensure_copy(const NS_tchar *path, const NS_tchar *dest) return READ_ERROR; } - left -= read; size_t written = 0; while (written < read) {