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
This commit is contained in:
psychon
2009-03-18 15:45:43 +00:00
parent f618ce2a8d
commit e51189ea43
2 changed files with 7 additions and 23 deletions

28
znc.cpp
View File

@@ -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;
}