message.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package protocol
  5. import "fmt"
  6. type IndexMessage struct {
  7. Folder string // max:64
  8. Files []FileInfo
  9. }
  10. type FileInfo struct {
  11. Name string // max:8192
  12. Flags uint32
  13. Modified int64
  14. Version uint64
  15. LocalVersion uint64
  16. Blocks []BlockInfo
  17. }
  18. func (f FileInfo) String() string {
  19. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, Blocks:%v}",
  20. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  21. }
  22. func (f FileInfo) Size() (bytes int64) {
  23. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  24. return 128
  25. }
  26. for _, b := range f.Blocks {
  27. bytes += int64(b.Size)
  28. }
  29. return
  30. }
  31. func (f FileInfo) IsDeleted() bool {
  32. return IsDeleted(f.Flags)
  33. }
  34. func (f FileInfo) IsInvalid() bool {
  35. return IsInvalid(f.Flags)
  36. }
  37. // Used for unmarshalling a FileInfo structure but skipping the actual block list
  38. type FileInfoTruncated struct {
  39. Name string // max:8192
  40. Flags uint32
  41. Modified int64
  42. Version uint64
  43. LocalVersion uint64
  44. NumBlocks uint32
  45. }
  46. // Returns a statistical guess on the size, not the exact figure
  47. func (f FileInfoTruncated) Size() int64 {
  48. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  49. return 128
  50. }
  51. if f.NumBlocks < 2 {
  52. return BlockSize / 2
  53. } else {
  54. return int64(f.NumBlocks-1)*BlockSize + BlockSize/2
  55. }
  56. }
  57. func (f FileInfoTruncated) IsDeleted() bool {
  58. return IsDeleted(f.Flags)
  59. }
  60. func (f FileInfoTruncated) IsInvalid() bool {
  61. return IsInvalid(f.Flags)
  62. }
  63. type FileIntf interface {
  64. Size() int64
  65. IsDeleted() bool
  66. IsInvalid() bool
  67. }
  68. type BlockInfo struct {
  69. Offset int64 // noencode (cache only)
  70. Size uint32
  71. Hash []byte // max:64
  72. }
  73. func (b BlockInfo) String() string {
  74. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  75. }
  76. type RequestMessage struct {
  77. Folder string // max:64
  78. Name string // max:8192
  79. Offset uint64
  80. Size uint32
  81. }
  82. type ResponseMessage struct {
  83. Data []byte
  84. }
  85. type ClusterConfigMessage struct {
  86. ClientName string // max:64
  87. ClientVersion string // max:64
  88. Folders []Folder // max:64
  89. Options []Option // max:64
  90. }
  91. func (o *ClusterConfigMessage) GetOption(key string) string {
  92. for _, option := range o.Options {
  93. if option.Key == key {
  94. return option.Value
  95. }
  96. }
  97. return ""
  98. }
  99. type Folder struct {
  100. ID string // max:64
  101. Devices []Device // max:64
  102. }
  103. type Device struct {
  104. ID []byte // max:32
  105. Flags uint32
  106. MaxLocalVersion uint64
  107. }
  108. type Option struct {
  109. Key string // max:64
  110. Value string // max:1024
  111. }
  112. type CloseMessage struct {
  113. Reason string // max:1024
  114. }
  115. type EmptyMessage struct{}