From 2afc06a793a8be7725f5650d2f2893994f2bd6e9 Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sun, 3 Mar 2013 23:04:54 +0100 Subject: [PATCH] libconfigfile: update config file atomically when saving Signed-off-by: Jacek Jendrzej --- lib/libconfigfile/configfile.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/libconfigfile/configfile.cpp b/lib/libconfigfile/configfile.cpp index 4814cdda9..dd2cea7e3 100644 --- a/lib/libconfigfile/configfile.cpp +++ b/lib/libconfigfile/configfile.cpp @@ -91,7 +91,9 @@ bool CConfigFile::loadConfig(const std::string & filename) bool CConfigFile::saveConfig(const char * const filename) { - std::fstream configFile(filename); + const char *tmpname = (std::string(filename) + ".tmp").c_str(); + unlink(tmpname); + std::fstream configFile(tmpname, std::ios::out); if (configFile != NULL) { @@ -104,7 +106,9 @@ bool CConfigFile::saveConfig(const char * const filename) configFile.sync(); configFile.close(); - chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + chmod(tmpname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + /* TODO: check available space? */ + rename(tmpname, filename); modifiedFlag = false; return true;