Closed
Description
We are working on an automated tool that finding file-system behavior problems. We found that the project saves a file via "QTextDocumentWriter", which opens a file with O_TRUNC flag. If just after the file open, the disk is ran out of space or system crashes, the file contents would be lost (user's markdown file is gone forever).
A safer way is to (1) write to a temporary file; (2) fsync to make sure it is persisted; (3) rename it to the old file. This is POSIX safe, though this does not give you 100% safety in Windows, the chance of losing data file is much smaller (e.g., out-of-space problem can be totally avoided).
Some discussions FYI: http://stackoverflow.com/questions/1812115/how-to-safely-write-to-a-file?lq=1