entry_codec.go 2.5 KB

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