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.

200 lines
4.5 KiB

3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 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. //
  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, _, 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 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, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  157. if status != fuse.OK {
  158. return status
  159. }
  160. if entry == nil {
  161. return fuse.OK
  162. }
  163. if entry.Extended == nil {
  164. return fuse.ENOATTR
  165. }
  166. _, found := entry.Extended[XATTR_PREFIX+attr]
  167. if !found {
  168. return fuse.ENOATTR
  169. }
  170. delete(entry.Extended, XATTR_PREFIX+attr)
  171. return wfs.saveEntry(path, entry)
  172. }