forked from TrueCloudLab/restic
Merge pull request #5084 from greatroar/utimesnano
Simplify and refactor restoring of timestamps
This commit is contained in:
commit
5fe6607127
10 changed files with 57 additions and 80 deletions
|
@ -292,18 +292,11 @@ func nodeRestoreMetadata(node *restic.Node, path string, warn func(msg string))
|
|||
}
|
||||
|
||||
func nodeRestoreTimestamps(node *restic.Node, path string) error {
|
||||
var utimes = [...]syscall.Timespec{
|
||||
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
||||
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
||||
}
|
||||
atime := node.AccessTime.UnixNano()
|
||||
mtime := node.ModTime.UnixNano()
|
||||
|
||||
if node.Type == restic.NodeTypeSymlink {
|
||||
return nodeRestoreSymlinkTimestamps(path, utimes)
|
||||
if err := utimesNano(fixpath(path), atime, mtime, node.Type); err != nil {
|
||||
return &os.PathError{Op: "UtimesNano", Path: path, Err: err}
|
||||
}
|
||||
|
||||
if err := syscall.UtimesNano(fixpath(path), utimes[:]); err != nil {
|
||||
return errors.Wrap(err, "UtimesNano")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,15 +3,7 @@
|
|||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(_ string, _ [2]syscall.Timespec) error {
|
||||
return nil
|
||||
}
|
||||
import "github.com/restic/restic/internal/restic"
|
||||
|
||||
// nodeRestoreExtendedAttributes is a no-op on AIX.
|
||||
func nodeRestoreExtendedAttributes(_ *restic.Node, _ string) error {
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package fs
|
||||
|
||||
import "syscall"
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
|
||||
return nil
|
||||
}
|
|
@ -1,33 +1,15 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
|
||||
dir, err := os.Open(fixpath(filepath.Dir(path)))
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
// utimesNano is like syscall.UtimesNano, except that it does not follow symlinks.
|
||||
func utimesNano(path string, atime, mtime int64, _ restic.NodeType) error {
|
||||
times := []unix.Timespec{
|
||||
{Sec: utimes[0].Sec, Nsec: utimes[0].Nsec},
|
||||
{Sec: utimes[1].Sec, Nsec: utimes[1].Nsec},
|
||||
unix.NsecToTimespec(atime),
|
||||
unix.NsecToTimespec(mtime),
|
||||
}
|
||||
|
||||
err = unix.UtimesNanoAt(int(dir.Fd()), filepath.Base(path), times, unix.AT_SYMLINK_NOFOLLOW)
|
||||
|
||||
if err != nil {
|
||||
// ignore subsequent errors
|
||||
_ = dir.Close()
|
||||
return errors.Wrap(err, "UtimesNanoAt")
|
||||
}
|
||||
|
||||
return dir.Close()
|
||||
return unix.UtimesNanoAt(unix.AT_FDCWD, path, times, unix.AT_SYMLINK_NOFOLLOW)
|
||||
}
|
||||
|
|
19
internal/fs/node_linux_test.go
Normal file
19
internal/fs/node_linux_test.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestRestoreSymlinkTimestampsError(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
node := restic.Node{Type: restic.NodeTypeSymlink}
|
||||
err := nodeRestoreTimestamps(&node, d+"/nosuchfile")
|
||||
rtest.Assert(t, errors.Is(err, fs.ErrNotExist), "want ErrNotExist, got %q", err)
|
||||
rtest.Assert(t, strings.Contains(err.Error(), d), "filename not in %q", err)
|
||||
}
|
|
@ -1,14 +1,6 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(_ string, _ [2]syscall.Timespec) error {
|
||||
return nil
|
||||
}
|
||||
import "github.com/restic/restic/internal/restic"
|
||||
|
||||
// nodeRestoreExtendedAttributes is a no-op on netbsd.
|
||||
func nodeRestoreExtendedAttributes(_ *restic.Node, _ string) error {
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(_ string, _ [2]syscall.Timespec) error {
|
||||
return nil
|
||||
}
|
||||
import "github.com/restic/restic/internal/restic"
|
||||
|
||||
// nodeRestoreExtendedAttributes is a no-op on openbsd.
|
||||
func nodeRestoreExtendedAttributes(_ *restic.Node, _ string) error {
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package fs
|
||||
|
||||
import "syscall"
|
||||
|
||||
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
|
||||
return nil
|
||||
}
|
21
internal/fs/node_unix_notlinux.go
Normal file
21
internal/fs/node_unix_notlinux.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
//go:build !linux && unix
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
// utimesNano is like syscall.UtimesNano, except that it skips symlinks.
|
||||
func utimesNano(path string, atime, mtime int64, typ restic.NodeType) error {
|
||||
if typ == restic.NodeTypeSymlink {
|
||||
return nil
|
||||
}
|
||||
|
||||
return syscall.UtimesNano(path, []syscall.Timespec{
|
||||
syscall.NsecToTimespec(atime),
|
||||
syscall.NsecToTimespec(mtime),
|
||||
})
|
||||
}
|
|
@ -42,8 +42,8 @@ func lchown(_ string, _ int, _ int) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// restoreSymlinkTimestamps restores timestamps for symlinks
|
||||
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
|
||||
// utimesNano is like syscall.UtimesNano, except that it sets FILE_FLAG_OPEN_REPARSE_POINT.
|
||||
func utimesNano(path string, atime, mtime int64, _ restic.NodeType) error {
|
||||
// tweaked version of UtimesNano from go/src/syscall/syscall_windows.go
|
||||
pathp, e := syscall.UTF16PtrFromString(fixpath(path))
|
||||
if e != nil {
|
||||
|
@ -63,8 +63,8 @@ func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error
|
|||
}
|
||||
}()
|
||||
|
||||
a := syscall.NsecToFiletime(syscall.TimespecToNsec(utimes[0]))
|
||||
w := syscall.NsecToFiletime(syscall.TimespecToNsec(utimes[1]))
|
||||
a := syscall.NsecToFiletime(atime)
|
||||
w := syscall.NsecToFiletime(mtime)
|
||||
return syscall.SetFileTime(h, nil, &a, &w)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue