2022-01-10 13:34:18 +05:30
|
|
|
//go:build darwin || freebsd || linux || solaris
|
|
|
|
// +build darwin freebsd linux solaris
|
2017-02-02 12:23:13 +01:00
|
|
|
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
2017-02-16 14:25:56 +01:00
|
|
|
|
2017-07-23 14:21:03 +02:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
|
2017-02-16 14:25:56 +01:00
|
|
|
"github.com/pkg/xattr"
|
2017-02-02 12:23:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Getxattr retrieves extended attribute data associated with path.
|
|
|
|
func Getxattr(path, name string) ([]byte, error) {
|
2022-08-01 12:42:56 +02:00
|
|
|
b, err := xattr.Get(path, name)
|
|
|
|
return b, handleXattrErr(err)
|
2017-02-02 12:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listxattr retrieves a list of names of extended attributes associated with the
|
|
|
|
// given path in the file system.
|
|
|
|
func Listxattr(path string) ([]string, error) {
|
2022-08-01 12:42:56 +02:00
|
|
|
l, err := xattr.List(path)
|
|
|
|
return l, handleXattrErr(err)
|
2017-02-02 12:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setxattr associates name and data together as an attribute of path.
|
|
|
|
func Setxattr(path, name string, data []byte) error {
|
2022-08-01 12:42:56 +02:00
|
|
|
return handleXattrErr(xattr.Set(path, name, data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleXattrErr(err error) error {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case nil:
|
2017-02-02 12:23:13 +01:00
|
|
|
return nil
|
2022-08-01 12:42:56 +02:00
|
|
|
|
|
|
|
case *xattr.Error:
|
|
|
|
// On Solaris, xattr not being supported on a file is signaled
|
|
|
|
// by EINVAL (https://github.com/pkg/xattr/issues/67).
|
|
|
|
// On Linux, xattr calls on files in an SMB/CIFS mount can return
|
|
|
|
// ENOATTR instead of ENOTSUP.
|
|
|
|
switch e.Err {
|
|
|
|
case syscall.EINVAL, syscall.ENOTSUP, xattr.ENOATTR:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.WithStack(e)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.WithStack(e)
|
2017-02-02 12:23:13 +01:00
|
|
|
}
|
|
|
|
}
|