message.go 3.2 KB

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