mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
use tmp file for file writes, avoid race condition
This commit is contained in:
1
NEWS
1
NEWS
@@ -1,3 +1,4 @@
|
||||
- use tmp file for file writes, avoid file lock race (Monte)
|
||||
- handle embedded "$smarty.config.foo.tpl" correctly (Monte)
|
||||
- add $smarty.config.varname variable for accessing config vars (Paul
|
||||
Lockaby, Monte)
|
||||
|
@@ -2041,22 +2041,25 @@ class Smarty
|
||||
*/
|
||||
function _write_file($filename, $contents, $create_dirs = false)
|
||||
{
|
||||
if ($create_dirs)
|
||||
$this->_create_dir_structure(dirname($filename));
|
||||
$_dirname = dirname($filename);
|
||||
|
||||
if (!($fd = @fopen($filename, 'w'))) {
|
||||
$this->trigger_error("problem writing '$filename.'");
|
||||
if ($create_dirs) {
|
||||
$this->_create_dir_structure($_dirname);
|
||||
}
|
||||
|
||||
// write to tmp file, then rename it to avoid
|
||||
// file locking race condition
|
||||
$_tmp_file = $_dirname . '/' . uniqid('');
|
||||
|
||||
if (!($fd = @fopen($_tmp_file, 'w'))) {
|
||||
$this->trigger_error("problem writing temporary file '$_tmp_file'");
|
||||
return false;
|
||||
}
|
||||
|
||||
// flock doesn't seem to work on several windows platforms (98, NT4, NT5, ?),
|
||||
// so we'll not use it at all in windows.
|
||||
|
||||
if ( strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' || (flock($fd, LOCK_EX)) ) {
|
||||
fwrite($fd, $contents);
|
||||
fclose($fd);
|
||||
rename($_tmp_file, $filename);
|
||||
chmod($filename, $this->_file_perms);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user