message.go 3.4 KB

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