deviceconfiguration.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 config
  7. import (
  8. "sort"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. "github.com/syncthing/syncthing/lib/util"
  11. )
  12. type DeviceConfiguration struct {
  13. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  14. Name string `xml:"name,attr,omitempty" json:"name"`
  15. Addresses []string `xml:"address,omitempty" json:"addresses" default:"dynamic"`
  16. Compression protocol.Compression `xml:"compression,attr" json:"compression"`
  17. CertName string `xml:"certName,attr,omitempty" json:"certName"`
  18. Introducer bool `xml:"introducer,attr" json:"introducer"`
  19. SkipIntroductionRemovals bool `xml:"skipIntroductionRemovals,attr" json:"skipIntroductionRemovals"`
  20. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  21. Paused bool `xml:"paused" json:"paused"`
  22. AllowedNetworks []string `xml:"allowedNetwork,omitempty" json:"allowedNetworks"`
  23. AutoAcceptFolders bool `xml:"autoAcceptFolders" json:"autoAcceptFolders"`
  24. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  25. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  26. IgnoredFolders []ObservedFolder `xml:"ignoredFolder" json:"ignoredFolders"`
  27. PendingFolders []ObservedFolder `xml:"pendingFolder" json:"pendingFolders"`
  28. MaxRequestKiB int `xml:"maxRequestKiB" json:"maxRequestKiB"`
  29. }
  30. func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
  31. d := DeviceConfiguration{
  32. DeviceID: id,
  33. Name: name,
  34. }
  35. util.SetDefaults(&d)
  36. d.prepare(nil)
  37. return d
  38. }
  39. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  40. c := cfg
  41. c.Addresses = make([]string, len(cfg.Addresses))
  42. copy(c.Addresses, cfg.Addresses)
  43. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  44. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  45. c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
  46. copy(c.IgnoredFolders, cfg.IgnoredFolders)
  47. c.PendingFolders = make([]ObservedFolder, len(cfg.PendingFolders))
  48. copy(c.PendingFolders, cfg.PendingFolders)
  49. return c
  50. }
  51. func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
  52. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  53. cfg.Addresses = []string{"dynamic"}
  54. }
  55. if len(cfg.AllowedNetworks) == 0 {
  56. cfg.AllowedNetworks = []string{}
  57. }
  58. ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
  59. pendingFolders := deduplicateObservedFoldersToMap(cfg.PendingFolders)
  60. for id := range ignoredFolders {
  61. delete(pendingFolders, id)
  62. }
  63. for _, sharedFolder := range sharedFolders {
  64. delete(ignoredFolders, sharedFolder)
  65. delete(pendingFolders, sharedFolder)
  66. }
  67. cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
  68. cfg.PendingFolders = sortedObservedFolderSlice(pendingFolders)
  69. }
  70. func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
  71. for _, ignoredFolder := range cfg.IgnoredFolders {
  72. if ignoredFolder.ID == folder {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
  79. output := make([]ObservedFolder, 0, len(input))
  80. for _, folder := range input {
  81. output = append(output, folder)
  82. }
  83. sort.Slice(output, func(i, j int) bool {
  84. return output[i].Time.Before(output[j].Time)
  85. })
  86. return output
  87. }
  88. func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
  89. output := make(map[string]ObservedFolder, len(input))
  90. for _, folder := range input {
  91. if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
  92. output[folder.ID] = folder
  93. }
  94. }
  95. return output
  96. }