Skip to content

Commit 0a9fa4f

Browse files
committed
Always use temporary file when writing backup
When writing a backup file, we should write it atomically (i.e. use a temporary file + rename) in all cases, not only when replacing an existing backup. Just like we do in util.SafeWrite(). Otherwise, if micro crashes while writing this backup, even if that doesn't result in corrupting an existing good backup, it still results in creating an undesired backup with invalid contents.
1 parent a862c97 commit 0a9fa4f

File tree

1 file changed

+1
-11
lines changed

1 file changed

+1
-11
lines changed

internal/buffer/backup.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,8 @@ func (b *SharedBuffer) writeBackup(path string) (string, error) {
104104
}
105105

106106
name := util.DetermineEscapePath(backupdir, path)
107-
108-
// If no existing backup, just write the backup.
109-
if _, err := os.Stat(name); errors.Is(err, fs.ErrNotExist) {
110-
_, err = b.overwriteFile(name)
111-
if err != nil {
112-
os.Remove(name)
113-
}
114-
return name, err
115-
}
116-
117-
// If a backup already exists, replace it atomically.
118107
tmp := util.AppendBackupSuffix(name)
108+
119109
_, err := b.overwriteFile(tmp)
120110
if err != nil {
121111
os.Remove(tmp)

0 commit comments

Comments
 (0)