2014-07-15 07:55:49 +08:00
|
|
|
package system
|
|
|
|
|
2017-05-30 22:32:06 +08:00
|
|
|
import "golang.org/x/sys/unix"
|
2014-09-24 17:02:54 +08:00
|
|
|
|
|
|
|
// Returns a []byte slice if the xattr is set and nil otherwise
|
|
|
|
// Requires path and its attribute as arguments
|
2014-07-15 07:55:49 +08:00
|
|
|
func Lgetxattr(path string, attr string) ([]byte, error) {
|
2014-09-24 17:02:54 +08:00
|
|
|
var sz int
|
|
|
|
// Start with a 128 length byte array
|
2017-05-30 22:32:06 +08:00
|
|
|
dest := make([]byte, 128)
|
|
|
|
sz, errno := unix.Lgetxattr(path, attr, dest)
|
2014-09-24 17:02:54 +08:00
|
|
|
|
|
|
|
switch {
|
2017-05-10 05:38:27 +08:00
|
|
|
case errno == unix.ENODATA:
|
2014-09-24 17:02:54 +08:00
|
|
|
return nil, errno
|
2017-05-10 05:38:27 +08:00
|
|
|
case errno == unix.ENOTSUP:
|
2014-09-24 17:02:54 +08:00
|
|
|
return nil, errno
|
2017-05-10 05:38:27 +08:00
|
|
|
case errno == unix.ERANGE:
|
2014-09-24 17:02:54 +08:00
|
|
|
// 128 byte array might just not be good enough,
|
2017-05-30 22:32:06 +08:00
|
|
|
// A dummy buffer is used to get the real size
|
2014-09-24 17:02:54 +08:00
|
|
|
// of the xattrs on disk
|
2017-05-30 22:32:06 +08:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, []byte{})
|
|
|
|
if errno != nil {
|
2014-09-24 17:02:54 +08:00
|
|
|
return nil, errno
|
|
|
|
}
|
2014-07-15 07:55:49 +08:00
|
|
|
dest = make([]byte, sz)
|
2017-05-30 22:32:06 +08:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, dest)
|
|
|
|
if errno != nil {
|
2014-09-24 17:02:54 +08:00
|
|
|
return nil, errno
|
|
|
|
}
|
2017-05-30 22:32:06 +08:00
|
|
|
case errno != nil:
|
2014-07-15 07:55:49 +08:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
return dest[:sz], nil
|
|
|
|
}
|