func TestLoadJSONPack(t *testing.T) { repo := SetupRepo() defer TeardownRepo(repo) if BenchArchiveDirectory == "" { t.Skip("benchdir not set, skipping") } // archive a few files sn := SnapshotDir(t, repo, BenchArchiveDirectory, nil) OK(t, repo.Flush()) tree := restic.NewTree() err := repo.LoadJSONPack(pack.Tree, *sn.Tree, &tree) OK(t, err) }
func TestLoadTree(t *testing.T) { repo := SetupRepo() defer TeardownRepo(repo) // save tree tree := restic.NewTree() id, err := repo.SaveJSON(pack.Tree, tree) OK(t, err) // save packs OK(t, repo.Flush()) // load tree again tree2, err := restic.LoadTree(repo, id) OK(t, err) Assert(t, tree.Equals(tree2), "trees are not equal: want %v, got %v", tree, tree2) }
func TestLoadTree(t *testing.T) { repo, cleanup := repository.TestRepository(t) defer cleanup() // save tree tree := restic.NewTree() id, err := repo.SaveTree(tree) OK(t, err) // save packs OK(t, repo.Flush()) // load tree again tree2, err := repo.LoadTree(id) OK(t, err) Assert(t, tree.Equals(tree2), "trees are not equal: want %v, got %v", tree, tree2) }
func BenchmarkLoadJSONPack(t *testing.B) { repo := SetupRepo() defer TeardownRepo(repo) if BenchArchiveDirectory == "" { t.Skip("benchdir not set, skipping") } // archive a few files sn := SnapshotDir(t, repo, BenchArchiveDirectory, nil) OK(t, repo.Flush()) tree := restic.NewTree() t.ResetTimer() for i := 0; i < t.N; i++ { err := repo.LoadJSONPack(pack.Tree, *sn.Tree, &tree) OK(t, err) } }
func (arch *Archiver) dirWorker(wg *sync.WaitGroup, p *restic.Progress, done <-chan struct{}, dirCh <-chan pipe.Dir) { debug.Log("start") defer func() { debug.Log("done") wg.Done() }() for { select { case dir, ok := <-dirCh: if !ok { // channel is closed return } debug.Log("save dir %v (%d entries), error %v\n", dir.Path(), len(dir.Entries), dir.Error()) // ignore dir nodes with errors if dir.Error() != nil { fmt.Fprintf(os.Stderr, "error walking dir %v: %v\n", dir.Path(), dir.Error()) dir.Result() <- nil p.Report(restic.Stat{Errors: 1}) continue } tree := restic.NewTree() // wait for all content for _, ch := range dir.Entries { debug.Log("receiving result from %v", ch) res := <-ch // if we get a nil pointer here, an error has happened while // processing this entry. Ignore it for now. if res == nil { debug.Log("got nil result?") continue } // else insert node node := res.(*restic.Node) tree.Insert(node) if node.Type == "dir" { debug.Log("got tree node for %s: %v", node.Path, node.Subtree) if node.Subtree.IsNull() { panic("invalid null subtree restic.ID") } } } node := &restic.Node{} if dir.Path() != "" && dir.Info() != nil { n, err := restic.NodeFromFileInfo(dir.Path(), dir.Info()) if err != nil { n.Error = err.Error() dir.Result() <- n continue } node = n } if err := dir.Error(); err != nil { node.Error = err.Error() } id, err := arch.SaveTreeJSON(tree) if err != nil { panic(err) } debug.Log("save tree for %s: %v", dir.Path(), id.Str()) if id.IsNull() { panic("invalid null subtree restic.ID return from SaveTreeJSON()") } node.Subtree = &id debug.Log("sending result to %v", dir.Result()) dir.Result() <- node if dir.Path() != "" { p.Report(restic.Stat{Dirs: 1}) } case <-done: // pipeline was cancelled return } } }
func (cmd CmdCat) Execute(args []string) error { if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) { return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } tpe := args[0] var id backend.ID if tpe != "masterkey" && tpe != "config" { id, err = backend.ParseID(args[1]) if err != nil { if tpe != "snapshot" { return err } // find snapshot id with prefix id, err = restic.FindSnapshot(repo, args[1]) if err != nil { return err } } } // handle all types that don't need an index switch tpe { case "config": buf, err := json.MarshalIndent(repo.Config, "", " ") 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 case "snapshot": sn := &restic.Snapshot{} err = repo.LoadJSONUnpacked(backend.Snapshot, id, sn) if err != nil { return err } buf, err := json.MarshalIndent(&sn, "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "key": h := backend.Handle{Type: backend.Key, Name: id.String()} buf, err := backend.LoadAll(repo.Backend(), h, nil) if err != nil { return err } key := &repository.Key{} err = json.Unmarshal(buf, key) if err != nil { return err } buf, err = json.MarshalIndent(&key, "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "masterkey": buf, err := json.MarshalIndent(repo.Key(), "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "lock": lock, err := restic.LoadLock(repo, id) 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": h := backend.Handle{Type: backend.Data, Name: id.String()} buf, err := backend.LoadAll(repo.Backend(), h, nil) if err != nil { return err } _, err = os.Stdout.Write(buf) return err case "blob": blob, err := repo.Index().Lookup(id) if err != nil { return err } buf := make([]byte, blob.Length) data, err := repo.LoadBlob(blob.Type, id, buf) if err != nil { return err } _, err = os.Stdout.Write(data) return err 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 default: return errors.New("invalid type") } }