リビジョン | ef13260aa62b4b4d6d7da5f9c85a568eee44aa24 (tree) |
---|---|
日時 | 2016-12-03 06:20:49 |
作者 | umorigu <umorigu@gmai...> |
コミッター | umorigu |
BugTrack/2401 Locked file_get_contents/file_put_contents on bugtrack
It needs locking for a bugtrack_list cache file.
* file_put_contents lock bug was fixed on PHP 5.2.6 .
* file_get_content has no lock mechanism. I added pkwk_file_get_contents.
@@ -841,3 +841,19 @@ function pkwk_touch_file($filename, $time = FALSE, $atime = FALSE) | ||
841 | 841 | htmlsc(basename($filename))); |
842 | 842 | } |
843 | 843 | } |
844 | + | |
845 | +/** | |
846 | + * Lock-enabled file_get_contents | |
847 | + * | |
848 | + * Require: PHP5+ | |
849 | + */ | |
850 | +function pkwk_file_get_contents($filename) { | |
851 | + if (! file_exists($filename)) { | |
852 | + return false; | |
853 | + } | |
854 | + $fp = fopen($filename, 'rb'); | |
855 | + flock($fp, LOCK_SH); | |
856 | + $file = file_get_contents($filename); | |
857 | + flock($fp, LOCK_UN); | |
858 | + return $file; | |
859 | +} |
@@ -307,7 +307,7 @@ function plugin_bugtrack_list_convert() | ||
307 | 307 | // Cache management |
308 | 308 | $data_updated = true; |
309 | 309 | $cache_filepath = CACHE_DIR . encode($page) . '.bugtrack'; |
310 | - $json_cached = file_get_contents($cache_filepath); | |
310 | + $json_cached = pkwk_file_get_contents($cache_filepath); | |
311 | 311 | if ($json_cached) { |
312 | 312 | $wrapdata = json_decode($json_cached); |
313 | 313 | if (is_object($wrapdata)) { |
@@ -394,7 +394,7 @@ EOD; | ||
394 | 394 | } |
395 | 395 | $json = array('refreshed_at'=>$refreshed_at, 'pages'=>$data, 'version'=>$cache_format_version); |
396 | 396 | $cache_body = json_encode($json, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES); |
397 | - file_put_contents($cache_filepath, $cache_body); | |
397 | + file_put_contents($cache_filepath, $cache_body, LOCK_EX); | |
398 | 398 | } |
399 | 399 | } |
400 | 400 | $table = array(); |