From af06376b5b858ab700f79250467854ffe4318876 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 14 May 2015 18:05:15 +0200 Subject: [PATCH 1/3] Add tests for node.CreateAt() --- node_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/node_test.go b/node_test.go index 4b0acd309..33d222d78 100644 --- a/node_test.go +++ b/node_test.go @@ -1,8 +1,15 @@ -package restic +package restic_test import ( "io/ioutil" + "os" + "path/filepath" "testing" + "time" + + "github.com/restic/restic" + "github.com/restic/restic/backend" + . "github.com/restic/restic/test" ) func BenchmarkNodeFillUser(t *testing.B) { @@ -17,13 +24,12 @@ func BenchmarkNodeFillUser(t *testing.B) { t.Fatal(err) } - node := &Node{} path := tempfile.Name() t.ResetTimer() for i := 0; i < t.N; i++ { - node.fillExtra(path, fi) + restic.NodeFromFileInfo(path, fi) } } @@ -44,9 +50,99 @@ func BenchmarkNodeFromFileInfo(t *testing.B) { t.ResetTimer() for i := 0; i < t.N; i++ { - _, err := NodeFromFileInfo(path, fi) + _, err := restic.NodeFromFileInfo(path, fi) if err != nil { t.Fatal(err) } } } + +func parseTime(s string) time.Time { + t, err := time.Parse("2006-01-02 15:04:05.999", s) + if err != nil { + panic(err) + } + + return t.Local() +} + +var nodeTests = []restic.Node{ + restic.Node{ + Name: "testFile", + Type: "file", + Content: []backend.ID{}, + UID: uint32(os.Getuid()), + GID: uint32(os.Getgid()), + Mode: 0604, + ModTime: parseTime("2015-05-14 21:07:23.111"), + AccessTime: parseTime("2015-05-14 21:07:24.222"), + ChangeTime: parseTime("2015-05-14 21:07:25.333"), + }, + restic.Node{ + Name: "testDir", + Type: "dir", + Subtree: nil, + UID: uint32(os.Getuid()), + GID: uint32(os.Getgid()), + Mode: 020000000750, + ModTime: parseTime("2015-05-14 21:07:23.111"), + AccessTime: parseTime("2015-05-14 21:07:24.222"), + ChangeTime: parseTime("2015-05-14 21:07:25.333"), + }, + restic.Node{ + Name: "testSymlink", + Type: "symlink", + LinkTarget: "invalid", + UID: uint32(os.Getuid()), + GID: uint32(os.Getgid()), + Mode: 01000000777, + ModTime: parseTime("2015-05-14 21:07:23.111"), + AccessTime: parseTime("2015-05-14 21:07:24.222"), + ChangeTime: parseTime("2015-05-14 21:07:25.333"), + }, +} + +func TestNodeRestoreAt(t *testing.T) { + tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") + OK(t, err) + + defer func() { + if *TestCleanup { + OK(t, os.RemoveAll(tempdir)) + } else { + t.Logf("leaving tempdir at %v", tempdir) + } + }() + + for _, test := range nodeTests { + nodePath := filepath.Join(tempdir, test.Name) + OK(t, test.CreateAt(nodePath, nil)) + + if test.Type == "dir" { + OK(t, test.RestoreTimestamps(nodePath)) + } + + fi, err := os.Lstat(nodePath) + OK(t, err) + + n2, err := restic.NodeFromFileInfo(nodePath, fi) + OK(t, err) + + Assert(t, test.Name == n2.Name, + "%v: name doesn't match", test.Type) + Assert(t, test.Type == n2.Type, + "%v: type doesn't match", test.Type) + Assert(t, test.Size == n2.Size, + "%v: size doesn't match", test.Size) + Assert(t, test.UID == n2.UID, + "%v: UID doesn't match", test.Type) + Assert(t, test.GID == n2.GID, + "%v: GID doesn't match", test.Type) + Assert(t, test.Mode == n2.Mode, + "%v: mode doesn't match", test.Type) + Assert(t, test.ModTime == n2.ModTime, + "%v: ModTime dosn't match", test.Type) + Assert(t, test.AccessTime == n2.AccessTime, + "%v: AccessTime doesn't match", test.Type) + } +} From 44219c5afe6c5c19b32b7ddfd8b6e9581ea7bc19 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 14 May 2015 23:06:11 +0200 Subject: [PATCH 2/3] node/Linux: Implement setting timestamps for symlinks --- node.go | 24 +++++++++++++++--------- node_darwin.go | 4 ++++ node_freebsd.go | 4 ++++ node_linux.go | 35 +++++++++++++++++++++++++++++++++++ node_openbsd.go | 4 ++++ 5 files changed, 62 insertions(+), 9 deletions(-) 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 +} From 14a3ed4e9d2a164586b3ce62e585bf763c1cd6e5 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 16 May 2015 13:25:10 +0200 Subject: [PATCH 3/3] Move error annotation for symlink timestamp restore --- node.go | 2 +- node_linux.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/node.go b/node.go index aa72adf56..c34a0ed52 100644 --- a/node.go +++ b/node.go @@ -171,7 +171,7 @@ func (node Node) RestoreTimestamps(path string) error { if node.Type == "symlink" { if err := node.restoreSymlinkTimestamps(path, utimes); err != nil { - return errors.Annotate(err, "UtimesNano") + return err } return nil diff --git a/node_linux.go b/node_linux.go index 8ce60efbb..1043397a8 100644 --- a/node_linux.go +++ b/node_linux.go @@ -6,6 +6,8 @@ import ( "syscall" "time" "unsafe" + + "github.com/juju/errors" ) func (node *Node) OpenForReading() (*os.File, error) { @@ -32,7 +34,13 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe return err } - return utimesNanoAt(int(dir.Fd()), filepath.Base(path), utimes, AT_SYMLINK_NOFOLLOW) + err = utimesNanoAt(int(dir.Fd()), filepath.Base(path), utimes, AT_SYMLINK_NOFOLLOW) + + if err != nil { + return errors.Annotate(err, "UtimesNanoAt") + } + + return nil } // very lowlevel below