2014-10-05 12:44:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-31 22:08:13 +00:00
|
|
|
"context"
|
2018-08-14 08:50:31 +00:00
|
|
|
"encoding/json"
|
2023-10-30 10:51:11 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-08-14 08:50:31 +00:00
|
|
|
"os"
|
2018-08-11 01:48:42 +00:00
|
|
|
"strings"
|
2018-08-14 08:50:31 +00:00
|
|
|
"time"
|
2014-10-05 12:44:59 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2018-08-11 21:25:22 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2018-06-09 21:31:31 +00:00
|
|
|
"github.com/restic/restic/internal/walker"
|
2014-10-05 12:44:59 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdLs = &cobra.Command{
|
2020-08-29 12:28:53 +00:00
|
|
|
Use: "ls [flags] snapshotID [dir...]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "List files in a snapshot",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
2018-08-11 04:10:02 +00:00
|
|
|
The "ls" command lists files and directories in a snapshot.
|
|
|
|
|
|
|
|
The special snapshot ID "latest" can be used to list files and
|
|
|
|
directories of the latest snapshot in the repository. The
|
|
|
|
--host flag can be used in conjunction to select the latest
|
|
|
|
snapshot originating from a certain host only.
|
|
|
|
|
|
|
|
File listings can optionally be filtered by directories. Any
|
|
|
|
positional arguments after the snapshot ID are interpreted as
|
2018-08-11 21:25:22 +00:00
|
|
|
absolute directory paths, and only files inside those directories
|
|
|
|
will be listed. If the --recursive flag is used, then the filter
|
2018-08-11 04:10:02 +00:00
|
|
|
will allow traversing into matching directories' subfolders.
|
2018-08-11 21:25:22 +00:00
|
|
|
Any directory paths specified must be absolute (starting with
|
|
|
|
a path separator); paths use the forward slash '/' as separator.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
2024-06-30 15:52:50 +00:00
|
|
|
Exit status is 0 if the command was successful.
|
|
|
|
Exit status is 1 if there was any error.
|
2024-07-10 19:46:26 +00:00
|
|
|
Exit status is 10 if the repository does not exist.
|
2024-06-30 15:53:33 +00:00
|
|
|
Exit status is 11 if the repository is already locked.
|
2024-07-30 23:06:18 +00:00
|
|
|
Exit status is 12 if the password is incorrect.
|
2016-09-17 10:36:05 +00:00
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2024-08-23 21:48:45 +00:00
|
|
|
GroupID: cmdGroupDefault,
|
2016-09-17 10:36:05 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-02 21:24:37 +00:00
|
|
|
return runLs(cmd.Context(), lsOptions, globalOptions, args)
|
2016-09-17 10:36:05 +00:00
|
|
|
},
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2017-01-14 03:19:47 +00:00
|
|
|
// LsOptions collects all options for the ls command.
|
|
|
|
type LsOptions struct {
|
2022-09-02 22:19:19 +00:00
|
|
|
ListLong bool
|
2023-02-17 15:13:46 +00:00
|
|
|
restic.SnapshotFilter
|
2023-06-02 01:18:18 +00:00
|
|
|
Recursive bool
|
|
|
|
HumanReadable bool
|
2023-10-30 10:51:11 +00:00
|
|
|
Ncdu bool
|
2017-01-14 03:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var lsOptions LsOptions
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdLs)
|
|
|
|
|
2017-01-14 03:19:47 +00:00
|
|
|
flags := cmdLs.Flags()
|
2023-02-17 15:13:46 +00:00
|
|
|
initSingleSnapshotFilter(flags, &lsOptions.SnapshotFilter)
|
2017-01-14 03:19:47 +00:00
|
|
|
flags.BoolVarP(&lsOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode")
|
2018-08-11 01:48:42 +00:00
|
|
|
flags.BoolVar(&lsOptions.Recursive, "recursive", false, "include files in subfolders of the listed directories")
|
2023-06-02 01:18:18 +00:00
|
|
|
flags.BoolVar(&lsOptions.HumanReadable, "human-readable", false, "print sizes in human readable format")
|
2024-01-21 15:09:05 +00:00
|
|
|
flags.BoolVar(&lsOptions.Ncdu, "ncdu", false, "output NCDU export format (pipe into 'ncdu -f -')")
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
type lsPrinter interface {
|
|
|
|
Snapshot(sn *restic.Snapshot)
|
2024-07-07 11:28:56 +00:00
|
|
|
Node(path string, node *restic.Node, isPrefixDirectory bool)
|
2024-01-20 22:59:33 +00:00
|
|
|
LeaveDir(path string)
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonLsPrinter struct {
|
|
|
|
enc *json.Encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *jsonLsPrinter) Snapshot(sn *restic.Snapshot) {
|
|
|
|
type lsSnapshot struct {
|
|
|
|
*restic.Snapshot
|
2024-01-27 14:48:24 +00:00
|
|
|
ID *restic.ID `json:"id"`
|
|
|
|
ShortID string `json:"short_id"`
|
|
|
|
MessageType string `json:"message_type"` // "snapshot"
|
|
|
|
StructType string `json:"struct_type"` // "snapshot", deprecated
|
2024-01-20 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.enc.Encode(lsSnapshot{
|
2024-01-27 14:48:24 +00:00
|
|
|
Snapshot: sn,
|
|
|
|
ID: sn.ID(),
|
|
|
|
ShortID: sn.ID().Str(),
|
|
|
|
MessageType: "snapshot",
|
|
|
|
StructType: "snapshot",
|
2024-01-20 22:59:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
Warnf("JSON encode failed: %v\n", err)
|
|
|
|
}
|
2018-08-14 08:50:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:56:36 +00:00
|
|
|
// Print node in our custom JSON format, followed by a newline.
|
2024-07-07 11:28:56 +00:00
|
|
|
func (p *jsonLsPrinter) Node(path string, node *restic.Node, isPrefixDirectory bool) {
|
|
|
|
if isPrefixDirectory {
|
|
|
|
return
|
|
|
|
}
|
2024-01-20 22:59:33 +00:00
|
|
|
err := lsNodeJSON(p.enc, path, node)
|
|
|
|
if err != nil {
|
|
|
|
Warnf("JSON encode failed: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:56:36 +00:00
|
|
|
func lsNodeJSON(enc *json.Encoder, path string, node *restic.Node) error {
|
|
|
|
n := &struct {
|
2021-11-13 12:37:03 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
UID uint32 `json:"uid"`
|
|
|
|
GID uint32 `json:"gid"`
|
|
|
|
Size *uint64 `json:"size,omitempty"`
|
|
|
|
Mode os.FileMode `json:"mode,omitempty"`
|
|
|
|
Permissions string `json:"permissions,omitempty"`
|
|
|
|
ModTime time.Time `json:"mtime,omitempty"`
|
|
|
|
AccessTime time.Time `json:"atime,omitempty"`
|
|
|
|
ChangeTime time.Time `json:"ctime,omitempty"`
|
2023-10-11 18:35:43 +00:00
|
|
|
Inode uint64 `json:"inode,omitempty"`
|
2024-01-27 14:48:24 +00:00
|
|
|
MessageType string `json:"message_type"` // "node"
|
|
|
|
StructType string `json:"struct_type"` // "node", deprecated
|
2021-02-01 14:56:36 +00:00
|
|
|
|
|
|
|
size uint64 // Target for Size pointer.
|
|
|
|
}{
|
2021-11-13 12:37:03 +00:00
|
|
|
Name: node.Name,
|
2024-07-09 17:51:44 +00:00
|
|
|
Type: string(node.Type),
|
2021-11-13 12:37:03 +00:00
|
|
|
Path: path,
|
|
|
|
UID: node.UID,
|
|
|
|
GID: node.GID,
|
|
|
|
size: node.Size,
|
|
|
|
Mode: node.Mode,
|
|
|
|
Permissions: node.Mode.String(),
|
|
|
|
ModTime: node.ModTime,
|
|
|
|
AccessTime: node.AccessTime,
|
|
|
|
ChangeTime: node.ChangeTime,
|
2023-10-11 18:35:43 +00:00
|
|
|
Inode: node.Inode,
|
2024-01-27 14:48:24 +00:00
|
|
|
MessageType: "node",
|
2021-11-13 12:37:03 +00:00
|
|
|
StructType: "node",
|
2021-02-01 14:56:36 +00:00
|
|
|
}
|
|
|
|
// Always print size for regular files, even when empty,
|
|
|
|
// but never for other types.
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeFile {
|
2021-02-01 14:56:36 +00:00
|
|
|
n.Size = &n.size
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc.Encode(n)
|
2018-08-14 08:50:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 14:58:49 +00:00
|
|
|
func (p *jsonLsPrinter) LeaveDir(_ string) {}
|
|
|
|
func (p *jsonLsPrinter) Close() {}
|
2024-01-20 22:59:33 +00:00
|
|
|
|
|
|
|
type ncduLsPrinter struct {
|
2024-01-20 22:37:54 +00:00
|
|
|
out io.Writer
|
|
|
|
depth int
|
|
|
|
}
|
|
|
|
|
2023-10-30 10:51:11 +00:00
|
|
|
// lsSnapshotNcdu prints a restic snapshot in Ncdu save format.
|
|
|
|
// It opens the JSON list. Nodes are added with lsNodeNcdu and the list is closed by lsCloseNcdu.
|
|
|
|
// Format documentation: https://dev.yorhel.nl/ncdu/jsonfmt
|
2024-01-20 22:59:33 +00:00
|
|
|
func (p *ncduLsPrinter) Snapshot(sn *restic.Snapshot) {
|
2023-10-30 10:51:11 +00:00
|
|
|
const NcduMajorVer = 1
|
|
|
|
const NcduMinorVer = 2
|
|
|
|
|
|
|
|
snapshotBytes, err := json.Marshal(sn)
|
|
|
|
if err != nil {
|
|
|
|
Warnf("JSON encode failed: %v\n", err)
|
|
|
|
}
|
2024-01-20 22:37:54 +00:00
|
|
|
p.depth++
|
2024-07-12 21:46:50 +00:00
|
|
|
fmt.Fprintf(p.out, "[%d, %d, %s, [{\"name\":\"/\"}", NcduMajorVer, NcduMinorVer, string(snapshotBytes))
|
2023-10-30 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 14:58:49 +00:00
|
|
|
func lsNcduNode(_ string, node *restic.Node) ([]byte, error) {
|
2023-10-30 10:51:11 +00:00
|
|
|
type NcduNode struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Asize uint64 `json:"asize"`
|
|
|
|
Dsize uint64 `json:"dsize"`
|
|
|
|
Dev uint64 `json:"dev"`
|
|
|
|
Ino uint64 `json:"ino"`
|
|
|
|
NLink uint64 `json:"nlink"`
|
|
|
|
NotReg bool `json:"notreg"`
|
2024-01-21 14:58:49 +00:00
|
|
|
UID uint32 `json:"uid"`
|
|
|
|
GID uint32 `json:"gid"`
|
2023-10-30 10:51:11 +00:00
|
|
|
Mode uint16 `json:"mode"`
|
|
|
|
Mtime int64 `json:"mtime"`
|
|
|
|
}
|
|
|
|
|
2024-07-07 11:33:25 +00:00
|
|
|
const blockSize = 512
|
|
|
|
|
2023-10-30 10:51:11 +00:00
|
|
|
outNode := NcduNode{
|
2024-07-07 11:33:25 +00:00
|
|
|
Name: node.Name,
|
|
|
|
Asize: node.Size,
|
|
|
|
// round up to nearest full blocksize
|
|
|
|
Dsize: (node.Size + blockSize - 1) / blockSize * blockSize,
|
2023-10-30 10:51:11 +00:00
|
|
|
Dev: node.DeviceID,
|
|
|
|
Ino: node.Inode,
|
|
|
|
NLink: node.Links,
|
2024-07-09 17:51:44 +00:00
|
|
|
NotReg: node.Type != restic.NodeTypeDir && node.Type != restic.NodeTypeFile,
|
2024-01-21 14:58:49 +00:00
|
|
|
UID: node.UID,
|
|
|
|
GID: node.GID,
|
2024-01-21 14:37:02 +00:00
|
|
|
Mode: uint16(node.Mode & os.ModePerm),
|
2023-10-30 10:51:11 +00:00
|
|
|
Mtime: node.ModTime.Unix(),
|
|
|
|
}
|
2024-01-21 14:37:02 +00:00
|
|
|
// bits according to inode(7) manpage
|
|
|
|
if node.Mode&os.ModeSetuid != 0 {
|
|
|
|
outNode.Mode |= 0o4000
|
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSetgid != 0 {
|
|
|
|
outNode.Mode |= 0o2000
|
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSticky != 0 {
|
|
|
|
outNode.Mode |= 0o1000
|
|
|
|
}
|
2024-07-13 08:21:02 +00:00
|
|
|
if outNode.Mtime < 0 {
|
|
|
|
// ncdu does not allow negative times
|
|
|
|
outNode.Mtime = 0
|
|
|
|
}
|
2023-10-30 10:51:11 +00:00
|
|
|
|
2024-01-21 14:37:02 +00:00
|
|
|
return json.Marshal(outNode)
|
|
|
|
}
|
|
|
|
|
2024-07-07 11:28:56 +00:00
|
|
|
func (p *ncduLsPrinter) Node(path string, node *restic.Node, _ bool) {
|
2024-01-21 14:58:49 +00:00
|
|
|
out, err := lsNcduNode(path, node)
|
2023-10-30 10:51:11 +00:00
|
|
|
if err != nil {
|
|
|
|
Warnf("JSON encode failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeDir {
|
2024-01-21 14:58:49 +00:00
|
|
|
fmt.Fprintf(p.out, ",\n%s[\n%s%s", strings.Repeat(" ", p.depth), strings.Repeat(" ", p.depth+1), string(out))
|
2024-01-20 22:37:54 +00:00
|
|
|
p.depth++
|
2023-10-30 10:51:11 +00:00
|
|
|
} else {
|
2024-01-21 14:58:49 +00:00
|
|
|
fmt.Fprintf(p.out, ",\n%s%s", strings.Repeat(" ", p.depth), string(out))
|
2023-10-30 10:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 14:58:49 +00:00
|
|
|
func (p *ncduLsPrinter) LeaveDir(_ string) {
|
2024-01-20 22:37:54 +00:00
|
|
|
p.depth--
|
|
|
|
fmt.Fprintf(p.out, "\n%s]", strings.Repeat(" ", p.depth))
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
func (p *ncduLsPrinter) Close() {
|
2024-07-12 19:22:37 +00:00
|
|
|
fmt.Fprint(p.out, "\n]\n]\n")
|
2023-10-30 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
type textLsPrinter struct {
|
|
|
|
dirs []string
|
|
|
|
ListLong bool
|
|
|
|
HumanReadable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *textLsPrinter) Snapshot(sn *restic.Snapshot) {
|
|
|
|
Verbosef("%v filtered by %v:\n", sn, p.dirs)
|
|
|
|
}
|
2024-07-07 11:28:56 +00:00
|
|
|
func (p *textLsPrinter) Node(path string, node *restic.Node, isPrefixDirectory bool) {
|
|
|
|
if !isPrefixDirectory {
|
|
|
|
Printf("%s\n", formatNode(path, node, p.ListLong, p.HumanReadable))
|
|
|
|
}
|
2024-01-20 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 14:58:49 +00:00
|
|
|
func (p *textLsPrinter) LeaveDir(_ string) {}
|
|
|
|
func (p *textLsPrinter) Close() {}
|
2024-01-20 22:59:33 +00:00
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []string) error {
|
2020-08-29 12:28:53 +00:00
|
|
|
if len(args) == 0 {
|
2021-12-27 17:12:27 +00:00
|
|
|
return errors.Fatal("no snapshot ID specified, specify snapshot ID or use special ID 'latest'")
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
2024-01-21 15:09:05 +00:00
|
|
|
if opts.Ncdu && gopts.JSON {
|
|
|
|
return errors.Fatal("only either '--json' or '--ncdu' can be specified")
|
|
|
|
}
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2018-08-11 01:48:42 +00:00
|
|
|
// extract any specific directories to walk
|
2018-08-11 04:10:02 +00:00
|
|
|
var dirs []string
|
|
|
|
if len(args) > 1 {
|
|
|
|
dirs = args[1:]
|
2018-08-11 21:25:22 +00:00
|
|
|
for _, dir := range dirs {
|
|
|
|
if !strings.HasPrefix(dir, "/") {
|
|
|
|
return errors.Fatal("All path filters must be absolute, starting with a forward slash '/'")
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 04:10:02 +00:00
|
|
|
}
|
2018-08-11 01:48:42 +00:00
|
|
|
|
2018-08-12 21:13:34 +00:00
|
|
|
withinDir := func(nodepath string) bool {
|
|
|
|
if len(dirs) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
// we're within one of the selected dirs, example:
|
|
|
|
// nodepath: "/test/foo"
|
|
|
|
// dir: "/test"
|
|
|
|
if fs.HasPathPrefix(dir, nodepath) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
approachingMatchingTree := func(nodepath string) bool {
|
|
|
|
if len(dirs) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
// the current node path is a prefix for one of the
|
|
|
|
// directories, so we're interested in something deeper in the
|
|
|
|
// tree. Example:
|
|
|
|
// nodepath: "/test"
|
|
|
|
// dir: "/test/foo"
|
|
|
|
if fs.HasPathPrefix(nodepath, dir) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-24 20:52:28 +00:00
|
|
|
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
|
2018-08-12 19:59:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 20:52:28 +00:00
|
|
|
defer unlock()
|
2018-08-12 19:59:57 +00:00
|
|
|
|
2023-10-01 11:05:56 +00:00
|
|
|
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
|
2021-11-06 00:14:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
if err = repo.LoadIndex(ctx, bar); err != nil {
|
2018-08-12 19:59:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
var printer lsPrinter
|
2018-08-14 08:50:31 +00:00
|
|
|
|
|
|
|
if gopts.JSON {
|
2024-01-20 22:59:33 +00:00
|
|
|
printer = &jsonLsPrinter{
|
|
|
|
enc: json.NewEncoder(globalOptions.stdout),
|
2018-08-14 08:50:31 +00:00
|
|
|
}
|
2023-10-30 10:51:11 +00:00
|
|
|
} else if opts.Ncdu {
|
2024-01-20 22:59:33 +00:00
|
|
|
printer = &ncduLsPrinter{
|
2024-01-20 22:37:54 +00:00
|
|
|
out: globalOptions.stdout,
|
2023-10-30 10:51:11 +00:00
|
|
|
}
|
2018-08-14 08:50:31 +00:00
|
|
|
} else {
|
2024-01-20 22:59:33 +00:00
|
|
|
printer = &textLsPrinter{
|
|
|
|
dirs: dirs,
|
|
|
|
ListLong: opts.ListLong,
|
|
|
|
HumanReadable: opts.HumanReadable,
|
2018-08-14 08:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 14:50:54 +00:00
|
|
|
sn, subfolder, err := (&restic.SnapshotFilter{
|
2023-02-17 15:13:46 +00:00
|
|
|
Hosts: opts.Hosts,
|
|
|
|
Paths: opts.Paths,
|
|
|
|
Tags: opts.Tags,
|
|
|
|
}).FindLatest(ctx, snapshotLister, repo, args[0])
|
2022-10-03 11:05:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-12 11:24:08 +00:00
|
|
|
|
2023-07-16 14:50:54 +00:00
|
|
|
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
|
2023-05-18 21:15:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
printer.Snapshot(sn)
|
2018-08-11 01:48:42 +00:00
|
|
|
|
2024-01-20 22:36:08 +00:00
|
|
|
processNode := func(_ restic.ID, nodepath string, node *restic.Node, err error) error {
|
2022-10-03 11:05:36 +00:00
|
|
|
if err != nil {
|
2024-01-06 12:59:47 +00:00
|
|
|
return err
|
2022-10-03 11:05:36 +00:00
|
|
|
}
|
|
|
|
if node == nil {
|
2024-01-06 12:59:47 +00:00
|
|
|
return nil
|
2022-10-03 11:05:36 +00:00
|
|
|
}
|
2018-08-11 21:25:22 +00:00
|
|
|
|
2024-07-07 11:08:41 +00:00
|
|
|
printedDir := false
|
2022-10-03 11:05:36 +00:00
|
|
|
if withinDir(nodepath) {
|
2024-07-07 11:28:56 +00:00
|
|
|
// if we're within a target path, print the node
|
|
|
|
printer.Node(nodepath, node, false)
|
2024-07-07 11:08:41 +00:00
|
|
|
printedDir = true
|
2018-08-11 01:48:42 +00:00
|
|
|
|
2022-10-03 11:05:36 +00:00
|
|
|
// if recursive listing is requested, signal the walker that it
|
|
|
|
// should continue walking recursively
|
|
|
|
if opts.Recursive {
|
2024-01-06 12:59:47 +00:00
|
|
|
return nil
|
2018-08-12 21:13:34 +00:00
|
|
|
}
|
2022-10-03 11:05:36 +00:00
|
|
|
}
|
2018-08-11 21:25:22 +00:00
|
|
|
|
2022-10-03 11:05:36 +00:00
|
|
|
// 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) {
|
2024-07-07 11:28:56 +00:00
|
|
|
// print node leading up to the target paths
|
|
|
|
if !printedDir {
|
|
|
|
printer.Node(nodepath, node, true)
|
|
|
|
}
|
2024-01-06 12:59:47 +00:00
|
|
|
return nil
|
2022-10-03 11:05:36 +00:00
|
|
|
}
|
2018-08-12 21:13:34 +00:00
|
|
|
|
2022-10-03 11:05:36 +00:00
|
|
|
// otherwise, signal the walker to not walk recursively into any
|
|
|
|
// subdirs
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeDir {
|
2024-07-07 11:28:56 +00:00
|
|
|
// immediately generate leaveDir if the directory is skipped
|
2024-07-07 11:08:41 +00:00
|
|
|
if printedDir {
|
|
|
|
printer.LeaveDir(nodepath)
|
|
|
|
}
|
2024-01-06 12:59:47 +00:00
|
|
|
return walker.ErrSkipNode
|
2017-01-12 11:24:08 +00:00
|
|
|
}
|
2024-01-06 12:59:47 +00:00
|
|
|
return nil
|
2024-01-20 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = walker.Walk(ctx, repo, *sn.Tree, walker.WalkVisitor{
|
|
|
|
ProcessNode: processNode,
|
2024-01-20 22:37:54 +00:00
|
|
|
LeaveDir: func(path string) {
|
2024-01-20 22:59:33 +00:00
|
|
|
// the root path `/` has no corresponding node and is thus also skipped by processNode
|
2024-07-07 11:28:56 +00:00
|
|
|
if path != "/" {
|
2024-01-20 22:59:33 +00:00
|
|
|
printer.LeaveDir(path)
|
2024-01-20 22:37:54 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-03 11:05:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
2018-08-19 06:18:43 +00:00
|
|
|
|
2024-01-20 22:59:33 +00:00
|
|
|
printer.Close()
|
2018-08-19 06:18:43 +00:00
|
|
|
return nil
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|