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
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
package fs
|
2017-02-02 11:23:13 +00:00
|
|
|
|
|
|
|
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"
|
2024-08-26 21:03:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
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-07-21 13:03:17 +00:00
|
|
|
func isListxattrPermissionError(err error) bool {
|
2024-01-31 19:48:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-13 20:21:00 +00:00
|
|
|
// removexattr removes the attribute name from path.
|
|
|
|
func removexattr(path, name string) error {
|
|
|
|
return handleXattrErr(xattr.LRemove(path, name))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-08-26 20:35:22 +00:00
|
|
|
// nodeRestoreGenericAttributes is no-op.
|
2024-08-26 21:03:25 +00:00
|
|
|
func nodeRestoreGenericAttributes(node *restic.Node, _ string, warn func(msg string)) error {
|
|
|
|
return restic.HandleAllUnknownGenericAttributesFound(node.GenericAttributes, warn)
|
2024-02-23 00:31:20 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 20:35:22 +00:00
|
|
|
// nodeFillGenericAttributes is a no-op.
|
2024-08-24 21:43:45 +00:00
|
|
|
func nodeFillGenericAttributes(_ *restic.Node, _ string, _ *ExtendedFileInfo) (allowExtended bool, err error) {
|
2024-02-23 00:31:20 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2024-05-17 20:18:20 +00:00
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
func nodeRestoreExtendedAttributes(node *restic.Node, path string) error {
|
2024-06-13 20:21:00 +00:00
|
|
|
expectedAttrs := map[string]struct{}{}
|
2024-05-17 20:18:20 +00:00
|
|
|
for _, attr := range node.ExtendedAttributes {
|
|
|
|
err := setxattr(path, attr.Name, attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-13 20:21:00 +00:00
|
|
|
expectedAttrs[attr.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove unexpected xattrs
|
|
|
|
xattrs, err := listxattr(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-05-17 20:18:20 +00:00
|
|
|
}
|
2024-06-13 20:21:00 +00:00
|
|
|
for _, name := range xattrs {
|
|
|
|
if _, ok := expectedAttrs[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := removexattr(path, name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 20:18:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
func nodeFillExtendedAttributes(node *restic.Node, path string, ignoreListError bool) error {
|
2024-05-17 20:18:20 +00:00
|
|
|
xattrs, err := listxattr(path)
|
|
|
|
debug.Log("fillExtendedAttributes(%v) %v %v", path, xattrs, err)
|
|
|
|
if err != nil {
|
2024-07-21 13:03:17 +00:00
|
|
|
if ignoreListError && isListxattrPermissionError(err) {
|
2024-05-17 20:18:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
node.ExtendedAttributes = make([]restic.ExtendedAttribute, 0, len(xattrs))
|
2024-05-17 20:18:20 +00:00
|
|
|
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
|
|
|
|
}
|
2024-08-26 21:03:25 +00:00
|
|
|
attr := restic.ExtendedAttribute{
|
2024-05-17 20:18:20 +00:00
|
|
|
Name: attr,
|
|
|
|
Value: attrVal,
|
|
|
|
}
|
|
|
|
|
|
|
|
node.ExtendedAttributes = append(node.ExtendedAttributes, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|