xattrs_linux.go 890 B

123456789101112131415161718192021222324252627282930313233343536
  1. package system
  2. import "golang.org/x/sys/unix"
  3. // Returns a []byte slice if the xattr is set and nil otherwise
  4. // Requires path and its attribute as arguments
  5. func Lgetxattr(path string, attr string) ([]byte, error) {
  6. var sz int
  7. // Start with a 128 length byte array
  8. dest := make([]byte, 128)
  9. sz, errno := unix.Lgetxattr(path, attr, dest)
  10. switch {
  11. case errno == unix.ENODATA:
  12. return nil, errno
  13. case errno == unix.ENOTSUP:
  14. return nil, errno
  15. case errno == unix.ERANGE:
  16. // 128 byte array might just not be good enough,
  17. // A dummy buffer is used to get the real size
  18. // of the xattrs on disk
  19. sz, errno = unix.Lgetxattr(path, attr, []byte{})
  20. if errno != nil {
  21. return nil, errno
  22. }
  23. dest = make([]byte, sz)
  24. sz, errno = unix.Lgetxattr(path, attr, dest)
  25. if errno != nil {
  26. return nil, errno
  27. }
  28. case errno != nil:
  29. return nil, errno
  30. }
  31. return dest[:sz], nil
  32. }