Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-41291 cache: Use fread to avoid race conditions

  • Loading branch information...
commit 1627ea320a5f283052e7e69359eba73ac4589897 1 parent 661777c
mr-russ mr-russ authored

Showing 1 changed file with 7 additions and 11 deletions. Show diff stats Hide diff stats

  1. +7 11 cache/stores/file/lib.php
18 cache/stores/file/lib.php
@@ -352,23 +352,19 @@ public function get($key) {
352 352 if (!$readfile) {
353 353 return false;
354 354 }
355   - // Check the filesize first, likely not needed but important none the less.
356   - $filesize = filesize($file);
357   - if (!$filesize) {
358   - return false;
359   - }
360   - // Open ensuring the file for writing, truncating it and setting the pointer to the start.
  355 + // Open ensuring the file for reading in binary format.
361 356 if (!$handle = fopen($file, 'rb')) {
362 357 return false;
363 358 }
364 359 // Lock it up!
365 360 // We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way.
366 361 flock($handle, LOCK_SH);
367   - // HACK ALERT
368   - // There is a problem when reading from the file during PHPUNIT tests. For one reason or another the filesize is not correct
369   - // Doesn't happen during normal operation, just during unit tests.
370   - // Read it.
371   - $data = fread($handle, $filesize+128);
  362 + $data = '';
  363 + // Read the data in 1Mb chunks. Small caches will not loop more than once. We don't use filesize as it may
  364 + // be cached with a different value than what we need to read from the file.
  365 + do {
  366 + $data .= fread($handle, 1048576);
  367 + } while (!feof($handle));
372 368 // Unlock it.
373 369 flock($handle, LOCK_UN);
374 370 // Return it unserialised.

0 comments on commit 1627ea3

Please sign in to comment.
Something went wrong with that request. Please try again.