weedfs_link.go 2.5 KB

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