Merge pull request #199 from MichaelEischer/support-macos-ventura

Ignore fsync errors
This commit is contained in:
Michael Eischer
2022-12-16 23:13:43 +01:00
committed by GitHub
3 changed files with 51 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
@@ -73,6 +74,8 @@ func New(path string, opt Options) (*Handler, error) {
type Handler struct {
path string // filesystem path of repo
opt Options
fsyncWarning sync.Once
}
// httpDefaultError write a HTTP error with the default description
@@ -604,7 +607,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,10 +630,15 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
return
}
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
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.fsyncWarning.Do(func() {
log.Print("WARNING: fsync is not supported by the data storage. This can lead to data loss, if the system crashes or the storage is unexpectedly disconnected.")
})
}
h.sendMetric(objectType, BlobWrite, uint64(written))
@@ -648,6 +657,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 +678,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
View 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
View File

@@ -0,0 +1,4 @@
package repo
// Windows is not macOS.
func isMacENOTTY(err error) bool { return false }