From 0666c4d244129def396945d6079e2b27e3758725 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Thu, 27 May 2021 21:29:51 +0200 Subject: [PATCH] Make restic.{lchown,mknod} regular functions This way, they can be inlined and dead code can be removed on Windows. Also fixed some comments. --- internal/restic/mknod_unix.go | 9 +++++++++ internal/restic/node.go | 6 ++---- internal/restic/node_aix.go | 4 ---- internal/restic/node_darwin.go | 4 ---- internal/restic/node_freebsd.go | 4 ++-- internal/restic/node_linux.go | 4 ---- internal/restic/node_netbsd.go | 4 ---- internal/restic/node_openbsd.go | 4 ---- internal/restic/node_solaris.go | 4 ---- internal/restic/node_unix.go | 7 +++---- internal/restic/node_windows.go | 12 +++--------- 11 files changed, 19 insertions(+), 43 deletions(-) create mode 100644 internal/restic/mknod_unix.go diff --git a/internal/restic/mknod_unix.go b/internal/restic/mknod_unix.go new file mode 100644 index 000000000..7021bf35c --- /dev/null +++ b/internal/restic/mknod_unix.go @@ -0,0 +1,9 @@ +// +build !freebsd,!windows + +package restic + +import "golang.org/x/sys/unix" + +func mknod(path string, mode uint32, dev uint64) (err error) { + return unix.Mknod(path, mode, int(dev)) +} diff --git a/internal/restic/node.go b/internal/restic/node.go index 42d240a5b..cd237ac43 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -188,8 +188,6 @@ func (node Node) restoreMetadata(path string) error { if err := lchown(path, int(node.UID), int(node.GID)); err != nil { // Like "cp -a" and "rsync -a" do, we only report lchown permission errors // if we run as root. - // On Windows, Geteuid always returns -1, and we always report lchown - // permission errors. if os.Geteuid() > 0 && os.IsPermission(err) { debug.Log("not running as root, ignoring lchown permission error for %v: %v", path, err) @@ -310,11 +308,11 @@ func (node Node) createSymlinkAt(path string) error { } func (node *Node) createDevAt(path string) error { - return mknod(path, syscall.S_IFBLK|0600, node.device()) + return mknod(path, syscall.S_IFBLK|0600, node.Device) } func (node *Node) createCharDevAt(path string) error { - return mknod(path, syscall.S_IFCHR|0600, node.device()) + return mknod(path, syscall.S_IFCHR|0600, node.Device) } func (node *Node) createFifoAt(path string) error { diff --git a/internal/restic/node_aix.go b/internal/restic/node_aix.go index 1e625296b..65914411c 100644 --- a/internal/restic/node_aix.go +++ b/internal/restic/node_aix.go @@ -8,10 +8,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - // AIX has a funny timespec type in syscall, with 32-bit nanoseconds. // golang.org/x/sys/unix handles this cleanly, but we're stuck with syscall // because os.Stat returns a syscall type in its os.FileInfo.Sys(). diff --git a/internal/restic/node_darwin.go b/internal/restic/node_darwin.go index 814def139..803aa68e5 100644 --- a/internal/restic/node_darwin.go +++ b/internal/restic/node_darwin.go @@ -6,10 +6,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - func (s statT) atim() syscall.Timespec { return s.Atimespec } func (s statT) mtim() syscall.Timespec { return s.Mtimespec } func (s statT) ctim() syscall.Timespec { return s.Ctimespec } diff --git a/internal/restic/node_freebsd.go b/internal/restic/node_freebsd.go index 7439e3963..c06701d24 100644 --- a/internal/restic/node_freebsd.go +++ b/internal/restic/node_freebsd.go @@ -8,8 +8,8 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() uint64 { - return node.Device +func mknod(path string, mode uint32, dev uint64) (err error) { + return syscall.Mknod(path, mode, dev) } func (s statT) atim() syscall.Timespec { return s.Atimespec } diff --git a/internal/restic/node_linux.go b/internal/restic/node_linux.go index bdb03651f..2eb80db90 100644 --- a/internal/restic/node_linux.go +++ b/internal/restic/node_linux.go @@ -32,10 +32,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return dir.Close() } -func (node Node) device() int { - return int(node.Device) -} - func (s statT) atim() syscall.Timespec { return s.Atim } func (s statT) mtim() syscall.Timespec { return s.Mtim } func (s statT) ctim() syscall.Timespec { return s.Ctim } diff --git a/internal/restic/node_netbsd.go b/internal/restic/node_netbsd.go index 052bf93be..0eade2f37 100644 --- a/internal/restic/node_netbsd.go +++ b/internal/restic/node_netbsd.go @@ -6,10 +6,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - func (s statT) atim() syscall.Timespec { return s.Atimespec } func (s statT) mtim() syscall.Timespec { return s.Mtimespec } func (s statT) ctim() syscall.Timespec { return s.Ctimespec } diff --git a/internal/restic/node_openbsd.go b/internal/restic/node_openbsd.go index b10fd3b83..a4ccc7211 100644 --- a/internal/restic/node_openbsd.go +++ b/internal/restic/node_openbsd.go @@ -6,10 +6,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - func (s statT) atim() syscall.Timespec { return s.Atim } func (s statT) mtim() syscall.Timespec { return s.Mtim } func (s statT) ctim() syscall.Timespec { return s.Ctim } diff --git a/internal/restic/node_solaris.go b/internal/restic/node_solaris.go index b10fd3b83..a4ccc7211 100644 --- a/internal/restic/node_solaris.go +++ b/internal/restic/node_solaris.go @@ -6,10 +6,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - func (s statT) atim() syscall.Timespec { return s.Atim } func (s statT) mtim() syscall.Timespec { return s.Mtim } func (s statT) ctim() syscall.Timespec { return s.Ctim } diff --git a/internal/restic/node_unix.go b/internal/restic/node_unix.go index e81b77076..05d577a98 100644 --- a/internal/restic/node_unix.go +++ b/internal/restic/node_unix.go @@ -5,12 +5,11 @@ package restic import ( "os" "syscall" - - "golang.org/x/sys/unix" ) -var mknod = unix.Mknod -var lchown = os.Lchown +func lchown(name string, uid, gid int) error { + return os.Lchown(name, uid, gid) +} type statT syscall.Stat_t diff --git a/internal/restic/node_windows.go b/internal/restic/node_windows.go index fd0fc369c..04a4fe62b 100644 --- a/internal/restic/node_windows.go +++ b/internal/restic/node_windows.go @@ -6,15 +6,13 @@ import ( "github.com/restic/restic/internal/errors" ) -// mknod() creates a filesystem node (file, device -// special file, or named pipe) named pathname, with attributes -// specified by mode and dev. -var mknod = func(path string, mode uint32, dev int) (err error) { +// mknod is not supported on Windows. +func mknod(path string, mode uint32, dev uint64) (err error) { return errors.New("device nodes cannot be created on windows") } // Windows doesn't need lchown -var lchown = func(path string, uid int, gid int) (err error) { +func lchown(path string, uid int, gid int) (err error) { return nil } @@ -22,10 +20,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return nil } -func (node Node) device() int { - return int(node.Device) -} - // Getxattr retrieves extended attribute data associated with path. func Getxattr(path, name string) ([]byte, error) { return nil, nil