MDL-41291 cache: Use fread to avoid race conditions

This commit is contained in:
Russell Smith
2013-08-26 10:50:57 +10:00
parent 661777c503
commit 1627ea320a
+7 -11
View File
@@ -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.