From ef13260aa62b4b4d6d7da5f9c85a568eee44aa24 Mon Sep 17 00:00:00 2001 From: umorigu Date: Sat, 3 Dec 2016 06:20:49 +0900 Subject: [PATCH] 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 . * 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 | 16 ++++++++++++++++ plugin/bugtrack.inc.php | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/file.php b/lib/file.php index e06d69d..b90a2f8 100644 --- a/lib/file.php +++ b/lib/file.php @@ -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; +} diff --git a/plugin/bugtrack.inc.php b/plugin/bugtrack.inc.php index cd1ae78..d0c0c58 100644 --- a/plugin/bugtrack.inc.php +++ b/plugin/bugtrack.inc.php @@ -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(); -- 2.11.0