diff --git a/cmd/restic/cmd_repair_snapshots.go b/cmd/restic/cmd_repair_snapshots.go index 52325ceb6..2fd910655 100644 --- a/cmd/restic/cmd_repair_snapshots.go +++ b/cmd/restic/cmd_repair_snapshots.go @@ -2,6 +2,7 @@ package main import ( "context" + "path" "slices" "github.com/restic/restic/internal/data" @@ -176,6 +177,10 @@ func runRepairSnapshots(ctx context.Context, gopts global.Options, opts RepairOp return slices.Values([]data.NodeOrError{}), nil }, AllowUnstableSerialization: true, + ReportDroppedNode: func(parent string, node *data.Node, err error) { + target := path.Join(parent, node.Name) + printer.E("warning: dropping directory entry %q: %v", target, err) + }, }) changedCount := 0 diff --git a/cmd/restic/cmd_rewrite.go b/cmd/restic/cmd_rewrite.go index 56cf3c21d..4e3c72f23 100644 --- a/cmd/restic/cmd_rewrite.go +++ b/cmd/restic/cmd_rewrite.go @@ -163,7 +163,7 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *data. rewriteNode = gatherExcludeFilters(rejectByNameFuncs, printer) } - rewriter, querySize := walker.NewSnapshotSizeRewriter(rewriteNode, keepEmptyDirectoryFunc) + rewriter, querySize := walker.NewSnapshotSizeRewriter(rewriteNode, keepEmptyDirectoryFunc, nil) filter = func(ctx context.Context, sn *data.Snapshot, uploader restic.BlobSaver) (restic.ID, *data.SnapshotSummary, error) { id, err := rewriter.RewriteTree(ctx, repo, uploader, "/", *sn.Tree) diff --git a/internal/walker/rewriter.go b/internal/walker/rewriter.go index 13ca096ad..f68a6e05c 100644 --- a/internal/walker/rewriter.go +++ b/internal/walker/rewriter.go @@ -15,6 +15,7 @@ type NodeRewriteFunc func(node *data.Node, path string) *data.Node type FailedTreeRewriteFunc func(nodeID restic.ID, path string, err error) (data.TreeNodeIterator, error) type QueryRewrittenSizeFunc func() SnapshotSize type NodeKeepEmptyDirectoryFunc func(path string) bool +type DroppedNodeFunc func(parent string, node *data.Node, err error) type SnapshotSize struct { FileCount uint @@ -27,6 +28,9 @@ type RewriteOpts struct { KeepEmptyDirectory NodeKeepEmptyDirectoryFunc // decide what to do with a tree that could not be loaded. Return nil to remove the node. By default the load error is returned which causes the operation to fail. RewriteFailedTree FailedTreeRewriteFunc + // DroppedNodeFunc is called when a directory entry is not stored due to an error. + // Intentional removals are not reported. Currently only used to report out-of-order nodes. + ReportDroppedNode DroppedNodeFunc AllowUnstableSerialization bool DisableNodeCache bool @@ -67,7 +71,7 @@ func NewTreeRewriter(opts RewriteOpts) *TreeRewriter { return rw } -func NewSnapshotSizeRewriter(rewriteNode NodeRewriteFunc, keepEmptyDirectoryFilter NodeKeepEmptyDirectoryFunc) (*TreeRewriter, QueryRewrittenSizeFunc) { +func NewSnapshotSizeRewriter(rewriteNode NodeRewriteFunc, keepEmptyDirectoryFilter NodeKeepEmptyDirectoryFunc, reportDropped DroppedNodeFunc) (*TreeRewriter, QueryRewrittenSizeFunc) { var count uint var size uint64 @@ -82,6 +86,7 @@ func NewSnapshotSizeRewriter(rewriteNode NodeRewriteFunc, keepEmptyDirectoryFilt }, DisableNodeCache: true, KeepEmptyDirectory: keepEmptyDirectoryFilter, + ReportDroppedNode: reportDropped, }) ss := func() SnapshotSize { @@ -144,7 +149,7 @@ func (t *TreeRewriter) RewriteTree(ctx context.Context, loader restic.BlobLoader return restic.ID{}, ctx.Err() } if errors.Is(item.Error, data.ErrTreeNotOrdered) { - debug.Log("skipping out-of-order node: %v\n", item.Error) + t.reportDropped(nodepath, item.Node, item.Error) continue } if item.Error != nil { @@ -199,3 +204,10 @@ func (t *TreeRewriter) RewriteTree(ctx context.Context, loader restic.BlobLoader } return newTreeID, err } + +func (t *TreeRewriter) reportDropped(parent string, node *data.Node, err error) { + debug.Log("skipping out-of-order node %q in %s: %v\n", node.Name, parent, err) + if t.opts.ReportDroppedNode != nil { + t.opts.ReportDroppedNode(parent, node, err) + } +} diff --git a/internal/walker/rewriter_test.go b/internal/walker/rewriter_test.go index 1fcfeb644..ad511e80b 100644 --- a/internal/walker/rewriter_test.go +++ b/internal/walker/rewriter_test.go @@ -1,6 +1,7 @@ package walker import ( + "errors" "slices" "testing" @@ -303,7 +304,7 @@ func TestSnapshotSizeQuery(t *testing.T) { } return node } - rewriter, querySize := NewSnapshotSizeRewriter(rewriteNode, nil) + rewriter, querySize := NewSnapshotSizeRewriter(rewriteNode, nil, nil) newRoot, err := rewriter.RewriteTree(ctx, modrepo, modrepo, "/", root) if err != nil { t.Error(err) @@ -426,6 +427,35 @@ func TestRewriterFailOnUnknownFields(t *testing.T) { test.Assert(t, root == expRoot, "mismatched trees") } +type reportedDrop struct { + parent string + name string + err error + typ data.NodeType +} + +func collectDrops(dst *[]reportedDrop) DroppedNodeFunc { + return func(parent string, node *data.Node, err error) { + *dst = append(*dst, reportedDrop{ + parent: parent, + name: node.Name, + err: err, + typ: node.Type, + }) + } +} + +func assertReportedDrops(t *testing.T, exp, got []reportedDrop) { + t.Helper() + test.Equals(t, len(exp), len(got), "dropped entry count") + for i := range exp { + test.Equals(t, exp[i].parent, got[i].parent, "parent of dropped entry %q", exp[i].name) + test.Equals(t, exp[i].name, got[i].name, "name of dropped entry") + test.Equals(t, exp[i].typ, got[i].typ, "type of dropped entry %q", exp[i].name) + test.Assert(t, errors.Is(got[i].err, exp[i].err), "dropped entry %q: got error %v", exp[i].name, got[i].err) + } +} + func TestRewriterSkipsOutOfOrderNodes(t *testing.T) { ctx := t.Context() @@ -444,12 +474,14 @@ func TestRewriterSkipsOutOfOrderNodes(t *testing.T) { }) var seen []string + var dropped []reportedDrop rewriter := NewTreeRewriter(RewriteOpts{ RewriteNode: func(node *data.Node, path string) *data.Node { seen = append(seen, path) return node }, AllowUnstableSerialization: true, + ReportDroppedNode: collectDrops(&dropped), }) newRoot, err := rewriter.RewriteTree(ctx, tm, tm, "/", root) test.OK(t, err) @@ -460,6 +492,10 @@ func TestRewriterSkipsOutOfOrderNodes(t *testing.T) { }) test.Equals(t, expRoot, newRoot, "out-of-order nodes should be dropped") test.Equals(t, []string{"/m", "/z"}, seen, "skipped nodes and their children must not be visited") + assertReportedDrops(t, []reportedDrop{ + {parent: "/", name: "a", err: data.ErrTreeNotOrdered, typ: data.NodeTypeDir}, + {parent: "/", name: "m", err: data.ErrTreeNotOrdered, typ: data.NodeTypeFile}, + }, dropped) }) t.Run("nested", func(t *testing.T) { @@ -473,7 +509,11 @@ func TestRewriterSkipsOutOfOrderNodes(t *testing.T) { {Name: "sub", Type: data.NodeTypeDir, Subtree: &childID}, }) - rewriter := NewTreeRewriter(RewriteOpts{AllowUnstableSerialization: true}) + var dropped []reportedDrop + rewriter := NewTreeRewriter(RewriteOpts{ + AllowUnstableSerialization: true, + ReportDroppedNode: collectDrops(&dropped), + }) newRoot, err := rewriter.RewriteTree(ctx, tm, tm, "/", root) test.OK(t, err) @@ -484,6 +524,9 @@ func TestRewriterSkipsOutOfOrderNodes(t *testing.T) { }, }) test.Equals(t, expRoot, newRoot, "nested out-of-order nodes should be dropped") + assertReportedDrops(t, []reportedDrop{ + {parent: "/sub", name: "a", err: data.ErrTreeNotOrdered, typ: data.NodeTypeFile}, + }, dropped) }) }