From e51189ea43cac4d8d31f240a7d2b29f01324d4dd Mon Sep 17 00:00:00 2001 From: psychon Date: Wed, 18 Mar 2009 15:45:43 +0000 Subject: [PATCH] Get rid of znc.conf-backup The old code created a copy of the config file before writing a new version. This backup is now gone. With this patch the config is written to a temporary file znc.conf~ and then fsync()d to make sure the data safely is on the disk. Then the real config file znc.conf is overwritten with this temporary file via a rename() call. This should be safer than the old way, plus it gets rid of a unneeded file. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1432 726aef4b-f618-498e-8847-2d620e286838 --- znc.cpp | 28 +++++++--------------------- znc.h | 2 -- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/znc.cpp b/znc.cpp index 338a9994..9b355f93 100644 --- a/znc.cpp +++ b/znc.cpp @@ -483,29 +483,11 @@ CString CZNC::ExpandConfigPath(const CString& sConfigFile) { return sRetPath; } -bool CZNC::BackupConfig() const { - CString sBackup = GetConfigFile() + "-backup"; - - // Create a new backup overwriting an old one we might have - if (CFile::Copy(m_sConfigFile, sBackup, true)) - return true; - - // Don't abort if no config file exists - if (!CFile::Exists(m_sConfigFile)) - // No backup because we got nothing to backup - return true; - - return false; -} - bool CZNC::WriteConfig() { - CFile File(GetConfigFile()); + // We first write to a temporary file and then move it to the right place + CFile File(GetConfigFile() + "~"); - if (!BackupConfig()) { - return false; - } - - if (m_sConfigFile.empty() || !File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) { + if (GetConfigFile().empty() || !File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) { return false; } @@ -576,8 +558,12 @@ bool CZNC::WriteConfig() { } } + File.Sync(); File.Close(); + // We wrote to a temporary name, move it to the right place + File.Move(GetConfigFile(), true); + return true; } diff --git a/znc.h b/znc.h index d3c67620..553eacf5 100644 --- a/znc.h +++ b/znc.h @@ -185,8 +185,6 @@ private: bool DoRehash(CString& sError); // Returns true if something was done bool HandleUserDeletion(); - // Backup znc.conf - bool BackupConfig() const; protected: time_t m_TimeStarted;