2014-10-05 12:44:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-31 22:08:13 +00:00
|
|
|
"context"
|
2014-10-05 12:44:59 +00:00
|
|
|
"encoding/json"
|
2023-08-12 18:24:40 +00:00
|
|
|
"strings"
|
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/backend"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2014-10-05 12:44:59 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdCat = &cobra.Command{
|
2023-07-16 14:50:54 +00:00
|
|
|
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:subfolder]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "Print internal objects to stdout",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
|
|
|
The "cat" command is used to print internal objects to stdout.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2016-09-17 10:36:05 +00:00
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
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 runCat(cmd.Context(), 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
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdCat)
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 17:17:46 +00:00
|
|
|
func validateCatArgs(args []string) error {
|
|
|
|
var allowedCmds = []string{"config", "index", "snapshot", "key", "masterkey", "lock", "pack", "blob", "tree"}
|
2023-08-12 18:24:40 +00:00
|
|
|
|
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.Fatal("type not specified")
|
|
|
|
}
|
|
|
|
|
2023-08-13 17:17:46 +00:00
|
|
|
validType := false
|
|
|
|
for _, v := range allowedCmds {
|
|
|
|
if v == args[0] {
|
|
|
|
validType = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !validType {
|
2023-08-12 18:24:40 +00:00
|
|
|
return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(allowedCmds, "|"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] != "masterkey" && args[0] != "config" && len(args) != 2 {
|
|
|
|
return errors.Fatal("ID not specified")
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 17:17:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCat(ctx context.Context, gopts GlobalOptions, args []string) error {
|
|
|
|
if err := validateCatArgs(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
repo, err := OpenRepository(ctx, gopts)
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 07:04:46 +00:00
|
|
|
if !gopts.NoLock {
|
2021-10-31 22:19:27 +00:00
|
|
|
var lock *restic.Lock
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, ctx, err = lockRepo(ctx, repo, gopts.RetryLock, gopts.JSON)
|
2021-10-31 22:19:27 +00:00
|
|
|
defer unlockRepo(lock)
|
2020-11-26 07:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 12:44:59 +00:00
|
|
|
tpe := args[0]
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
var id restic.ID
|
2023-05-18 21:17:12 +00:00
|
|
|
if tpe != "masterkey" && tpe != "config" && tpe != "snapshot" && tpe != "tree" {
|
2016-09-01 14:04:29 +00:00
|
|
|
id, err = restic.ParseID(args[1])
|
2015-03-22 20:03:01 +00:00
|
|
|
if err != nil {
|
2022-10-03 12:48:14 +00:00
|
|
|
return errors.Fatalf("unable to parse ID: %v\n", err)
|
2014-11-24 20:12:32 +00:00
|
|
|
}
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch tpe {
|
2015-05-03 15:37:21 +00:00
|
|
|
case "config":
|
2016-09-01 14:04:29 +00:00
|
|
|
buf, err := json.MarshalIndent(repo.Config(), "", " ")
|
2015-05-03 15:37:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Println(string(buf))
|
2015-05-03 15:37:21 +00:00
|
|
|
return nil
|
2015-04-26 15:44:38 +00:00
|
|
|
case "index":
|
2023-01-27 14:01:54 +00:00
|
|
|
buf, err := repo.LoadUnpacked(ctx, restic.IndexFile, id)
|
2014-12-21 16:29:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:45:36 +00:00
|
|
|
Println(string(buf))
|
|
|
|
return nil
|
2014-10-05 12:44:59 +00:00
|
|
|
case "snapshot":
|
2023-10-01 11:05:56 +00:00
|
|
|
sn, _, err := restic.FindSnapshot(ctx, repo, repo, args[1])
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
2022-10-03 12:48:14 +00:00
|
|
|
return errors.Fatalf("could not find snapshot: %v\n", err)
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
buf, err := json.MarshalIndent(sn, "", " ")
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Println(string(buf))
|
2014-10-05 12:44:59 +00:00
|
|
|
return nil
|
|
|
|
case "key":
|
2022-10-15 14:01:38 +00:00
|
|
|
key, err := repository.LoadKey(ctx, repo, id)
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:10:55 +00:00
|
|
|
buf, err := json.MarshalIndent(&key, "", " ")
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Println(string(buf))
|
2015-03-22 20:03:01 +00:00
|
|
|
return nil
|
|
|
|
case "masterkey":
|
2015-06-27 12:36:46 +00:00
|
|
|
buf, err := json.MarshalIndent(repo.Key(), "", " ")
|
2015-03-22 20:03:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-05 12:44:59 +00:00
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Println(string(buf))
|
2014-10-05 12:44:59 +00:00
|
|
|
return nil
|
|
|
|
case "lock":
|
2021-10-31 22:08:13 +00:00
|
|
|
lock, err := restic.LoadLock(ctx, 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
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Println(string(buf))
|
2015-06-24 16:17:01 +00:00
|
|
|
return nil
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
case "pack":
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{Type: restic.PackFile, Name: id.String()}
|
2021-10-31 22:08:13 +00:00
|
|
|
buf, err := backend.LoadAll(ctx, nil, repo.Backend(), h)
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
hash := restic.Hash(buf)
|
2016-08-21 16:39:17 +00:00
|
|
|
if !hash.Equal(id) {
|
2020-04-04 17:41:24 +00:00
|
|
|
Warnf("Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String())
|
2016-08-21 16:39:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 17:45:36 +00:00
|
|
|
_, err = globalOptions.stdout.Write(buf)
|
2015-04-26 15:44:38 +00:00
|
|
|
return err
|
|
|
|
|
|
|
|
case "blob":
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2020-08-02 10:17:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
for _, t := range []restic.BlobType{restic.DataBlob, restic.TreeBlob} {
|
2020-11-05 21:18:00 +00:00
|
|
|
bh := restic.BlobHandle{ID: id, Type: t}
|
|
|
|
if !repo.Index().Has(bh) {
|
2016-08-28 19:23:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-07-26 12:25:01 +00:00
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
buf, err := repo.LoadBlob(ctx, t, id, nil)
|
2016-08-28 19:23:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:45:36 +00:00
|
|
|
_, err = globalOptions.stdout.Write(buf)
|
2015-04-26 15:44:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("blob not found")
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2023-05-18 21:17:12 +00:00
|
|
|
case "tree":
|
2023-10-01 11:05:56 +00:00
|
|
|
sn, subfolder, err := restic.FindSnapshot(ctx, repo, repo, args[1])
|
2023-05-18 21:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Fatalf("could not find snapshot: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2023-05-18 21:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-16 14:50:54 +00:00
|
|
|
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
|
2023-05-18 21:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := repo.LoadBlob(ctx, restic.TreeBlob, *sn.Tree, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = globalOptions.stdout.Write(buf)
|
|
|
|
return err
|
|
|
|
|
2014-10-05 12:44:59 +00:00
|
|
|
default:
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("invalid type")
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
}
|