bep_clusterconfig.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "fmt"
  9. "github.com/syncthing/syncthing/internal/gen/bep"
  10. )
  11. type Compression = bep.Compression
  12. const (
  13. CompressionMetadata = bep.Compression_COMPRESSION_METADATA
  14. CompressionNever = bep.Compression_COMPRESSION_NEVER
  15. CompressionAlways = bep.Compression_COMPRESSION_ALWAYS
  16. )
  17. type ClusterConfig struct {
  18. Folders []Folder
  19. Secondary bool
  20. }
  21. func (c *ClusterConfig) toWire() *bep.ClusterConfig {
  22. folders := make([]*bep.Folder, len(c.Folders))
  23. for i, f := range c.Folders {
  24. folders[i] = f.toWire()
  25. }
  26. return &bep.ClusterConfig{
  27. Folders: folders,
  28. Secondary: c.Secondary,
  29. }
  30. }
  31. func clusterConfigFromWire(w *bep.ClusterConfig) *ClusterConfig {
  32. if w == nil {
  33. return nil
  34. }
  35. c := &ClusterConfig{
  36. Secondary: w.Secondary,
  37. }
  38. c.Folders = make([]Folder, len(w.Folders))
  39. for i, f := range w.Folders {
  40. c.Folders[i] = folderFromWire(f)
  41. }
  42. return c
  43. }
  44. type Folder struct {
  45. ID string
  46. Label string
  47. ReadOnly bool
  48. IgnorePermissions bool
  49. IgnoreDelete bool
  50. DisableTempIndexes bool
  51. Paused bool
  52. Devices []Device
  53. }
  54. func (f *Folder) toWire() *bep.Folder {
  55. devices := make([]*bep.Device, len(f.Devices))
  56. for i, d := range f.Devices {
  57. devices[i] = d.toWire()
  58. }
  59. return &bep.Folder{
  60. Id: f.ID,
  61. Label: f.Label,
  62. ReadOnly: f.ReadOnly,
  63. IgnorePermissions: f.IgnorePermissions,
  64. IgnoreDelete: f.IgnoreDelete,
  65. DisableTempIndexes: f.DisableTempIndexes,
  66. Paused: f.Paused,
  67. Devices: devices,
  68. }
  69. }
  70. func folderFromWire(w *bep.Folder) Folder {
  71. devices := make([]Device, len(w.Devices))
  72. for i, d := range w.Devices {
  73. devices[i] = deviceFromWire(d)
  74. }
  75. return Folder{
  76. ID: w.Id,
  77. Label: w.Label,
  78. ReadOnly: w.ReadOnly,
  79. IgnorePermissions: w.IgnorePermissions,
  80. IgnoreDelete: w.IgnoreDelete,
  81. DisableTempIndexes: w.DisableTempIndexes,
  82. Paused: w.Paused,
  83. Devices: devices,
  84. }
  85. }
  86. func (f Folder) Description() string {
  87. // used by logging stuff
  88. if f.Label == "" {
  89. return f.ID
  90. }
  91. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  92. }
  93. type Device struct {
  94. ID DeviceID
  95. Name string
  96. Addresses []string
  97. Compression Compression
  98. CertName string
  99. MaxSequence int64
  100. Introducer bool
  101. IndexID IndexID
  102. SkipIntroductionRemovals bool
  103. EncryptionPasswordToken []byte
  104. }
  105. func (d *Device) toWire() *bep.Device {
  106. return &bep.Device{
  107. Id: d.ID[:],
  108. Name: d.Name,
  109. Addresses: d.Addresses,
  110. Compression: d.Compression,
  111. CertName: d.CertName,
  112. MaxSequence: d.MaxSequence,
  113. Introducer: d.Introducer,
  114. IndexId: uint64(d.IndexID),
  115. SkipIntroductionRemovals: d.SkipIntroductionRemovals,
  116. EncryptionPasswordToken: d.EncryptionPasswordToken,
  117. }
  118. }
  119. func deviceFromWire(w *bep.Device) Device {
  120. return Device{
  121. ID: DeviceID(w.Id),
  122. Name: w.Name,
  123. Addresses: w.Addresses,
  124. Compression: w.Compression,
  125. CertName: w.CertName,
  126. MaxSequence: w.MaxSequence,
  127. Introducer: w.Introducer,
  128. IndexID: IndexID(w.IndexId),
  129. SkipIntroductionRemovals: w.SkipIntroductionRemovals,
  130. EncryptionPasswordToken: w.EncryptionPasswordToken,
  131. }
  132. }