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.

87 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "os"
  4. "time"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/gogo/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. }
  37. }
  38. func PbToEntryAttribute(attr *filer_pb.FuseAttributes) Attr {
  39. t := Attr{}
  40. t.Crtime = time.Unix(attr.Crtime, 0)
  41. t.Mtime = time.Unix(attr.Mtime, 0)
  42. t.Mode = os.FileMode(attr.FileMode)
  43. t.Uid = attr.Uid
  44. t.Gid = attr.Gid
  45. t.Mime = attr.Mime
  46. t.Collection = attr.Collection
  47. t.Replication = attr.Replication
  48. t.TtlSec = attr.TtlSec
  49. return t
  50. }
  51. func EqualEntry(a, b *Entry) bool {
  52. if a == b {
  53. return true
  54. }
  55. if a == nil && b != nil || a != nil && b == nil {
  56. return false
  57. }
  58. if !proto.Equal(EntryAttributeToPb(a), EntryAttributeToPb(b)) {
  59. return false
  60. }
  61. if len(a.Chunks) != len(b.Chunks) {
  62. return false
  63. }
  64. for i := 0; i < len(a.Chunks); i++ {
  65. if !proto.Equal(a.Chunks[i], b.Chunks[i]) {
  66. return false
  67. }
  68. }
  69. return true
  70. }