1
0

message.go 3.5 KB

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