Skip to content

Saving files safely #285

Closed
Closed
@jiangyy

Description

@jiangyy

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions