2014-10-05 12:44:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
2014-10-05 12:44:59 +00:00
|
|
|
)
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
type CmdLs struct{}
|
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2014-12-07 15:30:52 +00:00
|
|
|
_, err := parser.AddCommand("ls",
|
|
|
|
"list files",
|
|
|
|
"The ls command lists all files and directories in a snapshot",
|
|
|
|
&CmdLs{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
func print_node(prefix string, n *restic.Node) string {
|
2014-10-05 12:44:59 +00:00
|
|
|
switch n.Type {
|
|
|
|
case "file":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
|
|
n.Mode, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name))
|
|
|
|
case "dir":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
|
|
n.Mode|os.ModeDir, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name))
|
|
|
|
case "symlink":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s -> %s",
|
|
|
|
n.Mode|os.ModeSymlink, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name), n.LinkTarget)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
func print_tree(prefix string, s restic.Server, blob restic.Blob) error {
|
2015-02-17 21:37:34 +00:00
|
|
|
tree, err := restic.LoadTree(s, blob.Storage)
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, entry := range tree.Nodes {
|
2014-10-05 12:44:59 +00:00
|
|
|
fmt.Println(print_node(prefix, entry))
|
|
|
|
|
|
|
|
if entry.Type == "dir" && entry.Subtree != nil {
|
2015-01-10 22:40:10 +00:00
|
|
|
b, err := tree.Map.FindID(entry.Subtree)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = print_tree(filepath.Join(prefix, entry.Name), s, b)
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:30:52 +00:00
|
|
|
func (cmd CmdLs) Usage() string {
|
2014-12-22 13:46:54 +00:00
|
|
|
return "snapshot-ID [DIR]"
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 10:17:34 +00:00
|
|
|
func (cmd CmdLs) Execute(args []string) error {
|
2014-10-05 12:44:59 +00:00
|
|
|
if len(args) < 1 || len(args) > 2 {
|
2014-12-07 15:30:52 +00:00
|
|
|
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:10:19 +00:00
|
|
|
s, err := OpenRepo()
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-21 17:10:19 +00:00
|
|
|
id, err := backend.FindSnapshot(s, args[0])
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
sn, err := restic.LoadSnapshot(s, id)
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("snapshot of %s at %s:\n", sn.Dir, sn.Time)
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
return print_tree("", s, sn.Tree)
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|