-
-
Save celeron55/27245358fb5cdcc90c3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/filesys.cpp b/src/filesys.cpp | |
index 4cefdb8..5388c9d 100644 | |
--- a/src/filesys.cpp | |
+++ b/src/filesys.cpp | |
@@ -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 { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment