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"
|
2016-08-21 15:48:36 +00:00
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-02-02 11:23:13 +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 {
|
2017-02-02 11:23:13 +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"`
|
2017-04-05 18:42:15 +00:00
|
|
|
DeviceID uint64 `json:"device_id,omitempty"` // device id of the file, stat.st_dev
|
2017-02-02 11:23:13 +00:00
|
|
|
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
|
2017-02-02 11:23:13 +00:00
|
|
|
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 {
|
2017-12-03 16:34:37 +00:00
|
|
|
var mode os.FileMode
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2015-02-03 20:07:55 +00:00
|
|
|
case "file":
|
2017-12-03 16:34:37 +00:00
|
|
|
mode = 0
|
2015-02-03 20:07:55 +00:00
|
|
|
case "dir":
|
2017-12-03 16:34:37 +00:00
|
|
|
mode = os.ModeDir
|
|
|
|
case "symlink":
|
|
|
|
mode = os.ModeSymlink
|
|
|
|
case "dev":
|
|
|
|
mode = os.ModeDevice
|
|
|
|
case "chardev":
|
|
|
|
mode = os.ModeDevice | os.ModeCharDevice
|
|
|
|
case "fifo":
|
|
|
|
mode = os.ModeNamedPipe
|
|
|
|
case "socket":
|
|
|
|
mode = os.ModeSocket
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 16:34:37 +00:00
|
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
|
|
mode|node.Mode, node.UID, node.GID, node.Size, node.ModTime, node.Name)
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 16:46:06 +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) {
|
2015-12-20 18:45:36 +00:00
|
|
|
mask := os.ModePerm | os.ModeType | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
|
2015-03-22 15:38:03 +00:00
|
|
|
node := &Node{
|
2016-08-31 20:39:36 +00:00
|
|
|
Path: path,
|
2015-03-22 15:38:03 +00:00
|
|
|
Name: fi.Name(),
|
2015-12-20 18:45:36 +00:00
|
|
|
Mode: fi.Mode() & mask,
|
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
|
|
|
}
|
|
|
|
|
2017-02-02 11:23:13 +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
|
|
|
|
}
|
|
|
|
|
2018-04-08 02:43:14 +00:00
|
|
|
// CreateAt creates the node at the given path but does NOT restore node meta data.
|
2018-05-11 04:45:14 +00:00
|
|
|
func (node *Node) CreateAt(ctx context.Context, path string, repo Repository) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("create node %v at %v", node.Name, path)
|
2015-07-06 21:59:28 +00:00
|
|
|
|
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":
|
2018-05-11 04:45:14 +00:00
|
|
|
if err := node.createFileAt(ctx, path, repo); 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
|
|
|
}
|
|
|
|
|
2018-04-08 02:43:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreMetadata restores node metadata
|
|
|
|
func (node Node) RestoreMetadata(path string) error {
|
2015-07-06 21:59:28 +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)
|
2015-07-06 21:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node Node) restoreMetadata(path string) error {
|
2017-03-12 15:39:37 +00:00
|
|
|
var firsterr error
|
2015-04-29 01:24:43 +00:00
|
|
|
|
2017-03-12 15:39:37 +00:00
|
|
|
if err := lchown(path, int(node.UID), int(node.GID)); err != nil {
|
2018-09-09 09:43:18 +00:00
|
|
|
// Like "cp -a" and "rsync -a" do, we only report lchown permission errors
|
|
|
|
// if we run as root.
|
|
|
|
// On Windows, Geteuid always returns -1, and we always report lchown
|
|
|
|
// permission errors.
|
|
|
|
if os.Geteuid() > 0 && os.IsPermission(err) {
|
|
|
|
debug.Log("not running as root, ignoring lchown permission error for %v: %v",
|
|
|
|
path, err)
|
|
|
|
} else {
|
|
|
|
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" {
|
2017-03-12 15:39:37 +00:00
|
|
|
if err := fs.Chmod(path, node.Mode); err != nil {
|
|
|
|
if firsterr != nil {
|
|
|
|
firsterr = errors.Wrap(err, "Chmod")
|
|
|
|
}
|
2015-05-14 21:06:11 +00:00
|
|
|
}
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 02:43:14 +00:00
|
|
|
if err := node.RestoreTimestamps(path); err != nil {
|
|
|
|
debug.Log("error restoring timestamps for dir %v: %v", path, err)
|
|
|
|
if firsterr != nil {
|
|
|
|
firsterr = err
|
2015-05-14 03:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:39:37 +00:00
|
|
|
if err := node.restoreExtendedAttributes(path); err != nil {
|
2017-02-02 11:23:13 +00:00
|
|
|
debug.Log("error restoring extended attributes for %v: %v", path, err)
|
2017-03-12 15:39:37 +00:00
|
|
|
if firsterr != nil {
|
|
|
|
firsterr = err
|
|
|
|
}
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 15:39:37 +00:00
|
|
|
return firsterr
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-05-14 21:06:11 +00:00
|
|
|
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)
|
2015-05-14 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
err := fs.Mkdir(path, node.Mode)
|
2016-11-14 07:53:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-11 04:45:14 +00:00
|
|
|
func (node Node) createFileAt(ctx context.Context, path string, repo Repository) error {
|
2017-03-10 21:45:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-26 17:53:31 +00:00
|
|
|
err = node.writeNodeContent(ctx, repo, f)
|
|
|
|
closeErr := f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if closeErr != nil {
|
|
|
|
return errors.Wrap(closeErr, "Close")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:22:10 +00:00
|
|
|
func (node Node) writeNodeContent(ctx context.Context, repo Repository, f *os.File) error {
|
2015-07-26 12:25:01 +00:00
|
|
|
var buf []byte
|
2015-04-26 15:44:38 +00:00
|
|
|
for _, id := range node.Content {
|
2018-01-12 06:20:12 +00:00
|
|
|
size, found := repo.LookupBlobSize(id, DataBlob)
|
|
|
|
if !found {
|
|
|
|
return errors.Errorf("id %v not found in repository", id)
|
2015-07-26 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:cap(buf)]
|
2017-02-15 14:19:28 +00:00
|
|
|
if len(buf) < CiphertextLength(int(size)) {
|
2017-01-13 11:57:05 +00:00
|
|
|
buf = NewBlobBuffer(int(size))
|
2015-07-26 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-09-03 09:22:01 +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 {
|
2015-08-14 13:57:47 +00:00
|
|
|
// Windows does not allow non-admins to create soft links.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return nil
|
|
|
|
}
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-05 19:34:12 +00:00
|
|
|
func (node *Node) createDevAt(path string) error {
|
2019-03-06 14:41:49 +00:00
|
|
|
return mknod(path, syscall.S_IFBLK|0600, node.device())
|
2015-05-05 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) createCharDevAt(path string) error {
|
2019-03-06 14:41:49 +00:00
|
|
|
return mknod(path, syscall.S_IFCHR|0600, node.device())
|
2015-05-05 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) createFifoAt(path string) error {
|
2015-08-14 13:57:47 +00:00
|
|
|
return mkfifo(path, 0600)
|
2015-05-05 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 20:07:55 +00:00
|
|
|
func (node Node) MarshalJSON() ([]byte, error) {
|
2016-09-17 08:41:42 +00:00
|
|
|
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
|
|
|
|
}
|
2017-11-16 20:51:12 +00:00
|
|
|
if !node.ModTime.Equal(other.ModTime) {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-11-16 20:51:12 +00:00
|
|
|
if !node.AccessTime.Equal(other.AccessTime) {
|
2015-02-03 20:07:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-11-16 20:51:12 +00:00
|
|
|
if !node.ChangeTime.Equal(other.ChangeTime) {
|
2015-02-03 20:07:55 +00:00
|
|
|
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
|
|
|
}
|
2017-02-02 11:23:13 +00:00
|
|
|
if !node.sameExtendedAttributes(other) {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-25 15:05:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 11:23:13 +00:00
|
|
|
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
|
|
|
|
2015-08-14 13:57:47 +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
|
|
|
|
2015-08-14 13:57:47 +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
|
|
|
}
|
|
|
|
|
2017-10-04 18:11:54 +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
|
2017-10-04 18:11:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:13:00 +00:00
|
|
|
username := ""
|
|
|
|
|
2015-05-01 21:26:40 +00:00
|
|
|
u, err := user.LookupId(uid)
|
2015-05-14 21:13:00 +00:00
|
|
|
if err == nil {
|
|
|
|
username = u.Username
|
2015-05-01 21:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uidLookupCacheMutex.Lock()
|
2015-05-14 21:13:00 +00:00
|
|
|
uidLookupCache[uid] = username
|
2015-05-01 21:26:40 +00:00
|
|
|
uidLookupCacheMutex.Unlock()
|
|
|
|
|
2015-05-14 21:13:00 +00:00
|
|
|
return username, nil
|
2015-05-01 21:26:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 18:11:54 +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 {
|
2015-08-14 13:57:47 +00:00
|
|
|
stat, ok := toStatT(fi.Sys())
|
2015-04-29 03:45:24 +00:00
|
|
|
if !ok {
|
2018-03-31 11:22:36 +00:00
|
|
|
// fill minimal info with current values for uid, gid
|
|
|
|
node.UID = uint32(os.Getuid())
|
|
|
|
node.GID = uint32(os.Getgid())
|
|
|
|
node.ChangeTime = node.ModTime
|
2015-04-29 03:45:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-14 13:57:47 +00:00
|
|
|
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":
|
2015-08-14 13:57:47 +00:00
|
|
|
node.Size = uint64(stat.size())
|
|
|
|
node.Links = uint64(stat.nlink())
|
2015-04-29 03:45:24 +00:00
|
|
|
case "dir":
|
|
|
|
case "symlink":
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
node.LinkTarget, err = fs.Readlink(path)
|
2017-01-30 23:14:20 +00:00
|
|
|
node.Links = uint64(stat.nlink())
|
2017-02-02 11:23:13 +00:00
|
|
|
if err != nil {
|
2017-02-18 10:35:04 +00:00
|
|
|
return errors.Wrap(err, "Readlink")
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
2015-04-29 03:45:24 +00:00
|
|
|
case "dev":
|
2015-08-14 13:57:47 +00:00
|
|
|
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":
|
2015-08-14 13:57:47 +00:00
|
|
|
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:
|
2017-02-02 11:23:13 +00:00
|
|
|
return errors.Errorf("invalid node type %q", node.Type)
|
2015-04-29 03:45:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
if err = node.fillExtendedAttributes(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-18 10:35:04 +00:00
|
|
|
return nil
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) fillExtendedAttributes(path string) error {
|
|
|
|
if node.Type == "symlink" {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-16 13:25:56 +00:00
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
xattrs, err := Listxattr(path)
|
2017-02-16 13:25:56 +00:00
|
|
|
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,
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
2017-02-16 13:25:56 +00:00
|
|
|
|
|
|
|
node.ExtendedAttributes = append(node.ExtendedAttributes, attr)
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
2017-02-16 13:25:56 +00:00
|
|
|
|
|
|
|
return nil
|
2015-04-29 03:45:24 +00:00
|
|
|
}
|
2015-08-14 13:57:47 +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())
|
|
|
|
}
|