message.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. func (f FileInfoTruncated) String() string {
  58. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, NumBlocks:%d}",
  59. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.NumBlocks)
  60. }
  61. // Returns a statistical guess on the size, not the exact figure
  62. func (f FileInfoTruncated) Size() int64 {
  63. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  64. return 128
  65. }
  66. if f.NumBlocks < 2 {
  67. return BlockSize / 2
  68. } else {
  69. return int64(f.NumBlocks-1)*BlockSize + BlockSize/2
  70. }
  71. }
  72. func (f FileInfoTruncated) IsDeleted() bool {
  73. return IsDeleted(f.Flags)
  74. }
  75. func (f FileInfoTruncated) IsInvalid() bool {
  76. return IsInvalid(f.Flags)
  77. }
  78. type FileIntf interface {
  79. Size() int64
  80. IsDeleted() bool
  81. IsInvalid() bool
  82. }
  83. type BlockInfo struct {
  84. Offset int64 // noencode (cache only)
  85. Size uint32
  86. Hash []byte // max:64
  87. }
  88. func (b BlockInfo) String() string {
  89. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  90. }
  91. type RequestMessage struct {
  92. Folder string // max:64
  93. Name string // max:8192
  94. Offset uint64
  95. Size uint32
  96. }
  97. type ResponseMessage struct {
  98. Data []byte
  99. }
  100. type ClusterConfigMessage struct {
  101. ClientName string // max:64
  102. ClientVersion string // max:64
  103. Folders []Folder // max:64
  104. Options []Option // max:64
  105. }
  106. func (o *ClusterConfigMessage) GetOption(key string) string {
  107. for _, option := range o.Options {
  108. if option.Key == key {
  109. return option.Value
  110. }
  111. }
  112. return ""
  113. }
  114. type Folder struct {
  115. ID string // max:64
  116. Devices []Device // max:64
  117. }
  118. type Device struct {
  119. ID []byte // max:32
  120. Flags uint32
  121. MaxLocalVersion uint64
  122. }
  123. type Option struct {
  124. Key string // max:64
  125. Value string // max:1024
  126. }
  127. type CloseMessage struct {
  128. Reason string // max:1024
  129. }
  130. type EmptyMessage struct{}