Permalink
Browse files

MDL-41291 cache: Use fread to avoid race conditions

  • Loading branch information...
mr-russ committed Aug 19, 2013
1 parent 661777c commit 1627ea320a5f283052e7e69359eba73ac4589897
Showing with 7 additions and 11 deletions.
  1. +7 −11 cache/stores/file/lib.php
View
@@ -352,23 +352,19 @@ public function get($key) {
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.

0 comments on commit 1627ea3

Please sign in to comment.