message.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
  16. //go:generate genxdr -o message_xdr.go message.go
  17. package protocol
  18. import "fmt"
  19. type IndexMessage struct {
  20. Folder string // max:64
  21. Files []FileInfo
  22. Flags uint32
  23. Options []Option // max:64
  24. }
  25. type FileInfo struct {
  26. Name string // max:8192
  27. Flags uint32
  28. Modified int64
  29. Version uint64
  30. LocalVersion uint64
  31. Blocks []BlockInfo
  32. }
  33. func (f FileInfo) String() string {
  34. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, Blocks:%v}",
  35. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  36. }
  37. func (f FileInfo) Size() (bytes int64) {
  38. if f.IsDeleted() || f.IsDirectory() {
  39. return 128
  40. }
  41. for _, b := range f.Blocks {
  42. bytes += int64(b.Size)
  43. }
  44. return
  45. }
  46. func (f FileInfo) IsDeleted() bool {
  47. return f.Flags&FlagDeleted != 0
  48. }
  49. func (f FileInfo) IsInvalid() bool {
  50. return f.Flags&FlagInvalid != 0
  51. }
  52. func (f FileInfo) IsDirectory() bool {
  53. return f.Flags&FlagDirectory != 0
  54. }
  55. func (f FileInfo) IsSymlink() bool {
  56. return f.Flags&FlagSymlink != 0
  57. }
  58. func (f FileInfo) HasPermissionBits() bool {
  59. return f.Flags&FlagNoPermBits == 0
  60. }
  61. type BlockInfo struct {
  62. Offset int64 // noencode (cache only)
  63. Size uint32
  64. Hash []byte // max:64
  65. }
  66. func (b BlockInfo) String() string {
  67. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  68. }
  69. type RequestMessage struct {
  70. Folder string // max:64
  71. Name string // max:8192
  72. Offset uint64
  73. Size uint32
  74. Hash []byte // max:64
  75. Flags uint32
  76. Options []Option // max:64
  77. }
  78. type ResponseMessage struct {
  79. Data []byte
  80. Error uint32
  81. }
  82. type ClusterConfigMessage struct {
  83. ClientName string // max:64
  84. ClientVersion string // max:64
  85. Folders []Folder // max:64
  86. Options []Option // max:64
  87. }
  88. func (o *ClusterConfigMessage) GetOption(key string) string {
  89. for _, option := range o.Options {
  90. if option.Key == key {
  91. return option.Value
  92. }
  93. }
  94. return ""
  95. }
  96. type Folder struct {
  97. ID string // max:64
  98. Devices []Device
  99. }
  100. type Device struct {
  101. ID []byte // max:32
  102. Flags uint32
  103. MaxLocalVersion uint64
  104. }
  105. type Option struct {
  106. Key string // max:64
  107. Value string // max:1024
  108. }
  109. type CloseMessage struct {
  110. Reason string // max:1024
  111. Code uint32
  112. }
  113. type EmptyMessage struct{}