message.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:generate -command genxdr go run ../../vendor/github.com/calmh/xdr/cmd/genxdr/main.go
  3. //go:generate genxdr -o message_xdr.go message.go
  4. package protocol
  5. import (
  6. "bytes"
  7. "crypto/sha256"
  8. "fmt"
  9. )
  10. var (
  11. sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
  12. )
  13. type IndexMessage struct {
  14. Folder string // max:256
  15. Files []FileInfo // max:1000000
  16. Flags uint32
  17. Options []Option // max:64
  18. }
  19. type FileInfo struct {
  20. Name string // max:8192
  21. Flags uint32
  22. Modified int64
  23. Version Vector
  24. LocalVersion int64
  25. CachedSize int64 // noencode (cache only)
  26. Blocks []BlockInfo // max:10000000
  27. }
  28. func (f FileInfo) String() string {
  29. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%v, Size:%d, Blocks:%v}",
  30. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  31. }
  32. func (f FileInfo) Size() (bytes int64) {
  33. if f.IsDeleted() || f.IsDirectory() {
  34. return 128
  35. }
  36. if f.CachedSize > 0 {
  37. return f.CachedSize
  38. }
  39. for _, b := range f.Blocks {
  40. bytes += int64(b.Size)
  41. }
  42. f.CachedSize = bytes
  43. return
  44. }
  45. func (f FileInfo) IsDeleted() bool {
  46. return f.Flags&FlagDeleted != 0
  47. }
  48. func (f FileInfo) IsInvalid() bool {
  49. return f.Flags&FlagInvalid != 0
  50. }
  51. func (f FileInfo) IsDirectory() bool {
  52. return f.Flags&FlagDirectory != 0
  53. }
  54. func (f FileInfo) IsSymlink() bool {
  55. return f.Flags&FlagSymlink != 0
  56. }
  57. func (f FileInfo) HasPermissionBits() bool {
  58. return f.Flags&FlagNoPermBits == 0
  59. }
  60. // WinsConflict returns true if "f" is the one to choose when it is in
  61. // conflict with "other".
  62. func (f FileInfo) WinsConflict(other FileInfo) bool {
  63. // If a modification is in conflict with a delete, we pick the
  64. // modification.
  65. if !f.IsDeleted() && other.IsDeleted() {
  66. return true
  67. }
  68. if f.IsDeleted() && !other.IsDeleted() {
  69. return false
  70. }
  71. // The one with the newer modification time wins.
  72. if f.Modified > other.Modified {
  73. return true
  74. }
  75. if f.Modified < other.Modified {
  76. return false
  77. }
  78. // The modification times were equal. Use the device ID in the version
  79. // vector as tie breaker.
  80. return f.Version.Compare(other.Version) == ConcurrentGreater
  81. }
  82. type BlockInfo struct {
  83. Offset int64 // noencode (cache only)
  84. Size int32
  85. Hash []byte // max:64
  86. }
  87. func (b BlockInfo) String() string {
  88. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  89. }
  90. // IsEmpty returns true if the block is a full block of zeroes.
  91. func (b BlockInfo) IsEmpty() bool {
  92. return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
  93. }
  94. type RequestMessage struct {
  95. Folder string // max:256
  96. Name string // max:8192
  97. Offset int64
  98. Size int32
  99. Hash []byte // max:64
  100. Flags uint32
  101. Options []Option // max:64
  102. }
  103. type ResponseMessage struct {
  104. Data []byte
  105. Code int32
  106. }
  107. type ClusterConfigMessage struct {
  108. Folders []Folder // max:1000000
  109. Options []Option // max:64
  110. }
  111. type DownloadProgressMessage struct {
  112. Folder string // max:64
  113. Updates []FileDownloadProgressUpdate // max:1000000
  114. Flags uint32
  115. Options []Option // max:64
  116. }
  117. func (o *ClusterConfigMessage) GetOption(key string) string {
  118. for _, option := range o.Options {
  119. if option.Key == key {
  120. return option.Value
  121. }
  122. }
  123. return ""
  124. }
  125. type Folder struct {
  126. ID string // max:256
  127. Label string // max:256
  128. Devices []Device // max:1000000
  129. Flags uint32
  130. Options []Option // max:64
  131. }
  132. type Device struct {
  133. ID []byte // max:32
  134. Name string // max:64
  135. Addresses []string // max:64,2083
  136. Compression uint32
  137. CertName string // max:64
  138. MaxLocalVersion int64
  139. Flags uint32
  140. Options []Option // max:64
  141. }
  142. type FileDownloadProgressUpdate struct {
  143. UpdateType uint32
  144. Name string // max:8192
  145. Version Vector
  146. BlockIndexes []int32 // max:1000000
  147. }
  148. type Option struct {
  149. Key string // max:64
  150. Value string // max:1024
  151. }
  152. type CloseMessage struct {
  153. Reason string // max:1024
  154. Code int32
  155. }
  156. type EmptyMessage struct{}