restic/src/cmds/restic/cmd_cat.go

209 lines
3.9 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
"encoding/json"
"fmt"
"os"
"restic"
"restic/backend"
"restic/debug"
"restic/pack"
"restic/repository"
2014-10-05 12:44:59 +00:00
)
type CmdCat struct {
global *GlobalOptions
}
2014-12-07 15:30:52 +00:00
2014-11-30 21:39:58 +00:00
func init() {
2014-12-07 15:30:52 +00:00
_, err := parser.AddCommand("cat",
"dump something",
"The cat command dumps data structures or data from a repository",
&CmdCat{global: &globalOpts})
2014-12-07 15:30:52 +00:00
if err != nil {
panic(err)
}
}
func (cmd CmdCat) Usage() string {
2015-05-03 15:37:21 +00:00
return "[pack|blob|tree|snapshot|key|masterkey|config|lock] ID"
2014-11-30 21:39:58 +00:00
}
2014-12-07 15:30:52 +00:00
func (cmd CmdCat) Execute(args []string) error {
2015-05-03 15:37:21 +00:00
if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) {
return restic.Fatalf("type or ID not specified, Usage: %s", cmd.Usage())
2014-12-07 15:30:52 +00:00
}
repo, err := cmd.global.OpenRepository()
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-10-05 12:44:59 +00:00
}
lock, err := lockRepo(repo)
defer unlockRepo(lock)
2015-06-27 12:40:18 +00:00
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
tpe := args[0]
var id backend.ID
2015-05-03 15:37:21 +00:00
if tpe != "masterkey" && tpe != "config" {
id, err = backend.ParseID(args[1])
if err != nil {
if tpe != "snapshot" {
2016-08-30 19:19:04 +00:00
return restic.Fatalf("unable to parse ID: %v\n", err)
}
// find snapshot id with prefix
id, err = restic.FindSnapshot(repo, args[1])
if err != nil {
return err
}
}
2014-10-05 12:44:59 +00:00
}
// handle all types that don't need an index
2014-10-05 12:44:59 +00:00
switch tpe {
2015-05-03 15:37:21 +00:00
case "config":
buf, err := json.MarshalIndent(repo.Config, "", " ")
2015-05-03 15:37:21 +00:00
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "index":
buf, err := repo.LoadAndDecrypt(backend.Index, id)
if err != nil {
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return err
2014-10-05 12:44:59 +00:00
case "snapshot":
sn := &restic.Snapshot{}
err = repo.LoadJSONUnpacked(backend.Snapshot, id, sn)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&sn, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "key":
2016-01-23 23:18:12 +00:00
h := backend.Handle{Type: backend.Key, Name: id.String()}
buf, err := backend.LoadAll(repo.Backend(), h, nil)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2016-01-23 23:18:12 +00:00
key := &repository.Key{}
err = json.Unmarshal(buf, key)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2016-01-23 23:18:12 +00:00
buf, err = json.MarshalIndent(&key, "", " ")
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "masterkey":
buf, err := json.MarshalIndent(repo.Key(), "", " ")
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
fmt.Println(string(buf))
2014-10-05 12:44:59 +00:00
return nil
case "lock":
lock, err := restic.LoadLock(repo, id)
2015-06-24 16:17:01 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&lock, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
}
// load index, handle all the other types
err = repo.LoadIndex()
if err != nil {
return err
}
switch tpe {
case "pack":
2016-01-23 23:18:12 +00:00
h := backend.Handle{Type: backend.Data, Name: id.String()}
buf, err := backend.LoadAll(repo.Backend(), h, nil)
if err != nil {
return err
}
hash := backend.Hash(buf)
if !hash.Equal(id) {
fmt.Fprintf(cmd.global.stderr, "Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String())
}
2016-01-23 23:18:12 +00:00
_, err = os.Stdout.Write(buf)
return err
case "blob":
2016-08-28 19:23:46 +00:00
for _, t := range []pack.BlobType{pack.Data, pack.Tree} {
list, err := repo.Index().Lookup(id, t)
if err != nil {
continue
}
blob := list[0]
2016-08-28 19:23:46 +00:00
buf := make([]byte, blob.Length)
data, err := repo.LoadBlob(id, t, buf)
if err != nil {
return err
}
_, err = os.Stdout.Write(data)
return err
}
return restic.Fatal("blob not found")
case "tree":
debug.Log("cat", "cat tree %v", id.Str())
tree := restic.NewTree()
err = repo.LoadJSONPack(pack.Tree, id, tree)
if err != nil {
debug.Log("cat", "unable to load tree %v: %v", id.Str(), err)
return err
}
buf, err := json.MarshalIndent(&tree, "", " ")
if err != nil {
debug.Log("cat", "error json.MarshalIndent(): %v", err)
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return nil
2014-10-05 12:44:59 +00:00
default:
return restic.Fatal("invalid type")
2014-10-05 12:44:59 +00:00
}
}