entry_codec.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. entry.ToExistingProtoEntry(message)
  13. return proto.Marshal(message)
  14. }
  15. func (entry *Entry) DecodeAttributesAndChunks(blob []byte) error {
  16. message := &filer_pb.Entry{}
  17. if err := proto.UnmarshalMerge(blob, message); err != nil {
  18. return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
  19. }
  20. FromPbEntryToExistingEntry(message, entry)
  21. return nil
  22. }
  23. func EntryAttributeToPb(entry *Entry) *filer_pb.FuseAttributes {
  24. return &filer_pb.FuseAttributes{
  25. Crtime: entry.Attr.Crtime.Unix(),
  26. Mtime: entry.Attr.Mtime.Unix(),
  27. FileMode: uint32(entry.Attr.Mode),
  28. Uid: entry.Uid,
  29. Gid: entry.Gid,
  30. Mime: entry.Mime,
  31. Collection: entry.Attr.Collection,
  32. Replication: entry.Attr.Replication,
  33. TtlSec: entry.Attr.TtlSec,
  34. DiskType: entry.Attr.DiskType,
  35. UserName: entry.Attr.UserName,
  36. GroupName: entry.Attr.GroupNames,
  37. SymlinkTarget: entry.Attr.SymlinkTarget,
  38. Md5: entry.Attr.Md5,
  39. FileSize: entry.Attr.FileSize,
  40. }
  41. }
  42. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  43. t := Attr{}
  44. if attr == nil {
  45. return t
  46. }
  47. t.Crtime = time.Unix(attr.Crtime, 0)
  48. t.Mtime = time.Unix(attr.Mtime, 0)
  49. t.Mode = os.FileMode(attr.FileMode)
  50. t.Uid = attr.Uid
  51. t.Gid = attr.Gid
  52. t.Mime = attr.Mime
  53. t.Collection = attr.Collection
  54. t.Replication = attr.Replication
  55. t.TtlSec = attr.TtlSec
  56. t.DiskType = attr.DiskType
  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. if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
  89. return false
  90. }
  91. if a.HardLinkCounter != b.HardLinkCounter {
  92. return false
  93. }
  94. if !bytes.Equal(a.Content, b.Content) {
  95. return false
  96. }
  97. if !proto.Equal(a.Remote, b.Remote) {
  98. return false
  99. }
  100. return true
  101. }
  102. func eq(a, b map[string][]byte) bool {
  103. if len(a) != len(b) {
  104. return false
  105. }
  106. for k, v := range a {
  107. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  108. return false
  109. }
  110. }
  111. return true
  112. }