diff --git a/src/restic/backend/local/local_unix.go b/src/restic/backend/local/local_unix.go index 684181c3f..2f0826d79 100644 --- a/src/restic/backend/local/local_unix.go +++ b/src/restic/backend/local/local_unix.go @@ -5,15 +5,9 @@ package local import ( "os" "restic/fs" - "syscall" ) // set file to readonly func setNewFileMode(f string, fi os.FileInfo) error { - err := fs.Chmod(f, fi.Mode()&os.FileMode(^uint32(0222))) - // ignore the error if the FS does not support setting this mode (e.g. CIFS with gvfs on Linux) - if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ENOTSUP { - err = nil - } - return err + return fs.Chmod(f, fi.Mode()&os.FileMode(^uint32(0222))) } diff --git a/src/restic/fs/file.go b/src/restic/fs/file.go index 1d15a0300..a90d1b2e7 100644 --- a/src/restic/fs/file.go +++ b/src/restic/fs/file.go @@ -19,11 +19,6 @@ type File interface { Stat() (os.FileInfo, error) } -// Chmod changes the mode of the named file to mode. -func Chmod(name string, mode os.FileMode) error { - return os.Chmod(fixpath(name), mode) -} - // Mkdir creates a new directory with the specified name and permission bits. // If there is an error, it will be of type *PathError. func Mkdir(name string, perm os.FileMode) error { diff --git a/src/restic/fs/file_unix.go b/src/restic/fs/file_unix.go index 4815fcc8b..612465670 100644 --- a/src/restic/fs/file_unix.go +++ b/src/restic/fs/file_unix.go @@ -5,6 +5,7 @@ package fs import ( "io/ioutil" "os" + "syscall" ) // fixpath returns an absolute path on windows, so restic can open long file @@ -35,3 +36,23 @@ func TempFile(dir, prefix string) (f *os.File, err error) { return f, nil } + +// isNotSuported returns true if the error is caused by an unsupported file system feature. +func isNotSupported(err error) bool { + if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ENOTSUP { + return true + } + return false +} + +// Chmod changes the mode of the named file to mode. +func Chmod(name string, mode os.FileMode) error { + err := os.Chmod(fixpath(name), mode) + + // ignore the error if the FS does not support setting this mode (e.g. CIFS with gvfs on Linux) + if err != nil && isNotSupported(err) { + return nil + } + + return err +} diff --git a/src/restic/fs/file_windows.go b/src/restic/fs/file_windows.go index f8cbd1964..dd53cde5e 100644 --- a/src/restic/fs/file_windows.go +++ b/src/restic/fs/file_windows.go @@ -91,3 +91,8 @@ func MkdirAll(path string, perm os.FileMode) error { func TempFile(dir, prefix string) (f *os.File, err error) { return ioutil.TempFile(dir, prefix) } + +// Chmod changes the mode of the named file to mode. +func Chmod(name string, mode os.FileMode) error { + return os.Chmod(fixpath(name), mode) +}