2016-09-17 13:50:35 +00:00
|
|
|
package system
|
|
|
|
|
2017-07-20 04:07:01 +00:00
|
|
|
import "golang.org/x/sys/unix"
|
2016-09-17 13:50:35 +00:00
|
|
|
|
|
|
|
// Returns a []byte slice if the xattr is set and nil otherwise
|
|
|
|
// Requires path and its attribute as arguments
|
|
|
|
func Lgetxattr(path string, attr string) ([]byte, error) {
|
|
|
|
var sz int
|
|
|
|
// Start with a 128 length byte array
|
2017-07-20 04:07:01 +00:00
|
|
|
dest := make([]byte, 128)
|
|
|
|
sz, errno := unix.Lgetxattr(path, attr, dest)
|
2016-09-17 13:50:35 +00:00
|
|
|
|
|
|
|
switch {
|
2017-07-20 04:07:01 +00:00
|
|
|
case errno == unix.ENODATA:
|
2016-09-17 13:50:35 +00:00
|
|
|
return nil, errno
|
2017-07-20 04:07:01 +00:00
|
|
|
case errno == unix.ENOTSUP:
|
2016-09-17 13:50:35 +00:00
|
|
|
return nil, errno
|
2017-07-20 04:07:01 +00:00
|
|
|
case errno == unix.ERANGE:
|
2016-09-17 13:50:35 +00:00
|
|
|
// 128 byte array might just not be good enough,
|
2017-07-20 04:07:01 +00:00
|
|
|
// A dummy buffer is used to get the real size
|
2016-09-17 13:50:35 +00:00
|
|
|
// of the xattrs on disk
|
2017-07-20 04:07:01 +00:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, []byte{})
|
|
|
|
if errno != nil {
|
2016-09-17 13:50:35 +00:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
dest = make([]byte, sz)
|
2017-07-20 04:07:01 +00:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, dest)
|
|
|
|
if errno != nil {
|
2016-09-17 13:50:35 +00:00
|
|
|
return nil, errno
|
|
|
|
}
|
2017-07-20 04:07:01 +00:00
|
|
|
case errno != nil:
|
2016-09-17 13:50:35 +00:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
return dest[:sz], nil
|
|
|
|
}
|