message.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Returns a statistical guess on the size, not the exact figure
  71. func (f FileInfoTruncated) Size() int64 {
  72. if f.IsDeleted() || f.IsDirectory() {
  73. return 128
  74. }
  75. if f.NumBlocks < 2 {
  76. return BlockSize / 2
  77. } else {
  78. return int64(f.NumBlocks-1)*BlockSize + BlockSize/2
  79. }
  80. }
  81. func (f FileInfoTruncated) IsDeleted() bool {
  82. return f.Flags&FlagDeleted != 0
  83. }
  84. func (f FileInfoTruncated) IsInvalid() bool {
  85. return f.Flags&FlagInvalid != 0
  86. }
  87. func (f FileInfoTruncated) IsDirectory() bool {
  88. return f.Flags&FlagDirectory != 0
  89. }
  90. func (f FileInfoTruncated) IsSymlink() bool {
  91. return f.Flags&FlagSymlink != 0
  92. }
  93. func (f FileInfoTruncated) HasPermissionBits() bool {
  94. return f.Flags&FlagNoPermBits == 0
  95. }
  96. type FileIntf interface {
  97. Size() int64
  98. IsDeleted() bool
  99. IsInvalid() bool
  100. IsDirectory() bool
  101. IsSymlink() bool
  102. HasPermissionBits() bool
  103. }
  104. type BlockInfo struct {
  105. Offset int64 // noencode (cache only)
  106. Size uint32
  107. Hash []byte // max:64
  108. }
  109. func (b BlockInfo) String() string {
  110. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  111. }
  112. type RequestMessage struct {
  113. Folder string // max:64
  114. Name string // max:8192
  115. Offset uint64
  116. Size uint32
  117. }
  118. type ResponseMessage struct {
  119. Data []byte
  120. }
  121. type ClusterConfigMessage struct {
  122. ClientName string // max:64
  123. ClientVersion string // max:64
  124. Folders []Folder // max:64
  125. Options []Option // max:64
  126. }
  127. func (o *ClusterConfigMessage) GetOption(key string) string {
  128. for _, option := range o.Options {
  129. if option.Key == key {
  130. return option.Value
  131. }
  132. }
  133. return ""
  134. }
  135. type Folder struct {
  136. ID string // max:64
  137. Devices []Device
  138. }
  139. type Device struct {
  140. ID []byte // max:32
  141. Flags uint32
  142. MaxLocalVersion uint64
  143. }
  144. type Option struct {
  145. Key string // max:64
  146. Value string // max:1024
  147. }
  148. type CloseMessage struct {
  149. Reason string // max:1024
  150. }
  151. type EmptyMessage struct{}