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.

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