dir_link.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/util/log"
  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. log.Errorf("old node is not a file: %+v", old)
  24. }
  25. log.Tracef("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. log.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. log.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. log.Tracef("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. log.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. entry, err := file.maybeLoadEntry(ctx)
  111. if err != nil {
  112. return "", err
  113. }
  114. if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
  115. return "", fuse.Errno(syscall.EINVAL)
  116. }
  117. log.Tracef("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, entry.Attributes.SymlinkTarget)
  118. return entry.Attributes.SymlinkTarget, nil
  119. }