]> git.lizzy.rs Git - minetest.git/blobdiff - src/filesys.cpp
Remove ClientMap::m_camera_mutex
[minetest.git] / src / filesys.cpp
index 4cefdb807e2580c18a0d559ba0b405414050f34e..501f9ad6ca53f721f4d117b2a199bb6b2e251af9 100644 (file)
@@ -693,13 +693,22 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
        os.flush();
        os.close();
        if (os.fail()) {
+               // Remove the temporary file because writing it failed and it's useless.
                remove(tmp_file.c_str());
                return false;
        }
 
-       // Copy file
+       // Move the finished temporary file over the real file
+#ifdef _WIN32
+       // On POSIX compliant systems rename() is specified to be able to swap the
+       // file in place of the destination file, making this a truly error-proof
+       // transaction.
+       // However, on Windows, the target file has to be removed first.
        remove(path.c_str());
+#endif
        if(rename(tmp_file.c_str(), path.c_str())) {
+               // Remove the temporary file because moving it over the target file
+               // failed.
                remove(tmp_file.c_str());
                return false;
        } else {
@@ -707,5 +716,10 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
        }
 }
 
+bool Rename(const std::string &from, const std::string &to)
+{
+       return rename(from.c_str(), to.c_str()) == 0;
+}
+
 } // namespace fs