You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. // By default (i.e., flags is zero), the extended attribute will be
  56. // created if it does not exist, or the value will be replaced if
  57. // the attribute already exists. To modify these semantics, one of
  58. // the following values can be specified in flags:
  59. //
  60. // XATTR_CREATE
  61. // Perform a pure create, which fails if the named attribute
  62. // exists already.
  63. //
  64. // XATTR_REPLACE
  65. // Perform a pure replace operation, which fails if the named
  66. // attribute does not already exist.
  67. func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
  68. if wfs.option.DisableXAttr {
  69. return fuse.Status(syscall.ENOTSUP)
  70. }
  71. if wfs.IsOverQuota {
  72. return fuse.Status(syscall.ENOSPC)
  73. }
  74. //validate attr name
  75. if len(attr) > MAX_XATTR_NAME_SIZE {
  76. if runtime.GOOS == "darwin" {
  77. return fuse.EPERM
  78. } else {
  79. return fuse.ERANGE
  80. }
  81. }
  82. if len(attr) == 0 {
  83. return fuse.EINVAL
  84. }
  85. //validate attr value
  86. if len(data) > MAX_XATTR_VALUE_SIZE {
  87. if runtime.GOOS == "darwin" {
  88. return fuse.Status(syscall.E2BIG)
  89. } else {
  90. return fuse.ERANGE
  91. }
  92. }
  93. path, fh, entry, status := wfs.maybeReadEntry(input.NodeId)
  94. if status != fuse.OK {
  95. return status
  96. }
  97. if entry == nil {
  98. return fuse.ENOENT
  99. }
  100. if fh != nil {
  101. fh.entryLock.Lock()
  102. defer fh.entryLock.Unlock()
  103. }
  104. if entry.Extended == nil {
  105. entry.Extended = make(map[string][]byte)
  106. }
  107. oldData, _ := entry.Extended[XATTR_PREFIX+attr]
  108. switch input.Flags {
  109. case sys.XATTR_CREATE:
  110. if len(oldData) > 0 {
  111. break
  112. }
  113. fallthrough
  114. case sys.XATTR_REPLACE:
  115. fallthrough
  116. default:
  117. entry.Extended[XATTR_PREFIX+attr] = data
  118. }
  119. return wfs.saveEntry(path, entry)
  120. }
  121. // ListXAttr lists extended attributes as '\0' delimited byte
  122. // slice, and return the number of bytes. If the buffer is too
  123. // small, return ERANGE, with the required buffer size.
  124. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  125. if wfs.option.DisableXAttr {
  126. return 0, fuse.Status(syscall.ENOTSUP)
  127. }
  128. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  129. if status != fuse.OK {
  130. return 0, status
  131. }
  132. if entry == nil {
  133. return 0, fuse.ENOENT
  134. }
  135. if entry.Extended == nil {
  136. return 0, fuse.OK
  137. }
  138. var data []byte
  139. for k := range entry.Extended {
  140. if strings.HasPrefix(k, XATTR_PREFIX) {
  141. data = append(data, k[len(XATTR_PREFIX):]...)
  142. data = append(data, 0)
  143. }
  144. }
  145. if len(dest) < len(data) {
  146. return uint32(len(data)), fuse.ERANGE
  147. }
  148. copy(dest, data)
  149. return uint32(len(data)), fuse.OK
  150. }
  151. // RemoveXAttr removes an extended attribute.
  152. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  153. if wfs.option.DisableXAttr {
  154. return fuse.Status(syscall.ENOTSUP)
  155. }
  156. if len(attr) == 0 {
  157. return fuse.EINVAL
  158. }
  159. path, fh, entry, status := wfs.maybeReadEntry(header.NodeId)
  160. if status != fuse.OK {
  161. return status
  162. }
  163. if entry == nil {
  164. return fuse.OK
  165. }
  166. if fh != nil {
  167. fh.entryLock.Lock()
  168. defer fh.entryLock.Unlock()
  169. }
  170. if entry.Extended == nil {
  171. return fuse.ENOATTR
  172. }
  173. _, found := entry.Extended[XATTR_PREFIX+attr]
  174. if !found {
  175. return fuse.ENOATTR
  176. }
  177. delete(entry.Extended, XATTR_PREFIX+attr)
  178. return wfs.saveEntry(path, entry)
  179. }