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.

122 lines
2.4 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. Md5: entry.Attr.Md5,
  43. }
  44. }
  45. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  46. t := Attr{}
  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.UserName = attr.UserName
  57. t.GroupNames = attr.GroupName
  58. t.SymlinkTarget = attr.SymlinkTarget
  59. t.Md5 = attr.Md5
  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. return true
  87. }
  88. func eq(a, b map[string][]byte) bool {
  89. if len(a) != len(b) {
  90. return false
  91. }
  92. for k, v := range a {
  93. if w, ok := b[k]; !ok || !bytes.Equal(v, w) {
  94. return false
  95. }
  96. }
  97. return true
  98. }