diff options
author | Ermal Luçi <eri@pfsense.org> | 2010-03-03 17:20:24 +0000 |
---|---|---|
committer | Ermal Luçi <eri@pfsense.org> | 2010-03-03 17:20:24 +0000 |
commit | ddd42db3a9e46d19526bd2988b1a1e1faaf0b091 (patch) | |
tree | d3740483f566560406b50191eafb4892de002d2b | |
parent | 6280b10e83115be8aea11e89ad424fb04678f4fb (diff) | |
download | pfsense-ddd42db3a9e46d19526bd2988b1a1e1faaf0b091.zip pfsense-ddd42db3a9e46d19526bd2988b1a1e1faaf0b091.tar.gz |
Put safe_write_file to the include it belongs to.
-rw-r--r-- | etc/inc/config.lib.inc | 39 | ||||
-rw-r--r-- | etc/inc/pfsense-utils.inc | 40 |
2 files changed, 39 insertions, 40 deletions
diff --git a/etc/inc/config.lib.inc b/etc/inc/config.lib.inc index 240a3a3..255b45a 100644 --- a/etc/inc/config.lib.inc +++ b/etc/inc/config.lib.inc @@ -418,6 +418,45 @@ function convert_config() { echo "Loading new configuration..."; } +/****f* config/safe_write_file + * NAME + * safe_write_file - Write a file out atomically + * DESCRIPTION + * safe_write_file() Writes a file out atomically by first writing to a + * temporary file of the same name but ending with the pid of the current + * process, them renaming the temporary file over the original. + * INPUTS + * $filename - string containing the filename of the file to write + * $content - string containing the file content to write to file + * $force_binary - boolean denoting whether we should force binary + * mode writing. + * RESULT + * boolean - true if successful, false if not + ******/ +function safe_write_file($file, $content, $force_binary) { + $tmp_file = $file . "." . getmypid(); + $write_mode = $force_binary ? "wb" : "w"; + + $fd = fopen($tmp_file, $write_mode); + if (!$fd) { + // Unable to open temporary file for writing + return false; + } + if (!fwrite($fd, $content)) { + // Unable to write to temporary file + fclose($fd); + return false; + } + fclose($fd); + + if (!rename($tmp_file, $file)) { + // Unable to move temporary file to original + unlink($tmp_file); + return false; + } + return true; +} + /****f* config/write_config * NAME * write_config - Backup and write the firewall configuration. diff --git a/etc/inc/pfsense-utils.inc b/etc/inc/pfsense-utils.inc index 0dca970..4af5559 100644 --- a/etc/inc/pfsense-utils.inc +++ b/etc/inc/pfsense-utils.inc @@ -1687,44 +1687,4 @@ function update_alias_names_upon_change($section, $subsection, $fielda, $fieldb, if($debug) fclose($fd); } - -/****f* pfsense-utils/safe_write_file - * NAME - * safe_write_file - Write a file out atomically - * DESCRIPTION - * safe_write_file() Writes a file out atomically by first writing to a - * temporary file of the same name but ending with the pid of the current - * process, them renaming the temporary file over the original. - * INPUTS - * $filename - string containing the filename of the file to write - * $content - string containing the file content to write to file - * $force_binary - boolean denoting whether we should force binary - * mode writing. - * RESULT - * boolean - true if successful, false if not - ******/ -function safe_write_file($file, $content, $force_binary) { - $tmp_file = $file . "." . getmypid(); - $write_mode = $force_binary ? "wb" : "w"; - - $fd = fopen($tmp_file, $write_mode); - if (!$fd) { - // Unable to open temporary file for writing - return false; - } - if (!fwrite($fd, $content)) { - // Unable to write to temporary file - fclose($fd); - return false; - } - fclose($fd); - - if (!rename($tmp_file, $file)) { - // Unable to move temporary file to original - unlink($tmp_file); - return false; - } - return true; -} - ?> |