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.

201 lines
4.6 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 fh != nil {
  98. fh.entryLock.Lock()
  99. defer fh.entryLock.Unlock()
  100. }
  101. if entry.Extended == nil {
  102. entry.Extended = make(map[string][]byte)
  103. }
  104. oldData, _ := entry.Extended[XATTR_PREFIX+attr]
  105. switch input.Flags {
  106. case sys.XATTR_CREATE:
  107. if len(oldData) > 0 {
  108. break
  109. }
  110. fallthrough
  111. case sys.XATTR_REPLACE:
  112. fallthrough
  113. default:
  114. entry.Extended[XATTR_PREFIX+attr] = data
  115. }
  116. return wfs.saveEntry(path, entry)
  117. }
  118. // ListXAttr lists extended attributes as '\0' delimited byte
  119. // slice, and return the number of bytes. If the buffer is too
  120. // small, return ERANGE, with the required buffer size.
  121. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  122. if wfs.option.DisableXAttr {
  123. return 0, fuse.Status(syscall.ENOTSUP)
  124. }
  125. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  126. if status != fuse.OK {
  127. return 0, status
  128. }
  129. if entry == nil {
  130. return 0, fuse.ENOENT
  131. }
  132. if entry.Extended == nil {
  133. return 0, fuse.OK
  134. }
  135. var data []byte
  136. for k := range entry.Extended {
  137. if strings.HasPrefix(k, XATTR_PREFIX) {
  138. data = append(data, k[len(XATTR_PREFIX):]...)
  139. data = append(data, 0)
  140. }
  141. }
  142. if len(dest) < len(data) {
  143. return uint32(len(data)), fuse.ERANGE
  144. }
  145. copy(dest, data)
  146. return uint32(len(data)), fuse.OK
  147. }
  148. // RemoveXAttr removes an extended attribute.
  149. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  150. if wfs.option.DisableXAttr {
  151. return fuse.Status(syscall.ENOTSUP)
  152. }
  153. if len(attr) == 0 {
  154. return fuse.EINVAL
  155. }
  156. path, fh, entry, status := wfs.maybeReadEntry(header.NodeId)
  157. if status != fuse.OK {
  158. return status
  159. }
  160. if fh != nil {
  161. fh.entryLock.Lock()
  162. defer fh.entryLock.Unlock()
  163. }
  164. if entry.Extended == nil {
  165. return fuse.ENOATTR
  166. }
  167. _, found := entry.Extended[XATTR_PREFIX+attr]
  168. if !found {
  169. return fuse.ENOATTR
  170. }
  171. delete(entry.Extended, XATTR_PREFIX+attr)
  172. return wfs.saveEntry(path, entry)
  173. }