2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-30 21:34:21 +00:00
|
|
|
"errors"
|
2014-08-11 20:47:24 +00:00
|
|
|
"fmt"
|
2015-01-04 21:39:30 +00:00
|
|
|
"path/filepath"
|
2014-11-30 21:34:21 +00:00
|
|
|
"sort"
|
2015-02-04 21:36:49 +00:00
|
|
|
"sync"
|
2014-09-18 20:41:24 +00:00
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2015-01-14 21:08:48 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2014-04-21 21:25:31 +00:00
|
|
|
)
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
type Tree struct {
|
|
|
|
Nodes []*Node `json:"nodes"`
|
|
|
|
Map *Map `json:"map"`
|
|
|
|
}
|
2014-04-21 21:25:31 +00:00
|
|
|
|
2014-11-30 21:34:21 +00:00
|
|
|
var (
|
|
|
|
ErrNodeNotFound = errors.New("named node not found")
|
|
|
|
ErrNodeAlreadyInTree = errors.New("node already present")
|
|
|
|
)
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
func NewTree() *Tree {
|
|
|
|
return &Tree{
|
|
|
|
Nodes: []*Node{},
|
|
|
|
Map: NewMap(),
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
func (t Tree) String() string {
|
|
|
|
return fmt.Sprintf("Tree<%d nodes, %d blobs>", len(t.Nodes), len(t.Map.list))
|
|
|
|
}
|
2014-11-30 21:34:21 +00:00
|
|
|
|
2015-02-17 21:37:34 +00:00
|
|
|
func LoadTree(s Server, id backend.ID) (*Tree, error) {
|
2015-01-10 22:40:10 +00:00
|
|
|
tree := &Tree{}
|
2015-02-17 21:37:34 +00:00
|
|
|
err := s.LoadJSONID(backend.Tree, id, tree)
|
2014-11-30 21:34:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
// LoadTreeRecursive loads the tree and all subtrees via s.
|
|
|
|
func LoadTreeRecursive(path string, s Server, blob Blob) (*Tree, error) {
|
2015-01-04 21:58:58 +00:00
|
|
|
// TODO: load subtrees in parallel
|
2015-02-17 21:37:34 +00:00
|
|
|
tree, err := LoadTree(s, blob.Storage)
|
2015-01-04 21:39:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, n := range tree.Nodes {
|
2015-01-04 21:39:30 +00:00
|
|
|
n.path = filepath.Join(path, n.Name)
|
|
|
|
if n.Type == "dir" && n.Subtree != nil {
|
2015-01-10 22:40:10 +00:00
|
|
|
subtreeBlob, err := tree.Map.FindID(n.Subtree)
|
2015-01-04 21:39:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
t, err := LoadTreeRecursive(n.path, s, subtreeBlob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.tree = t
|
2015-01-04 21:39:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFrom recursively copies all content from other to t.
|
2015-01-10 22:40:10 +00:00
|
|
|
func (t Tree) CopyFrom(other *Tree, s *Server) error {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", "CopyFrom(%v)\n", other)
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, node := range t.Nodes {
|
2015-01-04 21:39:30 +00:00
|
|
|
// only process files and dirs
|
|
|
|
if node.Type != "file" && node.Type != "dir" {
|
2014-11-30 21:34:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// find entry in other tree
|
|
|
|
oldNode, err := other.Find(node.Name)
|
|
|
|
|
2015-01-06 21:09:39 +00:00
|
|
|
// if the node could not be found or the type has changed, proceed to the next
|
|
|
|
if err == ErrNodeNotFound || node.Type != oldNode.Type {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " node %v is new\n", node)
|
2014-11-30 21:34:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-06 21:09:39 +00:00
|
|
|
if node.Type == "file" {
|
2015-01-04 21:39:30 +00:00
|
|
|
// compare content
|
|
|
|
if node.SameContent(oldNode) {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " file node %v has same content\n", node)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
// check if all content is still available in the repository
|
|
|
|
for _, id := range oldNode.Content {
|
|
|
|
blob, err := other.Map.FindID(id)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := s.Test(backend.Data, blob.Storage); !ok || err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 21:39:30 +00:00
|
|
|
// copy Content
|
|
|
|
node.Content = oldNode.Content
|
2015-01-05 20:40:43 +00:00
|
|
|
|
|
|
|
// copy storage IDs
|
|
|
|
for _, id := range node.Content {
|
2015-01-10 22:40:10 +00:00
|
|
|
blob, err := other.Map.FindID(id)
|
2015-01-05 20:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " insert blob %v\n", blob)
|
2015-01-10 22:40:10 +00:00
|
|
|
t.Map.Insert(blob)
|
2015-01-05 20:40:43 +00:00
|
|
|
}
|
2015-01-04 21:39:30 +00:00
|
|
|
}
|
2015-01-06 21:09:39 +00:00
|
|
|
} else if node.Type == "dir" {
|
2015-01-04 21:39:30 +00:00
|
|
|
// fill in all subtrees from old subtree
|
2015-01-10 22:40:10 +00:00
|
|
|
err := node.tree.CopyFrom(oldNode.tree, s)
|
2015-01-05 20:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-04 21:39:30 +00:00
|
|
|
|
|
|
|
// check if tree has changed
|
|
|
|
if node.tree.Equals(*oldNode.tree) {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " tree node %v has same content\n", node)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-01-04 21:39:30 +00:00
|
|
|
// if nothing has changed, copy subtree ID
|
|
|
|
node.Subtree = oldNode.Subtree
|
2015-01-05 20:40:43 +00:00
|
|
|
|
|
|
|
// and store blob in bloblist
|
2015-01-10 22:40:10 +00:00
|
|
|
blob, err := other.Map.FindID(oldNode.Subtree)
|
2015-01-05 20:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " insert blob %v\n", blob)
|
2015-01-10 22:40:10 +00:00
|
|
|
t.Map.Insert(blob)
|
|
|
|
} else {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.CopyFrom", " trees are not equal: %v\n", node)
|
|
|
|
debug.Log("Tree.CopyFrom", " %#v\n", node.tree)
|
|
|
|
debug.Log("Tree.CopyFrom", " %#v\n", oldNode.tree)
|
2015-01-04 21:39:30 +00:00
|
|
|
}
|
2014-11-30 21:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-05 20:40:43 +00:00
|
|
|
|
|
|
|
return nil
|
2015-01-04 21:39:30 +00:00
|
|
|
}
|
2014-11-30 21:34:21 +00:00
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
// Equals returns true if t and other have exactly the same nodes and map.
|
2015-01-04 21:39:30 +00:00
|
|
|
func (t Tree) Equals(other Tree) bool {
|
2015-01-10 22:40:10 +00:00
|
|
|
if len(t.Nodes) != len(other.Nodes) {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.Equals", "tree.Equals(): trees have different number of nodes")
|
2015-01-04 21:39:30 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
if !t.Map.Equals(other.Map) {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.Equals", "tree.Equals(): maps aren't equal")
|
2015-01-10 22:40:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(t.Nodes); i++ {
|
|
|
|
if !t.Nodes[i].Equals(*other.Nodes[i]) {
|
2015-01-14 21:08:48 +00:00
|
|
|
debug.Log("Tree.Equals", "tree.Equals(): node %d is different:", i)
|
|
|
|
debug.Log("Tree.Equals", " %#v", t.Nodes[i])
|
|
|
|
debug.Log("Tree.Equals", " %#v", other.Nodes[i])
|
2015-01-10 22:40:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2014-11-30 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tree) Insert(node *Node) error {
|
|
|
|
pos, _, err := t.find(node.Name)
|
|
|
|
if err == nil {
|
|
|
|
// already present
|
|
|
|
return ErrNodeAlreadyInTree
|
|
|
|
}
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
// https://code.google.com/p/go-wiki/wiki/SliceTricks
|
2015-01-10 22:40:10 +00:00
|
|
|
t.Nodes = append(t.Nodes, &Node{})
|
|
|
|
copy(t.Nodes[pos+1:], t.Nodes[pos:])
|
|
|
|
t.Nodes[pos] = node
|
2014-11-30 21:34:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) find(name string) (int, *Node, error) {
|
2015-01-10 22:40:10 +00:00
|
|
|
pos := sort.Search(len(t.Nodes), func(i int) bool {
|
|
|
|
return t.Nodes[i].Name >= name
|
2014-11-30 21:34:21 +00:00
|
|
|
})
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
if pos < len(t.Nodes) && t.Nodes[pos].Name == name {
|
|
|
|
return pos, t.Nodes[pos], nil
|
2014-11-30 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos, nil, ErrNodeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) Find(name string) (*Node, error) {
|
|
|
|
_, node, err := t.find(name)
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:11:32 +00:00
|
|
|
func (t Tree) Stat() Stat {
|
|
|
|
s := Stat{}
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, n := range t.Nodes {
|
2015-01-04 19:11:32 +00:00
|
|
|
switch n.Type {
|
|
|
|
case "file":
|
|
|
|
s.Files++
|
|
|
|
s.Bytes += n.Size
|
|
|
|
case "dir":
|
|
|
|
s.Dirs++
|
2015-01-04 21:39:30 +00:00
|
|
|
if n.tree != nil {
|
|
|
|
s.Add(n.tree.Stat())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) StatTodo() Stat {
|
|
|
|
s := Stat{}
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, n := range t.Nodes {
|
2015-01-04 21:39:30 +00:00
|
|
|
switch n.Type {
|
|
|
|
case "file":
|
|
|
|
if n.Content == nil {
|
|
|
|
s.Files++
|
|
|
|
s.Bytes += n.Size
|
|
|
|
}
|
|
|
|
case "dir":
|
|
|
|
if n.Subtree == nil {
|
|
|
|
s.Dirs++
|
|
|
|
if n.tree != nil {
|
|
|
|
s.Add(n.tree.StatTodo())
|
|
|
|
}
|
|
|
|
}
|
2015-01-04 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2015-02-04 21:36:49 +00:00
|
|
|
|
|
|
|
// EachNode calls fn recursively for each node in t and all subtrees
|
|
|
|
// (depth-first). This in done concurrently in p goroutines.
|
|
|
|
func (t *Tree) EachNode(p int, fn func(*Node)) {
|
|
|
|
processNodeWorker := func(wg *sync.WaitGroup, ch <-chan *Node, f func(*Node)) {
|
|
|
|
for node := range ch {
|
|
|
|
f(node)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// start workers
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ch := make(chan *Node)
|
|
|
|
|
|
|
|
for i := 0; i < p; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go processNodeWorker(&wg, ch, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
var processTree func(t *Tree, ch chan<- *Node)
|
|
|
|
processTree = func(t *Tree, ch chan<- *Node) {
|
|
|
|
for _, n := range t.Nodes {
|
|
|
|
if n.Type == "dir" && n.Tree() != nil {
|
|
|
|
processTree(n.Tree(), ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run on root
|
|
|
|
processTree(t, ch)
|
|
|
|
close(ch)
|
|
|
|
|
|
|
|
// wait for all goroutines to terminate
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EachSubtree calls fn recursively for t and all subtrees (depth-first). This
|
|
|
|
// is done concurrently in p goroutines.
|
|
|
|
func (t *Tree) EachSubtree(p int, fn func(*Tree)) {
|
|
|
|
processTreeWorker := func(wg *sync.WaitGroup, ch <-chan *Tree, f func(*Tree)) {
|
|
|
|
for tree := range ch {
|
|
|
|
f(tree)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// start workers
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ch := make(chan *Tree)
|
|
|
|
|
|
|
|
for i := 0; i < p; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go processTreeWorker(&wg, ch, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra variable needed for recursion
|
|
|
|
var processTree func(t *Tree, ch chan<- *Tree)
|
|
|
|
processTree = func(t *Tree, ch chan<- *Tree) {
|
|
|
|
for _, n := range t.Nodes {
|
|
|
|
if n.Type == "dir" && n.Tree() != nil {
|
|
|
|
processTree(n.Tree(), ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- t
|
|
|
|
}
|
|
|
|
|
|
|
|
// run on root
|
|
|
|
processTree(t, ch)
|
|
|
|
close(ch)
|
|
|
|
|
|
|
|
// wait for all goroutines to terminate
|
|
|
|
wg.Wait()
|
|
|
|
}
|