mirror of
https://github.com/restic/rest-server.git
synced 2025-12-08 06:09:47 +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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tf.Sync(); err != nil {
|
syncNotSup, err := syncFile(tf)
|
||||||
|
if err != nil {
|
||||||
_ = tf.Close()
|
_ = tf.Close()
|
||||||
_ = os.Remove(tf.Name())
|
_ = os.Remove(tf.Name())
|
||||||
h.incrementRepoSpaceUsage(-written)
|
h.incrementRepoSpaceUsage(-written)
|
||||||
|
|
@ -626,11 +627,13 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !syncNotSup {
|
||||||
if err := syncDir(filepath.Dir(path)); err != nil {
|
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
|
// Don't call os.Remove(path) as this is prone to race conditions with parallel upload retries
|
||||||
h.internalServerError(w, err)
|
h.internalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
h.sendMetric(objectType, BlobWrite, uint64(written))
|
h.sendMetric(objectType, BlobWrite, uint64(written))
|
||||||
}
|
}
|
||||||
|
|
@ -648,6 +651,16 @@ func tempFile(fn string, perm os.FileMode) (f *os.File, err error) {
|
||||||
return
|
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 {
|
func syncDir(dirname string) error {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
// syncing a directory is not possible on windows
|
// syncing a directory is not possible on windows
|
||||||
|
|
@ -659,6 +672,10 @@ func syncDir(dirname string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = dir.Sync()
|
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 {
|
if err != nil {
|
||||||
_ = dir.Close()
|
_ = dir.Close()
|
||||||
return err
|
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