entry_codec.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package filer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "time"
  7. "google.golang.org/protobuf/proto"
  8. "github.com/seaweedfs/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.Unmarshal(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. TtlSec: entry.Attr.TtlSec,
  32. UserName: entry.Attr.UserName,
  33. GroupName: entry.Attr.GroupNames,
  34. SymlinkTarget: entry.Attr.SymlinkTarget,
  35. Md5: entry.Attr.Md5,
  36. FileSize: entry.Attr.FileSize,
  37. Rdev: entry.Attr.Rdev,
  38. Inode: entry.Attr.Inode,
  39. }
  40. }
  41. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  42. t := Attr{}
  43. if attr == nil {
  44. return t
  45. }
  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.TtlSec = attr.TtlSec
  53. t.UserName = attr.UserName
  54. t.GroupNames = attr.GroupName
  55. t.SymlinkTarget = attr.SymlinkTarget
  56. t.Md5 = attr.Md5
  57. t.FileSize = attr.FileSize
  58. t.Rdev = attr.Rdev
  59. t.Inode = attr.Inode
  60. return t
  61. }
  62. func EqualEntry(a, b *Entry) bool {
  63. if a == b {
  64. return true
  65. }
  66. if a == nil && b != nil || a != nil && b == nil {
  67. return false
  68. }
  69. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  70. return false
  71. }
  72. if len(a.Chunks) != len(b.Chunks) {
  73. return false
  74. }
  75. if !eq(a.Extended, b.Extended) {
  76. return false
  77. }
  78. if !bytes.Equal(a.Md5, b.Md5) {
  79. return false
  80. }
  81. for i := 0; i < len(a.Chunks); i++ {
  82. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  83. return false
  84. }
  85. }
  86. if !bytes.Equal(a.HardLinkId, b.HardLinkId) {
  87. return false
  88. }
  89. if a.HardLinkCounter != b.HardLinkCounter {
  90. return false
  91. }
  92. if !bytes.Equal(a.Content, b.Content) {
  93. return false
  94. }
  95. if !proto.Equal(a.Remote, b.Remote) {
  96. return false
  97. }
  98. if a.Quota != b.Quota {
  99. return false
  100. }
  101. return true
  102. }
  103. func eq(a, b map[string][]byte) bool {
  104. if len(a) != len(b) {
  105. return false
  106. }
  107. for k, v := range a {
  108. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  109. return false
  110. }
  111. }
  112. return true
  113. }