diff --git a/node.go b/node.go index 13d44ca04..aa72adf56 100644 --- a/node.go +++ b/node.go @@ -146,13 +146,11 @@ func (node Node) restoreMetadata(path string) error { return errors.Annotate(err, "Lchown") } - if node.Type == "symlink" { - return nil - } - - err = os.Chmod(path, node.Mode) - if err != nil { - return errors.Annotate(err, "Chmod") + if node.Type != "symlink" { + err = os.Chmod(path, node.Mode) + if err != nil { + return errors.Annotate(err, "Chmod") + } } if node.Type != "dir" { @@ -166,12 +164,20 @@ func (node Node) restoreMetadata(path string) error { } func (node Node) RestoreTimestamps(path string) error { - var utimes = []syscall.Timespec{ + var utimes = [...]syscall.Timespec{ syscall.NsecToTimespec(node.AccessTime.UnixNano()), syscall.NsecToTimespec(node.ModTime.UnixNano()), } - if err := syscall.UtimesNano(path, utimes); err != nil { + if node.Type == "symlink" { + if err := node.restoreSymlinkTimestamps(path, utimes); err != nil { + return errors.Annotate(err, "UtimesNano") + } + + return nil + } + + if err := syscall.UtimesNano(path, utimes[:]); err != nil { return errors.Annotate(err, "UtimesNano") } diff --git a/node_darwin.go b/node_darwin.go index e699f16d1..f63b41f13 100644 --- a/node_darwin.go +++ b/node_darwin.go @@ -18,3 +18,7 @@ func (node *Node) fillTimes(stat *syscall.Stat_t) { node.ChangeTime = time.Unix(stat.Ctimespec.Unix()) node.AccessTime = time.Unix(stat.Atimespec.Unix()) } + +func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { + return nil +} diff --git a/node_freebsd.go b/node_freebsd.go index a757e3ec9..231cf8db7 100644 --- a/node_freebsd.go +++ b/node_freebsd.go @@ -18,3 +18,7 @@ func (node *Node) fillTimes(stat *syscall.Stat_t) { func changeTime(stat *syscall.Stat_t) time.Time { return time.Unix(stat.Ctimespec.Unix()) } + +func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { + return nil +} diff --git a/node_linux.go b/node_linux.go index 0d5eb171d..8ce60efbb 100644 --- a/node_linux.go +++ b/node_linux.go @@ -2,8 +2,10 @@ package restic import ( "os" + "path/filepath" "syscall" "time" + "unsafe" ) func (node *Node) OpenForReading() (*os.File, error) { @@ -22,3 +24,36 @@ func (node *Node) fillTimes(stat *syscall.Stat_t) { func changeTime(stat *syscall.Stat_t) time.Time { return time.Unix(stat.Ctim.Unix()) } + +func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { + dir, err := os.Open(filepath.Dir(path)) + defer dir.Close() + if err != nil { + return err + } + + return utimesNanoAt(int(dir.Fd()), filepath.Base(path), utimes, AT_SYMLINK_NOFOLLOW) +} + +// very lowlevel below + +const AT_SYMLINK_NOFOLLOW = 0x100 + +func utimensat(dirfd int, path string, times *[2]syscall.Timespec, flags int) (err error) { + var _p0 *byte + _p0, err = syscall.BytePtrFromString(path) + if err != nil { + return + } + _, _, e1 := syscall.Syscall6(syscall.SYS_UTIMENSAT, uintptr(dirfd), uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(times)), uintptr(flags), 0, 0) + if e1 != 0 { + err = e1 + } + return +} + +//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) + +func utimesNanoAt(dirfd int, path string, ts [2]syscall.Timespec, flags int) (err error) { + return utimensat(dirfd, path, (*[2]syscall.Timespec)(unsafe.Pointer(&ts[0])), flags) +} diff --git a/node_openbsd.go b/node_openbsd.go index a577ab754..38f1d8441 100644 --- a/node_openbsd.go +++ b/node_openbsd.go @@ -22,3 +22,7 @@ func (node *Node) fillTimes(stat *syscall.Stat_t) { func changeTime(stat *syscall.Stat_t) time.Time { return time.Unix(stat.Ctim.Unix()) } + +func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { + return nil +}