entry_codec.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package filer2
  2. import (
  3. "os"
  4. "time"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. func (entry *Entry) EncodeAttributesAndChunks() ([]byte, error) {
  10. message := &filer_pb.Entry{
  11. Attributes: EntryAttributeToPb(entry),
  12. Chunks: entry.Chunks,
  13. }
  14. return proto.Marshal(message)
  15. }
  16. func (entry *Entry) DecodeAttributesAndChunks(blob []byte) error {
  17. message := &filer_pb.Entry{}
  18. if err := proto.UnmarshalMerge(blob, message); err != nil {
  19. return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
  20. }
  21. entry.Attr = PbToEntryAttribute(message.Attributes)
  22. entry.Chunks = message.Chunks
  23. return nil
  24. }
  25. func EntryAttributeToPb(entry *Entry) *filer_pb.FuseAttributes {
  26. return &filer_pb.FuseAttributes{
  27. Crtime: entry.Attr.Crtime.Unix(),
  28. Mtime: entry.Attr.Mtime.Unix(),
  29. FileMode: uint32(entry.Attr.Mode),
  30. Uid: entry.Uid,
  31. Gid: entry.Gid,
  32. Mime: entry.Mime,
  33. Collection: entry.Attr.Collection,
  34. Replication: entry.Attr.Replication,
  35. TtlSec: entry.Attr.TtlSec,
  36. UserName: entry.Attr.UserName,
  37. GroupName: entry.Attr.GroupNames,
  38. SymlinkTarget: entry.Attr.SymlinkTarget,
  39. }
  40. }
  41. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  42. t := Attr{}
  43. t.Crtime = time.Unix(attr.Crtime, 0)
  44. t.Mtime = time.Unix(attr.Mtime, 0)
  45. t.Mode = os.FileMode(attr.FileMode)
  46. t.Uid = attr.Uid
  47. t.Gid = attr.Gid
  48. t.Mime = attr.Mime
  49. t.Collection = attr.Collection
  50. t.Replication = attr.Replication
  51. t.TtlSec = attr.TtlSec
  52. t.UserName = attr.UserName
  53. t.GroupNames = attr.GroupName
  54. t.SymlinkTarget = attr.SymlinkTarget
  55. return t
  56. }
  57. func EqualEntry(a, b *Entry) bool {
  58. if a == b {
  59. return true
  60. }
  61. if a == nil && b != nil || a != nil && b == nil {
  62. return false
  63. }
  64. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  65. return false
  66. }
  67. if len(a.Chunks) != len(b.Chunks) {
  68. return false
  69. }
  70. for i := 0; i < len(a.Chunks); i++ {
  71. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  72. return false
  73. }
  74. }
  75. return true
  76. }