mirror of
https://github.com/restic/restic.git
synced 2026-11-21 13:02:42 +00:00
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>
This commit is contained in:
1 parent
c4f9e50609
commit
4dfa381b9f
3 files changed
+66
No files matched your search
@@ -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
|
||||||
@@ -159,6 +159,9 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str
|
|||||||
tree, err := data.LoadTree(ctx, res.repo, treeID)
|
tree, err := data.LoadTree(ctx, res.repo, treeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debug.Log("error loading tree %v: %v", treeID, err)
|
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)
|
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 {
|
for item := range tree {
|
||||||
if item.Error != nil {
|
if item.Error != nil {
|
||||||
debug.Log("error iterating tree %v: %v", treeID, item.Error)
|
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)
|
return nil, hasRestored, res.sanitizeError(location, item.Error)
|
||||||
}
|
}
|
||||||
node := item.Node
|
node := item.Node
|
||||||
|
|||||||
@@ -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) {
|
func TestRestoreToFile(t *testing.T) {
|
||||||
snapshot := Snapshot{
|
snapshot := Snapshot{
|
||||||
Nodes: map[string]Node{
|
Nodes: map[string]Node{
|
||||||
|
|||||||
Reference in new issue
Block a user