Sync directory to get durable writes in local backend

This commit is contained in:
greatroar 2021-07-09 17:11:39 +02:00 committed by Michael Eischer
parent 195a5cf996
commit 81e2499d19
3 changed files with 36 additions and 1 deletions

View File

@ -142,7 +142,9 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
} }
// Ignore error if filesystem does not support fsync. // Ignore error if filesystem does not support fsync.
if err = f.Sync(); err != nil && !errors.Is(err, syscall.ENOTSUP) { err = f.Sync()
syncNotSup := errors.Is(err, syscall.ENOTSUP)
if err != nil && !syncNotSup {
return errors.WithStack(err) return errors.WithStack(err)
} }
@ -154,6 +156,14 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
return errors.WithStack(err) return errors.WithStack(err)
} }
// Now sync the directory to commit the Rename.
if !syncNotSup {
err = fsyncDir(dir)
if err != nil {
return errors.WithStack(err)
}
}
// try to mark file as read-only to avoid accidential modifications // try to mark file as read-only to avoid accidential modifications
// ignore if the operation fails as some filesystems don't allow the chmod call // ignore if the operation fails as some filesystems don't allow the chmod call
// e.g. exfat and network file systems with certain mount options // e.g. exfat and network file systems with certain mount options

View File

@ -3,11 +3,33 @@
package local package local
import ( import (
"errors"
"os" "os"
"syscall"
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
) )
// fsyncDir flushes changes to the directory dir.
func fsyncDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
err = d.Sync()
if errors.Is(err, syscall.ENOTSUP) {
err = nil
}
cerr := d.Close()
if err == nil {
err = cerr
}
return err
}
// set file to readonly // set file to readonly
func setFileReadonly(f string, mode os.FileMode) error { func setFileReadonly(f string, mode os.FileMode) error {
return fs.Chmod(f, mode&^0222) return fs.Chmod(f, mode&^0222)

View File

@ -4,6 +4,9 @@ import (
"os" "os"
) )
// Can't explicitly flush directory changes on Windows.
func fsyncDir(dir string) error { return nil }
// We don't modify read-only on windows, // We don't modify read-only on windows,
// since it will make us unable to delete the file, // since it will make us unable to delete the file,
// and this isn't common practice on this platform. // and this isn't common practice on this platform.