Skip to content
Permalink
Browse files

Adding better file locking in write(). This should reduce issues

with file locking and unserialize().
Fixes #1660
  • Loading branch information...
0x20h authored and markstory committed May 10, 2011
1 parent b5a533b commit ea7c8c47100b674e8c2ec2f7737c311885cf5e40
Showing with 16 additions and 5 deletions.
  1. +16 −5 cake/libs/cache/file.php
@@ -136,13 +136,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;
}

0 comments on commit ea7c8c4

Please sign in to comment.
You can’t perform that action at this time.