From b8a8b097968a2cac9a571689c836ffd378d57748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 21 Feb 2015 00:21:19 +0100 Subject: [PATCH] NOISSUE make sure saving config files is atomic --- logic/settings/INIFile.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/logic/settings/INIFile.cpp b/logic/settings/INIFile.cpp index 0202030e..901dd1ba 100644 --- a/logic/settings/INIFile.cpp +++ b/logic/settings/INIFile.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include INIFile::INIFile() { @@ -72,10 +74,14 @@ QString INIFile::escape(QString orig) bool INIFile::saveFile(QString fileName) { - // TODO Handle errors. - QFile file(fileName); - file.open(QIODevice::WriteOnly); - QTextStream out(&file); + QSaveFile file(fileName); + if(!file.open(QIODevice::WriteOnly)) + { + qCritical() << "Unable to open INI config file" << fileName << "for saving"; + return false; + } + QByteArray outArray; + QTextStream out(&outArray); out.setCodec("UTF-8"); for (Iterator iter = begin(); iter != end(); iter++) @@ -84,7 +90,18 @@ bool INIFile::saveFile(QString fileName) value = escape(value); out << iter.key() << "=" << value << "\n"; } - + if(file.write(outArray) != outArray.size()) + { + qCritical() << "Unable to write to the INI config file" << fileName; + file.cancelWriting(); + return false; + } + if(!file.commit()) + { + qCritical() << "Unable to commit the INI config file" << fileName; + file.cancelWriting(); + return false; + } return true; }