entry_codec.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. DiskType: entry.Attr.DiskType,
  46. UserName: entry.Attr.UserName,
  47. GroupName: entry.Attr.GroupNames,
  48. SymlinkTarget: entry.Attr.SymlinkTarget,
  49. Md5: entry.Attr.Md5,
  50. FileSize: entry.Attr.FileSize,
  51. }
  52. }
  53. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  54. t := Attr{}
  55. if attr == nil {
  56. return t
  57. }
  58. t.Crtime = time.Unix(attr.Crtime, 0)
  59. t.Mtime = time.Unix(attr.Mtime, 0)
  60. t.Mode = os.FileMode(attr.FileMode)
  61. t.Uid = attr.Uid
  62. t.Gid = attr.Gid
  63. t.Mime = attr.Mime
  64. t.Collection = attr.Collection
  65. t.Replication = attr.Replication
  66. t.TtlSec = attr.TtlSec
  67. t.DiskType = attr.DiskType
  68. t.UserName = attr.UserName
  69. t.GroupNames = attr.GroupName
  70. t.SymlinkTarget = attr.SymlinkTarget
  71. t.Md5 = attr.Md5
  72. t.FileSize = attr.FileSize
  73. return t
  74. }
  75. func EqualEntry(a, b *Entry) bool {
  76. if a == b {
  77. return true
  78. }
  79. if a == nil && b != nil || a != nil && b == nil {
  80. return false
  81. }
  82. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  83. return false
  84. }
  85. if len(a.Chunks) != len(b.Chunks) {
  86. return false
  87. }
  88. if !eq(a.Extended, b.Extended) {
  89. return false
  90. }
  91. if !bytes.Equal(a.Md5, b.Md5) {
  92. return false
  93. }
  94. for i := 0; i < len(a.Chunks); i++ {
  95. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  96. return false
  97. }
  98. }
  99. if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
  100. return false
  101. }
  102. if a.HardLinkCounter != b.HardLinkCounter {
  103. return false
  104. }
  105. if !bytes.Equal(a.Content, b.Content) {
  106. return false
  107. }
  108. return true
  109. }
  110. func eq(a, b map[string][]byte) bool {
  111. if len(a) != len(b) {
  112. return false
  113. }
  114. for k, v := range a {
  115. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  116. return false
  117. }
  118. }
  119. return true
  120. }