2024-08-26 21:03:25 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeFromFileInfo returns a new node from the given path and FileInfo. It
|
|
|
|
// returns the first error that is encountered, together with a node.
|
|
|
|
func NodeFromFileInfo(path string, fi os.FileInfo, ignoreXattrListError bool) (*restic.Node, error) {
|
|
|
|
mask := os.ModePerm | os.ModeType | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
|
|
|
|
node := &restic.Node{
|
|
|
|
Path: path,
|
|
|
|
Name: fi.Name(),
|
|
|
|
Mode: fi.Mode() & mask,
|
|
|
|
ModTime: fi.ModTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Type = nodeTypeFromFileInfo(fi)
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeFile {
|
2024-08-26 21:03:25 +00:00
|
|
|
node.Size = uint64(fi.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
err := nodeFillExtra(node, path, fi, ignoreXattrListError)
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
func nodeTypeFromFileInfo(fi os.FileInfo) restic.NodeType {
|
2024-08-26 21:03:25 +00:00
|
|
|
switch fi.Mode() & os.ModeType {
|
|
|
|
case 0:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeFile
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeDir:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeDir
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeSymlink:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeSymlink
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeDevice | os.ModeCharDevice:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeCharDev
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeDevice:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeDev
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeNamedPipe:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeFifo
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeSocket:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeSocket
|
2024-08-26 21:03:25 +00:00
|
|
|
case os.ModeIrregular:
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeIrregular
|
2024-08-26 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
return restic.NodeTypeInvalid
|
2024-08-26 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func nodeFillExtra(node *restic.Node, path string, fi os.FileInfo, ignoreXattrListError bool) error {
|
2024-08-24 21:43:45 +00:00
|
|
|
if fi.Sys() == nil {
|
2024-08-26 21:03:25 +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
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 21:43:45 +00:00
|
|
|
stat := ExtendedStat(fi)
|
2024-08-26 21:03:25 +00:00
|
|
|
|
2024-08-24 21:43:45 +00:00
|
|
|
node.Inode = stat.Inode
|
|
|
|
node.DeviceID = stat.DeviceID
|
|
|
|
node.ChangeTime = stat.ChangeTime
|
|
|
|
node.AccessTime = stat.AccessTime
|
2024-08-26 21:03:25 +00:00
|
|
|
|
2024-08-24 21:43:45 +00:00
|
|
|
node.UID = stat.UID
|
|
|
|
node.GID = stat.GID
|
|
|
|
node.User = lookupUsername(stat.UID)
|
|
|
|
node.Group = lookupGroup(stat.GID)
|
2024-08-26 21:03:25 +00:00
|
|
|
|
|
|
|
switch node.Type {
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeFile:
|
2024-08-24 21:43:45 +00:00
|
|
|
node.Size = uint64(stat.Size)
|
|
|
|
node.Links = stat.Links
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeDir:
|
|
|
|
case restic.NodeTypeSymlink:
|
2024-08-26 21:03:25 +00:00
|
|
|
var err error
|
2024-07-21 13:58:41 +00:00
|
|
|
node.LinkTarget, err = os.Readlink(fixpath(path))
|
2024-08-24 21:43:45 +00:00
|
|
|
node.Links = stat.Links
|
2024-08-26 21:03:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeDev:
|
2024-08-24 21:43:45 +00:00
|
|
|
node.Device = stat.Device
|
|
|
|
node.Links = stat.Links
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeCharDev:
|
2024-08-24 21:43:45 +00:00
|
|
|
node.Device = stat.Device
|
|
|
|
node.Links = stat.Links
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeFifo:
|
|
|
|
case restic.NodeTypeSocket:
|
2024-08-26 21:03:25 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported file type %q", node.Type)
|
|
|
|
}
|
|
|
|
|
2024-08-24 21:43:45 +00:00
|
|
|
allowExtended, err := nodeFillGenericAttributes(node, path, &stat)
|
2024-08-26 21:03:25 +00:00
|
|
|
if allowExtended {
|
|
|
|
// Skip processing ExtendedAttributes if allowExtended is false.
|
|
|
|
err = errors.CombineErrors(err, nodeFillExtendedAttributes(node, path, ignoreXattrListError))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
uidLookupCache = make(map[uint32]string)
|
|
|
|
uidLookupCacheMutex = sync.RWMutex{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cached user name lookup by uid. Returns "" when no name can be found.
|
|
|
|
func lookupUsername(uid uint32) string {
|
|
|
|
uidLookupCacheMutex.RLock()
|
|
|
|
username, ok := uidLookupCache[uid]
|
|
|
|
uidLookupCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.LookupId(strconv.Itoa(int(uid)))
|
|
|
|
if err == nil {
|
|
|
|
username = u.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
uidLookupCacheMutex.Lock()
|
|
|
|
uidLookupCache[uid] = username
|
|
|
|
uidLookupCacheMutex.Unlock()
|
|
|
|
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
gidLookupCache = make(map[uint32]string)
|
|
|
|
gidLookupCacheMutex = sync.RWMutex{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cached group name lookup by gid. Returns "" when no name can be found.
|
|
|
|
func lookupGroup(gid uint32) string {
|
|
|
|
gidLookupCacheMutex.RLock()
|
|
|
|
group, ok := gidLookupCache[gid]
|
|
|
|
gidLookupCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := user.LookupGroupId(strconv.Itoa(int(gid)))
|
|
|
|
if err == nil {
|
|
|
|
group = g.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
gidLookupCacheMutex.Lock()
|
|
|
|
gidLookupCache[gid] = group
|
|
|
|
gidLookupCacheMutex.Unlock()
|
|
|
|
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeCreateAt creates the node at the given path but does NOT restore node meta data.
|
2024-10-03 19:36:48 +00:00
|
|
|
func NodeCreateAt(node *restic.Node, path string) (err error) {
|
2024-08-26 21:03:25 +00:00
|
|
|
debug.Log("create node %v at %v", node.Name, path)
|
|
|
|
|
|
|
|
switch node.Type {
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeDir:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateDirAt(node, path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeFile:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateFileAt(path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeSymlink:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateSymlinkAt(node, path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeDev:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateDevAt(node, path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeCharDev:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateCharDevAt(node, path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeFifo:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nodeCreateFifoAt(path)
|
2024-07-09 17:51:44 +00:00
|
|
|
case restic.NodeTypeSocket:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = nil
|
2024-08-26 21:03:25 +00:00
|
|
|
default:
|
2024-10-03 19:36:48 +00:00
|
|
|
err = errors.Errorf("filetype %q not implemented", node.Type)
|
2024-08-26 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 19:36:48 +00:00
|
|
|
return err
|
2024-08-26 21:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateDirAt(node *restic.Node, path string) error {
|
2024-07-21 13:58:41 +00:00
|
|
|
err := os.Mkdir(fixpath(path), node.Mode)
|
2024-08-26 21:03:25 +00:00
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateFileAt(path string) error {
|
|
|
|
f, err := OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateSymlinkAt(node *restic.Node, path string) error {
|
2024-07-21 13:58:41 +00:00
|
|
|
if err := os.Symlink(node.LinkTarget, fixpath(path)); err != nil {
|
2024-08-26 21:03:25 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateDevAt(node *restic.Node, path string) error {
|
|
|
|
return mknod(path, syscall.S_IFBLK|0600, node.Device)
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateCharDevAt(node *restic.Node, path string) error {
|
|
|
|
return mknod(path, syscall.S_IFCHR|0600, node.Device)
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeCreateFifoAt(path string) error {
|
|
|
|
return mkfifo(path, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkfifo(path string, mode uint32) (err error) {
|
|
|
|
return mknod(path, mode|syscall.S_IFIFO, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeRestoreMetadata restores node metadata
|
|
|
|
func NodeRestoreMetadata(node *restic.Node, path string, warn func(msg string)) error {
|
|
|
|
err := nodeRestoreMetadata(node, path, warn)
|
|
|
|
if err != nil {
|
|
|
|
// It is common to have permission errors for folders like /home
|
|
|
|
// unless you're running as root, so ignore those.
|
|
|
|
if os.Geteuid() > 0 && errors.Is(err, os.ErrPermission) {
|
|
|
|
debug.Log("not running as root, ignoring permission error for %v: %v",
|
|
|
|
path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
debug.Log("restoreMetadata(%s) error %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeRestoreMetadata(node *restic.Node, path string, warn func(msg string)) error {
|
|
|
|
var firsterr error
|
|
|
|
|
|
|
|
if err := lchown(path, int(node.UID), int(node.GID)); err != nil {
|
|
|
|
firsterr = errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeRestoreExtendedAttributes(node, path); err != nil {
|
|
|
|
debug.Log("error restoring extended attributes for %v: %v", path, err)
|
|
|
|
if firsterr == nil {
|
|
|
|
firsterr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeRestoreGenericAttributes(node, path, warn); err != nil {
|
|
|
|
debug.Log("error restoring generic attributes for %v: %v", path, err)
|
|
|
|
if firsterr == nil {
|
|
|
|
firsterr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 13:34:53 +00:00
|
|
|
if err := nodeRestoreTimestamps(node, path); err != nil {
|
2024-08-26 21:03:25 +00:00
|
|
|
debug.Log("error restoring timestamps for %v: %v", path, err)
|
|
|
|
if firsterr == nil {
|
|
|
|
firsterr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moving RestoreTimestamps and restoreExtendedAttributes calls above as for readonly files in windows
|
|
|
|
// calling Chmod below will no longer allow any modifications to be made on the file and the
|
|
|
|
// calls above would fail.
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type != restic.NodeTypeSymlink {
|
2024-07-21 14:00:47 +00:00
|
|
|
if err := chmod(path, node.Mode); err != nil {
|
2024-08-26 21:03:25 +00:00
|
|
|
if firsterr == nil {
|
|
|
|
firsterr = errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return firsterr
|
|
|
|
}
|
|
|
|
|
2024-08-27 13:34:53 +00:00
|
|
|
func nodeRestoreTimestamps(node *restic.Node, path string) error {
|
2024-08-26 21:03:25 +00:00
|
|
|
var utimes = [...]syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeSymlink {
|
2024-08-26 21:03:25 +00:00
|
|
|
return nodeRestoreSymlinkTimestamps(path, utimes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.UtimesNano(path, utimes[:]); err != nil {
|
|
|
|
return errors.Wrap(err, "UtimesNano")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|