message.go 3.7 KB

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