Хранилища Subversion ant

Редакция

Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 2 Редакция 3
1
<?php
1
<?php
2
/**
2
/**
3
 * Smarty plugin
3
 * Smarty plugin
4
 * @package Smarty
4
 * @package Smarty
5
 * @subpackage plugins
5
 * @subpackage plugins
6
 */
6
 */
7
7
8
/**
8
/**
9
 * write out a file to disk
9
 * write out a file to disk
10
 *
10
 *
11
 * @param string $filename
11
 * @param string $filename
12
 * @param string $contents
12
 * @param string $contents
13
 * @param boolean $create_dirs
13
 * @param boolean $create_dirs
14
 * @return boolean
14
 * @return boolean
15
 */
15
 */
16
function smarty_core_write_file($params, &$smarty)
16
function smarty_core_write_file($params, &$smarty)
17
{
17
{
18
    $_dirname = dirname($params['filename']);
18
    $_dirname = dirname($params['filename']);
19
19
20
    if ($params['create_dirs']) {
20
    if ($params['create_dirs']) {
21
        $_params = array('dir' => $_dirname);
21
        $_params = array('dir' => $_dirname);
22
        require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
22
        require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
23
        smarty_core_create_dir_structure($_params, $smarty);
23
        smarty_core_create_dir_structure($_params, $smarty);
24
    }
24
    }
25
25
26
    // write to tmp file, then rename it to avoid file locking race condition
26
    // write to tmp file, then rename it to avoid file locking race condition
27
    $_tmp_file = tempnam($_dirname, 'wrt');
27
    $_tmp_file = tempnam($_dirname, 'wrt');
28
28
29
    if (!($fd = @fopen($_tmp_file, 'wb'))) {
29
    if (!($fd = @fopen($_tmp_file, 'wb'))) {
30
        $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
30
        $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
31
        if (!($fd = @fopen($_tmp_file, 'wb'))) {
31
        if (!($fd = @fopen($_tmp_file, 'wb'))) {
32
            $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
32
            $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
33
            return false;
33
            return false;
34
        }
34
        }
35
    }
35
    }
36
36
37
    fwrite($fd, $params['contents']);
37
    fwrite($fd, $params['contents']);
38
    fclose($fd);
38
    fclose($fd);
39
39
40
    if (DIRECTORY_SEPARATOR == '\\' || !@rename($_tmp_file, $params['filename'])) {
40
    if (DIRECTORY_SEPARATOR == '\\' || !@rename($_tmp_file, $params['filename'])) {
41
        // On platforms and filesystems that cannot overwrite with rename() 
41
        // On platforms and filesystems that cannot overwrite with rename() 
42
        // delete the file before renaming it -- because windows always suffers
42
        // delete the file before renaming it -- because windows always suffers
43
        // this, it is short-circuited to avoid the initial rename() attempt
43
        // this, it is short-circuited to avoid the initial rename() attempt
44
        @unlink($params['filename']);
44
        @unlink($params['filename']);
45
        @rename($_tmp_file, $params['filename']);
45
        @rename($_tmp_file, $params['filename']);
46
    }
46
    }
47
    @chmod($params['filename'], $smarty->_file_perms);
47
    @chmod($params['filename'], $smarty->_file_perms);
48
48
49
    return true;
49
    return true;
50
}
50
}
51
51
52
/* vim: set expandtab: */
52
/* vim: set expandtab: */
53
53
54
?>
54
?>