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.

173 lines
4.2 KiB

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. //validate attr name
  20. if len(attr) > MAX_XATTR_NAME_SIZE {
  21. if runtime.GOOS == "darwin" {
  22. return 0, fuse.EPERM
  23. } else {
  24. return 0, fuse.ERANGE
  25. }
  26. }
  27. if len(attr) == 0 {
  28. return 0, fuse.EINVAL
  29. }
  30. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  31. if status != fuse.OK {
  32. return 0, status
  33. }
  34. if entry == nil {
  35. return 0, fuse.ENOENT
  36. }
  37. if entry.Extended == nil {
  38. return 0, fuse.ENOATTR
  39. }
  40. data, found := entry.Extended[XATTR_PREFIX+attr]
  41. if !found {
  42. return 0, fuse.ENOATTR
  43. }
  44. if len(dest) < len(data) {
  45. return uint32(len(data)), fuse.ERANGE
  46. }
  47. copy(dest, data)
  48. return uint32(len(data)), fuse.OK
  49. }
  50. // SetXAttr writes an extended attribute.
  51. // https://man7.org/linux/man-pages/man2/setxattr.2.html
  52. // By default (i.e., flags is zero), the extended attribute will be
  53. // created if it does not exist, or the value will be replaced if
  54. // the attribute already exists. To modify these semantics, one of
  55. // the following values can be specified in flags:
  56. //
  57. // XATTR_CREATE
  58. // Perform a pure create, which fails if the named attribute
  59. // exists already.
  60. //
  61. // XATTR_REPLACE
  62. // Perform a pure replace operation, which fails if the named
  63. // attribute does not already exist.
  64. func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
  65. if wfs.IsOverQuota {
  66. return fuse.Status(syscall.ENOSPC)
  67. }
  68. //validate attr name
  69. if len(attr) > MAX_XATTR_NAME_SIZE {
  70. if runtime.GOOS == "darwin" {
  71. return fuse.EPERM
  72. } else {
  73. return fuse.ERANGE
  74. }
  75. }
  76. if len(attr) == 0 {
  77. return fuse.EINVAL
  78. }
  79. //validate attr value
  80. if len(data) > MAX_XATTR_VALUE_SIZE {
  81. if runtime.GOOS == "darwin" {
  82. return fuse.Status(syscall.E2BIG)
  83. } else {
  84. return fuse.ERANGE
  85. }
  86. }
  87. path, _, entry, status := wfs.maybeReadEntry(input.NodeId)
  88. if status != fuse.OK {
  89. return status
  90. }
  91. if entry.Extended == nil {
  92. entry.Extended = make(map[string][]byte)
  93. }
  94. oldData, _ := entry.Extended[XATTR_PREFIX+attr]
  95. switch input.Flags {
  96. case sys.XATTR_CREATE:
  97. if len(oldData) > 0 {
  98. break
  99. }
  100. fallthrough
  101. case sys.XATTR_REPLACE:
  102. fallthrough
  103. default:
  104. entry.Extended[XATTR_PREFIX+attr] = data
  105. }
  106. return wfs.saveEntry(path, entry)
  107. }
  108. // ListXAttr lists extended attributes as '\0' delimited byte
  109. // slice, and return the number of bytes. If the buffer is too
  110. // small, return ERANGE, with the required buffer size.
  111. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  112. _, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  113. if status != fuse.OK {
  114. return 0, status
  115. }
  116. if entry == nil {
  117. return 0, fuse.ENOENT
  118. }
  119. if entry.Extended == nil {
  120. return 0, fuse.OK
  121. }
  122. var data []byte
  123. for k := range entry.Extended {
  124. if strings.HasPrefix(k, XATTR_PREFIX) {
  125. data = append(data, k[len(XATTR_PREFIX):]...)
  126. data = append(data, 0)
  127. }
  128. }
  129. if len(dest) < len(data) {
  130. return uint32(len(data)), fuse.ERANGE
  131. }
  132. copy(dest, data)
  133. return uint32(len(data)), fuse.OK
  134. }
  135. // RemoveXAttr removes an extended attribute.
  136. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  137. if len(attr) == 0 {
  138. return fuse.EINVAL
  139. }
  140. path, _, entry, status := wfs.maybeReadEntry(header.NodeId)
  141. if status != fuse.OK {
  142. return status
  143. }
  144. if entry.Extended == nil {
  145. return fuse.ENOATTR
  146. }
  147. _, found := entry.Extended[XATTR_PREFIX+attr]
  148. if !found {
  149. return fuse.ENOATTR
  150. }
  151. delete(entry.Extended, XATTR_PREFIX+attr)
  152. return wfs.saveEntry(path, entry)
  153. }