From 0aa73bbd39b087ab960191d26c4e8de243d8aead Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 3 Oct 2022 13:05:36 +0200 Subject: [PATCH] ls: proper error handling for non-existent snapshot Use restic.FindFilteredSnapshot to resolve the snapshot ID. This ensures consistent behavior for all commands using initSingleSnapshotFilterOptions. --- cmd/restic/cmd_ls.go | 77 ++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index fd9a0ef0d..5b8c845d1 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -210,45 +210,52 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri } } - for sn := range FindFilteredSnapshots(ctx, snapshotLister, repo, opts.Hosts, opts.Tags, opts.Paths, args[:1]) { - printSnapshot(sn) + id, err := restic.FindFilteredSnapshot(ctx, snapshotLister, repo, opts.Hosts, opts.Tags, opts.Paths, args[0]) + if err != nil { + return err + } + sn, err := restic.LoadSnapshot(ctx, repo, id) + if err != nil { + return err + } - err := walker.Walk(ctx, repo, *sn.Tree, nil, func(_ restic.ID, nodepath string, node *restic.Node, err error) (bool, error) { - if err != nil { - return false, err - } - if node == nil { - return false, nil - } - - if withinDir(nodepath) { - // if we're within a dir, print the node - printNode(nodepath, node) - - // if recursive listing is requested, signal the walker that it - // should continue walking recursively - if opts.Recursive { - return false, nil - } - } - - // if there's an upcoming match deeper in the tree (but we're not - // there yet), signal the walker to descend into any subdirs - if approachingMatchingTree(nodepath) { - return false, nil - } - - // otherwise, signal the walker to not walk recursively into any - // subdirs - if node.Type == "dir" { - return false, walker.ErrSkipNode - } - return false, nil - }) + printSnapshot(sn) + err = walker.Walk(ctx, repo, *sn.Tree, nil, func(_ restic.ID, nodepath string, node *restic.Node, err error) (bool, error) { if err != nil { - return err + return false, err } + if node == nil { + return false, nil + } + + if withinDir(nodepath) { + // if we're within a dir, print the node + printNode(nodepath, node) + + // if recursive listing is requested, signal the walker that it + // should continue walking recursively + if opts.Recursive { + return false, nil + } + } + + // if there's an upcoming match deeper in the tree (but we're not + // there yet), signal the walker to descend into any subdirs + if approachingMatchingTree(nodepath) { + return false, nil + } + + // otherwise, signal the walker to not walk recursively into any + // subdirs + if node.Type == "dir" { + return false, walker.ErrSkipNode + } + return false, nil + }) + + if err != nil { + return err } return nil