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"
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-08-21 15:48:36 +00:00
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
"bytes"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/debug"
|
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
|
|
|
"restic/fs"
|
2017-02-02 11:23:13 +00:00
|
|
|
"runtime"
|
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"`
|
|
|
|
Size uint64 `json:"size,omitempty"`
|
|
|
|
Links uint64 `json:"links,omitempty"`
|
|
|
|
LinkTarget string `json:"linktarget,omitempty"`
|
|
|
|
ExtendedAttributes []ExtendedAttribute `json:"extended_attributes,omitempty"`
|
|
|
|
Device uint64 `json:"device,omitempty"`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-02 14:22:43 +00:00
|
|
|
// CreateAt creates the node at the given path and restores all the meta data.
|
2017-01-30 23:14:20 +00:00
|
|
|
func (node *Node) CreateAt(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)
|
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":
|
2017-01-30 23:14:20 +00:00
|
|
|
if err := node.createFileAt(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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
var err error
|
|
|
|
|
2015-08-14 13:57:47 +00:00
|
|
|
err = lchown(path, int(node.UID), int(node.GID))
|
2015-04-29 01:24:43 +00:00
|
|
|
if err != nil {
|
2016-08-29 19:38:34 +00:00
|
|
|
return errors.Wrap(err, "Lchown")
|
2015-04-29 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:20:03 +00:00
|
|
|
if node.Type != "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
|
|
|
err = fs.Chmod(path, node.Mode)
|
2015-05-14 21:06:11 +00:00
|
|
|
if err != nil {
|
2016-08-29 19:38:34 +00:00
|
|
|
return errors.Wrap(err, "Chmod")
|
2015-05-14 21:06:11 +00:00
|
|
|
}
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:20:03 +00:00
|
|
|
if node.Type != "dir" {
|
2015-05-14 03:11:31 +00:00
|
|
|
err = node.RestoreTimestamps(path)
|
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("error restoring timestamps for dir %v: %v", path, err)
|
2015-05-14 03:11:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
err = node.restoreExtendedAttributes(path)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("error restoring extended attributes for %v: %v", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-30 23:14:20 +00:00
|
|
|
func (node Node) createFileAt(path string, repo Repository, idx *HardlinkIndex) error {
|
|
|
|
if node.Links > 1 && idx.Has(node.Inode, node.Device) {
|
|
|
|
err := fs.Link(idx.GetFilename(node.Inode, node.Device), path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateHardlink")
|
|
|
|
}
|
|
|
|
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
|
|
|
f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
2015-04-29 01:24:43 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2016-08-29 19:38:34 +00:00
|
|
|
return errors.Wrap(err, "OpenFile")
|
2015-02-03 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-26 12:25:01 +00:00
|
|
|
var buf []byte
|
2015-04-26 15:44:38 +00:00
|
|
|
for _, id := range node.Content {
|
2016-08-31 18:58:57 +00:00
|
|
|
size, err := repo.LookupBlobSize(id, DataBlob)
|
2015-07-26 12:25:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-03 18:20:14 +00:00
|
|
|
n, err := repo.LoadBlob(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
|
|
|
}
|
|
|
|
|
2017-01-30 23:14:20 +00:00
|
|
|
idx.Add(node.Inode, node.Device, path)
|
|
|
|
|
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 {
|
2015-08-14 13:57:47 +00:00
|
|
|
return mknod(path, syscall.S_IFBLK|0600, int(node.Device))
|
2015-05-05 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) createCharDevAt(path string) error {
|
2015-08-14 13:57:47 +00:00
|
|
|
return mknod(path, syscall.S_IFCHR|0600, int(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
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-14 13:57:47 +00:00
|
|
|
size := uint64(fi.Size())
|
|
|
|
|
|
|
|
extendedStat, ok := toStatT(fi.Sys())
|
|
|
|
if !ok {
|
|
|
|
if node.ModTime != fi.ModTime() ||
|
|
|
|
node.Size != size {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("node %v is newer: timestamp or size changed", path)
|
2015-08-14 13:57:47 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
inode := extendedStat.ino()
|
2015-04-29 03:31:07 +00:00
|
|
|
|
|
|
|
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 {
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-14 13:57:47 +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 {
|
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())
|
|
|
|
}
|