Adding file locking.

This commit is contained in:
andrey
2001-02-07 23:08:00 +00:00
parent ec0b835e7f
commit 08c386c2e6
3 changed files with 29 additions and 6 deletions

1
NEWS
View File

@@ -1,3 +1,4 @@
- added file locking to prevent reader/writer problem. (Andrei)
- made Smarty catch unimplemented modifiers and custom functions and output - made Smarty catch unimplemented modifiers and custom functions and output
error messages during compilation instead of failing during run time. error messages during compilation instead of failing during run time.
(Andrei) (Andrei)

View File

@@ -241,6 +241,9 @@ class Smarty
function is_cached($tpl_file, $cache_id = null) function is_cached($tpl_file, $cache_id = null)
{ {
if (!$this->caching)
return false;
// cache name = template path + cache_id // cache name = template path + cache_id
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file)); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_id_md5 = md5($cache_id); $cache_id_md5 = md5($cache_id);
@@ -485,7 +488,7 @@ class Smarty
if (!($template_contents = $this->_read_file($filepath))) if (!($template_contents = $this->_read_file($filepath)))
return false; return false;
$this->_current_file = str_replace($this->template_dir . "/", "", $filepath); $this->_current_file = str_replace($this->template_dir . '/', '', $filepath);
$this->_current_line_no = 1; $this->_current_line_no = 1;
$ldq = preg_quote($this->left_delimiter, '!'); $ldq = preg_quote($this->left_delimiter, '!');
$rdq = preg_quote($this->right_delimiter, '!'); $rdq = preg_quote($this->right_delimiter, '!');
@@ -1228,11 +1231,13 @@ class Smarty
\*======================================================================*/ \*======================================================================*/
function _read_file($filename) function _read_file($filename)
{ {
if (!($fd = fopen($filename, 'r'))) { if (!($fd = fopen($filename, 'r'))) {
$this->_set_error_msg("problem reading '$filename.'"); $this->_set_error_msg("problem reading '$filename.'");
return false; return false;
} }
flock($fd, LOCK_SH);
$contents = fread($fd, filesize($filename)); $contents = fread($fd, filesize($filename));
fclose($fd); fclose($fd);
return $contents; return $contents;
@@ -1248,12 +1253,18 @@ class Smarty
if($create_dirs) if($create_dirs)
$this->_create_dir_structure(dirname($filename)); $this->_create_dir_structure(dirname($filename));
if (!($fd = fopen($filename, 'w'))) { if (!($fd = fopen($filename, 'a'))) {
$this->_set_error_msg("problem writing '$filename.'"); $this->_set_error_msg("problem writing '$filename.'");
return false; return false;
} }
fwrite($fd, $contents); flock($fd, LOCK_EX);
$fd_safe = fopen($filename, 'w');
fwrite($fd_safe, $contents);
fclose($fd_safe);
fclose($fd); fclose($fd);
return true; return true;
} }

View File

@@ -241,6 +241,9 @@ class Smarty
function is_cached($tpl_file, $cache_id = null) function is_cached($tpl_file, $cache_id = null)
{ {
if (!$this->caching)
return false;
// cache name = template path + cache_id // cache name = template path + cache_id
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file)); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_id_md5 = md5($cache_id); $cache_id_md5 = md5($cache_id);
@@ -485,7 +488,7 @@ class Smarty
if (!($template_contents = $this->_read_file($filepath))) if (!($template_contents = $this->_read_file($filepath)))
return false; return false;
$this->_current_file = str_replace($this->template_dir . "/", "", $filepath); $this->_current_file = str_replace($this->template_dir . '/', '', $filepath);
$this->_current_line_no = 1; $this->_current_line_no = 1;
$ldq = preg_quote($this->left_delimiter, '!'); $ldq = preg_quote($this->left_delimiter, '!');
$rdq = preg_quote($this->right_delimiter, '!'); $rdq = preg_quote($this->right_delimiter, '!');
@@ -1228,11 +1231,13 @@ class Smarty
\*======================================================================*/ \*======================================================================*/
function _read_file($filename) function _read_file($filename)
{ {
if (!($fd = fopen($filename, 'r'))) { if (!($fd = fopen($filename, 'r'))) {
$this->_set_error_msg("problem reading '$filename.'"); $this->_set_error_msg("problem reading '$filename.'");
return false; return false;
} }
flock($fd, LOCK_SH);
$contents = fread($fd, filesize($filename)); $contents = fread($fd, filesize($filename));
fclose($fd); fclose($fd);
return $contents; return $contents;
@@ -1248,12 +1253,18 @@ class Smarty
if($create_dirs) if($create_dirs)
$this->_create_dir_structure(dirname($filename)); $this->_create_dir_structure(dirname($filename));
if (!($fd = fopen($filename, 'w'))) { if (!($fd = fopen($filename, 'a'))) {
$this->_set_error_msg("problem writing '$filename.'"); $this->_set_error_msg("problem writing '$filename.'");
return false; return false;
} }
fwrite($fd, $contents); flock($fd, LOCK_EX);
$fd_safe = fopen($filename, 'w');
fwrite($fd_safe, $contents);
fclose($fd_safe);
fclose($fd); fclose($fd);
return true; return true;
} }