summaryrefslogtreecommitdiffstats
path: root/etc/inc/config.lib.inc
diff options
context:
space:
mode:
authorErmal Luçi <eri@pfsense.org>2010-03-03 17:20:24 +0000
committerErmal Luçi <eri@pfsense.org>2010-03-03 17:20:24 +0000
commitddd42db3a9e46d19526bd2988b1a1e1faaf0b091 (patch)
treed3740483f566560406b50191eafb4892de002d2b /etc/inc/config.lib.inc
parent6280b10e83115be8aea11e89ad424fb04678f4fb (diff)
downloadpfsense-ddd42db3a9e46d19526bd2988b1a1e1faaf0b091.zip
pfsense-ddd42db3a9e46d19526bd2988b1a1e1faaf0b091.tar.gz
Put safe_write_file to the include it belongs to.
Diffstat (limited to 'etc/inc/config.lib.inc')
-rw-r--r--etc/inc/config.lib.inc39
1 files changed, 39 insertions, 0 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.
OpenPOWER on IntegriCloud