weedfs_link.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 s := checkName(name); s != fuse.OK {
  22. return s
  23. }
  24. newParentPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  25. if code != fuse.OK {
  26. return
  27. }
  28. oldEntryPath, code := wfs.inodeToPath.GetPath(in.Oldnodeid)
  29. if code != fuse.OK {
  30. return
  31. }
  32. oldParentPath, _ := oldEntryPath.DirAndName()
  33. oldEntry, status := wfs.maybeLoadEntry(oldEntryPath)
  34. if status != fuse.OK {
  35. return status
  36. }
  37. // update old file to hardlink mode
  38. if len(oldEntry.HardLinkId) == 0 {
  39. oldEntry.HardLinkId = filer.NewHardLinkId()
  40. oldEntry.HardLinkCounter = 1
  41. }
  42. oldEntry.HardLinkCounter++
  43. updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
  44. Directory: oldParentPath,
  45. Entry: oldEntry,
  46. Signatures: []int32{wfs.signature},
  47. }
  48. // CreateLink 1.2 : update new file to hardlink mode
  49. oldEntry.Attributes.Mtime = time.Now().Unix()
  50. request := &filer_pb.CreateEntryRequest{
  51. Directory: string(newParentPath),
  52. Entry: &filer_pb.Entry{
  53. Name: name,
  54. IsDirectory: false,
  55. Attributes: oldEntry.Attributes,
  56. Chunks: oldEntry.Chunks,
  57. Extended: oldEntry.Extended,
  58. HardLinkId: oldEntry.HardLinkId,
  59. HardLinkCounter: oldEntry.HardLinkCounter,
  60. },
  61. Signatures: []int32{wfs.signature},
  62. }
  63. // apply changes to the filer, and also apply to local metaCache
  64. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  65. wfs.mapPbIdFromLocalToFiler(request.Entry)
  66. defer wfs.mapPbIdFromFilerToLocal(request.Entry)
  67. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  68. return err
  69. }
  70. wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  71. if err := filer_pb.CreateEntry(client, request); err != nil {
  72. return err
  73. }
  74. wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  75. return nil
  76. })
  77. newEntryPath := newParentPath.Child(name)
  78. if err != nil {
  79. glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
  80. return fuse.EIO
  81. }
  82. inode := wfs.inodeToPath.Lookup(newEntryPath, oldEntry.FileMode(), false, oldEntry.Attributes.Inode, true)
  83. wfs.outputPbEntry(out, inode, request.Entry)
  84. return fuse.OK
  85. }