message.go 3.1 KB

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