summaryrefslogtreecommitdiffstats
path: root/logic
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2015-02-21 00:21:19 +0100
committerPetr Mrázek <peterix@gmail.com>2015-02-21 00:21:19 +0100
commitb8a8b097968a2cac9a571689c836ffd378d57748 (patch)
treeef408908e5a2ceccbf301239d36100a199d6a796 /logic
parenta53f8d506e212f862f54e5a758fb50666ec7c3ba (diff)
downloadMultiMC-b8a8b097968a2cac9a571689c836ffd378d57748.tar
MultiMC-b8a8b097968a2cac9a571689c836ffd378d57748.tar.gz
MultiMC-b8a8b097968a2cac9a571689c836ffd378d57748.tar.lz
MultiMC-b8a8b097968a2cac9a571689c836ffd378d57748.tar.xz
MultiMC-b8a8b097968a2cac9a571689c836ffd378d57748.zip
NOISSUE make sure saving config files is atomic
Diffstat (limited to 'logic')
-rw-r--r--logic/settings/INIFile.cpp27
1 files 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 <QFile>
#include <QTextStream>
#include <QStringList>
+#include <QSaveFile>
+#include <QDebug>
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;
}