Permalink
Browse files

backporting fix for #1660 to branch 1.2, fixed broken locking when wr…

…iting cache files
  • Loading branch information...
0x20h committed Aug 23, 2011
1 parent 561b8e2 commit b5db77ea5b4cbe21c203b7a0012bc1a3126de2b2
Showing with 17 additions and 6 deletions.
  1. +17 −6 cake/libs/cache/file.php
View
@@ -139,13 +139,24 @@ function write($key, &$data, $duration) {
}
}
- if ($this->settings['lock']) {
- $this->__File->lock = true;
- }
$expires = time() + $duration;
$contents = $expires . $lineBreak . $data . $lineBreak;
- $success = $this->__File->write($contents);
- $this->__File->close();
+
+ if (!$handle = fopen($this->__File->path, 'c')) {
+ return false;
+ }
+
+ if ($this->settings['lock']) {
+ flock($handle, LOCK_EX);
+ }
+
+ $success = ftruncate($handle, 0) && fwrite($handle, $contents) && fflush($handle);
+
+ if ($this->settings['lock']) {
+ flock($handle, LOCK_UN);
+ }
+
+ fclose($handle);
return $success;
}
/**
@@ -265,4 +276,4 @@ function __active() {
return true;
}
}
-?>
+?>

0 comments on commit b5db77e

Please sign in to comment.