entry_codec.go 3.0 KB

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