Skip to content

Commit 1f20474

Browse files
committed
Merge pull request #161 from arcfieldOSS/arcfieldOSS-patch-1
Modify File Driver to Prevent Partial Reads
2 parents cafc8b4 + a262341 commit 1f20474

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

phpfastcache/3.0.0/abstract.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,13 @@ protected function readfile($file) {
280280
throw new Exception("Can't Read File",96);
281281

282282
}
283-
while (!feof($file_handle)) {
284-
$line = fgets($file_handle);
285-
$string .= $line;
283+
if (flock($file_handle, LOCK_SH | LOCK_NB)) {
284+
while (!feof($file_handle)) {
285+
$line = fgets($file_handle);
286+
$string .= $line;
287+
}
288+
} else {
289+
throw new Exception("Can't Read File",96);
286290
}
287291
fclose($file_handle);
288292

@@ -424,4 +428,4 @@ protected function __setChmodAuto() {
424428
}
425429

426430

427-
}
431+
}

phpfastcache/3.0.0/drivers/files.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,14 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
8888

8989
if($toWrite == true) {
9090
try {
91-
$f = @fopen($file_path, "w+");
92-
fwrite($f, $data);
91+
$f = @fopen($file_path, "c");
92+
if (flock($f,LOCK_EX| LOCK_NB)) { //got a lock to write;
93+
fwrite($f, $data);
94+
fflush($f);
95+
flock($f,LOCK_UN);
96+
} else {
97+
//arguably the file is being written to so the job is done
98+
}
9399
fclose($f);
94100
} catch (Exception $e) {
95101
// miss cache

0 commit comments

Comments
 (0)