xattr_bsd.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build freebsd || netbsd
  5. package unix
  6. import (
  7. "strings"
  8. "unsafe"
  9. )
  10. // Derive extattr namespace and attribute name
  11. func xattrnamespace(fullattr string) (ns int, attr string, err error) {
  12. s := strings.IndexByte(fullattr, '.')
  13. if s == -1 {
  14. return -1, "", ENOATTR
  15. }
  16. namespace := fullattr[0:s]
  17. attr = fullattr[s+1:]
  18. switch namespace {
  19. case "user":
  20. return EXTATTR_NAMESPACE_USER, attr, nil
  21. case "system":
  22. return EXTATTR_NAMESPACE_SYSTEM, attr, nil
  23. default:
  24. return -1, "", ENOATTR
  25. }
  26. }
  27. func initxattrdest(dest []byte, idx int) (d unsafe.Pointer) {
  28. if len(dest) > idx {
  29. return unsafe.Pointer(&dest[idx])
  30. }
  31. if dest != nil {
  32. // extattr_get_file and extattr_list_file treat NULL differently from
  33. // a non-NULL pointer of length zero. Preserve the property of nilness,
  34. // even if we can't use dest directly.
  35. return unsafe.Pointer(&_zero)
  36. }
  37. return nil
  38. }
  39. // FreeBSD and NetBSD implement their own syscalls to handle extended attributes
  40. func Getxattr(file string, attr string, dest []byte) (sz int, err error) {
  41. d := initxattrdest(dest, 0)
  42. destsize := len(dest)
  43. nsid, a, err := xattrnamespace(attr)
  44. if err != nil {
  45. return -1, err
  46. }
  47. return ExtattrGetFile(file, nsid, a, uintptr(d), destsize)
  48. }
  49. func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) {
  50. d := initxattrdest(dest, 0)
  51. destsize := len(dest)
  52. nsid, a, err := xattrnamespace(attr)
  53. if err != nil {
  54. return -1, err
  55. }
  56. return ExtattrGetFd(fd, nsid, a, uintptr(d), destsize)
  57. }
  58. func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
  59. d := initxattrdest(dest, 0)
  60. destsize := len(dest)
  61. nsid, a, err := xattrnamespace(attr)
  62. if err != nil {
  63. return -1, err
  64. }
  65. return ExtattrGetLink(link, nsid, a, uintptr(d), destsize)
  66. }
  67. // flags are unused on FreeBSD
  68. func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) {
  69. var d unsafe.Pointer
  70. if len(data) > 0 {
  71. d = unsafe.Pointer(&data[0])
  72. }
  73. datasiz := len(data)
  74. nsid, a, err := xattrnamespace(attr)
  75. if err != nil {
  76. return
  77. }
  78. _, err = ExtattrSetFd(fd, nsid, a, uintptr(d), datasiz)
  79. return
  80. }
  81. func Setxattr(file string, attr string, data []byte, flags int) (err error) {
  82. var d unsafe.Pointer
  83. if len(data) > 0 {
  84. d = unsafe.Pointer(&data[0])
  85. }
  86. datasiz := len(data)
  87. nsid, a, err := xattrnamespace(attr)
  88. if err != nil {
  89. return
  90. }
  91. _, err = ExtattrSetFile(file, nsid, a, uintptr(d), datasiz)
  92. return
  93. }
  94. func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
  95. var d unsafe.Pointer
  96. if len(data) > 0 {
  97. d = unsafe.Pointer(&data[0])
  98. }
  99. datasiz := len(data)
  100. nsid, a, err := xattrnamespace(attr)
  101. if err != nil {
  102. return
  103. }
  104. _, err = ExtattrSetLink(link, nsid, a, uintptr(d), datasiz)
  105. return
  106. }
  107. func Removexattr(file string, attr string) (err error) {
  108. nsid, a, err := xattrnamespace(attr)
  109. if err != nil {
  110. return
  111. }
  112. err = ExtattrDeleteFile(file, nsid, a)
  113. return
  114. }
  115. func Fremovexattr(fd int, attr string) (err error) {
  116. nsid, a, err := xattrnamespace(attr)
  117. if err != nil {
  118. return
  119. }
  120. err = ExtattrDeleteFd(fd, nsid, a)
  121. return
  122. }
  123. func Lremovexattr(link string, attr string) (err error) {
  124. nsid, a, err := xattrnamespace(attr)
  125. if err != nil {
  126. return
  127. }
  128. err = ExtattrDeleteLink(link, nsid, a)
  129. return
  130. }
  131. func Listxattr(file string, dest []byte) (sz int, err error) {
  132. destsiz := len(dest)
  133. // FreeBSD won't allow you to list xattrs from multiple namespaces
  134. s, pos := 0, 0
  135. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  136. stmp, e := ListxattrNS(file, nsid, dest[pos:])
  137. /* Errors accessing system attrs are ignored so that
  138. * we can implement the Linux-like behavior of omitting errors that
  139. * we don't have read permissions on
  140. *
  141. * Linux will still error if we ask for user attributes on a file that
  142. * we don't have read permissions on, so don't ignore those errors
  143. */
  144. if e != nil {
  145. if e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  146. continue
  147. }
  148. return s, e
  149. }
  150. s += stmp
  151. pos = s
  152. if pos > destsiz {
  153. pos = destsiz
  154. }
  155. }
  156. return s, nil
  157. }
  158. func ListxattrNS(file string, nsid int, dest []byte) (sz int, err error) {
  159. d := initxattrdest(dest, 0)
  160. destsiz := len(dest)
  161. s, e := ExtattrListFile(file, nsid, uintptr(d), destsiz)
  162. if e != nil {
  163. return 0, err
  164. }
  165. return s, nil
  166. }
  167. func Flistxattr(fd int, dest []byte) (sz int, err error) {
  168. destsiz := len(dest)
  169. s, pos := 0, 0
  170. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  171. stmp, e := FlistxattrNS(fd, nsid, dest[pos:])
  172. if e != nil {
  173. if e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  174. continue
  175. }
  176. return s, e
  177. }
  178. s += stmp
  179. pos = s
  180. if pos > destsiz {
  181. pos = destsiz
  182. }
  183. }
  184. return s, nil
  185. }
  186. func FlistxattrNS(fd int, nsid int, dest []byte) (sz int, err error) {
  187. d := initxattrdest(dest, 0)
  188. destsiz := len(dest)
  189. s, e := ExtattrListFd(fd, nsid, uintptr(d), destsiz)
  190. if e != nil {
  191. return 0, err
  192. }
  193. return s, nil
  194. }
  195. func Llistxattr(link string, dest []byte) (sz int, err error) {
  196. destsiz := len(dest)
  197. s, pos := 0, 0
  198. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  199. stmp, e := LlistxattrNS(link, nsid, dest[pos:])
  200. if e != nil {
  201. if e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  202. continue
  203. }
  204. return s, e
  205. }
  206. s += stmp
  207. pos = s
  208. if pos > destsiz {
  209. pos = destsiz
  210. }
  211. }
  212. return s, nil
  213. }
  214. func LlistxattrNS(link string, nsid int, dest []byte) (sz int, err error) {
  215. d := initxattrdest(dest, 0)
  216. destsiz := len(dest)
  217. s, e := ExtattrListLink(link, nsid, uintptr(d), destsiz)
  218. if e != nil {
  219. return 0, err
  220. }
  221. return s, nil
  222. }