2022-03-28 20:23:47 +00:00
|
|
|
//go:build !windows
|
2015-08-16 10:39:38 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2021-07-09 15:11:39 +00:00
|
|
|
"errors"
|
2015-08-16 10:39:38 +00:00
|
|
|
"os"
|
2022-11-11 13:53:42 +00:00
|
|
|
"runtime"
|
2021-07-09 15:11:39 +00:00
|
|
|
"syscall"
|
2015-08-16 10:39:38 +00:00
|
|
|
)
|
|
|
|
|
2021-07-09 15:11:39 +00:00
|
|
|
// 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()
|
2022-11-11 13:53:42 +00:00
|
|
|
if err != nil &&
|
|
|
|
(errors.Is(err, syscall.ENOTSUP) || errors.Is(err, syscall.ENOENT) ||
|
|
|
|
errors.Is(err, syscall.EINVAL) || isMacENOTTY(err)) {
|
2021-07-09 15:11:39 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cerr := d.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-11 13:53:42 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-08-16 10:39:38 +00:00
|
|
|
// set file to readonly
|
2020-10-06 16:46:19 +00:00
|
|
|
func setFileReadonly(f string, mode os.FileMode) error {
|
2024-07-21 13:22:21 +00:00
|
|
|
return os.Chmod(f, mode&^0222)
|
2015-08-16 10:39:38 +00:00
|
|
|
}
|