2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
import (
|
2014-12-06 21:01:54 +00:00
|
|
|
"encoding/json"
|
2014-11-30 21:34:21 +00:00
|
|
|
"errors"
|
2014-08-11 20:47:24 +00:00
|
|
|
"fmt"
|
2014-04-21 21:25:31 +00:00
|
|
|
"os"
|
2014-08-11 20:47:24 +00:00
|
|
|
"os/user"
|
2015-01-04 21:39:30 +00:00
|
|
|
"path/filepath"
|
2014-11-30 21:34:21 +00:00
|
|
|
"sort"
|
2014-08-11 20:47:24 +00:00
|
|
|
"strconv"
|
2014-04-21 21:25:31 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2014-09-18 20:41:24 +00:00
|
|
|
|
2014-11-16 11:05:54 +00:00
|
|
|
"github.com/juju/arrar"
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic/backend"
|
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
|
|
|
|
|
|
|
type Node struct {
|
2014-09-23 20:39:12 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Mode os.FileMode `json:"mode,omitempty"`
|
|
|
|
ModTime time.Time `json:"mtime,omitempty"`
|
|
|
|
AccessTime time.Time `json:"atime,omitempty"`
|
|
|
|
ChangeTime time.Time `json:"ctime,omitempty"`
|
|
|
|
UID uint32 `json:"uid"`
|
|
|
|
GID uint32 `json:"gid"`
|
|
|
|
User string `json:"user,omitempty"`
|
|
|
|
Group string `json:"group,omitempty"`
|
|
|
|
Inode uint64 `json:"inode,omitempty"`
|
|
|
|
Size uint64 `json:"size,omitempty"`
|
|
|
|
Links uint64 `json:"links,omitempty"`
|
|
|
|
LinkTarget string `json:"linktarget,omitempty"`
|
|
|
|
Device uint64 `json:"device,omitempty"`
|
2014-11-30 21:34:21 +00:00
|
|
|
Content []backend.ID `json:"content"`
|
2014-09-23 20:39:12 +00:00
|
|
|
Subtree backend.ID `json:"subtree,omitempty"`
|
|
|
|
|
2015-01-04 21:58:58 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
2015-01-04 21:39:30 +00:00
|
|
|
tree *Tree
|
2014-11-16 20:29:11 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
path string
|
2015-01-04 17:23:00 +00:00
|
|
|
err error
|
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")
|
|
|
|
)
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
type Blob struct {
|
|
|
|
ID backend.ID `json:"id,omitempty"`
|
2015-01-10 22:40:10 +00:00
|
|
|
Offset uint64 `json:"offset,omitempty"`
|
2014-09-23 20:39:12 +00:00
|
|
|
Size uint64 `json:"size,omitempty"`
|
|
|
|
Storage backend.ID `json:"sid,omitempty"` // encrypted ID
|
|
|
|
StorageSize uint64 `json:"ssize,omitempty"` // encrypted Size
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 20:21:44 +00:00
|
|
|
type Blobs []Blob
|
2014-08-11 21:14:40 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func (n Node) String() string {
|
|
|
|
switch n.Type {
|
|
|
|
case "file":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
|
|
n.Mode, n.UID, n.GID, n.Size, n.ModTime, 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, n.Name)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
|
|
|
|
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-01-10 22:40:10 +00:00
|
|
|
func LoadTree(s Server, blob Blob) (*Tree, error) {
|
|
|
|
tree := &Tree{}
|
|
|
|
err := s.LoadJSON(backend.Tree, blob, 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-01-10 22:40:10 +00:00
|
|
|
tree, err := LoadTree(s, blob)
|
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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("Tree.CopyFrom", " insert blob %v\n", blob)
|
2015-01-10 22:40:10 +00:00
|
|
|
t.Map.Insert(blob)
|
|
|
|
} else {
|
2015-01-11 13:09:44 +00:00
|
|
|
debug("Tree.CopyFrom", " trees are not equal: %v\n", node)
|
|
|
|
debug("Tree.CopyFrom", " %#v\n", node.tree)
|
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("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-11 13:09:44 +00:00
|
|
|
debug("Tree.Equals", "tree.Equals(): node %d is different:", i)
|
|
|
|
debug("Tree.Equals", " %#v", t.Nodes[i])
|
|
|
|
debug("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
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert blob
|
|
|
|
// https://code.google.com/p/go-wiki/wiki/bliceTricks
|
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-01-04 21:39:30 +00:00
|
|
|
func (node Node) Tree() *Tree {
|
|
|
|
return node.tree
|
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
func (node *Node) fill_extra(path string, fi os.FileInfo) (err error) {
|
|
|
|
stat, ok := fi.Sys().(*syscall.Stat_t)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node.ChangeTime = time.Unix(stat.Ctim.Unix())
|
|
|
|
node.AccessTime = time.Unix(stat.Atim.Unix())
|
|
|
|
node.UID = stat.Uid
|
|
|
|
node.GID = stat.Gid
|
|
|
|
|
2014-12-25 13:13:34 +00:00
|
|
|
// TODO: cache uid lookup
|
2014-08-11 20:47:24 +00:00
|
|
|
if u, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
|
|
|
|
node.User = u.Username
|
|
|
|
}
|
|
|
|
|
2014-10-05 14:22:39 +00:00
|
|
|
// TODO: implement getgrnam() or use https://github.com/kless/osutil
|
2014-08-11 20:47:24 +00:00
|
|
|
// if g, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
|
|
|
|
// node.User = u.Username
|
|
|
|
// }
|
|
|
|
|
|
|
|
node.Inode = stat.Ino
|
|
|
|
|
|
|
|
switch node.Type {
|
|
|
|
case "file":
|
|
|
|
node.Size = uint64(stat.Size)
|
|
|
|
node.Links = stat.Nlink
|
|
|
|
case "dir":
|
|
|
|
// nothing to do
|
|
|
|
case "symlink":
|
|
|
|
node.LinkTarget, err = os.Readlink(path)
|
|
|
|
case "dev":
|
|
|
|
node.Device = stat.Rdev
|
|
|
|
case "chardev":
|
|
|
|
node.Device = stat.Rdev
|
|
|
|
case "fifo":
|
|
|
|
// nothing to do
|
|
|
|
case "socket":
|
|
|
|
// nothing to do
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid node type %q", node.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
|
2014-11-23 15:48:00 +00:00
|
|
|
node := GetNode()
|
|
|
|
node.path = path
|
|
|
|
node.Name = fi.Name()
|
|
|
|
node.Mode = fi.Mode() & os.ModePerm
|
|
|
|
node.ModTime = fi.ModTime()
|
2014-04-21 21:25:31 +00:00
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
|
|
|
|
case 0:
|
|
|
|
node.Type = "file"
|
|
|
|
case os.ModeDir:
|
|
|
|
node.Type = "dir"
|
|
|
|
case os.ModeSymlink:
|
|
|
|
node.Type = "symlink"
|
|
|
|
case os.ModeDevice | os.ModeCharDevice:
|
|
|
|
node.Type = "chardev"
|
|
|
|
case os.ModeDevice:
|
|
|
|
node.Type = "dev"
|
|
|
|
case os.ModeNamedPipe:
|
|
|
|
node.Type = "fifo"
|
|
|
|
case os.ModeSocket:
|
|
|
|
node.Type = "socket"
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
err := node.fill_extra(path, fi)
|
|
|
|
return node, err
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
func (t Tree) CreateNodeAt(node *Node, s Server, path string) error {
|
2014-08-11 21:14:40 +00:00
|
|
|
switch node.Type {
|
2014-09-23 20:39:12 +00:00
|
|
|
case "dir":
|
|
|
|
err := os.Mkdir(path, node.Mode)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mkdir")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var utimes = []syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
err = syscall.UtimesNano(path, utimes)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Utimesnano")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
case "file":
|
|
|
|
// TODO: handle hard links
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "OpenFile")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 20:41:24 +00:00
|
|
|
for _, blobid := range node.Content {
|
2015-01-10 22:40:10 +00:00
|
|
|
blob, err := t.Map.FindID(blobid)
|
|
|
|
if err != nil {
|
|
|
|
return arrar.Annotate(err, "Find Blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := s.Load(backend.Data, blob)
|
2014-09-18 20:41:24 +00:00
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Load")
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
_, err = f.Write(buf)
|
2014-09-18 20:41:24 +00:00
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Write")
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var utimes = []syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
err = syscall.UtimesNano(path, utimes)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Utimesnano")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
case "symlink":
|
|
|
|
err := os.Symlink(node.LinkTarget, path)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Symlink")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, O_PATH|syscall.O_NOFOLLOW, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "OpenFile")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 11:05:54 +00:00
|
|
|
// TODO: Get Futimes() working on older Linux kernels (fails with 3.2.0)
|
|
|
|
// var utimes = []syscall.Timeval{
|
|
|
|
// syscall.NsecToTimeval(node.AccessTime.UnixNano()),
|
|
|
|
// syscall.NsecToTimeval(node.ModTime.UnixNano()),
|
|
|
|
// }
|
|
|
|
// err = syscall.Futimes(int(f.Fd()), utimes)
|
|
|
|
// if err != nil {
|
|
|
|
// return arrar.Annotate(err, "Futimes")
|
|
|
|
// }
|
2014-08-11 21:14:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
case "dev":
|
|
|
|
err := syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mknod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "chardev":
|
|
|
|
err := syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mknod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "fifo":
|
|
|
|
err := syscall.Mkfifo(path, 0600)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mkfifo")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "socket":
|
|
|
|
// nothing to do, we do not restore sockets
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Chmod(path, node.Mode)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chmod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chown")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chtimes(path, node.AccessTime, node.ModTime)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chtimes")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-23 15:48:00 +00:00
|
|
|
|
2014-11-30 21:34:21 +00:00
|
|
|
func (node Node) SameContent(olderNode *Node) bool {
|
|
|
|
// if this node has a type other than "file", treat as if content has changed
|
|
|
|
if node.Type != "file" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the name or type has changed, this is surely something different
|
|
|
|
if node.Name != olderNode.Name || node.Type != olderNode.Type {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if timestamps or inodes differ, content has changed
|
|
|
|
if node.ModTime != olderNode.ModTime ||
|
|
|
|
node.ChangeTime != olderNode.ChangeTime ||
|
|
|
|
node.Inode != olderNode.Inode {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise the node is assumed to have the same content
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-12-06 21:01:54 +00:00
|
|
|
func (node Node) MarshalJSON() ([]byte, error) {
|
|
|
|
type nodeJSON Node
|
|
|
|
nj := nodeJSON(node)
|
|
|
|
name := strconv.Quote(node.Name)
|
|
|
|
nj.Name = name[1 : len(name)-1]
|
|
|
|
|
|
|
|
return json.Marshal(nj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) UnmarshalJSON(data []byte) error {
|
|
|
|
type nodeJSON Node
|
|
|
|
var nj *nodeJSON = (*nodeJSON)(node)
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, nj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
func (node Node) Equals(other Node) bool {
|
|
|
|
// TODO: add generatored code for this
|
|
|
|
if node.Name != other.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Type != other.Type {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Mode != other.Mode {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.ModTime != other.ModTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.AccessTime != other.AccessTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.ChangeTime != other.ChangeTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.UID != other.UID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.GID != other.GID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.User != other.User {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Group != other.Group {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Inode != other.Inode {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Size != other.Size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Links != other.Links {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.LinkTarget != other.LinkTarget {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Device != other.Device {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node.Content != nil && other.Content == nil {
|
|
|
|
return false
|
|
|
|
} else if node.Content == nil && other.Content != nil {
|
|
|
|
return false
|
|
|
|
} else if node.Content != nil && other.Content != nil {
|
|
|
|
if len(node.Content) != len(other.Content) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(node.Content); i++ {
|
|
|
|
if !node.Content[i].Equal(other.Content[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !node.Subtree.Equal(other.Subtree) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Error != other.Error {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
func (b Blob) Free() {
|
|
|
|
if b.ID != nil {
|
|
|
|
b.ID.Free()
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Storage != nil {
|
|
|
|
b.Storage.Free()
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
func (b Blob) Valid() bool {
|
|
|
|
if b.ID == nil || b.Storage == nil || b.StorageSize == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Blob) String() string {
|
|
|
|
return fmt.Sprintf("Blob<%s -> %s>",
|
|
|
|
b.ID.Str(),
|
|
|
|
b.Storage.Str())
|
|
|
|
}
|