xattrs.go 700 B

123456789101112131415161718192021222324252627282930
  1. // +build selinux,linux
  2. package selinux
  3. import (
  4. "golang.org/x/sys/unix"
  5. )
  6. // Returns a []byte slice if the xattr is set and nil otherwise
  7. // Requires path and its attribute as arguments
  8. func lgetxattr(path string, attr string) ([]byte, error) {
  9. // Start with a 128 length byte array
  10. dest := make([]byte, 128)
  11. sz, errno := unix.Lgetxattr(path, attr, dest)
  12. for errno == unix.ERANGE {
  13. // Buffer too small, use zero-sized buffer to get the actual size
  14. sz, errno = unix.Lgetxattr(path, attr, []byte{})
  15. if errno != nil {
  16. return nil, errno
  17. }
  18. dest = make([]byte, sz)
  19. sz, errno = unix.Lgetxattr(path, attr, dest)
  20. }
  21. if errno != nil {
  22. return nil, errno
  23. }
  24. return dest[:sz], nil
  25. }