You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
2.7 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. if a.Quota != b.Quota {
  101. return false
  102. }
  103. return true
  104. }
  105. func eq(a, b map[string][]byte) bool {
  106. if len(a) != len(b) {
  107. return false
  108. }
  109. for k, v := range a {
  110. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  111. return false
  112. }
  113. }
  114. return true
  115. }