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.

95 lines
2.2 KiB

  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. )
  8. func (fsw *FilerStoreWrapper) handleUpdateToHardLinks(ctx context.Context, entry *Entry) error {
  9. if entry.HardLinkId == 0 {
  10. return nil
  11. }
  12. // handle hard links
  13. if err := fsw.setHardLink(ctx, entry); err != nil {
  14. return fmt.Errorf("setHardLink %d: %v", entry.HardLinkId, err)
  15. }
  16. // check what is existing entry
  17. existingEntry, err := fsw.ActualStore.FindEntry(ctx, entry.FullPath)
  18. if err != nil && err != filer_pb.ErrNotFound {
  19. return fmt.Errorf("update existing entry %s: %v", entry.FullPath, err)
  20. }
  21. // remove old hard link
  22. if err == nil && existingEntry.HardLinkId != entry.HardLinkId {
  23. if err = fsw.DeleteHardLink(ctx, existingEntry.HardLinkId); err != nil {
  24. return err
  25. }
  26. }
  27. return nil
  28. }
  29. func (fsw *FilerStoreWrapper) setHardLink(ctx context.Context, entry *Entry) error {
  30. if entry.HardLinkId == 0 {
  31. return nil
  32. }
  33. key := entry.HardLinkId.Key()
  34. newBlob, encodeErr := entry.EncodeAttributesAndChunks()
  35. if encodeErr != nil {
  36. return encodeErr
  37. }
  38. return fsw.KvPut(ctx, key, newBlob)
  39. }
  40. func (fsw *FilerStoreWrapper) maybeReadHardLink(ctx context.Context, entry *Entry) error {
  41. if entry.HardLinkId == 0 {
  42. return nil
  43. }
  44. key := entry.HardLinkId.Key()
  45. value, err := fsw.KvGet(ctx, key)
  46. if err != nil {
  47. glog.Errorf("read %s hardlink %d: %v", entry.FullPath, entry.HardLinkId, err)
  48. return err
  49. }
  50. if err = entry.DecodeAttributesAndChunks(value); err != nil {
  51. glog.Errorf("decode %s hardlink %d: %v", entry.FullPath, entry.HardLinkId, err)
  52. return err
  53. }
  54. return nil
  55. }
  56. func (fsw *FilerStoreWrapper) DeleteHardLink(ctx context.Context, hardLinkId HardLinkId) error {
  57. key := hardLinkId.Key()
  58. value, err := fsw.KvGet(ctx, key)
  59. if err == ErrKvNotFound {
  60. return nil
  61. }
  62. if err != nil {
  63. return err
  64. }
  65. entry := &Entry{}
  66. if err = entry.DecodeAttributesAndChunks(value); err != nil {
  67. return err
  68. }
  69. entry.HardLinkCounter--
  70. if entry.HardLinkCounter <= 0 {
  71. return fsw.KvDelete(ctx, key)
  72. }
  73. newBlob, encodeErr := entry.EncodeAttributesAndChunks()
  74. if encodeErr != nil {
  75. return encodeErr
  76. }
  77. return fsw.KvPut(ctx, key, newBlob)
  78. }