summaryrefslogtreecommitdiffstats
path: root/logic/FileSystem.cpp
diff options
context:
space:
mode:
authorJan Dalheimer <jan@dalheimer.de>2015-05-28 19:38:29 +0200
committerPetr Mrázek <peterix@gmail.com>2015-06-06 21:23:05 +0200
commit3a8b238052163952831fb5924b2483a375e86ebd (patch)
treeab120b4fac3a5345a20e7a09e1e7477e67d9ed6f /logic/FileSystem.cpp
parent161dc66c2c8d5f973ee69dab36c3969a7efd7495 (diff)
downloadMultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar
MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.gz
MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.lz
MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.xz
MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.zip
NOISSUE Various changes from multiauth that are unrelated to it
Diffstat (limited to 'logic/FileSystem.cpp')
-rw-r--r--logic/FileSystem.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/logic/FileSystem.cpp b/logic/FileSystem.cpp
new file mode 100644
index 00000000..b8d82c51
--- /dev/null
+++ b/logic/FileSystem.cpp
@@ -0,0 +1,56 @@
+// Licensed under the Apache-2.0 license. See README.md for details.
+
+#include "FileSystem.h"
+
+#include <QDir>
+#include <QSaveFile>
+#include <QFileInfo>
+
+void ensureExists(const QDir &dir)
+{
+ if (!QDir().mkpath(dir.absolutePath()))
+ {
+ throw FS::FileSystemException("Unable to create directory " + dir.dirName() + " (" +
+ dir.absolutePath() + ")");
+ }
+}
+
+void FS::write(const QString &filename, const QByteArray &data)
+{
+ ensureExists(QFileInfo(filename).dir());
+ QSaveFile file(filename);
+ if (!file.open(QSaveFile::WriteOnly))
+ {
+ throw FileSystemException("Couldn't open " + filename + " for writing: " +
+ file.errorString());
+ }
+ if (data.size() != file.write(data))
+ {
+ throw FileSystemException("Error writing data to " + filename + ": " +
+ file.errorString());
+ }
+ if (!file.commit())
+ {
+ throw FileSystemException("Error while committing data to " + filename + ": " +
+ file.errorString());
+ }
+}
+
+QByteArray FS::read(const QString &filename)
+{
+ QFile file(filename);
+ if (!file.open(QFile::ReadOnly))
+ {
+ throw FileSystemException("Unable to open " + filename + " for reading: " +
+ file.errorString());
+ }
+ const qint64 size = file.size();
+ QByteArray data(int(size), 0);
+ const qint64 ret = file.read(data.data(), size);
+ if (ret == -1 || ret != size)
+ {
+ throw FileSystemException("Error reading data from " + filename + ": " +
+ file.errorString());
+ }
+ return data;
+}