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.

168 lines
4.5 KiB

4 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "os"
  6. "syscall"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/fuse"
  12. "github.com/seaweedfs/fuse/fs"
  13. )
  14. var _ = fs.NodeLinker(&Dir{})
  15. var _ = fs.NodeSymlinker(&Dir{})
  16. var _ = fs.NodeReadlinker(&File{})
  17. const (
  18. HARD_LINK_MARKER = '\x01'
  19. )
  20. func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (fs.Node, error) {
  21. if err := checkName(req.NewName); err != nil {
  22. return nil, err
  23. }
  24. oldFile, ok := old.(*File)
  25. if !ok {
  26. glog.Errorf("old node is not a file: %+v", old)
  27. }
  28. glog.V(4).Infof("Link: %v/%v -> %v/%v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName)
  29. oldEntry, err := oldFile.maybeLoadEntry(ctx)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if oldEntry == nil {
  34. return nil, fuse.EIO
  35. }
  36. // update old file to hardlink mode
  37. if len(oldEntry.HardLinkId) == 0 {
  38. oldEntry.HardLinkId = append(util.RandomBytes(16), HARD_LINK_MARKER)
  39. oldEntry.HardLinkCounter = 1
  40. }
  41. oldEntry.HardLinkCounter++
  42. updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
  43. Directory: oldFile.dir.FullPath(),
  44. Entry: oldEntry,
  45. Signatures: []int32{dir.wfs.signature},
  46. }
  47. // CreateLink 1.2 : update new file to hardlink mode
  48. request := &filer_pb.CreateEntryRequest{
  49. Directory: dir.FullPath(),
  50. Entry: &filer_pb.Entry{
  51. Name: req.NewName,
  52. IsDirectory: false,
  53. Attributes: oldEntry.Attributes,
  54. Chunks: oldEntry.Chunks,
  55. Extended: oldEntry.Extended,
  56. HardLinkId: oldEntry.HardLinkId,
  57. HardLinkCounter: oldEntry.HardLinkCounter,
  58. },
  59. Signatures: []int32{dir.wfs.signature},
  60. }
  61. // apply changes to the filer, and also apply to local metaCache
  62. err = dir.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  63. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  64. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  65. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  66. glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
  67. return fuse.EIO
  68. }
  69. dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  70. if err := filer_pb.CreateEntry(client, request); err != nil {
  71. glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
  72. return fuse.EIO
  73. }
  74. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  75. return nil
  76. })
  77. if err != nil {
  78. return nil, fuse.EIO
  79. }
  80. // create new file node
  81. newNode := dir.newFile(req.NewName)
  82. newFile := newNode.(*File)
  83. return newFile, err
  84. }
  85. func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
  86. if err := checkName(req.NewName); err != nil {
  87. return nil, err
  88. }
  89. glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target)
  90. request := &filer_pb.CreateEntryRequest{
  91. Directory: dir.FullPath(),
  92. Entry: &filer_pb.Entry{
  93. Name: req.NewName,
  94. IsDirectory: false,
  95. Attributes: &filer_pb.FuseAttributes{
  96. Mtime: time.Now().Unix(),
  97. Crtime: time.Now().Unix(),
  98. FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
  99. Uid: req.Uid,
  100. Gid: req.Gid,
  101. SymlinkTarget: req.Target,
  102. },
  103. },
  104. Signatures: []int32{dir.wfs.signature},
  105. }
  106. err := dir.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  107. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  108. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  109. if err := filer_pb.CreateEntry(client, request); err != nil {
  110. glog.V(0).Infof("symlink %s/%s: %v", dir.FullPath(), req.NewName, err)
  111. return fuse.EIO
  112. }
  113. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  114. return nil
  115. })
  116. symlink := dir.newFile(req.NewName)
  117. return symlink, err
  118. }
  119. func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
  120. entry, err := file.maybeLoadEntry(ctx)
  121. if err != nil {
  122. return "", err
  123. }
  124. if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
  125. return "", fuse.Errno(syscall.EINVAL)
  126. }
  127. glog.V(4).Infof("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, entry.Attributes.SymlinkTarget)
  128. return entry.Attributes.SymlinkTarget, nil
  129. }