basicfs_xattr_bsdish.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 freebsd || netbsd
  7. // +build freebsd netbsd
  8. package fs
  9. import (
  10. "errors"
  11. "fmt"
  12. "slices"
  13. "unsafe"
  14. "golang.org/x/sys/unix"
  15. )
  16. var (
  17. namespaces = [...]int{unix.EXTATTR_NAMESPACE_USER, unix.EXTATTR_NAMESPACE_SYSTEM}
  18. namespacePrefixes = [...]string{unix.EXTATTR_NAMESPACE_USER: "user.", unix.EXTATTR_NAMESPACE_SYSTEM: "system."}
  19. )
  20. func listXattr(path string) ([]string, error) {
  21. var attrs []string
  22. // List the two namespaces explicitly and prefix any results with the
  23. // namespace name.
  24. for _, nsid := range namespaces {
  25. buf := make([]byte, 1024)
  26. size, err := unixLlistxattr(path, buf, nsid)
  27. if errors.Is(err, unix.ERANGE) || size == len(buf) {
  28. // Buffer is too small. Try again with a zero sized buffer to
  29. // get the size, then allocate a buffer of the correct size. We
  30. // include the size == len(buf) because apparently macOS doesn't
  31. // return ERANGE as it should -- no harm done, just an extra
  32. // read if we happened to need precisely 1024 bytes on the first
  33. // pass.
  34. size, err = unixLlistxattr(path, nil, nsid)
  35. if err != nil {
  36. return nil, fmt.Errorf("Listxattr %s: %w", path, err)
  37. }
  38. buf = make([]byte, size)
  39. size, err = unixLlistxattr(path, buf, nsid)
  40. }
  41. if err != nil {
  42. return nil, fmt.Errorf("Listxattr %s: %w", path, err)
  43. }
  44. buf = buf[:size]
  45. // "Each list entry consists of a single byte containing the length
  46. // of the attribute name, followed by the attribute name. The
  47. // attribute name is not terminated by ASCII 0 (nul)."
  48. i := 0
  49. for i < len(buf) {
  50. l := int(buf[i])
  51. i++
  52. if i+l > len(buf) {
  53. // uh-oh
  54. return nil, fmt.Errorf("get xattr %s: attribute length %d at offset %d exceeds buffer length %d", path, l, i, len(buf))
  55. }
  56. if l > 0 {
  57. attrs = append(attrs, namespacePrefixes[nsid]+string(buf[i:i+l]))
  58. i += l
  59. }
  60. }
  61. }
  62. slices.Sort(attrs)
  63. return attrs, nil
  64. }
  65. // This is unix.Llistxattr except taking a namespace parameter to dodge
  66. // https://github.com/golang/go/issues/54357 ("Listxattr on FreeBSD loses
  67. // namespace info")
  68. func unixLlistxattr(link string, dest []byte, nsid int) (sz int, err error) {
  69. d := initxattrdest(dest, 0)
  70. destsiz := len(dest)
  71. s, e := unix.ExtattrListLink(link, nsid, uintptr(d), destsiz)
  72. if e != nil && e == unix.EPERM && nsid != unix.EXTATTR_NAMESPACE_USER {
  73. return 0, nil
  74. } else if e != nil {
  75. return s, e
  76. }
  77. return s, nil
  78. }
  79. var _zero uintptr
  80. func initxattrdest(dest []byte, idx int) (d unsafe.Pointer) {
  81. if len(dest) > idx {
  82. return unsafe.Pointer(&dest[idx])
  83. } else {
  84. return unsafe.Pointer(_zero)
  85. }
  86. }