Saving is not atomic
Submitted by Dmitry Chestnykh
Assigned to Matthew Brush @matt
Description
Here are the problems with saving files (file_save_real function) in Mousepad:
-
It overwrites the original file. If there's a crash/power outage/other failure, the original file may get corrupted.
-
The result of fclose() is not checked. If there's an error flushing data on disk, it will not be reported to the user.
-
There's no fsync() before fclose(). The overwritten file is still not on disk for some period of time. If there's a power outage, the original file may get corrupted.
Suggestion on how to do it properly:
- Create a temporary file in the same directory and open it (Check for errors).
- Write to this temporary file. (Check for errors.)
- fsync(). (Check for errors.)
- fclose(). (Check for errors).
- rename temporary file to the original file. (Check for errors).
Version: 0.3.0
Edited by Gaël Bonithon