2024-08-26 17:03:25 -04:00
|
|
|
package fs
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-11 15:37:28 -05:00
|
|
|
"fmt"
|
2024-08-26 17:03:25 -04:00
|
|
|
"os"
|
|
|
|
|
"os/user"
|
|
|
|
|
"strconv"
|
|
|
|
|
"sync"
|
|
|
|
|
"syscall"
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
"github.com/restic/restic/internal/data"
|
2024-08-26 17:03:25 -04:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-28 04:58:07 -04:00
|
|
|
// nodeFromFileInfo returns a new node from the given path and FileInfo. It
|
2024-08-26 17:03:25 -04:00
|
|
|
// returns the first error that is encountered, together with a node.
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeFromFileInfo(path string, fi *ExtendedFileInfo, ignoreXattrListError bool, warnf func(format string, args ...any)) (*data.Node, error) {
|
2024-11-30 10:58:04 -05:00
|
|
|
node := buildBasicNode(path, fi)
|
2024-08-28 04:58:07 -04:00
|
|
|
|
2024-11-03 10:01:59 -05:00
|
|
|
if err := nodeFillExtendedStat(node, path, fi); err != nil {
|
2024-08-28 04:58:07 -04:00
|
|
|
return node, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 10:01:59 -05:00
|
|
|
err := nodeFillGenericAttributes(node, path, fi)
|
2025-09-21 13:24:48 -04:00
|
|
|
err = errors.Join(err, nodeFillExtendedAttributes(node, path, ignoreXattrListError, warnf))
|
2024-08-28 04:58:07 -04:00
|
|
|
return node, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func buildBasicNode(path string, fi *ExtendedFileInfo) *data.Node {
|
2024-08-26 17:03:25 -04:00
|
|
|
mask := os.ModePerm | os.ModeType | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
|
2025-09-23 14:01:09 -04:00
|
|
|
node := &data.Node{
|
2024-08-26 17:03:25 -04:00
|
|
|
Path: path,
|
2024-11-30 10:58:04 -05:00
|
|
|
Name: fi.Name,
|
|
|
|
|
Mode: fi.Mode & mask,
|
|
|
|
|
ModTime: fi.ModTime,
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-11-30 10:58:04 -05:00
|
|
|
node.Type = nodeTypeFromFileInfo(fi.Mode)
|
2025-09-23 14:01:09 -04:00
|
|
|
if node.Type == data.NodeTypeFile {
|
2024-11-30 10:58:04 -05:00
|
|
|
node.Size = uint64(fi.Size)
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
2024-08-28 04:58:07 -04:00
|
|
|
return node
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeTypeFromFileInfo(mode os.FileMode) data.NodeType {
|
2024-11-03 10:01:59 -05:00
|
|
|
switch mode & os.ModeType {
|
2024-08-26 17:03:25 -04:00
|
|
|
case 0:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeFile
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeDir:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeDir
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeSymlink:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeSymlink
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeDevice | os.ModeCharDevice:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeCharDev
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeDevice:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeDev
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeNamedPipe:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeFifo
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeSocket:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeSocket
|
2024-08-26 17:03:25 -04:00
|
|
|
case os.ModeIrregular:
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeIrregular
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
return data.NodeTypeInvalid
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeFillExtendedStat(node *data.Node, path string, stat *ExtendedFileInfo) error {
|
2024-08-24 17:43:45 -04:00
|
|
|
node.Inode = stat.Inode
|
|
|
|
|
node.DeviceID = stat.DeviceID
|
|
|
|
|
node.ChangeTime = stat.ChangeTime
|
|
|
|
|
node.AccessTime = stat.AccessTime
|
2024-08-26 17:03:25 -04:00
|
|
|
|
2024-08-24 17:43:45 -04:00
|
|
|
node.UID = stat.UID
|
|
|
|
|
node.GID = stat.GID
|
|
|
|
|
node.User = lookupUsername(stat.UID)
|
|
|
|
|
node.Group = lookupGroup(stat.GID)
|
2024-08-26 17:03:25 -04:00
|
|
|
|
|
|
|
|
switch node.Type {
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeFile:
|
2024-08-24 17:43:45 -04:00
|
|
|
node.Size = uint64(stat.Size)
|
|
|
|
|
node.Links = stat.Links
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeDir:
|
|
|
|
|
case data.NodeTypeSymlink:
|
2024-08-26 17:03:25 -04:00
|
|
|
var err error
|
2024-07-21 09:58:41 -04:00
|
|
|
node.LinkTarget, err = os.Readlink(fixpath(path))
|
2024-08-24 17:43:45 -04:00
|
|
|
node.Links = stat.Links
|
2024-08-26 17:03:25 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
|
}
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeDev:
|
2024-08-24 17:43:45 -04:00
|
|
|
node.Device = stat.Device
|
|
|
|
|
node.Links = stat.Links
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeCharDev:
|
2024-08-24 17:43:45 -04:00
|
|
|
node.Device = stat.Device
|
|
|
|
|
node.Links = stat.Links
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeFifo:
|
|
|
|
|
case data.NodeTypeSocket:
|
2024-08-26 17:03:25 -04:00
|
|
|
default:
|
|
|
|
|
return errors.Errorf("unsupported file type %q", node.Type)
|
|
|
|
|
}
|
2024-08-28 04:58:07 -04:00
|
|
|
return nil
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 13:08:08 -04:00
|
|
|
var (
|
|
|
|
|
userNameLookupCache = make(map[string]uint32)
|
|
|
|
|
userNameLookupCacheMutex = sync.RWMutex{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Cached uid lookup by user name. Returns 0 when no id can be found.
|
|
|
|
|
//
|
|
|
|
|
//nolint:revive // captialization is correct as is
|
|
|
|
|
func lookupUid(userName string) uint32 {
|
|
|
|
|
userNameLookupCacheMutex.RLock()
|
|
|
|
|
uid, ok := userNameLookupCache[userName]
|
|
|
|
|
userNameLookupCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
|
return uid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u, err := user.Lookup(userName)
|
|
|
|
|
if err == nil {
|
|
|
|
|
var s int
|
|
|
|
|
s, err = strconv.Atoi(u.Uid)
|
|
|
|
|
if err == nil {
|
|
|
|
|
uid = uint32(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userNameLookupCacheMutex.Lock()
|
|
|
|
|
userNameLookupCache[userName] = uid
|
|
|
|
|
userNameLookupCacheMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
return uid
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 17:03:25 -04:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 13:08:08 -04:00
|
|
|
var (
|
|
|
|
|
groupNameLookupCache = make(map[string]uint32)
|
|
|
|
|
groupNameLookupCacheMutex = sync.RWMutex{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Cached uid lookup by group name. Returns 0 when no id can be found.
|
|
|
|
|
func lookupGid(groupName string) uint32 {
|
|
|
|
|
groupNameLookupCacheMutex.RLock()
|
|
|
|
|
gid, ok := groupNameLookupCache[groupName]
|
|
|
|
|
groupNameLookupCacheMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
|
return gid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g, err := user.LookupGroup(groupName)
|
|
|
|
|
if err == nil {
|
|
|
|
|
var s int
|
|
|
|
|
s, err = strconv.Atoi(g.Gid)
|
|
|
|
|
if err == nil {
|
|
|
|
|
gid = uint32(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupNameLookupCacheMutex.Lock()
|
|
|
|
|
groupNameLookupCache[groupName] = gid
|
|
|
|
|
groupNameLookupCacheMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
return gid
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 17:03:25 -04:00
|
|
|
// NodeCreateAt creates the node at the given path but does NOT restore node meta data.
|
2025-09-23 14:01:09 -04:00
|
|
|
func NodeCreateAt(node *data.Node, path string) (err error) {
|
2024-08-26 17:03:25 -04:00
|
|
|
debug.Log("create node %v at %v", node.Name, path)
|
|
|
|
|
|
|
|
|
|
switch node.Type {
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeDir:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateDirAt(node, path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeFile:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateFileAt(path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeSymlink:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateSymlinkAt(node, path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeDev:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateDevAt(node, path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeCharDev:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateCharDevAt(node, path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeFifo:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nodeCreateFifoAt(path)
|
2025-09-23 14:01:09 -04:00
|
|
|
case data.NodeTypeSocket:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = nil
|
2024-08-26 17:03:25 -04:00
|
|
|
default:
|
2024-10-03 15:36:48 -04:00
|
|
|
err = errors.Errorf("filetype %q not implemented", node.Type)
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 15:36:48 -04:00
|
|
|
return err
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeCreateDirAt(node *data.Node, path string) error {
|
2024-07-21 09:58:41 -04:00
|
|
|
err := os.Mkdir(fixpath(path), node.Mode)
|
2024-08-26 17:03:25 -04: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
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeCreateSymlinkAt(node *data.Node, path string) error {
|
2024-07-21 09:58:41 -04:00
|
|
|
if err := os.Symlink(node.LinkTarget, fixpath(path)); err != nil {
|
2024-08-26 17:03:25 -04:00
|
|
|
return errors.WithStack(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeCreateDevAt(node *data.Node, path string) error {
|
2024-08-26 17:03:25 -04:00
|
|
|
return mknod(path, syscall.S_IFBLK|0600, node.Device)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeCreateCharDevAt(node *data.Node, path string) error {
|
2024-08-26 17:03:25 -04:00
|
|
|
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
|
2025-07-16 13:08:08 -04:00
|
|
|
func NodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xattrSelectFilter func(xattrName string) bool, ownershipByName bool) error {
|
|
|
|
|
err := nodeRestoreMetadata(node, path, warn, xattrSelectFilter, ownershipByName)
|
2024-08-26 17:03:25 -04:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 13:08:08 -04:00
|
|
|
func nodeRestoreMetadata(node *data.Node, path string, warn func(msg string), xattrSelectFilter func(xattrName string) bool, ownershipByName bool) error {
|
2024-08-26 17:03:25 -04:00
|
|
|
var firsterr error
|
|
|
|
|
|
2025-07-16 13:08:08 -04:00
|
|
|
if err := lchown(path, node, ownershipByName); err != nil {
|
2024-08-26 17:03:25 -04:00
|
|
|
firsterr = errors.WithStack(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 02:14:45 -05:00
|
|
|
if err := nodeRestoreExtendedAttributes(node, path, xattrSelectFilter); err != nil {
|
2024-08-26 17:03:25 -04:00
|
|
|
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 09:34:53 -04:00
|
|
|
if err := nodeRestoreTimestamps(node, path); err != nil {
|
2024-08-26 17:03:25 -04: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.
|
2025-09-23 14:01:09 -04:00
|
|
|
if node.Type != data.NodeTypeSymlink {
|
2024-07-21 10:00:47 -04:00
|
|
|
if err := chmod(path, node.Mode); err != nil {
|
2024-08-26 17:03:25 -04:00
|
|
|
if firsterr == nil {
|
|
|
|
|
firsterr = errors.WithStack(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return firsterr
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:01:09 -04:00
|
|
|
func nodeRestoreTimestamps(node *data.Node, path string) error {
|
2024-10-04 04:06:18 -04:00
|
|
|
atime := node.AccessTime.UnixNano()
|
|
|
|
|
mtime := node.ModTime.UnixNano()
|
2024-08-26 17:03:25 -04:00
|
|
|
|
2024-10-04 04:06:18 -04:00
|
|
|
if err := utimesNano(fixpath(path), atime, mtime, node.Type); err != nil {
|
2024-11-11 15:37:28 -05:00
|
|
|
return fmt.Errorf("failed to restore timestamp of %q: %w", path, err)
|
2024-08-26 17:03:25 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|