entry_codec.go 2.9 KB

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