basicfs_xattr_linuxish.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2022 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build linux || darwin
  7. // +build linux darwin
  8. package fs
  9. import (
  10. "errors"
  11. "fmt"
  12. "slices"
  13. "strings"
  14. "golang.org/x/sys/unix"
  15. )
  16. func listXattr(path string) ([]string, error) {
  17. buf := make([]byte, 1024)
  18. size, err := unix.Llistxattr(path, buf)
  19. if errors.Is(err, unix.ERANGE) {
  20. // Buffer is too small. Try again with a zero sized buffer to get
  21. // the size, then allocate a buffer of the correct size.
  22. size, err = unix.Llistxattr(path, nil)
  23. if err != nil {
  24. return nil, fmt.Errorf("Listxattr %s: %w", path, err)
  25. }
  26. buf = make([]byte, size)
  27. size, err = unix.Llistxattr(path, buf)
  28. }
  29. if err != nil {
  30. return nil, fmt.Errorf("Listxattr %s: %w", path, err)
  31. }
  32. buf = buf[:size]
  33. attrs := compact(strings.Split(string(buf), "\x00"))
  34. slices.Sort(attrs)
  35. return attrs, nil
  36. }