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