Skip to content

Commit

Permalink
Merge pull request restic#3409 from greatroar/lchown-mknod
Browse files Browse the repository at this point in the history
Make restic.{lchown,mknod} regular functions
  • Loading branch information
MichaelEischer committed Jun 12, 2021
2 parents fdbd654 + 0666c4d commit e8d20ea
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 43 deletions.
9 changes: 9 additions & 0 deletions internal/restic/mknod_unix.go
Original file line number Diff line number Diff line change
@@ -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))
}
6 changes: 2 additions & 4 deletions internal/restic/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions internal/restic/node_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
4 changes: 0 additions & 4 deletions internal/restic/node_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
4 changes: 2 additions & 2 deletions internal/restic/node_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 0 additions & 4 deletions internal/restic/node_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
4 changes: 0 additions & 4 deletions internal/restic/node_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 0 additions & 4 deletions internal/restic/node_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 0 additions & 4 deletions internal/restic/node_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 3 additions & 4 deletions internal/restic/node_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 3 additions & 9 deletions internal/restic/node_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ 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
}

func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
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
Expand Down

0 comments on commit e8d20ea

Please sign in to comment.