2022-01-10 08:04:18 +00:00
|
|
|
//go:build darwin || freebsd || linux || solaris
|
|
|
|
// +build darwin freebsd linux solaris
|
2017-02-02 11:23:13 +00:00
|
|
|
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2024-05-17 20:18:20 +00:00
|
|
|
"fmt"
|
2024-02-23 00:31:20 +00:00
|
|
|
"os"
|
2017-02-02 11:23:13 +00:00
|
|
|
"syscall"
|
2017-02-16 13:25:56 +00:00
|
|
|
|
2024-05-17 20:18:20 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
|
2017-02-16 13:25:56 +00:00
|
|
|
"github.com/pkg/xattr"
|
2017-02-02 11:23:13 +00:00
|
|
|
)
|
|
|
|
|
2024-05-17 20:18:20 +00:00
|
|
|
// getxattr retrieves extended attribute data associated with path.
|
|
|
|
func getxattr(path, name string) ([]byte, error) {
|
2023-06-19 18:24:47 +00:00
|
|
|
b, err := xattr.LGet(path, name)
|
2022-08-01 10:42:56 +00:00
|
|
|
return b, handleXattrErr(err)
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 20:18:20 +00:00
|
|
|
// listxattr retrieves a list of names of extended attributes associated with the
|
2017-02-02 11:23:13 +00:00
|
|
|
// given path in the file system.
|
2024-05-17 20:18:20 +00:00
|
|
|
func listxattr(path string) ([]string, error) {
|
2023-06-19 18:24:47 +00:00
|
|
|
l, err := xattr.LList(path)
|
2022-08-01 10:42:56 +00:00
|
|
|
return l, handleXattrErr(err)
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 19:48:03 +00:00
|
|
|
func IsListxattrPermissionError(err error) bool {
|
|
|
|
var xerr *xattr.Error
|
|
|
|
if errors.As(err, &xerr) {
|
|
|
|
return xerr.Op == "xattr.list" && errors.Is(xerr.Err, os.ErrPermission)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-17 20:18:20 +00:00
|
|
|
// setxattr associates name and data together as an attribute of path.
|
|
|
|
func setxattr(path, name string, data []byte) error {
|
2023-06-19 18:24:47 +00:00
|
|
|
return handleXattrErr(xattr.LSet(path, name, data))
|
2022-08-01 10:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleXattrErr(err error) error {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case nil:
|
2017-02-02 11:23:13 +00:00
|
|
|
return nil
|
2022-08-01 10:42:56 +00:00
|
|
|
|
|
|
|
case *xattr.Error:
|
|
|
|
// On Linux, xattr calls on files in an SMB/CIFS mount can return
|
|
|
|
// ENOATTR instead of ENOTSUP.
|
|
|
|
switch e.Err {
|
2022-08-21 08:47:05 +00:00
|
|
|
case syscall.ENOTSUP, xattr.ENOATTR:
|
2022-08-01 10:42:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.WithStack(e)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.WithStack(e)
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-23 00:31:20 +00:00
|
|
|
|
|
|
|
// restoreGenericAttributes is no-op.
|
2024-02-23 00:52:26 +00:00
|
|
|
func (node *Node) restoreGenericAttributes(_ string, warn func(msg string)) error {
|
|
|
|
return node.handleAllUnknownGenericAttributesFound(warn)
|
2024-02-23 00:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fillGenericAttributes is a no-op.
|
|
|
|
func (node *Node) fillGenericAttributes(_ string, _ os.FileInfo, _ *statT) (allowExtended bool, err error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2024-05-17 20:18:20 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) fillExtendedAttributes(path string, ignoreListError bool) error {
|
|
|
|
xattrs, err := listxattr(path)
|
|
|
|
debug.Log("fillExtendedAttributes(%v) %v %v", path, xattrs, err)
|
|
|
|
if err != nil {
|
|
|
|
if ignoreListError && IsListxattrPermissionError(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.ExtendedAttributes = make([]ExtendedAttribute, 0, len(xattrs))
|
|
|
|
for _, attr := range xattrs {
|
|
|
|
attrVal, err := getxattr(path, attr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "can not obtain extended attribute %v for %v:\n", attr, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attr := ExtendedAttribute{
|
|
|
|
Name: attr,
|
|
|
|
Value: attrVal,
|
|
|
|
}
|
|
|
|
|
|
|
|
node.ExtendedAttributes = append(node.ExtendedAttributes, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|