OSDN Git Service

BugTrack/2401 Locked file_get_contents/file_put_contents on bugtrack
authorumorigu <umorigu@gmail.com>
Fri, 2 Dec 2016 21:20:49 +0000 (06:20 +0900)
committerumorigu <umorigu@gmail.com>
Fri, 2 Dec 2016 21:20:49 +0000 (06:20 +0900)
It needs locking for a bugtrack_list cache file.

* file_put_contents lock bug was fixed on PHP 5.2.6 .
  * bugtrack_list is for PHP5.4+. So we can use it.
* file_get_content has no lock mechanism. I added pkwk_file_get_contents.

lib/file.php
plugin/bugtrack.inc.php

index e06d69d..b90a2f8 100644 (file)
@@ -841,3 +841,19 @@ function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE)
                        htmlsc(basename($filename)));
        }
 }
+
+/**
+ * Lock-enabled file_get_contents
+ *
+ * Require: PHP5+
+ */
+function pkwk_file_get_contents($filename) {
+       if (! file_exists($filename)) {
+               return false;
+       }
+       $fp   = fopen($filename, 'rb');
+       flock($fp, LOCK_SH);
+       $file = file_get_contents($filename);
+       flock($fp, LOCK_UN);
+       return $file;
+}
index cd1ae78..d0c0c58 100644 (file)
@@ -307,7 +307,7 @@ function plugin_bugtrack_list_convert()
                // Cache management
                $data_updated = true;
                $cache_filepath = CACHE_DIR . encode($page) . '.bugtrack';
-               $json_cached = file_get_contents($cache_filepath);
+               $json_cached = pkwk_file_get_contents($cache_filepath);
                if ($json_cached) {
                        $wrapdata = json_decode($json_cached);
                        if (is_object($wrapdata)) {
@@ -394,7 +394,7 @@ EOD;
                        }
                        $json = array('refreshed_at'=>$refreshed_at, 'pages'=>$data, 'version'=>$cache_format_version);
                        $cache_body = json_encode($json, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
-                       file_put_contents($cache_filepath, $cache_body);
+                       file_put_contents($cache_filepath, $cache_body, LOCK_EX);
                }
        }
        $table = array();