mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
dump: report unordered directory entries
This commit is contained in:
1 parent
25751a5856
commit
d7cdb55ebf
6 files changed
+221
-32
No files matched your search
+20
-1
@@ -93,8 +93,16 @@ func printFromTree(ctx context.Context, tree data.TreeNodeIterator, repo restic.
|
||||
|
||||
item := filepath.Join(prefix, pathComponents[0])
|
||||
l := len(pathComponents)
|
||||
for it := range tree {
|
||||
// Track the first out-of-order node for the requested name.
|
||||
var skipped error
|
||||
for it := range data.EnforceTreeOrder(tree) {
|
||||
if it.Error != nil {
|
||||
if errors.Is(it.Error, data.ErrTreeNotOrdered) {
|
||||
if skipped == nil && it.Node.Name == pathComponents[0] {
|
||||
skipped = it.Error
|
||||
}
|
||||
continue
|
||||
}
|
||||
return it.Error
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
@@ -129,6 +137,9 @@ func printFromTree(ctx context.Context, tree data.TreeNodeIterator, repo restic.
|
||||
}
|
||||
}
|
||||
}
|
||||
if skipped != nil {
|
||||
return fmt.Errorf("path %q not found in snapshot: %w", item, skipped)
|
||||
}
|
||||
return fmt.Errorf("path %q not found in snapshot", item)
|
||||
}
|
||||
|
||||
@@ -195,10 +206,18 @@ func runDump(ctx context.Context, opts DumpOptions, gopts global.Options, args [
|
||||
}
|
||||
|
||||
d := dump.New(opts.Archive, repo, outputFileWriter)
|
||||
totalErrors := 0
|
||||
d.Error = func(nodePath string, err error) {
|
||||
totalErrors++
|
||||
printer.E("ignoring error for %s: %s\n", nodePath, err)
|
||||
}
|
||||
err = printFromTree(ctx, tree, repo, "/", splittedPath, d, canWriteArchiveFunc)
|
||||
if err != nil {
|
||||
return errors.Fatalf("cannot dump file: %v", err)
|
||||
}
|
||||
if totalErrors > 0 {
|
||||
return errors.Fatalf("ignored %d directory entries", totalErrors)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/dump"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/repository"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
@@ -25,3 +32,53 @@ func TestDumpSplitPath(t *testing.T) {
|
||||
rtest.Equals(t, path.result, parts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintFromTreeSkipsUnorderedNodes(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
repo := repository.TestRepository(t)
|
||||
|
||||
var rootID restic.ID
|
||||
rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
|
||||
file := func(name string) *data.Node {
|
||||
return &data.Node{Name: name, Type: data.NodeTypeFile, Content: restic.IDs{}}
|
||||
}
|
||||
childID := data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{file("m"), file("a")})
|
||||
rootID = data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{
|
||||
file("m"),
|
||||
file("b"),
|
||||
{Name: "sub", Type: data.NodeTypeDir, Subtree: &childID},
|
||||
file("a"),
|
||||
file("z"),
|
||||
})
|
||||
return nil
|
||||
}))
|
||||
|
||||
load := func() data.TreeNodeIterator {
|
||||
t.Helper()
|
||||
tree, err := data.LoadTree(ctx, repo, rootID)
|
||||
rtest.OK(t, err)
|
||||
return tree
|
||||
}
|
||||
dumpFile := func(tree data.TreeNodeIterator, parts []string) (reported []string, err error) {
|
||||
t.Helper()
|
||||
d := dump.New("tar", repo, &bytes.Buffer{})
|
||||
d.Error = func(nodePath string, err error) {
|
||||
rtest.Assert(t, errors.Is(err, data.ErrTreeNotOrdered), "unexpected error %v", err)
|
||||
reported = append(reported, nodePath)
|
||||
}
|
||||
err = printFromTree(ctx, tree, repo, "/", parts, d, func() error { return nil })
|
||||
return reported, err
|
||||
}
|
||||
|
||||
reported, err := dumpFile(load(), []string{"a"})
|
||||
rtest.Assert(t, errors.Is(err, data.ErrTreeNotOrdered), "missing unordered file: %v", err)
|
||||
rtest.Assert(t, len(reported) == 0, "lookup failure was reported via Error: %v", reported)
|
||||
|
||||
reported, err = dumpFile(load(), []string{"z"})
|
||||
rtest.OK(t, err)
|
||||
rtest.Assert(t, len(reported) == 0, "in-order match reported unordered siblings: %v", reported)
|
||||
|
||||
reported, err = dumpFile(load(), []string{"missing"})
|
||||
rtest.Assert(t, err != nil && !errors.Is(err, data.ErrTreeNotOrdered), "missing path: %v", err)
|
||||
rtest.Assert(t, len(reported) == 0, "unrelated unordered nodes were reported: %v", reported)
|
||||
}
|
||||
+38
-28
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/restic/restic/internal/bloblru"
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/walker"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -19,6 +20,8 @@ type Dumper struct {
|
||||
format string
|
||||
repo restic.Loader
|
||||
w io.Writer
|
||||
// Error reports a node that is skipped due to an error. Currently only used to report out-of-order directory entries.
|
||||
Error func(nodePath string, err error)
|
||||
}
|
||||
|
||||
func New(format string, repo restic.Loader, w io.Writer) *Dumper {
|
||||
@@ -27,6 +30,7 @@ func New(format string, repo restic.Loader, w io.Writer) *Dumper {
|
||||
format: format,
|
||||
repo: repo,
|
||||
w: w,
|
||||
Error: func(string, error) {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +40,7 @@ func (d *Dumper) DumpTree(ctx context.Context, tree data.TreeNodeIterator, rootP
|
||||
// ch is buffered to deal with variable download/write speeds.
|
||||
ch := make(chan *data.Node, 10)
|
||||
wg.Go(func() error {
|
||||
return sendTrees(ctx, d.repo, tree, rootPath, ch)
|
||||
return d.sendTrees(ctx, tree, rootPath, ch)
|
||||
})
|
||||
|
||||
wg.Go(func() error {
|
||||
@@ -52,23 +56,27 @@ func (d *Dumper) DumpTree(ctx context.Context, tree data.TreeNodeIterator, rootP
|
||||
return wg.Wait()
|
||||
}
|
||||
|
||||
func sendTrees(ctx context.Context, repo restic.BlobLoader, nodes data.TreeNodeIterator, rootPath string, ch chan *data.Node) error {
|
||||
func (d *Dumper) sendTrees(ctx context.Context, nodes data.TreeNodeIterator, rootPath string, ch chan *data.Node) error {
|
||||
defer close(ch)
|
||||
|
||||
for item := range nodes {
|
||||
for item := range data.EnforceTreeOrder(nodes) {
|
||||
if item.Error != nil {
|
||||
if errors.Is(item.Error, data.ErrTreeNotOrdered) {
|
||||
d.Error(path.Join(rootPath, item.Node.Name), item.Error)
|
||||
continue
|
||||
}
|
||||
return item.Error
|
||||
}
|
||||
node := item.Node
|
||||
node.Path = path.Join(rootPath, node.Name)
|
||||
if err := sendNodes(ctx, repo, node, ch); err != nil {
|
||||
if err := d.sendNodes(ctx, node, ch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendNodes(ctx context.Context, repo restic.BlobLoader, root *data.Node, ch chan *data.Node) error {
|
||||
func (d *Dumper) sendNodes(ctx context.Context, root *data.Node, ch chan *data.Node) error {
|
||||
select {
|
||||
case ch <- root:
|
||||
case <-ctx.Done():
|
||||
@@ -80,30 +88,32 @@ func sendNodes(ctx context.Context, repo restic.BlobLoader, root *data.Node, ch
|
||||
return nil
|
||||
}
|
||||
|
||||
err := walker.Walk(ctx, repo, *root.Subtree, walker.WalkVisitor{ProcessNode: func(_ restic.ID, nodepath string, node *data.Node, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if node == nil {
|
||||
return walker.Walk(ctx, d.repo, *root.Subtree, walker.WalkVisitor{
|
||||
EnforceTreeOrder: true,
|
||||
ProcessNode: func(_ restic.ID, nodePath string, node *data.Node, err error) error {
|
||||
if errors.Is(err, data.ErrTreeNotOrdered) {
|
||||
d.Error(path.Join(root.Path, nodePath), err)
|
||||
// Skip only this entry. ErrSkipNode would drop the rest of a non-directory.
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
node.Path = path.Join(root.Path, nodePath)
|
||||
if node.Type != data.NodeTypeFile && node.Type != data.NodeTypeDir && node.Type != data.NodeTypeSymlink {
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case ch <- node:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
node.Path = path.Join(root.Path, nodepath)
|
||||
|
||||
if node.Type != data.NodeTypeFile && node.Type != data.NodeTypeDir && node.Type != data.NodeTypeSymlink {
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case ch <- node:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}})
|
||||
|
||||
return err
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// WriteNode writes a file node's contents directly to d's Writer,
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package dump
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/archiver"
|
||||
"github.com/restic/restic/internal/backend"
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/fs"
|
||||
"github.com/restic/restic/internal/repository"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
@@ -113,3 +117,51 @@ func WriteTest(t *testing.T, format string, cd CheckDump) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDumpSkipsUnorderedNodes(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
repo := repository.TestRepository(t)
|
||||
|
||||
var rootID restic.ID
|
||||
rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
|
||||
file := func(name string) *data.Node {
|
||||
return &data.Node{Name: name, Type: data.NodeTypeFile, Content: restic.IDs{}}
|
||||
}
|
||||
// "z" stays in order after the skipped file "a".
|
||||
childID := data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{file("m"), file("a"), file("z")})
|
||||
skippedID := data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{file("kept")})
|
||||
rootID = data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{
|
||||
file("m"),
|
||||
{Name: "b", Type: data.NodeTypeDir, Subtree: &skippedID},
|
||||
{Name: "sub", Type: data.NodeTypeDir, Subtree: &childID},
|
||||
file("a"),
|
||||
file("z"),
|
||||
})
|
||||
return nil
|
||||
}))
|
||||
|
||||
tree, err := data.LoadTree(ctx, repo, rootID)
|
||||
rtest.OK(t, err)
|
||||
|
||||
var reported []string
|
||||
dst := &bytes.Buffer{}
|
||||
d := New("tar", repo, dst)
|
||||
d.Error = func(nodePath string, err error) {
|
||||
rtest.Assert(t, errors.Is(err, data.ErrTreeNotOrdered), "unexpected error %v", err)
|
||||
reported = append(reported, nodePath)
|
||||
}
|
||||
rtest.OK(t, d.DumpTree(ctx, tree, "/"))
|
||||
rtest.Equals(t, []string{"/b", "/sub/a", "/a"}, reported)
|
||||
|
||||
var names []string
|
||||
tr := tar.NewReader(dst)
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
rtest.OK(t, err)
|
||||
names = append(names, hdr.Name)
|
||||
}
|
||||
rtest.Equals(t, []string{"m", "sub/", "sub/m", "sub/z", "z"}, names)
|
||||
}
|
||||
@@ -29,6 +29,8 @@ type WalkVisitor struct {
|
||||
ProcessNode WalkFunc
|
||||
// Optional callback
|
||||
LeaveDir func(path string) error
|
||||
// If true, out-of-order nodes are reported with ErrTreeNotOrdered.
|
||||
EnforceTreeOrder bool
|
||||
}
|
||||
|
||||
// Walk calls walkFn recursively for each node in root. If walkFn returns an
|
||||
@@ -52,13 +54,29 @@ func Walk(ctx context.Context, repo restic.BlobLoader, root restic.ID, visitor W
|
||||
// subtree is in ignoreTrees. If err is nil and ignore is true, the subtree ID
|
||||
// will be added to ignoreTrees by walk.
|
||||
func walk(ctx context.Context, repo restic.BlobLoader, prefix string, parentTreeID restic.ID, tree data.TreeNodeIterator, visitor WalkVisitor) (err error) {
|
||||
if visitor.EnforceTreeOrder {
|
||||
tree = data.EnforceTreeOrder(tree)
|
||||
}
|
||||
for item := range tree {
|
||||
if item.Error != nil {
|
||||
return item.Error
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
if errors.Is(item.Error, data.ErrTreeNotOrdered) {
|
||||
p := path.Join(prefix, item.Node.Name)
|
||||
err = visitor.ProcessNode(parentTreeID, p, item.Node, item.Error)
|
||||
if err == ErrSkipNode {
|
||||
if item.Node.Type == data.NodeTypeDir {
|
||||
continue
|
||||
}
|
||||
// skip the remaining entries in this tree
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
} else if item.Error != nil {
|
||||
return item.Error
|
||||
}
|
||||
node := item.Node
|
||||
|
||||
p := path.Join(prefix, node.Name)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package walker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
@@ -473,3 +474,35 @@ func TestWalker(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkEnforceTreeOrder(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
tm := data.TestWritableTreeMap{TestTreeMap: data.TestTreeMap{}}
|
||||
childID := data.TestSaveUnorderedTree(t, ctx, tm, []*data.Node{
|
||||
{Name: "child", Type: data.NodeTypeFile},
|
||||
})
|
||||
root := data.TestSaveUnorderedTree(t, ctx, tm, []*data.Node{
|
||||
{Name: "m", Type: data.NodeTypeFile},
|
||||
{Name: "a", Type: data.NodeTypeDir, Subtree: &childID},
|
||||
{Name: "z", Type: data.NodeTypeFile},
|
||||
{Name: "m", Type: data.NodeTypeFile},
|
||||
})
|
||||
|
||||
var got []string
|
||||
err := Walk(ctx, tm, root, WalkVisitor{
|
||||
EnforceTreeOrder: true,
|
||||
ProcessNode: func(_ restic.ID, path string, node *data.Node, err error) error {
|
||||
if errors.Is(err, data.ErrTreeNotOrdered) {
|
||||
got = append(got, "skip:"+path)
|
||||
return ErrSkipNode
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
got = append(got, path)
|
||||
return nil
|
||||
},
|
||||
})
|
||||
rtest.OK(t, err)
|
||||
rtest.Equals(t, []string{"/", "/m", "skip:/a", "/z", "skip:/m"}, got)
|
||||
}
|
||||
Reference in new issue
Block a user