dir_link.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. oldEntry.Attributes.Mtime = time.Now().Unix()
  49. request := &filer_pb.CreateEntryRequest{
  50. Directory: dir.FullPath(),
  51. Entry: &filer_pb.Entry{
  52. Name: req.NewName,
  53. IsDirectory: false,
  54. Attributes: oldEntry.Attributes,
  55. Chunks: oldEntry.Chunks,
  56. Extended: oldEntry.Extended,
  57. HardLinkId: oldEntry.HardLinkId,
  58. HardLinkCounter: oldEntry.HardLinkCounter,
  59. },
  60. Signatures: []int32{dir.wfs.signature},
  61. }
  62. // apply changes to the filer, and also apply to local metaCache
  63. err = dir.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  64. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  65. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  66. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  67. glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
  68. return fuse.EIO
  69. }
  70. dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  71. if err := filer_pb.CreateEntry(client, request); err != nil {
  72. glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
  73. return fuse.EIO
  74. }
  75. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  76. return nil
  77. })
  78. if err != nil {
  79. return nil, fuse.EIO
  80. }
  81. // create new file node
  82. newNode := dir.newFile(req.NewName, 0)
  83. newFile := newNode.(*File)
  84. return newFile, err
  85. }
  86. func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
  87. if err := checkName(req.NewName); err != nil {
  88. return nil, err
  89. }
  90. glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target)
  91. request := &filer_pb.CreateEntryRequest{
  92. Directory: dir.FullPath(),
  93. Entry: &filer_pb.Entry{
  94. Name: req.NewName,
  95. IsDirectory: false,
  96. Attributes: &filer_pb.FuseAttributes{
  97. Mtime: time.Now().Unix(),
  98. Crtime: time.Now().Unix(),
  99. FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
  100. Uid: req.Uid,
  101. Gid: req.Gid,
  102. SymlinkTarget: req.Target,
  103. },
  104. },
  105. Signatures: []int32{dir.wfs.signature},
  106. }
  107. err := dir.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  108. dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
  109. defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
  110. if err := filer_pb.CreateEntry(client, request); err != nil {
  111. glog.V(0).Infof("symlink %s/%s: %v", dir.FullPath(), req.NewName, err)
  112. return fuse.EIO
  113. }
  114. dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  115. return nil
  116. })
  117. symlink := dir.newFile(req.NewName, os.ModeSymlink)
  118. return symlink, err
  119. }
  120. func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
  121. entry, err := file.maybeLoadEntry(ctx)
  122. if err != nil {
  123. return "", err
  124. }
  125. if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
  126. return "", fuse.Errno(syscall.EINVAL)
  127. }
  128. glog.V(4).Infof("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, entry.Attributes.SymlinkTarget)
  129. return entry.Attributes.SymlinkTarget, nil
  130. }