2017-04-17 18:08:24 -04:00
|
|
|
package system
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
import "golang.org/x/sys/unix"
|
2017-04-17 18:08:24 -04: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-08-07 05:52:40 -04:00
|
|
|
dest := make([]byte, 128)
|
|
|
|
sz, errno := unix.Lgetxattr(path, attr, dest)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
switch {
|
2017-08-07 05:52:40 -04:00
|
|
|
case errno == unix.ENODATA:
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, errno
|
2017-08-07 05:52:40 -04:00
|
|
|
case errno == unix.ENOTSUP:
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, errno
|
2017-08-07 05:52:40 -04:00
|
|
|
case errno == unix.ERANGE:
|
2017-04-17 18:08:24 -04:00
|
|
|
// 128 byte array might just not be good enough,
|
2017-08-07 05:52:40 -04:00
|
|
|
// A dummy buffer is used to get the real size
|
2017-04-17 18:08:24 -04:00
|
|
|
// of the xattrs on disk
|
2017-08-07 05:52:40 -04:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, []byte{})
|
|
|
|
if errno != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
dest = make([]byte, sz)
|
2017-08-07 05:52:40 -04:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, dest)
|
|
|
|
if errno != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, errno
|
|
|
|
}
|
2017-08-07 05:52:40 -04:00
|
|
|
case errno != nil:
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
return dest[:sz], nil
|
|
|
|
}
|