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.

99 lines
2.6 KiB

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