deviceconfiguration.go 3.9 KB

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