message.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
  16. //go:generate genxdr -o message_xdr.go message.go
  17. package protocol
  18. import "fmt"
  19. type IndexMessage struct {
  20. Folder string // max:64
  21. Files []FileInfo
  22. Flags uint32
  23. Options []Option // max:64
  24. }
  25. type FileInfo struct {
  26. Name string // max:8192
  27. Flags uint32
  28. Modified int64
  29. Version uint64
  30. LocalVersion uint64
  31. Blocks []BlockInfo
  32. }
  33. func (f FileInfo) String() string {
  34. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, Blocks:%v}",
  35. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  36. }
  37. func (f FileInfo) Size() (bytes int64) {
  38. if f.IsDeleted() || f.IsDirectory() {
  39. return 128
  40. }
  41. for _, b := range f.Blocks {
  42. bytes += int64(b.Size)
  43. }
  44. return
  45. }
  46. func (f FileInfo) IsDeleted() bool {
  47. return f.Flags&FlagDeleted != 0
  48. }
  49. func (f FileInfo) IsInvalid() bool {
  50. return f.Flags&FlagInvalid != 0
  51. }
  52. func (f FileInfo) IsDirectory() bool {
  53. return f.Flags&FlagDirectory != 0
  54. }
  55. func (f FileInfo) IsSymlink() bool {
  56. return f.Flags&FlagSymlink != 0
  57. }
  58. func (f FileInfo) HasPermissionBits() bool {
  59. return f.Flags&FlagNoPermBits == 0
  60. }
  61. func (f FileInfo) ToTruncated() FileInfoTruncated {
  62. return FileInfoTruncated{
  63. Name: f.Name,
  64. Flags: f.Flags,
  65. Modified: f.Modified,
  66. Version: f.Version,
  67. LocalVersion: f.LocalVersion,
  68. NumBlocks: uint32(len(f.Blocks)),
  69. }
  70. }
  71. // Used for unmarshalling a FileInfo structure but skipping the actual block list
  72. type FileInfoTruncated struct {
  73. Name string // max:8192
  74. Flags uint32
  75. Modified int64
  76. Version uint64
  77. LocalVersion uint64
  78. NumBlocks uint32
  79. }
  80. func (f FileInfoTruncated) String() string {
  81. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, NumBlocks:%d}",
  82. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.NumBlocks)
  83. }
  84. func BlocksToSize(num uint32) int64 {
  85. if num < 2 {
  86. return BlockSize / 2
  87. }
  88. return int64(num-1)*BlockSize + BlockSize/2
  89. }
  90. // Returns a statistical guess on the size, not the exact figure
  91. func (f FileInfoTruncated) Size() int64 {
  92. if f.IsDeleted() || f.IsDirectory() {
  93. return 128
  94. }
  95. return BlocksToSize(f.NumBlocks)
  96. }
  97. func (f FileInfoTruncated) IsDeleted() bool {
  98. return f.Flags&FlagDeleted != 0
  99. }
  100. func (f FileInfoTruncated) IsInvalid() bool {
  101. return f.Flags&FlagInvalid != 0
  102. }
  103. func (f FileInfoTruncated) IsDirectory() bool {
  104. return f.Flags&FlagDirectory != 0
  105. }
  106. func (f FileInfoTruncated) IsSymlink() bool {
  107. return f.Flags&FlagSymlink != 0
  108. }
  109. func (f FileInfoTruncated) HasPermissionBits() bool {
  110. return f.Flags&FlagNoPermBits == 0
  111. }
  112. type BlockInfo struct {
  113. Offset int64 // noencode (cache only)
  114. Size uint32
  115. Hash []byte // max:64
  116. }
  117. func (b BlockInfo) String() string {
  118. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  119. }
  120. type RequestMessage struct {
  121. Folder string // max:64
  122. Name string // max:8192
  123. Offset uint64
  124. Size uint32
  125. Hash []byte // max:64
  126. Flags uint32
  127. Options []Option // max:64
  128. }
  129. type ResponseMessage struct {
  130. Data []byte
  131. Error uint32
  132. }
  133. type ClusterConfigMessage struct {
  134. ClientName string // max:64
  135. ClientVersion string // max:64
  136. Folders []Folder // max:64
  137. Options []Option // max:64
  138. }
  139. func (o *ClusterConfigMessage) GetOption(key string) string {
  140. for _, option := range o.Options {
  141. if option.Key == key {
  142. return option.Value
  143. }
  144. }
  145. return ""
  146. }
  147. type Folder struct {
  148. ID string // max:64
  149. Devices []Device
  150. }
  151. type Device struct {
  152. ID []byte // max:32
  153. Flags uint32
  154. MaxLocalVersion uint64
  155. }
  156. type Option struct {
  157. Key string // max:64
  158. Value string // max:1024
  159. }
  160. type CloseMessage struct {
  161. Reason string // max:1024
  162. Code uint32
  163. }
  164. type EmptyMessage struct{}