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.

128 lines
3.4 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. println("input node id", input.NodeId)
  12. if input.NodeId == 1 {
  13. wfs.setRootAttr(out)
  14. return fuse.OK
  15. }
  16. _, entry, status := wfs.maybeReadEntry(input.NodeId)
  17. if status != fuse.OK {
  18. return status
  19. }
  20. out.AttrValid = 1
  21. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  22. return fuse.OK
  23. }
  24. func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
  25. return fuse.ENOSYS
  26. }
  27. func (wfs *WFS) GetXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string, dest []byte) (size uint32, code fuse.Status) {
  28. return 0, fuse.ENOSYS
  29. }
  30. func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
  31. return fuse.ENOSYS
  32. }
  33. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
  34. return 0, fuse.ENOSYS
  35. }
  36. func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
  37. return fuse.ENOSYS
  38. }
  39. func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
  40. now := uint64(time.Now().Unix())
  41. out.AttrValid = 119
  42. out.Ino = 1
  43. setBlksize(&out.Attr, blockSize)
  44. out.Uid = wfs.option.MountUid
  45. out.Gid = wfs.option.MountGid
  46. out.Mtime = now
  47. out.Ctime = now
  48. out.Atime = now
  49. out.Mode = toSystemType(os.ModeDir) | uint32(wfs.option.MountMode)
  50. out.Nlink = 1
  51. }
  52. func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry) {
  53. out.Ino = inode
  54. out.Size = filer.FileSize(entry)
  55. out.Blocks = (out.Size + blockSize - 1) / blockSize
  56. setBlksize(out, blockSize)
  57. out.Mtime = uint64(entry.Attributes.Mtime)
  58. out.Ctime = uint64(entry.Attributes.Mtime)
  59. out.Atime = uint64(entry.Attributes.Mtime)
  60. out.Mode = toSystemMode(os.FileMode(entry.Attributes.FileMode))
  61. if entry.HardLinkCounter > 0 {
  62. out.Nlink = uint32(entry.HardLinkCounter)
  63. } else {
  64. out.Nlink = 1
  65. }
  66. out.Uid = entry.Attributes.Uid
  67. out.Gid = entry.Attributes.Gid
  68. }
  69. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  70. out.Ino = inode
  71. out.Size = entry.FileSize
  72. out.Blocks = (out.Size + blockSize - 1) / blockSize
  73. setBlksize(out, blockSize)
  74. out.Atime = uint64(entry.Attr.Mtime.Unix())
  75. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  76. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  77. out.Crtime_ = uint64(entry.Attr.Crtime.Unix())
  78. out.Mode = toSystemMode(entry.Attr.Mode)
  79. if entry.HardLinkCounter > 0 {
  80. out.Nlink = uint32(entry.HardLinkCounter)
  81. } else {
  82. out.Nlink = 1
  83. }
  84. out.Uid = entry.Attr.Uid
  85. out.Gid = entry.Attr.Gid
  86. }
  87. func (wfs *WFS) outputEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  88. out.NodeId = inode
  89. out.Generation = 1
  90. out.EntryValid = 1
  91. out.AttrValid = 1
  92. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  93. }
  94. func toSystemMode(mode os.FileMode) uint32 {
  95. return toSystemType(mode) | uint32(mode)
  96. }
  97. func toSystemType(mode os.FileMode) uint32 {
  98. switch mode & os.ModeType {
  99. case os.ModeDir:
  100. return syscall.S_IFDIR
  101. case os.ModeSymlink:
  102. return syscall.S_IFLNK
  103. case os.ModeNamedPipe:
  104. return syscall.S_IFIFO
  105. case os.ModeSocket:
  106. return syscall.S_IFSOCK
  107. case os.ModeDevice:
  108. return syscall.S_IFBLK
  109. case os.ModeCharDevice:
  110. return syscall.S_IFCHR
  111. default:
  112. return syscall.S_IFREG
  113. }
  114. }