summaryrefslogtreecommitdiff
path: root/src/filesys.cpp
diff options
context:
space:
mode:
authorPilzAdam <pilzadam@minetest.net>2013-08-13 19:15:06 +0200
committerPilzAdam <pilzadam@minetest.net>2013-08-13 22:05:45 +0200
commitd718b0b34eda84744778fa12a01d5be5f03753d3 (patch)
tree6ee450e7f1078a114c71f73ead16310f13bbc00c /src/filesys.cpp
parentc8930850e37dab9820049152a3e668a315a97560 (diff)
downloadminetest-d718b0b34eda84744778fa12a01d5be5f03753d3.tar.gz
minetest-d718b0b34eda84744778fa12a01d5be5f03753d3.tar.bz2
minetest-d718b0b34eda84744778fa12a01d5be5f03753d3.zip
Dont write directly to files but rather write and copy a tmp file
Diffstat (limited to 'src/filesys.cpp')
-rw-r--r--src/filesys.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/filesys.cpp b/src/filesys.cpp
index 356d3018d..a1795c8ea 100644
--- a/src/filesys.cpp
+++ b/src/filesys.cpp
@@ -23,6 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <sstream>
+#include <fstream>
#include "log.h"
namespace fs
@@ -684,5 +686,28 @@ std::string RemoveRelativePathComponents(std::string path)
return path.substr(0, pos);
}
+bool safeWriteToFile(const std::string &path, const std::string &content)
+{
+ std::string tmp_file = path + ".~mt";
+
+ // Write to a tmp file
+ std::ofstream os(tmp_file.c_str(), std::ios::binary);
+ if (!os.good())
+ return false;
+ os << content;
+ os.flush();
+ os.close();
+ if (os.fail())
+ return false;
+
+ // Copy file
+#ifdef _WIN32
+ remove(path.c_str());
+ return (rename(tmp_file.c_str(), path.c_str()) == 0);
+#else
+ return (rename(tmp_file.c_str(), path.c_str()) == 0);
+#endif
+}
+
} // namespace fs