mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-07 23:50:18 +09:00
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.
This commit is contained in:
@@ -104,18 +104,8 @@ func (b *SharedBuffer) writeBackup(path string) (string, error) {
|
||||
}
|
||||
|
||||
name := util.DetermineEscapePath(backupdir, path)
|
||||
|
||||
// If no existing backup, just write the backup.
|
||||
if _, err := os.Stat(name); errors.Is(err, fs.ErrNotExist) {
|
||||
_, err = b.overwriteFile(name)
|
||||
if err != nil {
|
||||
os.Remove(name)
|
||||
}
|
||||
return name, err
|
||||
}
|
||||
|
||||
// If a backup already exists, replace it atomically.
|
||||
tmp := util.AppendBackupSuffix(name)
|
||||
|
||||
_, err := b.overwriteFile(tmp)
|
||||
if err != nil {
|
||||
os.Remove(tmp)
|
||||
|
||||
Reference in New Issue
Block a user