2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-29 07:00:49 +00:00
restic/internal/restic/node.go

729 lines
16 KiB
Go
Raw Normal View History

2015-02-03 20:07:55 +00:00
package restic
import (
2017-06-04 09:16:55 +00:00
"context"
2015-02-03 20:07:55 +00:00
"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"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"bytes"
"runtime"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs"
2015-02-03 20:07:55 +00:00
)
// ExtendedAttribute is a tuple storing the xattr name and value.
type ExtendedAttribute struct {
Name string `json:"name"`
Value []byte `json:"value"`
}
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"`
2017-04-05 18:42:15 +00:00
DeviceID uint64 `json:"device_id,omitempty"` // device id of the file, stat.st_dev
Size uint64 `json:"size,omitempty"`
Links uint64 `json:"links,omitempty"`
LinkTarget string `json:"linktarget,omitempty"`
ExtendedAttributes []ExtendedAttribute `json:"extended_attributes,omitempty"`
2017-04-05 18:42:15 +00:00
Device uint64 `json:"device,omitempty"` // in case of Type == "dev", stat.st_rdev
Content IDs `json:"content"`
Subtree *ID `json:"subtree,omitempty"`
2015-02-03 20:07:55 +00:00
Error string `json:"error,omitempty"`
2016-08-31 20:39:36 +00:00
Path string `json:"-"`
2015-02-03 20:07:55 +00:00
}
2017-06-10 20:55:12 +00:00
// Nodes is a slice of nodes that can be sorted.
type Nodes []*Node
func (n Nodes) Len() int { return len(n) }
func (n Nodes) Less(i, j int) bool { return n[i].Name < n[j].Name }
func (n Nodes) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
2015-04-28 06:11:01 +00:00
func (node Node) String() string {
2016-09-01 19:20:03 +00:00
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
}
2016-09-01 19:20:03 +00:00
return fmt.Sprintf("<Node(%s) %s>", node.Type, node.Name)
2015-02-03 20:07:55 +00:00
}
// NodeFromFileInfo returns a new node from the given path and FileInfo. It
// returns the first error that is encountered, together with a node.
2015-02-03 20:07:55 +00:00
func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
mask := os.ModePerm | os.ModeType | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
Remove pools for nodes and IDs This removes the allocation pools for nodes and IDs. I feel they aren't really needed. Benchmarks: benchmark old ns/op new ns/op delta BenchmarkChunkEncrypt 197890867 198616293 +0.37% BenchmarkChunkEncryptParallel 196127004 198819818 +1.37% BenchmarkArchiveDirectory 1098848419 1087237723 -1.06% BenchmarkPreload 30464455 29910239 -1.82% BenchmarkLoadTree 3265092 3088543 -5.41% BenchmarkEncryptWriter 37213511 37134683 -0.21% BenchmarkEncrypt 36037879 36166546 +0.36% BenchmarkDecryptReader 38165659 38556734 +1.02% BenchmarkEncryptDecryptReader 77027044 77194987 +0.22% BenchmarkDecrypt 36017602 35937888 -0.22% BenchmarkSaveJSON 47906 50270 +4.93% BenchmarkSaveFrom 49775973 50520969 +1.50% BenchmarkLoadJSONID 105290245 107281849 +1.89% BenchmarkChunkerWithSHA256 151501430 148264078 -2.14% BenchmarkChunkerWithMD5 93606346 94036392 +0.46% BenchmarkChunker 74285431 75933882 +2.22% BenchmarkPipelineWalker 387689 346467 -10.63% benchmark old MB/s new MB/s speedup BenchmarkChunkEncrypt 52.99 52.79 1.00x BenchmarkChunkEncryptParallel 53.46 52.74 0.99x BenchmarkEncryptWriter 225.42 225.90 1.00x BenchmarkEncrypt 232.77 231.94 1.00x BenchmarkDecryptReader 219.79 217.57 0.99x BenchmarkEncryptDecryptReader 108.90 108.67 1.00x BenchmarkDecrypt 232.90 233.42 1.00x BenchmarkSaveFrom 84.26 83.02 0.99x BenchmarkChunkerWithSHA256 69.21 70.72 1.02x BenchmarkChunkerWithMD5 112.02 111.51 1.00x BenchmarkChunker 141.15 138.09 0.98x benchmark old allocs new allocs delta BenchmarkChunkEncrypt 110 110 +0.00% BenchmarkChunkEncryptParallel 100 100 +0.00% BenchmarkArchiveDirectory 475591 476635 +0.22% BenchmarkPreload 28059 24182 -13.82% BenchmarkLoadTree 3124 2889 -7.52% BenchmarkEncryptWriter 19 19 +0.00% BenchmarkEncrypt 13 13 +0.00% BenchmarkDecryptReader 16 15 -6.25% BenchmarkEncryptDecryptReader 39 39 +0.00% BenchmarkDecrypt 11 11 +0.00% BenchmarkSaveJSON 74 74 +0.00% BenchmarkSaveFrom 109 112 +2.75% BenchmarkLoadJSONID 103630 97849 -5.58% BenchmarkChunkerWithSHA256 13 13 +0.00% BenchmarkChunkerWithMD5 12 12 +0.00% BenchmarkChunker 6 6 +0.00% BenchmarkPipelineWalker 212 165 -22.17% benchmark old bytes new bytes delta BenchmarkChunkEncrypt 64697 64697 +0.00% BenchmarkChunkEncryptParallel 64681 64681 +0.00% BenchmarkArchiveDirectory 193385504 193790864 +0.21% BenchmarkPreload 4064701 3942000 -3.02% BenchmarkLoadTree 344954 325396 -5.67% BenchmarkEncryptWriter 12793 12793 +0.00% BenchmarkEncrypt 1950 1950 +0.00% BenchmarkDecryptReader 3120 2774 -11.09% BenchmarkEncryptDecryptReader 1528036 1528036 +0.00% BenchmarkDecrypt 1919 1919 +0.00% BenchmarkSaveJSON 5524 5524 +0.00% BenchmarkSaveFrom 31353 40804 +30.14% BenchmarkLoadJSONID 12872020 16010968 +24.39% BenchmarkChunkerWithSHA256 26821 26821 +0.00% BenchmarkChunkerWithMD5 13554 13554 +0.00% BenchmarkChunker 13458 13458 +0.00% BenchmarkPipelineWalker 58584 55560 -5.16%
2015-03-22 15:38:03 +00:00
node := &Node{
2016-08-31 20:39:36 +00:00
Path: path,
Remove pools for nodes and IDs This removes the allocation pools for nodes and IDs. I feel they aren't really needed. Benchmarks: benchmark old ns/op new ns/op delta BenchmarkChunkEncrypt 197890867 198616293 +0.37% BenchmarkChunkEncryptParallel 196127004 198819818 +1.37% BenchmarkArchiveDirectory 1098848419 1087237723 -1.06% BenchmarkPreload 30464455 29910239 -1.82% BenchmarkLoadTree 3265092 3088543 -5.41% BenchmarkEncryptWriter 37213511 37134683 -0.21% BenchmarkEncrypt 36037879 36166546 +0.36% BenchmarkDecryptReader 38165659 38556734 +1.02% BenchmarkEncryptDecryptReader 77027044 77194987 +0.22% BenchmarkDecrypt 36017602 35937888 -0.22% BenchmarkSaveJSON 47906 50270 +4.93% BenchmarkSaveFrom 49775973 50520969 +1.50% BenchmarkLoadJSONID 105290245 107281849 +1.89% BenchmarkChunkerWithSHA256 151501430 148264078 -2.14% BenchmarkChunkerWithMD5 93606346 94036392 +0.46% BenchmarkChunker 74285431 75933882 +2.22% BenchmarkPipelineWalker 387689 346467 -10.63% benchmark old MB/s new MB/s speedup BenchmarkChunkEncrypt 52.99 52.79 1.00x BenchmarkChunkEncryptParallel 53.46 52.74 0.99x BenchmarkEncryptWriter 225.42 225.90 1.00x BenchmarkEncrypt 232.77 231.94 1.00x BenchmarkDecryptReader 219.79 217.57 0.99x BenchmarkEncryptDecryptReader 108.90 108.67 1.00x BenchmarkDecrypt 232.90 233.42 1.00x BenchmarkSaveFrom 84.26 83.02 0.99x BenchmarkChunkerWithSHA256 69.21 70.72 1.02x BenchmarkChunkerWithMD5 112.02 111.51 1.00x BenchmarkChunker 141.15 138.09 0.98x benchmark old allocs new allocs delta BenchmarkChunkEncrypt 110 110 +0.00% BenchmarkChunkEncryptParallel 100 100 +0.00% BenchmarkArchiveDirectory 475591 476635 +0.22% BenchmarkPreload 28059 24182 -13.82% BenchmarkLoadTree 3124 2889 -7.52% BenchmarkEncryptWriter 19 19 +0.00% BenchmarkEncrypt 13 13 +0.00% BenchmarkDecryptReader 16 15 -6.25% BenchmarkEncryptDecryptReader 39 39 +0.00% BenchmarkDecrypt 11 11 +0.00% BenchmarkSaveJSON 74 74 +0.00% BenchmarkSaveFrom 109 112 +2.75% BenchmarkLoadJSONID 103630 97849 -5.58% BenchmarkChunkerWithSHA256 13 13 +0.00% BenchmarkChunkerWithMD5 12 12 +0.00% BenchmarkChunker 6 6 +0.00% BenchmarkPipelineWalker 212 165 -22.17% benchmark old bytes new bytes delta BenchmarkChunkEncrypt 64697 64697 +0.00% BenchmarkChunkEncryptParallel 64681 64681 +0.00% BenchmarkArchiveDirectory 193385504 193790864 +0.21% BenchmarkPreload 4064701 3942000 -3.02% BenchmarkLoadTree 344954 325396 -5.67% BenchmarkEncryptWriter 12793 12793 +0.00% BenchmarkEncrypt 1950 1950 +0.00% BenchmarkDecryptReader 3120 2774 -11.09% BenchmarkEncryptDecryptReader 1528036 1528036 +0.00% BenchmarkDecrypt 1919 1919 +0.00% BenchmarkSaveJSON 5524 5524 +0.00% BenchmarkSaveFrom 31353 40804 +30.14% BenchmarkLoadJSONID 12872020 16010968 +24.39% BenchmarkChunkerWithSHA256 26821 26821 +0.00% BenchmarkChunkerWithMD5 13554 13554 +0.00% BenchmarkChunker 13458 13458 +0.00% BenchmarkPipelineWalker 58584 55560 -5.16%
2015-03-22 15:38:03 +00:00
Name: fi.Name(),
Mode: fi.Mode() & mask,
Remove pools for nodes and IDs This removes the allocation pools for nodes and IDs. I feel they aren't really needed. Benchmarks: benchmark old ns/op new ns/op delta BenchmarkChunkEncrypt 197890867 198616293 +0.37% BenchmarkChunkEncryptParallel 196127004 198819818 +1.37% BenchmarkArchiveDirectory 1098848419 1087237723 -1.06% BenchmarkPreload 30464455 29910239 -1.82% BenchmarkLoadTree 3265092 3088543 -5.41% BenchmarkEncryptWriter 37213511 37134683 -0.21% BenchmarkEncrypt 36037879 36166546 +0.36% BenchmarkDecryptReader 38165659 38556734 +1.02% BenchmarkEncryptDecryptReader 77027044 77194987 +0.22% BenchmarkDecrypt 36017602 35937888 -0.22% BenchmarkSaveJSON 47906 50270 +4.93% BenchmarkSaveFrom 49775973 50520969 +1.50% BenchmarkLoadJSONID 105290245 107281849 +1.89% BenchmarkChunkerWithSHA256 151501430 148264078 -2.14% BenchmarkChunkerWithMD5 93606346 94036392 +0.46% BenchmarkChunker 74285431 75933882 +2.22% BenchmarkPipelineWalker 387689 346467 -10.63% benchmark old MB/s new MB/s speedup BenchmarkChunkEncrypt 52.99 52.79 1.00x BenchmarkChunkEncryptParallel 53.46 52.74 0.99x BenchmarkEncryptWriter 225.42 225.90 1.00x BenchmarkEncrypt 232.77 231.94 1.00x BenchmarkDecryptReader 219.79 217.57 0.99x BenchmarkEncryptDecryptReader 108.90 108.67 1.00x BenchmarkDecrypt 232.90 233.42 1.00x BenchmarkSaveFrom 84.26 83.02 0.99x BenchmarkChunkerWithSHA256 69.21 70.72 1.02x BenchmarkChunkerWithMD5 112.02 111.51 1.00x BenchmarkChunker 141.15 138.09 0.98x benchmark old allocs new allocs delta BenchmarkChunkEncrypt 110 110 +0.00% BenchmarkChunkEncryptParallel 100 100 +0.00% BenchmarkArchiveDirectory 475591 476635 +0.22% BenchmarkPreload 28059 24182 -13.82% BenchmarkLoadTree 3124 2889 -7.52% BenchmarkEncryptWriter 19 19 +0.00% BenchmarkEncrypt 13 13 +0.00% BenchmarkDecryptReader 16 15 -6.25% BenchmarkEncryptDecryptReader 39 39 +0.00% BenchmarkDecrypt 11 11 +0.00% BenchmarkSaveJSON 74 74 +0.00% BenchmarkSaveFrom 109 112 +2.75% BenchmarkLoadJSONID 103630 97849 -5.58% BenchmarkChunkerWithSHA256 13 13 +0.00% BenchmarkChunkerWithMD5 12 12 +0.00% BenchmarkChunker 6 6 +0.00% BenchmarkPipelineWalker 212 165 -22.17% benchmark old bytes new bytes delta BenchmarkChunkEncrypt 64697 64697 +0.00% BenchmarkChunkEncryptParallel 64681 64681 +0.00% BenchmarkArchiveDirectory 193385504 193790864 +0.21% BenchmarkPreload 4064701 3942000 -3.02% BenchmarkLoadTree 344954 325396 -5.67% BenchmarkEncryptWriter 12793 12793 +0.00% BenchmarkEncrypt 1950 1950 +0.00% BenchmarkDecryptReader 3120 2774 -11.09% BenchmarkEncryptDecryptReader 1528036 1528036 +0.00% BenchmarkDecrypt 1919 1919 +0.00% BenchmarkSaveJSON 5524 5524 +0.00% BenchmarkSaveFrom 31353 40804 +30.14% BenchmarkLoadJSONID 12872020 16010968 +24.39% BenchmarkChunkerWithSHA256 26821 26821 +0.00% BenchmarkChunkerWithMD5 13554 13554 +0.00% BenchmarkChunker 13458 13458 +0.00% BenchmarkPipelineWalker 58584 55560 -5.16%
2015-03-22 15:38:03 +00:00
ModTime: fi.ModTime(),
}
2015-02-03 20:07:55 +00:00
2016-09-01 19:20:03 +00:00
node.Type = nodeTypeFromFileInfo(fi)
if node.Type == "file" {
2015-03-07 10:53:32 +00:00
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
}
// GetExtendedAttribute gets the extended attribute.
func (node Node) GetExtendedAttribute(a string) []byte {
for _, attr := range node.ExtendedAttributes {
if attr.Name == a {
return attr.Value
}
}
return nil
}
2015-05-02 14:22:43 +00:00
// CreateAt creates the node at the given path and restores all the meta data.
2017-06-04 09:16:55 +00:00
func (node *Node) CreateAt(ctx context.Context, path string, repo Repository, idx *HardlinkIndex) error {
2016-09-27 20:35:08 +00:00
debug.Log("create node %v at %v", node.Name, path)
2016-09-01 19:20:03 +00:00
switch node.Type {
2015-02-03 20:07:55 +00:00
case "dir":
2015-04-29 01:24:43 +00:00
if err := node.createDirAt(path); err != nil {
2016-08-21 15:24:13 +00:00
return err
2015-02-03 20:07:55 +00:00
}
case "file":
2017-06-04 09:16:55 +00:00
if err := node.createFileAt(ctx, path, repo, idx); err != nil {
2016-08-21 15:24:13 +00:00
return err
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 {
2016-08-21 15:24:13 +00:00
return err
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 {
2016-08-21 15:24:13 +00:00
return err
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 {
2016-08-21 15:24:13 +00:00
return err
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 {
2016-08-21 15:24:13 +00:00
return err
2015-02-03 20:07:55 +00:00
}
case "socket":
return nil
default:
2016-09-01 19:20:03 +00:00
return errors.Errorf("filetype %q not implemented!\n", node.Type)
2015-02-03 20:07:55 +00:00
}
err := node.restoreMetadata(path)
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("restoreMetadata(%s) error %v", path, err)
}
return err
2015-04-29 01:24:43 +00:00
}
func (node Node) restoreMetadata(path string) error {
var firsterr error
2015-04-29 01:24:43 +00:00
if err := lchown(path, int(node.UID), int(node.GID)); err != nil {
firsterr = errors.Wrap(err, "Lchown")
2015-04-29 01:24:43 +00:00
}
2016-09-01 19:20:03 +00:00
if node.Type != "symlink" {
if err := fs.Chmod(path, node.Mode); err != nil {
if firsterr != nil {
firsterr = errors.Wrap(err, "Chmod")
}
}
2015-02-03 20:07:55 +00:00
}
2016-09-01 19:20:03 +00:00
if node.Type != "dir" {
if err := node.RestoreTimestamps(path); err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("error restoring timestamps for dir %v: %v", path, err)
if firsterr != nil {
firsterr = err
}
2015-05-14 03:11:31 +00:00
}
}
if err := node.restoreExtendedAttributes(path); err != nil {
debug.Log("error restoring extended attributes for %v: %v", path, err)
if firsterr != nil {
firsterr = err
}
}
return firsterr
}
func (node Node) restoreExtendedAttributes(path string) error {
for _, attr := range node.ExtendedAttributes {
err := Setxattr(path, attr.Name, attr.Value)
if err != nil {
return err
}
}
2015-05-14 03:11:31 +00:00
return nil
}
func (node Node) RestoreTimestamps(path string) error {
var utimes = [...]syscall.Timespec{
2015-04-29 01:24:43 +00:00
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
syscall.NsecToTimespec(node.ModTime.UnixNano()),
}
2015-05-14 03:11:31 +00:00
2016-09-01 19:20:03 +00:00
if node.Type == "symlink" {
2016-08-29 19:38:34 +00:00
return node.restoreSymlinkTimestamps(path, utimes)
}
if err := syscall.UtimesNano(path, utimes[:]); err != nil {
2016-08-29 19:38:34 +00:00
return errors.Wrap(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 := fs.Mkdir(path, node.Mode)
if err != nil && !os.IsExist(err) {
2016-08-29 19:38:34 +00:00
return errors.Wrap(err, "Mkdir")
2015-02-03 20:07:55 +00:00
}
return nil
}
2017-06-04 09:16:55 +00:00
func (node Node) createFileAt(ctx context.Context, path string, repo Repository, idx *HardlinkIndex) error {
2017-04-05 18:45:24 +00:00
if node.Links > 1 && idx.Has(node.Inode, node.DeviceID) {
if err := fs.Remove(path); !os.IsNotExist(err) {
return errors.Wrap(err, "RemoveCreateHardlink")
}
2017-04-05 18:45:24 +00:00
err := fs.Link(idx.GetFilename(node.Inode, node.DeviceID), path)
2017-01-30 23:14:20 +00:00
if err != nil {
return errors.Wrap(err, "CreateHardlink")
}
return nil
}
f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
2015-04-29 01:24:43 +00:00
if err != nil {
2016-08-29 19:38:34 +00:00
return errors.Wrap(err, "OpenFile")
2015-02-03 20:07:55 +00:00
}
err = node.writeNodeContent(ctx, repo, f)
closeErr := f.Close()
if err != nil {
return err
}
if closeErr != nil {
return errors.Wrap(closeErr, "Close")
}
if node.Links > 1 {
idx.Add(node.Inode, node.DeviceID, path)
}
return nil
}
2017-10-26 20:22:10 +00:00
func (node Node) writeNodeContent(ctx context.Context, repo Repository, f *os.File) error {
var buf []byte
for _, id := range node.Content {
2016-08-31 18:58:57 +00:00
size, err := repo.LookupBlobSize(id, DataBlob)
if err != nil {
return err
}
buf = buf[:cap(buf)]
if len(buf) < CiphertextLength(int(size)) {
buf = NewBlobBuffer(int(size))
}
2017-06-04 09:16:55 +00:00
n, err := repo.LoadBlob(ctx, DataBlob, id, buf)
2015-04-29 01:24:43 +00:00
if err != nil {
2016-08-21 15:24:13 +00:00
return err
2015-04-29 01:24:43 +00:00
}
buf = buf[:n]
2015-04-29 01:24:43 +00:00
_, err = f.Write(buf)
if err != nil {
2016-08-29 19:38:34 +00:00
return errors.Wrap(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 {
// Windows does not allow non-admins to create soft links.
if runtime.GOOS == "windows" {
return nil
}
err := fs.Symlink(node.LinkTarget, path)
2015-04-29 01:24:43 +00:00
if err != nil {
2016-08-29 19:38:34 +00:00
return errors.Wrap(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
}
func (node *Node) createDevAt(path string) error {
return mknod(path, syscall.S_IFBLK|0600, int(node.Device))
}
func (node *Node) createCharDevAt(path string) error {
return mknod(path, syscall.S_IFCHR|0600, int(node.Device))
}
func (node *Node) createFifoAt(path string) error {
return mkfifo(path, 0600)
}
2015-02-03 20:07:55 +00:00
func (node Node) MarshalJSON() ([]byte, error) {
if node.ModTime.Year() < 0 || node.ModTime.Year() > 9999 {
err := errors.Errorf("node %v has invalid ModTime year %d: %v",
node.Path, node.ModTime.Year(), node.ModTime)
return nil, err
}
if node.ChangeTime.Year() < 0 || node.ChangeTime.Year() > 9999 {
err := errors.Errorf("node %v has invalid ChangeTime year %d: %v",
node.Path, node.ChangeTime.Year(), node.ChangeTime)
return nil, err
}
if node.AccessTime.Year() < 0 || node.AccessTime.Year() > 9999 {
err := errors.Errorf("node %v has invalid AccessTime year %d: %v",
node.Path, node.AccessTime.Year(), node.AccessTime)
return nil, err
}
2015-02-03 20:07:55 +00:00
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 {
2016-08-29 19:38:34 +00:00
return errors.Wrap(err, "Unmarshal")
2015-02-03 20:07:55 +00:00
}
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
2016-08-29 19:38:34 +00:00
return errors.Wrap(err, "Unquote")
2015-02-03 20:07:55 +00:00
}
func (node Node) Equals(other Node) bool {
if node.Name != other.Name {
return false
}
2016-09-01 19:20:03 +00:00
if node.Type != other.Type {
2015-02-03 20:07:55 +00:00
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
}
2017-04-05 18:42:15 +00:00
if node.DeviceID != other.DeviceID {
return false
}
2015-02-03 20:07:55 +00:00
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.sameExtendedAttributes(other) {
return false
}
if node.Subtree != nil {
if other.Subtree == nil {
return false
}
if !node.Subtree.Equal(*other.Subtree) {
return false
}
} else {
if other.Subtree != nil {
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
}
}
return true
}
func (node Node) sameExtendedAttributes(other Node) bool {
if len(node.ExtendedAttributes) != len(other.ExtendedAttributes) {
return false
}
// build a set of all attributes that node has
type mapvalue struct {
value []byte
present bool
}
attributes := make(map[string]mapvalue)
for _, attr := range node.ExtendedAttributes {
attributes[attr.Name] = mapvalue{value: attr.Value}
}
for _, attr := range other.ExtendedAttributes {
v, ok := attributes[attr.Name]
if !ok {
// extended attribute is not set for node
debug.Log("other node has attribute %v, which is not present in node", attr.Name)
return false
}
if !bytes.Equal(v.value, attr.Value) {
// attribute has different value
debug.Log("attribute %v has different value", attr.Name)
return false
}
// remember that this attribute is present in other.
v.present = true
attributes[attr.Name] = v
}
// check for attributes that are not present in other
for name, v := range attributes {
if !v.present {
debug.Log("attribute %v not present in other node", name)
return false
}
}
2015-04-29 01:24:43 +00:00
2015-02-03 20:07:55 +00:00
return true
}
2015-04-29 03:31:07 +00:00
2016-09-03 12:13:26 +00:00
// IsNewer returns true of the file has been updated since the last Stat().
2016-08-31 20:39:36 +00:00
func (node *Node) IsNewer(path string, fi os.FileInfo) bool {
2016-09-01 19:20:03 +00:00
if node.Type != "file" {
2016-09-27 20:35:08 +00:00
debug.Log("node %v is newer: not file", path)
2015-04-29 03:31:07 +00:00
return true
}
tpe := nodeTypeFromFileInfo(fi)
2016-09-01 19:20:03 +00:00
if node.Name != fi.Name() || node.Type != tpe {
2016-09-27 20:35:08 +00:00
debug.Log("node %v is newer: name or type changed", path)
2015-04-29 03:31:07 +00:00
return true
}
size := uint64(fi.Size())
extendedStat, ok := toStatT(fi.Sys())
if !ok {
if !node.ModTime.Equal(fi.ModTime()) ||
node.Size != size {
2016-09-27 20:35:08 +00:00
debug.Log("node %v is newer: timestamp or size changed", path)
return true
}
return false
}
inode := extendedStat.ino()
2015-04-29 03:31:07 +00:00
if !node.ModTime.Equal(fi.ModTime()) ||
!node.ChangeTime.Equal(changeTime(extendedStat)) ||
node.Inode != uint64(inode) ||
2015-04-29 03:31:07 +00:00
node.Size != size {
2016-09-27 20:35:08 +00:00
debug.Log("node %v is newer: timestamp, size or inode changed", path)
2015-04-29 03:31:07 +00:00
return true
}
2016-09-27 20:35:08 +00:00
debug.Log("node %v is not newer", path)
2015-04-29 03:31:07 +00:00
return false
}
2015-04-29 03:45:24 +00:00
func (node *Node) fillUser(stat statT) error {
node.UID = stat.uid()
node.GID = stat.gid()
2015-04-29 03:45:24 +00:00
username, err := lookupUsername(strconv.Itoa(int(stat.uid())))
2015-04-29 03:45:24 +00:00
if err != nil {
2016-08-21 15:24:13 +00:00
return err
2015-04-29 03:45:24 +00:00
}
group, err := lookupGroup(strconv.Itoa(int(stat.gid())))
if err != nil {
return err
}
2015-05-01 21:26:40 +00:00
node.User = username
node.Group = group
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
}
username := ""
2015-05-01 21:26:40 +00:00
u, err := user.LookupId(uid)
if err == nil {
username = u.Username
2015-05-01 21:26:40 +00:00
}
uidLookupCacheMutex.Lock()
uidLookupCache[uid] = username
2015-05-01 21:26:40 +00:00
uidLookupCacheMutex.Unlock()
return username, nil
2015-05-01 21:26:40 +00:00
}
var (
gidLookupCache = make(map[string]string)
gidLookupCacheMutex = sync.RWMutex{}
)
func lookupGroup(gid string) (string, error) {
gidLookupCacheMutex.RLock()
value, ok := gidLookupCache[gid]
gidLookupCacheMutex.RUnlock()
if ok {
return value, nil
}
group := ""
g, err := user.LookupGroupId(gid)
if err == nil {
group = g.Name
}
gidLookupCacheMutex.Lock()
gidLookupCache[gid] = group
gidLookupCacheMutex.Unlock()
return group, nil
}
2015-04-29 03:45:24 +00:00
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
stat, ok := toStatT(fi.Sys())
2015-04-29 03:45:24 +00:00
if !ok {
return nil
}
node.Inode = uint64(stat.ino())
2017-04-05 18:45:24 +00:00
node.DeviceID = uint64(stat.dev())
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 {
2016-08-21 15:24:13 +00:00
return err
2015-05-01 21:31:02 +00:00
}
2016-09-01 19:20:03 +00:00
switch node.Type {
2015-04-29 03:45:24 +00:00
case "file":
node.Size = uint64(stat.size())
node.Links = uint64(stat.nlink())
2015-04-29 03:45:24 +00:00
case "dir":
case "symlink":
node.LinkTarget, err = fs.Readlink(path)
2017-01-30 23:14:20 +00:00
node.Links = uint64(stat.nlink())
if err != nil {
return errors.Wrap(err, "Readlink")
}
2015-04-29 03:45:24 +00:00
case "dev":
node.Device = uint64(stat.rdev())
2017-01-30 23:14:20 +00:00
node.Links = uint64(stat.nlink())
2015-04-29 03:45:24 +00:00
case "chardev":
node.Device = uint64(stat.rdev())
2017-01-30 23:14:20 +00:00
node.Links = uint64(stat.nlink())
2015-04-29 03:45:24 +00:00
case "fifo":
case "socket":
default:
return errors.Errorf("invalid node type %q", node.Type)
2015-04-29 03:45:24 +00:00
}
if err = node.fillExtendedAttributes(path); err != nil {
return err
}
return nil
}
func (node *Node) fillExtendedAttributes(path string) error {
if node.Type == "symlink" {
return nil
}
xattrs, err := Listxattr(path)
debug.Log("fillExtendedAttributes(%v) %v %v", path, xattrs, err)
if err != nil {
return err
}
node.ExtendedAttributes = make([]ExtendedAttribute, 0, len(xattrs))
for _, attr := range xattrs {
attrVal, err := Getxattr(path, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "can not obtain extended attribute %v for %v:\n", attr, path)
continue
}
attr := ExtendedAttribute{
Name: attr,
Value: attrVal,
}
node.ExtendedAttributes = append(node.ExtendedAttributes, attr)
}
return nil
2015-04-29 03:45:24 +00:00
}
type statT interface {
dev() uint64
ino() uint64
nlink() uint64
uid() uint32
gid() uint32
rdev() uint64
size() int64
atim() syscall.Timespec
mtim() syscall.Timespec
ctim() syscall.Timespec
}
func mkfifo(path string, mode uint32) (err error) {
return mknod(path, mode|syscall.S_IFIFO, 0)
}
func (node *Node) fillTimes(stat statT) {
ctim := stat.ctim()
atim := stat.atim()
node.ChangeTime = time.Unix(ctim.Unix())
node.AccessTime = time.Unix(atim.Unix())
}
func changeTime(stat statT) time.Time {
ctim := stat.ctim()
return time.Unix(ctim.Unix())
}