entry_codec.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  43. }
  44. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  45. t := Attr{}
  46. t.Crtime = time.Unix(attr.Crtime, 0)
  47. t.Mtime = time.Unix(attr.Mtime, 0)
  48. t.Mode = os.FileMode(attr.FileMode)
  49. t.Uid = attr.Uid
  50. t.Gid = attr.Gid
  51. t.Mime = attr.Mime
  52. t.Collection = attr.Collection
  53. t.Replication = attr.Replication
  54. t.TtlSec = attr.TtlSec
  55. t.UserName = attr.UserName
  56. t.GroupNames = attr.GroupName
  57. t.SymlinkTarget = attr.SymlinkTarget
  58. return t
  59. }
  60. func EqualEntry(a, b *Entry) bool {
  61. if a == b {
  62. return true
  63. }
  64. if a == nil && b != nil || a != nil && b == nil {
  65. return false
  66. }
  67. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  68. return false
  69. }
  70. if len(a.Chunks) != len(b.Chunks) {
  71. return false
  72. }
  73. if !eq(a.Extended, b.Extended) {
  74. return false
  75. }
  76. for i := 0; i < len(a.Chunks); i++ {
  77. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  78. return false
  79. }
  80. }
  81. return true
  82. }
  83. func eq(a, b map[string][]byte) bool {
  84. if len(a) != len(b) {
  85. return false
  86. }
  87. for k, v := range a {
  88. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  89. return false
  90. }
  91. }
  92. return true
  93. }