message.go 4.0 KB

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