diff --git a/internal/restic/node.go b/internal/restic/node.go index 5bdc5ba27..51c6071b7 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -348,11 +348,6 @@ func (node Node) writeNodeContent(ctx context.Context, repo BlobLoader, f *os.Fi } func (node Node) createSymlinkAt(path string) error { - - if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { - return errors.Wrap(err, "Symlink") - } - if err := fs.Symlink(node.LinkTarget, path); err != nil { return errors.WithStack(err) } diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index 6002d6f0e..313174fc3 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -221,6 +221,9 @@ func (res *Restorer) traverseTree(ctx context.Context, target, location string, func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, target, location string) error { debug.Log("restoreNode %v %v %v", node.Name, target, location) + if err := fs.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) { + return errors.Wrap(err, "RemoveNode") + } err := node.CreateAt(ctx, target, res.repo) if err != nil { @@ -242,7 +245,7 @@ func (res *Restorer) restoreNodeMetadataTo(node *restic.Node, target, location s } func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location string) error { - if err := fs.Remove(path); !os.IsNotExist(err) { + if err := fs.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { return errors.Wrap(err, "RemoveCreateHardlink") } err := fs.Link(target, path) diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index b1fb0ff92..5c23a88e4 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -37,6 +37,11 @@ type File struct { attributes *FileAttributes } +type Symlink struct { + Target string + ModTime time.Time +} + type Dir struct { Nodes map[string]Node Mode os.FileMode @@ -103,6 +108,20 @@ func saveDir(t testing.TB, repo restic.BlobSaver, nodes map[string]Node, inode u GenericAttributes: getGenericAttributes(node.attributes, false), }) rtest.OK(t, err) + case Symlink: + symlink := n.(Symlink) + err := tree.Insert(&restic.Node{ + Type: "symlink", + Mode: os.ModeSymlink | 0o777, + ModTime: symlink.ModTime, + Name: name, + UID: uint32(os.Getuid()), + GID: uint32(os.Getgid()), + LinkTarget: symlink.Target, + Inode: inode, + Links: 1, + }) + rtest.OK(t, err) case Dir: id := saveDir(t, repo, node.Nodes, inode, getGenericAttributes) @@ -1013,6 +1032,54 @@ func TestRestorerOverwriteBehavior(t *testing.T) { } } +func TestRestorerOverwriteSpecial(t *testing.T) { + baseTime := time.Now() + baseSnapshot := Snapshot{ + Nodes: map[string]Node{ + "dirtest": Dir{ModTime: baseTime}, + "link": Symlink{Target: "foo", ModTime: baseTime}, + "file": File{Data: "content: file\n", Inode: 42, Links: 2, ModTime: baseTime}, + "hardlink": File{Data: "content: file\n", Inode: 42, Links: 2, ModTime: baseTime}, + }, + } + overwriteSnapshot := Snapshot{ + Nodes: map[string]Node{ + "dirtest": Symlink{Target: "foo", ModTime: baseTime}, + "link": File{Data: "content: link\n", Inode: 42, Links: 2, ModTime: baseTime.Add(time.Second)}, + "file": Symlink{Target: "foo2", ModTime: baseTime}, + "hardlink": File{Data: "content: link\n", Inode: 42, Links: 2, ModTime: baseTime.Add(time.Second)}, + }, + } + + files := map[string]string{ + "link": "content: link\n", + "hardlink": "content: link\n", + } + links := map[string]string{ + "dirtest": "foo", + "file": "foo2", + } + + tempdir := saveSnapshotsAndOverwrite(t, baseSnapshot, overwriteSnapshot, Options{Overwrite: OverwriteAlways}) + + for filename, content := range files { + data, err := os.ReadFile(filepath.Join(tempdir, filepath.FromSlash(filename))) + if err != nil { + t.Errorf("unable to read file %v: %v", filename, err) + continue + } + + if !bytes.Equal(data, []byte(content)) { + t.Errorf("file %v has wrong content: want %q, got %q", filename, content, data) + } + } + for filename, target := range links { + link, err := fs.Readlink(filepath.Join(tempdir, filepath.FromSlash(filename))) + rtest.OK(t, err) + rtest.Equals(t, link, target, "wrong symlink target") + } +} + func TestRestoreModified(t *testing.T) { // overwrite files between snapshots and also change their filesize snapshots := []Snapshot{