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.

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