Use QSaveFile to save the file more safely

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 144
5d73a6717321
child 146
235941b7322f

Use QSaveFile to save the file more safely

src/model.cpp file | annotate | diff | comparison | revisions
--- a/src/model.cpp	Tue Sep 28 22:17:52 2021 +0300
+++ b/src/model.cpp	Tue Sep 28 23:07:23 2021 +0300
@@ -20,6 +20,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QFont>
+#include <QSaveFile>
 #include "model.h"
 #include "modeleditcontext.h"
 
@@ -302,22 +303,28 @@
 }
 
 /**
- * @brief Write out the model as text
+ * @brief Attempts the save the model
+ * @param errors Where to write any errors
+ * @returns whether it succeeded
  */
 bool Model::save(QTextStream &errors) const
 {
-	QFile file{this->storedPath};
-	if (file.open(QIODevice::WriteOnly))
+	// Write the model first into a temporary file
+	QSaveFile file{this->path()};
+	file.setDirectWriteFallback(true);
+	if (file.open(QSaveFile::WriteOnly))
 	{
 		QTextStream out{&file};
 		for (const ModelObjectPointer& object : this->body)
 		{
 			out << object.get()->toLDrawCode() << "\r\n";
 		}
-		file.close();
-		if (out.status() != QTextStream::Ok)
+		const bool commitSucceeded = file.commit();
+		if (not commitSucceeded)
 		{
-			errors << tr("Write error while writing to %1").arg(this->storedPath);
+			errors << tr("Could not save to %1: %2")
+				.arg(this->path())
+				.arg(file.errorString());
 			return false;
 		}
 		else
@@ -328,7 +335,7 @@
 	else
 	{
 		errors << tr("Could not open %1 for writing: %2")
-			.arg(this->storedPath)
+			.arg(file.fileName())
 			.arg(file.errorString());
 		return false;
 	}

mercurial