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.

125 lines
3.4 KiB

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().Second())
  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.Uid = entry.Attributes.Uid
  55. out.Gid = entry.Attributes.Gid
  56. out.Mode = toSystemMode(os.FileMode(entry.Attributes.FileMode))
  57. out.Mtime = uint64(entry.Attributes.Mtime)
  58. out.Ctime = uint64(entry.Attributes.Mtime)
  59. out.Atime = uint64(entry.Attributes.Mtime)
  60. if entry.HardLinkCounter > 0 {
  61. out.Nlink = uint32(entry.HardLinkCounter)
  62. }
  63. out.Size = filer.FileSize(entry)
  64. out.Blocks = out.Size/blockSize + 1
  65. setBlksize(out, blockSize)
  66. out.Nlink = 1
  67. }
  68. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  69. out.Ino = inode
  70. out.Size = entry.FileSize
  71. out.Blocks = out.Size/blockSize + 1
  72. setBlksize(out, blockSize)
  73. out.Atime = uint64(entry.Attr.Mtime.Unix())
  74. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  75. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  76. out.Crtime_ = uint64(entry.Attr.Crtime.Unix())
  77. out.Mode = toSystemMode(entry.Attr.Mode)
  78. if entry.HardLinkCounter > 0 {
  79. out.Nlink = uint32(entry.HardLinkCounter)
  80. }
  81. out.Nlink = 1
  82. out.Uid = entry.Attr.Uid
  83. out.Gid = entry.Attr.Gid
  84. }
  85. func (wfs *WFS) outputEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  86. // out.Generation = 1
  87. out.EntryValid = 1
  88. out.AttrValid = 1
  89. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  90. }
  91. func toSystemMode(mode os.FileMode) uint32 {
  92. return toSystemType(mode) | uint32(mode)
  93. }
  94. func toSystemType(mode os.FileMode) uint32 {
  95. switch mode & os.ModeType {
  96. case os.ModeDir:
  97. return syscall.S_IFDIR
  98. case os.ModeSymlink:
  99. return syscall.S_IFLNK
  100. case os.ModeNamedPipe:
  101. return syscall.S_IFIFO
  102. case os.ModeSocket:
  103. return syscall.S_IFSOCK
  104. case os.ModeDevice:
  105. return syscall.S_IFBLK
  106. case os.ModeCharDevice:
  107. return syscall.S_IFCHR
  108. default:
  109. return syscall.S_IFREG
  110. }
  111. }