From ab2d7d4f3d863f80c0affb57f5f78b5394c6a5c6 Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Mon, 19 Aug 2013 20:18:57 +1000 Subject: [PATCH] MDL-41291 cache: Use fread to avoid race conditions --- cache/stores/file/lib.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 7ef6531b0f1..94f81626e3a 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -352,23 +352,19 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i if (!$readfile) { return false; } - // Check the filesize first, likely not needed but important none the less. - $filesize = filesize($file); - if (!$filesize) { - return false; - } - // Open ensuring the file for writing, truncating it and setting the pointer to the start. + // Open ensuring the file for reading in binary format. if (!$handle = fopen($file, 'rb')) { return false; } // Lock it up! // We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way. flock($handle, LOCK_SH); - // HACK ALERT - // There is a problem when reading from the file during PHPUNIT tests. For one reason or another the filesize is not correct - // Doesn't happen during normal operation, just during unit tests. - // Read it. - $data = fread($handle, $filesize+128); + $data = ''; + // Read the data in 1Mb chunks. Small caches will not loop more than once. We don't use filesize as it may + // be cached with a different value than what we need to read from the file. + do { + $data .= fread($handle, 1048576); + } while (!feof($handle)); // Unlock it. flock($handle, LOCK_UN); // Return it unserialised.