weedfs_link.go 2.8 KB

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