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.

156 lines
4.0 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/chrislusf/seaweedfs/weed/filer"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "github.com/hanwen/go-fuse/v2/fuse"
  6. "os"
  7. "syscall"
  8. "time"
  9. )
  10. func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
  11. if input.NodeId == 1 {
  12. wfs.setRootAttr(out)
  13. return fuse.OK
  14. }
  15. _, entry, status := wfs.maybeReadEntry(input.NodeId)
  16. if status != fuse.OK {
  17. return status
  18. }
  19. out.AttrValid = 1
  20. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  21. return fuse.OK
  22. }
  23. func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
  24. // TODO this is only for directory. Filet setAttr involves open files and truncate to a size
  25. path, entry, status := wfs.maybeReadEntry(input.NodeId)
  26. if status != fuse.OK {
  27. return status
  28. }
  29. if mode, ok := input.GetMode(); ok {
  30. entry.Attributes.FileMode = uint32(mode)
  31. }
  32. if uid, ok := input.GetUID(); ok {
  33. entry.Attributes.Uid = uid
  34. }
  35. if gid, ok := input.GetGID(); ok {
  36. entry.Attributes.Gid = gid
  37. }
  38. if mtime, ok := input.GetMTime(); ok {
  39. entry.Attributes.Mtime = mtime.Unix()
  40. }
  41. entry.Attributes.Mtime = time.Now().Unix()
  42. out.AttrValid = 1
  43. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  44. return wfs.saveEntry(path, entry)
  45. }
  46. func (wfs *WFS) GetXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string, dest []byte) (size uint32, code fuse.Status) {
  47. return 0, fuse.ENOSYS
  48. }
  49. func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
  50. return fuse.ENOSYS
  51. }
  52. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  53. return 0, fuse.ENOSYS
  54. }
  55. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  56. return fuse.ENOSYS
  57. }
  58. func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
  59. now := uint64(time.Now().Unix())
  60. out.AttrValid = 119
  61. out.Ino = 1
  62. setBlksize(&out.Attr, blockSize)
  63. out.Uid = wfs.option.MountUid
  64. out.Gid = wfs.option.MountGid
  65. out.Mtime = now
  66. out.Ctime = now
  67. out.Atime = now
  68. out.Mode = toSystemType(os.ModeDir) | uint32(wfs.option.MountMode)
  69. out.Nlink = 1
  70. }
  71. func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry) {
  72. out.Ino = inode
  73. out.Size = filer.FileSize(entry)
  74. out.Blocks = (out.Size + blockSize - 1) / blockSize
  75. setBlksize(out, blockSize)
  76. out.Mtime = uint64(entry.Attributes.Mtime)
  77. out.Ctime = uint64(entry.Attributes.Mtime)
  78. out.Atime = uint64(entry.Attributes.Mtime)
  79. out.Mode = toSystemMode(os.FileMode(entry.Attributes.FileMode))
  80. if entry.HardLinkCounter > 0 {
  81. out.Nlink = uint32(entry.HardLinkCounter)
  82. } else {
  83. out.Nlink = 1
  84. }
  85. out.Uid = entry.Attributes.Uid
  86. out.Gid = entry.Attributes.Gid
  87. }
  88. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  89. out.Ino = inode
  90. out.Size = entry.FileSize
  91. out.Blocks = (out.Size + blockSize - 1) / blockSize
  92. setBlksize(out, blockSize)
  93. out.Atime = uint64(entry.Attr.Mtime.Unix())
  94. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  95. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  96. out.Crtime_ = uint64(entry.Attr.Crtime.Unix())
  97. out.Mode = toSystemMode(entry.Attr.Mode)
  98. if entry.HardLinkCounter > 0 {
  99. out.Nlink = uint32(entry.HardLinkCounter)
  100. } else {
  101. out.Nlink = 1
  102. }
  103. out.Uid = entry.Attr.Uid
  104. out.Gid = entry.Attr.Gid
  105. }
  106. func (wfs *WFS) outputEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  107. out.NodeId = inode
  108. out.Generation = 1
  109. out.EntryValid = 1
  110. out.AttrValid = 1
  111. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  112. }
  113. func toSystemMode(mode os.FileMode) uint32 {
  114. return toSystemType(mode) | uint32(mode)
  115. }
  116. func toSystemType(mode os.FileMode) uint32 {
  117. switch mode & os.ModeType {
  118. case os.ModeDir:
  119. return syscall.S_IFDIR
  120. case os.ModeSymlink:
  121. return syscall.S_IFLNK
  122. case os.ModeNamedPipe:
  123. return syscall.S_IFIFO
  124. case os.ModeSocket:
  125. return syscall.S_IFSOCK
  126. case os.ModeDevice:
  127. return syscall.S_IFBLK
  128. case os.ModeCharDevice:
  129. return syscall.S_IFCHR
  130. default:
  131. return syscall.S_IFREG
  132. }
  133. }