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.

137 lines
2.6 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
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. 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. }