message.go 4.4 KB

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