weedfs_link.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // hardlink is not allowed in WORM mode
  42. if wfs.wormEnabledForEntry(oldEntryPath, oldEntry) {
  43. return fuse.EPERM
  44. }
  45. // update old file to hardlink mode
  46. if len(oldEntry.HardLinkId) == 0 {
  47. oldEntry.HardLinkId = filer.NewHardLinkId()
  48. oldEntry.HardLinkCounter = 1
  49. }
  50. oldEntry.HardLinkCounter++
  51. updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
  52. Directory: oldParentPath,
  53. Entry: oldEntry,
  54. Signatures: []int32{wfs.signature},
  55. }
  56. // CreateLink 1.2 : update new file to hardlink mode
  57. oldEntry.Attributes.Mtime = time.Now().Unix()
  58. request := &filer_pb.CreateEntryRequest{
  59. Directory: string(newParentPath),
  60. Entry: &filer_pb.Entry{
  61. Name: name,
  62. IsDirectory: false,
  63. Attributes: oldEntry.Attributes,
  64. Chunks: oldEntry.GetChunks(),
  65. Extended: oldEntry.Extended,
  66. HardLinkId: oldEntry.HardLinkId,
  67. HardLinkCounter: oldEntry.HardLinkCounter,
  68. },
  69. Signatures: []int32{wfs.signature},
  70. SkipCheckParentDirectory: true,
  71. }
  72. // apply changes to the filer, and also apply to local metaCache
  73. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  74. wfs.mapPbIdFromLocalToFiler(request.Entry)
  75. defer wfs.mapPbIdFromFilerToLocal(request.Entry)
  76. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  77. return err
  78. }
  79. wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  80. if err := filer_pb.CreateEntry(client, request); err != nil {
  81. return err
  82. }
  83. wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  84. return nil
  85. })
  86. newEntryPath := newParentPath.Child(name)
  87. if err != nil {
  88. glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
  89. return fuse.EIO
  90. }
  91. wfs.inodeToPath.AddPath(oldEntry.Attributes.Inode, newEntryPath)
  92. wfs.outputPbEntry(out, oldEntry.Attributes.Inode, request.Entry)
  93. return fuse.OK
  94. }