message.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package protocol
  16. import "fmt"
  17. type IndexMessage struct {
  18. Folder string // max:64
  19. Files []FileInfo
  20. }
  21. type FileInfo struct {
  22. Name string // max:8192
  23. Flags uint32
  24. Modified int64
  25. Version uint64
  26. LocalVersion uint64
  27. Blocks []BlockInfo
  28. }
  29. func (f FileInfo) String() string {
  30. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, Blocks:%v}",
  31. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  32. }
  33. func (f FileInfo) Size() (bytes int64) {
  34. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  35. return 128
  36. }
  37. for _, b := range f.Blocks {
  38. bytes += int64(b.Size)
  39. }
  40. return
  41. }
  42. func (f FileInfo) IsDeleted() bool {
  43. return IsDeleted(f.Flags)
  44. }
  45. func (f FileInfo) IsInvalid() bool {
  46. return IsInvalid(f.Flags)
  47. }
  48. // Used for unmarshalling a FileInfo structure but skipping the actual block list
  49. type FileInfoTruncated struct {
  50. Name string // max:8192
  51. Flags uint32
  52. Modified int64
  53. Version uint64
  54. LocalVersion uint64
  55. NumBlocks uint32
  56. }
  57. // Returns a statistical guess on the size, not the exact figure
  58. func (f FileInfoTruncated) Size() int64 {
  59. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  60. return 128
  61. }
  62. if f.NumBlocks < 2 {
  63. return BlockSize / 2
  64. } else {
  65. return int64(f.NumBlocks-1)*BlockSize + BlockSize/2
  66. }
  67. }
  68. func (f FileInfoTruncated) IsDeleted() bool {
  69. return IsDeleted(f.Flags)
  70. }
  71. func (f FileInfoTruncated) IsInvalid() bool {
  72. return IsInvalid(f.Flags)
  73. }
  74. type FileIntf interface {
  75. Size() int64
  76. IsDeleted() bool
  77. IsInvalid() bool
  78. }
  79. type BlockInfo struct {
  80. Offset int64 // noencode (cache only)
  81. Size uint32
  82. Hash []byte // max:64
  83. }
  84. func (b BlockInfo) String() string {
  85. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  86. }
  87. type RequestMessage struct {
  88. Folder string // max:64
  89. Name string // max:8192
  90. Offset uint64
  91. Size uint32
  92. }
  93. type ResponseMessage struct {
  94. Data []byte
  95. }
  96. type ClusterConfigMessage struct {
  97. ClientName string // max:64
  98. ClientVersion string // max:64
  99. Folders []Folder // max:64
  100. Options []Option // max:64
  101. }
  102. func (o *ClusterConfigMessage) GetOption(key string) string {
  103. for _, option := range o.Options {
  104. if option.Key == key {
  105. return option.Value
  106. }
  107. }
  108. return ""
  109. }
  110. type Folder struct {
  111. ID string // max:64
  112. Devices []Device // max:64
  113. }
  114. type Device struct {
  115. ID []byte // max:32
  116. Flags uint32
  117. MaxLocalVersion uint64
  118. }
  119. type Option struct {
  120. Key string // max:64
  121. Value string // max:1024
  122. }
  123. type CloseMessage struct {
  124. Reason string // max:1024
  125. }
  126. type EmptyMessage struct{}