Skip to content

Saving is not atomic

Submitted by Dmitry Chestnykh

Assigned to Matthew Brush @matt

Link to original bug (#8754)

Related upstream issue

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:

  1. Create a temporary file in the same directory and open it (Check for errors).
  2. Write to this temporary file. (Check for errors.)
  3. fsync(). (Check for errors.)
  4. fclose(). (Check for errors).
  5. rename temporary file to the original file. (Check for errors).

Version: 0.3.0

Edited by Gaël Bonithon