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.

110 lines
2.8 KiB

3 years ago
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. "syscall"
  9. "time"
  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.Chunks,
  61. Extended: oldEntry.Extended,
  62. HardLinkId: oldEntry.HardLinkId,
  63. HardLinkCounter: oldEntry.HardLinkCounter,
  64. },
  65. Signatures: []int32{wfs.signature},
  66. }
  67. // apply changes to the filer, and also apply to local metaCache
  68. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  69. wfs.mapPbIdFromLocalToFiler(request.Entry)
  70. defer wfs.mapPbIdFromFilerToLocal(request.Entry)
  71. if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
  72. return err
  73. }
  74. wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
  75. if err := filer_pb.CreateEntry(client, request); err != nil {
  76. return err
  77. }
  78. wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  79. return nil
  80. })
  81. newEntryPath := newParentPath.Child(name)
  82. if err != nil {
  83. glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
  84. return fuse.EIO
  85. }
  86. inode := wfs.inodeToPath.Lookup(newEntryPath, oldEntry.Attributes.Crtime, oldEntry.IsDirectory, true, oldEntry.Attributes.Inode, true)
  87. wfs.outputPbEntry(out, inode, request.Entry)
  88. return fuse.OK
  89. }