xattr_bsd.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // +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. } else {
  31. return unsafe.Pointer(_zero)
  32. }
  33. }
  34. // FreeBSD and NetBSD implement their own syscalls to handle extended attributes
  35. func Getxattr(file string, attr string, dest []byte) (sz int, err error) {
  36. d := initxattrdest(dest, 0)
  37. destsize := len(dest)
  38. nsid, a, err := xattrnamespace(attr)
  39. if err != nil {
  40. return -1, err
  41. }
  42. return ExtattrGetFile(file, nsid, a, uintptr(d), destsize)
  43. }
  44. func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) {
  45. d := initxattrdest(dest, 0)
  46. destsize := len(dest)
  47. nsid, a, err := xattrnamespace(attr)
  48. if err != nil {
  49. return -1, err
  50. }
  51. return ExtattrGetFd(fd, nsid, a, uintptr(d), destsize)
  52. }
  53. func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
  54. d := initxattrdest(dest, 0)
  55. destsize := len(dest)
  56. nsid, a, err := xattrnamespace(attr)
  57. if err != nil {
  58. return -1, err
  59. }
  60. return ExtattrGetLink(link, nsid, a, uintptr(d), destsize)
  61. }
  62. // flags are unused on FreeBSD
  63. func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) {
  64. var d unsafe.Pointer
  65. if len(data) > 0 {
  66. d = unsafe.Pointer(&data[0])
  67. }
  68. datasiz := len(data)
  69. nsid, a, err := xattrnamespace(attr)
  70. if err != nil {
  71. return
  72. }
  73. _, err = ExtattrSetFd(fd, nsid, a, uintptr(d), datasiz)
  74. return
  75. }
  76. func Setxattr(file string, attr string, data []byte, flags int) (err error) {
  77. var d unsafe.Pointer
  78. if len(data) > 0 {
  79. d = unsafe.Pointer(&data[0])
  80. }
  81. datasiz := len(data)
  82. nsid, a, err := xattrnamespace(attr)
  83. if err != nil {
  84. return
  85. }
  86. _, err = ExtattrSetFile(file, nsid, a, uintptr(d), datasiz)
  87. return
  88. }
  89. func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
  90. var d unsafe.Pointer
  91. if len(data) > 0 {
  92. d = unsafe.Pointer(&data[0])
  93. }
  94. datasiz := len(data)
  95. nsid, a, err := xattrnamespace(attr)
  96. if err != nil {
  97. return
  98. }
  99. _, err = ExtattrSetLink(link, nsid, a, uintptr(d), datasiz)
  100. return
  101. }
  102. func Removexattr(file string, attr string) (err error) {
  103. nsid, a, err := xattrnamespace(attr)
  104. if err != nil {
  105. return
  106. }
  107. err = ExtattrDeleteFile(file, nsid, a)
  108. return
  109. }
  110. func Fremovexattr(fd int, attr string) (err error) {
  111. nsid, a, err := xattrnamespace(attr)
  112. if err != nil {
  113. return
  114. }
  115. err = ExtattrDeleteFd(fd, nsid, a)
  116. return
  117. }
  118. func Lremovexattr(link string, attr string) (err error) {
  119. nsid, a, err := xattrnamespace(attr)
  120. if err != nil {
  121. return
  122. }
  123. err = ExtattrDeleteLink(link, nsid, a)
  124. return
  125. }
  126. func Listxattr(file string, dest []byte) (sz int, err error) {
  127. d := initxattrdest(dest, 0)
  128. destsiz := len(dest)
  129. // FreeBSD won't allow you to list xattrs from multiple namespaces
  130. s := 0
  131. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  132. stmp, e := ExtattrListFile(file, nsid, uintptr(d), destsiz)
  133. /* Errors accessing system attrs are ignored so that
  134. * we can implement the Linux-like behavior of omitting errors that
  135. * we don't have read permissions on
  136. *
  137. * Linux will still error if we ask for user attributes on a file that
  138. * we don't have read permissions on, so don't ignore those errors
  139. */
  140. if e != nil && e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  141. continue
  142. } else if e != nil {
  143. return s, e
  144. }
  145. s += stmp
  146. destsiz -= s
  147. if destsiz < 0 {
  148. destsiz = 0
  149. }
  150. d = initxattrdest(dest, s)
  151. }
  152. return s, nil
  153. }
  154. func Flistxattr(fd int, dest []byte) (sz int, err error) {
  155. d := initxattrdest(dest, 0)
  156. destsiz := len(dest)
  157. s := 0
  158. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  159. stmp, e := ExtattrListFd(fd, nsid, uintptr(d), destsiz)
  160. if e != nil && e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  161. continue
  162. } else if e != nil {
  163. return s, e
  164. }
  165. s += stmp
  166. destsiz -= s
  167. if destsiz < 0 {
  168. destsiz = 0
  169. }
  170. d = initxattrdest(dest, s)
  171. }
  172. return s, nil
  173. }
  174. func Llistxattr(link string, dest []byte) (sz int, err error) {
  175. d := initxattrdest(dest, 0)
  176. destsiz := len(dest)
  177. s := 0
  178. for _, nsid := range [...]int{EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM} {
  179. stmp, e := ExtattrListLink(link, nsid, uintptr(d), destsiz)
  180. if e != nil && e == EPERM && nsid != EXTATTR_NAMESPACE_USER {
  181. continue
  182. } else if e != nil {
  183. return s, e
  184. }
  185. s += stmp
  186. destsiz -= s
  187. if destsiz < 0 {
  188. destsiz = 0
  189. }
  190. d = initxattrdest(dest, s)
  191. }
  192. return s, nil
  193. }