]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
safer storage for diskcacheplugin
authorEvan Prodromou <evan@status.net>
Tue, 12 Jan 2010 00:23:34 +0000 (16:23 -0800)
committerEvan Prodromou <evan@status.net>
Tue, 12 Jan 2010 00:23:34 +0000 (16:23 -0800)
plugins/DiskCachePlugin.php

index 2b788decb94b06c60a780b355574b328ed0f8c36..b709ea3b31417ce5367d35724162aae0bf18c983 100644 (file)
@@ -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));