From 4dfa381b9fefc1ecf27d8ee12d26ffdd0d85df52 Mon Sep 17 00:00:00 2001 From: Dean Chen <51218137+locker95@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:50:17 -0500 Subject: [PATCH] restorer: don't delete files when a directory tree can't be loaded (#22189) When loading or iterating a directory tree failed, traverseTreeInner returned a nil file list together with the error. If the error handler ignored the error, the caller still called leaveDir with that nil list, and restore --delete removed every existing entry of the directory in the target, even though the expected content was simply unknown. Force disable deletion in that case, the same way it is already done for invalid node names. Signed-off-by: Dean Chen <862469039@qq.com> --- changelog/unreleased/issue-22181 | 8 +++++ internal/restorer/restorer.go | 6 ++++ internal/restorer/restorer_test.go | 52 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 changelog/unreleased/issue-22181 diff --git a/changelog/unreleased/issue-22181 b/changelog/unreleased/issue-22181 new file mode 100644 index 000000000..44f75ef34 --- /dev/null +++ b/changelog/unreleased/issue-22181 @@ -0,0 +1,8 @@ +Bugfix: Do not delete files in directories whose tree cannot be loaded + +When `restore --delete` failed to load or read the tree of a directory, for +example because a pack file was missing, restic reported an error but then +deleted all existing content of that directory in the target. Restic now +disables deletion for the rest of the restore when a tree cannot be loaded. + +https://github.com/restic/restic/issues/22181 diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index ac8509e6f..0f22e36d6 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -159,6 +159,9 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str tree, err := data.LoadTree(ctx, res.repo, treeID) if err != nil { debug.Log("error loading tree %v: %v", treeID, err) + // the expected directory content is unknown, force disable deletion + // to avoid removing existing files + res.opts.Delete = false return nil, hasRestored, res.sanitizeError(location, err) } @@ -168,6 +171,9 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str for item := range tree { if item.Error != nil { debug.Log("error iterating tree %v: %v", treeID, item.Error) + // the expected directory content is incomplete, force disable deletion + // to avoid removing existing files + res.opts.Delete = false return nil, hasRestored, res.sanitizeError(location, item.Error) } node := item.Node diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index fea41f3d2..5ca53c478 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -1466,6 +1466,58 @@ func TestRestoreDelete(t *testing.T) { } } +func TestRestoreDeleteUnloadableTree(t *testing.T) { + repo := repository.TestRepository(t) + tempdir := rtest.TempDir(t) + ctx := t.Context() + + // existing content in the target that is not known to the snapshot + rtest.OK(t, os.MkdirAll(filepath.Join(tempdir, "dir", "sub"), 0o755)) + rtest.OK(t, os.WriteFile(filepath.Join(tempdir, "dir", "keep"), []byte("keep"), 0o644)) + rtest.OK(t, os.WriteFile(filepath.Join(tempdir, "dir", "sub", "x"), []byte("x"), 0o644)) + + // the subtree of "dir" references a tree blob that does not exist + missing := restic.NewRandomID() + var treeID restic.ID + rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error { + treeID = data.TestSaveNodes(t, ctx, uploader, []*data.Node{ + { + Type: data.NodeTypeDir, + Mode: 0o755, + Name: "dir", + UID: uint32(os.Getuid()), + GID: uint32(os.Getgid()), + Subtree: &missing, + }, + }) + return nil + })) + + sn, err := data.NewSnapshot([]string{"test"}, nil, "", time.Now()) + rtest.OK(t, err) + sn.Tree = &treeID + + res := NewRestorer(repo, sn, Options{Delete: true}) + var errs []error + res.Error = func(location string, err error) error { + errs = append(errs, err) + return nil + } + + _, err = res.RestoreTo(ctx, tempdir) + rtest.OK(t, err) + rtest.Assert(t, len(errs) > 0, "expected an error for the unloadable tree") + + for _, fn := range []string{ + filepath.Join("dir", "keep"), + filepath.Join("dir", "sub"), + filepath.Join("dir", "sub", "x"), + } { + _, err := os.Stat(filepath.Join(tempdir, fn)) + rtest.OK(t, err) + } +} + func TestRestoreToFile(t *testing.T) { snapshot := Snapshot{ Nodes: map[string]Node{