diff options
author | Evan Prodromou <evan@status.net> | 2010-01-11 16:23:34 -0800 |
---|---|---|
committer | Evan Prodromou <evan@status.net> | 2010-01-11 16:23:34 -0800 |
commit | 04c76fc4e5d711ba38d22ffe201bb93d40126071 (patch) | |
tree | 2756f27d160d32be29fac9afbaeaa206773b00e9 /plugins/DiskCachePlugin.php | |
parent | bd6571c2e17939b21e01afd3772acb5cebbbadfe (diff) |
safer storage for diskcacheplugin
Diffstat (limited to 'plugins/DiskCachePlugin.php')
-rw-r--r-- | plugins/DiskCachePlugin.php | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/plugins/DiskCachePlugin.php b/plugins/DiskCachePlugin.php index 2b788decb..b709ea3b3 100644 --- a/plugins/DiskCachePlugin.php +++ b/plugins/DiskCachePlugin.php @@ -68,9 +68,12 @@ class DiskCachePlugin extends Plugin function onStartCacheGet(&$key, &$value) { $filename = $this->keyToFilename($key); + if (file_exists($filename)) { $data = file_get_contents($filename); - $value = unserialize($data); + if ($data !== false) { + $value = unserialize($data); + } } Event::handle('EndCacheGet', array($key, &$value)); @@ -116,7 +119,24 @@ class DiskCachePlugin extends Plugin return false; } - file_put_contents($filename, serialize($value)); + // Write to a temp file and move to destination + + $tempname = tempnam(null, 'statusnetdiskcache'); + + $result = file_put_contents($tempname, serialize($value)); + + if ($result === false) { + $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'"); + return false; + } + + $result = rename($tempname, $filename); + + if (!$result) { + $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'"); + @unlink($tempname); + return false; + } Event::handle('EndCacheSet', array($key, $value, $flag, $expiry)); |