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.

158 lines
4.5 KiB

4 years ago
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. if err != nil {
  71. return nil, fuse.EIO
  72. }
  73. // create new file node
  74. newNode := dir.newFile(req.NewName, request.Entry)
  75. newFile := newNode.(*File)
  76. if _, err := newFile.maybeLoadEntry(ctx); err != nil {
  77. return nil, err
  78. }
  79. return newFile, err
  80. }
  81. func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
  82. glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target)
  83. request := &filer_pb.CreateEntryRequest{
  84. Directory: dir.FullPath(),
  85. Entry: &filer_pb.Entry{
  86. Name: req.NewName,
  87. IsDirectory: false,
  88. Attributes: &filer_pb.FuseAttributes{
  89. Mtime: time.Now().Unix(),
  90. Crtime: time.Now().Unix(),
  91. FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
  92. Uid: req.Uid,
  93. Gid: req.Gid,
  94. SymlinkTarget: req.Target,
  95. },
  96. },
  97. Signatures: []int32{dir.wfs.signature},
  98. }
  99. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  100. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  101. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  102. if err := filer_pb.CreateEntry(client, request); err != nil {
  103. glog.V(0).Infof("symlink %s/%s: %v", dir.FullPath(), req.NewName, err)
  104. return fuse.EIO
  105. }
  106. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  107. return nil
  108. })
  109. symlink := dir.newFile(req.NewName, request.Entry)
  110. return symlink, err
  111. }
  112. func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
  113. entry, err := file.maybeLoadEntry(ctx)
  114. if err != nil {
  115. return "", err
  116. }
  117. if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
  118. return "", fuse.Errno(syscall.EINVAL)
  119. }
  120. glog.V(4).Infof("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, entry.Attributes.SymlinkTarget)
  121. return entry.Attributes.SymlinkTarget, nil
  122. }