mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 15:43:21 +00:00
Port fsync error handling from restic
This ignores several different combinations of errnos which are returned if the storage destination is not able to fsync correctly. See also https://github.com/restic/restic/pull/4021
This commit is contained in:
parent
2dd87ced0a
commit
408dcab92e
3 changed files with 45 additions and 5 deletions
19
repo/repo.go
19
repo/repo.go
|
@ -604,7 +604,8 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := tf.Sync(); err != nil {
|
||||
syncNotSup, err := syncFile(tf)
|
||||
if err != nil {
|
||||
_ = tf.Close()
|
||||
_ = os.Remove(tf.Name())
|
||||
h.incrementRepoSpaceUsage(-written)
|
||||
|
@ -626,11 +627,13 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if !syncNotSup {
|
||||
if err := syncDir(filepath.Dir(path)); err != nil {
|
||||
// Don't call os.Remove(path) as this is prone to race conditions with parallel upload retries
|
||||
h.internalServerError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
h.sendMetric(objectType, BlobWrite, uint64(written))
|
||||
}
|
||||
|
@ -648,6 +651,16 @@ func tempFile(fn string, perm os.FileMode) (f *os.File, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func syncFile(f *os.File) (bool, error) {
|
||||
err := f.Sync()
|
||||
// Ignore error if filesystem does not support fsync.
|
||||
syncNotSup := err != nil && (errors.Is(err, syscall.ENOTSUP) || isMacENOTTY(err))
|
||||
if syncNotSup {
|
||||
err = nil
|
||||
}
|
||||
return syncNotSup, err
|
||||
}
|
||||
|
||||
func syncDir(dirname string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
// syncing a directory is not possible on windows
|
||||
|
@ -659,6 +672,10 @@ func syncDir(dirname string) error {
|
|||
return err
|
||||
}
|
||||
err = dir.Sync()
|
||||
// Ignore error if filesystem does not support fsync.
|
||||
if errors.Is(err, syscall.ENOTSUP) || errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.EINVAL) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
_ = dir.Close()
|
||||
return err
|
||||
|
|
19
repo/repo_unix.go
Normal file
19
repo/repo_unix.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// The ExFAT driver on some versions of macOS can return ENOTTY,
|
||||
// "inappropriate ioctl for device", for fsync.
|
||||
//
|
||||
// https://github.com/restic/restic/issues/4016
|
||||
// https://github.com/realm/realm-core/issues/5789
|
||||
func isMacENOTTY(err error) bool {
|
||||
return runtime.GOOS == "darwin" && errors.Is(err, syscall.ENOTTY)
|
||||
}
|
4
repo/repo_windows.go
Normal file
4
repo/repo_windows.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
package repo
|
||||
|
||||
// Windows is not macOS.
|
||||
func isMacENOTTY(err error) bool { return false }
|
Loading…
Add table
Add a link
Reference in a new issue