weedfs_xattr.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package mount
  2. import (
  3. "github.com/hanwen/go-fuse/v2/fuse"
  4. sys "golang.org/x/sys/unix"
  5. "runtime"
  6. "strings"
  7. "syscall"
  8. )
  9. const (
  10. // https://man7.org/linux/man-pages/man7/xattr.7.html#:~:text=The%20VFS%20imposes%20limitations%20that,in%20listxattr(2)).
  11. MAX_XATTR_NAME_SIZE = 255
  12. MAX_XATTR_VALUE_SIZE = 65536
  13. XATTR_PREFIX = "xattr-" // same as filer
  14. )
  15. // GetXAttr reads an extended attribute, and should return the
  16. // number of bytes. If the buffer is too small, return ERANGE,
  17. // with the required buffer size.
  18. func (wfs *WFS) GetXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string, dest []byte) (size uint32, code fuse.Status) {
  19. if wfs.option.DisableXAttr {
  20. return 0, fuse.Status(syscall.ENOTSUP)
  21. }
  22. //validate attr name
  23. if len(attr) > MAX_XATTR_NAME_SIZE {
  24. if runtime.GOOS == "darwin" {
  25. return 0, fuse.EPERM
  26. } else {
  27. return 0, fuse.ERANGE
  28. }
  29. }
  30. if len(attr) == 0 {
  31. return 0, fuse.EINVAL
  32. }
  33. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  34. if status != fuse.OK {
  35. return 0, status
  36. }
  37. if entry == nil {
  38. return 0, fuse.ENOENT
  39. }
  40. if entry.Extended == nil {
  41. return 0, fuse.ENOATTR
  42. }
  43. data, found := entry.Extended[XATTR_PREFIX+attr]
  44. if !found {
  45. return 0, fuse.ENOATTR
  46. }
  47. if len(dest) < len(data) {
  48. return uint32(len(data)), fuse.ERANGE
  49. }
  50. copy(dest, data)
  51. return uint32(len(data)), fuse.OK
  52. }
  53. // SetXAttr writes an extended attribute.
  54. // https://man7.org/linux/man-pages/man2/setxattr.2.html
  55. //
  56. // By default (i.e., flags is zero), the extended attribute will be
  57. // created if it does not exist, or the value will be replaced if
  58. // the attribute already exists. To modify these semantics, one of
  59. // the following values can be specified in flags:
  60. //
  61. // XATTR_CREATE
  62. // Perform a pure create, which fails if the named attribute
  63. // exists already.
  64. //
  65. // XATTR_REPLACE
  66. // Perform a pure replace operation, which fails if the named
  67. // attribute does not already exist.
  68. func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
  69. if wfs.option.DisableXAttr {
  70. return fuse.Status(syscall.ENOTSUP)
  71. }
  72. if wfs.IsOverQuota {
  73. return fuse.Status(syscall.ENOSPC)
  74. }
  75. //validate attr name
  76. if len(attr) > MAX_XATTR_NAME_SIZE {
  77. if runtime.GOOS == "darwin" {
  78. return fuse.EPERM
  79. } else {
  80. return fuse.ERANGE
  81. }
  82. }
  83. if len(attr) == 0 {
  84. return fuse.EINVAL
  85. }
  86. //validate attr value
  87. if len(data) > MAX_XATTR_VALUE_SIZE {
  88. if runtime.GOOS == "darwin" {
  89. return fuse.Status(syscall.E2BIG)
  90. } else {
  91. return fuse.ERANGE
  92. }
  93. }
  94. path, fh, entry, status := wfs.maybeReadEntry(input.NodeId)
  95. if status != fuse.OK {
  96. return status
  97. }
  98. if entry == nil {
  99. return fuse.ENOENT
  100. }
  101. if fh != nil {
  102. //fh.entryLock.Lock()
  103. //defer fh.entryLock.Unlock()
  104. }
  105. if entry.Extended == nil {
  106. entry.Extended = make(map[string][]byte)
  107. }
  108. oldData, _ := entry.Extended[XATTR_PREFIX+attr]
  109. switch input.Flags {
  110. case sys.XATTR_CREATE:
  111. if len(oldData) > 0 {
  112. break
  113. }
  114. fallthrough
  115. case sys.XATTR_REPLACE:
  116. fallthrough
  117. default:
  118. entry.Extended[XATTR_PREFIX+attr] = data
  119. }
  120. return wfs.saveEntry(path, entry)
  121. }
  122. // ListXAttr lists extended attributes as '\0' delimited byte
  123. // slice, and return the number of bytes. If the buffer is too
  124. // small, return ERANGE, with the required buffer size.
  125. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  126. if wfs.option.DisableXAttr {
  127. return 0, fuse.Status(syscall.ENOTSUP)
  128. }
  129. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  130. if status != fuse.OK {
  131. return 0, status
  132. }
  133. if entry == nil {
  134. return 0, fuse.ENOENT
  135. }
  136. if entry.Extended == nil {
  137. return 0, fuse.OK
  138. }
  139. var data []byte
  140. for k := range entry.Extended {
  141. if strings.HasPrefix(k, XATTR_PREFIX) {
  142. data = append(data, k[len(XATTR_PREFIX):]...)
  143. data = append(data, 0)
  144. }
  145. }
  146. if len(dest) < len(data) {
  147. return uint32(len(data)), fuse.ERANGE
  148. }
  149. copy(dest, data)
  150. return uint32(len(data)), fuse.OK
  151. }
  152. // RemoveXAttr removes an extended attribute.
  153. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  154. if wfs.option.DisableXAttr {
  155. return fuse.Status(syscall.ENOTSUP)
  156. }
  157. if len(attr) == 0 {
  158. return fuse.EINVAL
  159. }
  160. path, fh, entry, status := wfs.maybeReadEntry(header.NodeId)
  161. if status != fuse.OK {
  162. return status
  163. }
  164. if entry == nil {
  165. return fuse.OK
  166. }
  167. if fh != nil {
  168. //fh.entryLock.Lock()
  169. //defer fh.entryLock.Unlock()
  170. }
  171. if entry.Extended == nil {
  172. return fuse.ENOATTR
  173. }
  174. _, found := entry.Extended[XATTR_PREFIX+attr]
  175. if !found {
  176. return fuse.ENOATTR
  177. }
  178. delete(entry.Extended, XATTR_PREFIX+attr)
  179. return wfs.saveEntry(path, entry)
  180. }