2015-02-03 20:07:55 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-04-29 03:45:24 +00:00
|
|
|
"os/user"
|
2015-02-03 20:07:55 +00:00
|
|
|
"strconv"
|
2015-05-01 21:26:40 +00:00
|
|
|
"sync"
|
2015-02-03 20:07:55 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2015-05-01 23:29:54 +00:00
|
|
|
"github.com/juju/errors"
|
2015-02-03 20:07:55 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2015-04-29 03:31:07 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2015-04-26 15:44:38 +00:00
|
|
|
"github.com/restic/restic/pack"
|
2015-05-09 21:52:03 +00:00
|
|
|
"github.com/restic/restic/repository"
|
2015-02-03 20:07:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-02 14:12:19 +00:00
|
|
|
// Node is a file, directory or other item in a backup.
|
2015-02-03 20:07:55 +00:00
|
|
|
type Node struct {
|
|
|
|
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"`
|
|
|
|
Content []backend.ID `json:"content"`
|
|
|
|
Subtree backend.ID `json:"subtree,omitempty"`
|
|
|
|
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
|
|
|
tree *Tree
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
path string
|
|
|
|
err error
|
2015-05-09 21:52:03 +00:00
|
|
|
blobs repository.Blobs
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 06:11:01 +00:00
|
|
|
func (node Node) String() string {
|
|
|
|
switch node.Type {
|
2015-02-03 20:07:55 +00:00
|
|
|
case "file":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
2015-04-28 06:11:01 +00:00
|
|
|
node.Mode, node.UID, node.GID, node.Size, node.ModTime, node.Name)
|
2015-02-03 20:07:55 +00:00
|
|
|
case "dir":
|
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
2015-04-28 06:11:01 +00:00
|
|
|
node.Mode|os.ModeDir, node.UID, node.GID, node.Size, node.ModTime, node.Name)
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 06:11:01 +00:00
|
|
|
return fmt.Sprintf("<Node(%s) %s>", node.Type, node.Name)
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) Tree() *Tree {
|
|
|
|
return node.tree
|
|
|
|
}
|
|
|
|
|
2015-05-02 14:12:19 +00:00
|
|
|
// NodeFromFileInfo returns a new node from the given path and FileInfo.
|
2015-02-03 20:07:55 +00:00
|
|
|
func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
|
2015-03-22 15:38:03 +00:00
|
|
|
node := &Node{
|
|
|
|
path: path,
|
|
|
|
Name: fi.Name(),
|
2015-04-07 19:57:07 +00:00
|
|
|
Mode: fi.Mode() & (os.ModePerm | os.ModeType),
|
2015-03-22 15:38:03 +00:00
|
|
|
ModTime: fi.ModTime(),
|
|
|
|
}
|
2015-02-03 20:07:55 +00:00
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
node.Type = nodeTypeFromFileInfo(fi)
|
2015-03-07 10:53:32 +00:00
|
|
|
if node.Type == "file" {
|
|
|
|
node.Size = uint64(fi.Size())
|
|
|
|
}
|
|
|
|
|
2015-04-24 23:39:32 +00:00
|
|
|
err := node.fillExtra(path, fi)
|
2015-03-07 10:53:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
func nodeTypeFromFileInfo(fi os.FileInfo) string {
|
2015-02-03 20:07:55 +00:00
|
|
|
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
|
|
|
|
case 0:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "file"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeDir:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "dir"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeSymlink:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "symlink"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeDevice | os.ModeCharDevice:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "chardev"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeDevice:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "dev"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeNamedPipe:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "fifo"
|
2015-02-03 20:07:55 +00:00
|
|
|
case os.ModeSocket:
|
2015-03-07 10:53:32 +00:00
|
|
|
return "socket"
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 10:53:32 +00:00
|
|
|
return ""
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 14:22:43 +00:00
|
|
|
// CreateAt creates the node at the given path and restores all the meta data.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (node *Node) CreateAt(path string, repo *repository.Repository) error {
|
2015-02-03 20:07:55 +00:00
|
|
|
switch node.Type {
|
|
|
|
case "dir":
|
2015-04-29 01:24:43 +00:00
|
|
|
if err := node.createDirAt(path); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createDirAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "file":
|
2015-05-09 11:32:52 +00:00
|
|
|
if err := node.createFileAt(path, repo); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createFileAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "symlink":
|
2015-04-29 01:24:43 +00:00
|
|
|
if err := node.createSymlinkAt(path); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createSymlinkAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "dev":
|
2015-04-29 01:24:43 +00:00
|
|
|
if err := node.createDevAt(path); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createDevAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "chardev":
|
2015-04-29 01:24:43 +00:00
|
|
|
if err := node.createCharDevAt(path); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createCharDevAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "fifo":
|
2015-04-29 01:24:43 +00:00
|
|
|
if err := node.createFifoAt(path); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "createFifoAt")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
case "socket":
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
return node.restoreMetadata(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) restoreMetadata(path string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Lchown")
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node.Type == "symlink" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(path, node.Mode)
|
2015-02-03 20:07:55 +00:00
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Chmod")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
var utimes = []syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
err = syscall.UtimesNano(path, utimes)
|
2015-02-03 20:07:55 +00:00
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "UtimesNano")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) createDirAt(path string) error {
|
|
|
|
err := os.Mkdir(path, node.Mode)
|
2015-02-03 20:07:55 +00:00
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Mkdir")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (node Node) createFileAt(path string, repo *repository.Repository) error {
|
2015-04-29 01:24:43 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "OpenFile")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
for _, id := range node.Content {
|
2015-05-09 11:32:52 +00:00
|
|
|
buf, err := repo.LoadBlob(pack.Data, id)
|
2015-04-29 01:24:43 +00:00
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Load")
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(buf)
|
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Write")
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) createSymlinkAt(path string) error {
|
|
|
|
err := os.Symlink(node.LinkTarget, path)
|
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "Symlink")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
return nil
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 19:34:12 +00:00
|
|
|
func (node *Node) createDevAt(path string) error {
|
|
|
|
return syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) createCharDevAt(path string) error {
|
|
|
|
return syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) createFifoAt(path string) error {
|
|
|
|
return syscall.Mkfifo(path, 0600)
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:07:55 +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
|
2015-04-28 06:11:01 +00:00
|
|
|
nj := (*nodeJSON)(node)
|
2015-02-03 20:07:55 +00:00
|
|
|
|
|
|
|
err := json.Unmarshal(data, nj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) Equals(other Node) bool {
|
|
|
|
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
|
|
|
|
}
|
2015-04-29 01:24:43 +00:00
|
|
|
if !node.sameContent(other) {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
if !node.Subtree.Equal(other.Subtree) {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
if node.Error != other.Error {
|
|
|
|
return false
|
|
|
|
}
|
2015-02-03 20:07:55 +00:00
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) sameContent(other Node) bool {
|
|
|
|
if node.Content == nil {
|
|
|
|
return other.Content == nil
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
if other.Content == nil {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
if len(node.Content) != len(other.Content) {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
for i := 0; i < len(node.Content); i++ {
|
|
|
|
if !node.Content[i].Equal(other.Content[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:07:55 +00:00
|
|
|
return true
|
|
|
|
}
|
2015-04-29 03:31:07 +00:00
|
|
|
|
|
|
|
func (node *Node) isNewer(path string, fi os.FileInfo) bool {
|
|
|
|
if node.Type != "file" {
|
|
|
|
debug.Log("node.isNewer", "node %v is newer: not file", path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
tpe := nodeTypeFromFileInfo(fi)
|
|
|
|
if node.Name != fi.Name() || node.Type != tpe {
|
|
|
|
debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
extendedStat := fi.Sys().(*syscall.Stat_t)
|
|
|
|
inode := extendedStat.Ino
|
|
|
|
size := uint64(extendedStat.Size)
|
|
|
|
|
|
|
|
if node.ModTime != fi.ModTime() ||
|
|
|
|
node.ChangeTime != changeTime(extendedStat) ||
|
2015-05-05 23:06:36 +00:00
|
|
|
node.Inode != uint64(inode) ||
|
2015-04-29 03:31:07 +00:00
|
|
|
node.Size != size {
|
|
|
|
debug.Log("node.isNewer", "node %v is newer: timestamp, size or inode changed", path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("node.isNewer", "node %v is not newer", path)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-29 03:45:24 +00:00
|
|
|
|
|
|
|
func (node *Node) fillUser(stat *syscall.Stat_t) error {
|
|
|
|
node.UID = stat.Uid
|
|
|
|
node.GID = stat.Gid
|
|
|
|
|
2015-05-01 21:26:40 +00:00
|
|
|
username, err := lookupUsername(strconv.Itoa(int(stat.Uid)))
|
2015-04-29 03:45:24 +00:00
|
|
|
if err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "fillUser")
|
2015-04-29 03:45:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 21:26:40 +00:00
|
|
|
node.User = username
|
2015-04-29 03:45:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:26:40 +00:00
|
|
|
var (
|
|
|
|
uidLookupCache = make(map[string]string)
|
|
|
|
uidLookupCacheMutex = sync.RWMutex{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func lookupUsername(uid string) (string, error) {
|
|
|
|
uidLookupCacheMutex.RLock()
|
|
|
|
value, ok := uidLookupCache[uid]
|
|
|
|
uidLookupCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.LookupId(uid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uidLookupCacheMutex.Lock()
|
|
|
|
uidLookupCache[uid] = u.Username
|
|
|
|
uidLookupCacheMutex.Unlock()
|
|
|
|
|
|
|
|
return u.Username, nil
|
|
|
|
}
|
|
|
|
|
2015-04-29 03:45:24 +00:00
|
|
|
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
|
|
|
stat, ok := fi.Sys().(*syscall.Stat_t)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:06:36 +00:00
|
|
|
node.Inode = uint64(stat.Ino)
|
2015-04-29 03:45:24 +00:00
|
|
|
|
|
|
|
node.fillTimes(stat)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2015-05-01 21:31:02 +00:00
|
|
|
if err = node.fillUser(stat); err != nil {
|
2015-05-01 23:29:54 +00:00
|
|
|
return errors.Annotate(err, "fillExtra")
|
2015-05-01 21:31:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 03:45:24 +00:00
|
|
|
switch node.Type {
|
|
|
|
case "file":
|
|
|
|
node.Size = uint64(stat.Size)
|
|
|
|
node.Links = uint64(stat.Nlink)
|
|
|
|
case "dir":
|
|
|
|
case "symlink":
|
|
|
|
node.LinkTarget, err = os.Readlink(path)
|
|
|
|
case "dev":
|
2015-04-29 03:53:49 +00:00
|
|
|
node.Device = uint64(stat.Rdev)
|
2015-04-29 03:45:24 +00:00
|
|
|
case "chardev":
|
2015-04-29 03:53:49 +00:00
|
|
|
node.Device = uint64(stat.Rdev)
|
2015-04-29 03:45:24 +00:00
|
|
|
case "fifo":
|
|
|
|
case "socket":
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("invalid node type %q", node.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|