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.

109 lines
2.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/hanwen/go-fuse/v2/fuse"
  8. "time"
  9. )
  10. /*
  11. What is an inode?
  12. If the file is an hardlinked file:
  13. use the hardlink id as inode
  14. Otherwise:
  15. use the file path as inode
  16. When creating a link:
  17. use the original file inode
  18. */
  19. /** Create a hard link to a file */
  20. func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out *fuse.EntryOut) (code fuse.Status) {
  21. if wfs.IsOverQuota {
  22. return fuse.EPERM
  23. }
  24. if s := checkName(name); s != fuse.OK {
  25. return s
  26. }
  27. newParentPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  28. if code != fuse.OK {
  29. return
  30. }
  31. oldEntryPath, code := wfs.inodeToPath.GetPath(in.Oldnodeid)
  32. if code != fuse.OK {
  33. return
  34. }
  35. oldParentPath, _ := oldEntryPath.DirAndName()
  36. oldEntry, status := wfs.maybeLoadEntry(oldEntryPath)
  37. if status != fuse.OK {
  38. return status
  39. }
  40. // update old file to hardlink mode
  41. if len(oldEntry.HardLinkId) == 0 {
  42. oldEntry.HardLinkId = filer.NewHardLinkId()
  43. oldEntry.HardLinkCounter = 1
  44. }
  45. oldEntry.HardLinkCounter++
  46. updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
  47. Directory: oldParentPath,
  48. Entry: oldEntry,
  49. Signatures: []int32{wfs.signature},
  50. }
  51. // CreateLink 1.2 : update new file to hardlink mode
  52. oldEntry.Attributes.Mtime = time.Now().Unix()
  53. request := &filer_pb.CreateEntryRequest{
  54. Directory: string(newParentPath),
  55. Entry: &filer_pb.Entry{
  56. Name: name,
  57. IsDirectory: false,
  58. Attributes: oldEntry.Attributes,
  59. Chunks: oldEntry.Chunks,
  60. Extended: oldEntry.Extended,
  61. HardLinkId: oldEntry.HardLinkId,
  62. HardLinkCounter: oldEntry.HardLinkCounter,
  63. },
  64. Signatures: []int32{wfs.signature},
  65. }
  66. // apply changes to the filer, and also apply to local metaCache
  67. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  68. wfs.mapPbIdFromLocalToFiler(request.Entry)
  69. defer wfs.mapPbIdFromFilerToLocal(request.Entry)
  70. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  71. return err
  72. }
  73. wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  74. if err := filer_pb.CreateEntry(client, request); err != nil {
  75. return err
  76. }
  77. wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  78. return nil
  79. })
  80. newEntryPath := newParentPath.Child(name)
  81. if err != nil {
  82. glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
  83. return fuse.EIO
  84. }
  85. inode := wfs.inodeToPath.Lookup(newEntryPath, oldEntry.FileMode(), true, oldEntry.Attributes.Inode, true)
  86. wfs.outputPbEntry(out, inode, request.Entry)
  87. return fuse.OK
  88. }