entry_codec.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package filer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func (entry *Entry) EncodeAttributesAndChunks() ([]byte, error) {
  11. message := &filer_pb.Entry{
  12. Attributes: EntryAttributeToPb(entry),
  13. Chunks: entry.Chunks,
  14. Extended: entry.Extended,
  15. HardLinkId: entry.HardLinkId,
  16. HardLinkCounter: entry.HardLinkCounter,
  17. }
  18. return proto.Marshal(message)
  19. }
  20. func (entry *Entry) DecodeAttributesAndChunks(blob []byte) error {
  21. message := &filer_pb.Entry{}
  22. if err := proto.UnmarshalMerge(blob, message); err != nil {
  23. return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
  24. }
  25. entry.Attr = PbToEntryAttribute(message.Attributes)
  26. entry.Extended = message.Extended
  27. entry.Chunks = message.Chunks
  28. entry.HardLinkId = message.HardLinkId
  29. entry.HardLinkCounter = message.HardLinkCounter
  30. return nil
  31. }
  32. func EntryAttributeToPb(entry *Entry) *filer_pb.FuseAttributes {
  33. return &filer_pb.FuseAttributes{
  34. Crtime: entry.Attr.Crtime.Unix(),
  35. Mtime: entry.Attr.Mtime.Unix(),
  36. FileMode: uint32(entry.Attr.Mode),
  37. Uid: entry.Uid,
  38. Gid: entry.Gid,
  39. Mime: entry.Mime,
  40. Collection: entry.Attr.Collection,
  41. Replication: entry.Attr.Replication,
  42. TtlSec: entry.Attr.TtlSec,
  43. UserName: entry.Attr.UserName,
  44. GroupName: entry.Attr.GroupNames,
  45. SymlinkTarget: entry.Attr.SymlinkTarget,
  46. Md5: entry.Attr.Md5,
  47. FileSize: entry.Attr.FileSize,
  48. }
  49. }
  50. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  51. t := Attr{}
  52. if attr == nil {
  53. return t
  54. }
  55. t.Crtime = time.Unix(attr.Crtime, 0)
  56. t.Mtime = time.Unix(attr.Mtime, 0)
  57. t.Mode = os.FileMode(attr.FileMode)
  58. t.Uid = attr.Uid
  59. t.Gid = attr.Gid
  60. t.Mime = attr.Mime
  61. t.Collection = attr.Collection
  62. t.Replication = attr.Replication
  63. t.TtlSec = attr.TtlSec
  64. t.UserName = attr.UserName
  65. t.GroupNames = attr.GroupName
  66. t.SymlinkTarget = attr.SymlinkTarget
  67. t.Md5 = attr.Md5
  68. t.FileSize = attr.FileSize
  69. return t
  70. }
  71. func EqualEntry(a, b *Entry) bool {
  72. if a == b {
  73. return true
  74. }
  75. if a == nil && b != nil || a != nil && b == nil {
  76. return false
  77. }
  78. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  79. return false
  80. }
  81. if len(a.Chunks) != len(b.Chunks) {
  82. return false
  83. }
  84. if !eq(a.Extended, b.Extended) {
  85. return false
  86. }
  87. if !bytes.Equal(a.Md5, b.Md5) {
  88. return false
  89. }
  90. for i := 0; i < len(a.Chunks); i++ {
  91. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  92. return false
  93. }
  94. }
  95. if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
  96. return false
  97. }
  98. if a.HardLinkCounter != b.HardLinkCounter {
  99. return false
  100. }
  101. return true
  102. }
  103. func eq(a, b map[string][]byte) bool {
  104. if len(a) != len(b) {
  105. return false
  106. }
  107. for k, v := range a {
  108. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  109. return false
  110. }
  111. }
  112. return true
  113. }