restic/src/cmds/restic/cmd_ls.go

116 lines
2.5 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
"fmt"
"os"
"path/filepath"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
"restic"
2016-09-01 20:17:37 +00:00
"restic/errors"
"restic/repository"
2014-10-05 12:44:59 +00:00
)
2016-09-17 10:36:05 +00:00
var cmdLs = &cobra.Command{
Use: "ls [flags] snapshot-ID",
Short: "list files in a snapshot",
Long: `
The "ls" command allows listing files and directories in a snapshot.
2017-01-12 11:24:08 +00:00
The special snapshot-ID "latest" can be used to list files and directories of the latest snapshot in the repository.
2016-09-17 10:36:05 +00:00
`,
RunE: func(cmd *cobra.Command, args []string) error {
return runLs(globalOptions, args)
},
}
2014-12-07 15:30:52 +00:00
2016-09-17 10:36:05 +00:00
var listLong bool
2014-11-30 21:39:58 +00:00
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdLs)
cmdLs.Flags().BoolVarP(&listLong, "long", "l", false, "use a long listing format showing size and mode")
2014-11-30 21:39:58 +00:00
}
2016-09-17 10:36:05 +00:00
func printNode(prefix string, n *restic.Node) string {
if !listLong {
return filepath.Join(prefix, n.Name)
}
2016-09-01 19:20:03 +00:00
switch n.Type {
2014-10-05 12:44:59 +00:00
case "file":
return fmt.Sprintf("%s %5d %5d %6d %s %s",
2016-10-02 18:31:28 +00:00
n.Mode, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name))
2014-10-05 12:44:59 +00:00
case "dir":
return fmt.Sprintf("%s %5d %5d %6d %s %s",
2016-10-02 18:31:28 +00:00
n.Mode|os.ModeDir, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name))
2014-10-05 12:44:59 +00:00
case "symlink":
return fmt.Sprintf("%s %5d %5d %6d %s %s -> %s",
2016-10-02 18:31:28 +00:00
n.Mode|os.ModeSymlink, n.UID, n.GID, n.Size, n.ModTime.Format(TimeFormat), filepath.Join(prefix, n.Name), n.LinkTarget)
2014-10-05 12:44:59 +00:00
default:
2016-09-01 19:20:03 +00:00
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
2014-10-05 12:44:59 +00:00
}
}
2016-09-17 10:36:05 +00:00
func printTree(prefix string, repo *repository.Repository, id restic.ID) error {
tree, err := repo.LoadTree(id)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
for _, entry := range tree.Nodes {
2016-09-17 10:36:05 +00:00
Printf(printNode(prefix, entry) + "\n")
2014-10-05 12:44:59 +00:00
2016-09-01 19:20:03 +00:00
if entry.Type == "dir" && entry.Subtree != nil {
2016-09-17 10:36:05 +00:00
err = printTree(filepath.Join(prefix, entry.Name), repo, *entry.Subtree)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
}
}
return nil
}
2016-09-17 10:36:05 +00:00
func runLs(gopts GlobalOptions, args []string) error {
2014-10-05 12:44:59 +00:00
if len(args) < 1 || len(args) > 2 {
2016-09-17 10:36:05 +00:00
return errors.Fatalf("no snapshot ID given")
2014-12-07 15:30:52 +00:00
}
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-10-05 12:44:59 +00:00
}
err = repo.LoadIndex()
if err != nil {
return err
}
2017-01-12 11:24:08 +00:00
snapshotIDString := args[0]
var id restic.ID
var paths []string
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(repo, paths, "")
if err != nil {
return err
}
} else {
id, err = restic.FindSnapshot(repo, snapshotIDString)
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
}
sn, err := restic.LoadSnapshot(repo, id)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("snapshot of %v at %s:\n", sn.Paths, sn.Time)
2014-10-05 12:44:59 +00:00
2016-09-17 10:36:05 +00:00
return printTree("", repo, *sn.Tree)
2014-10-05 12:44:59 +00:00
}