deviceconfiguration.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
  13. d := DeviceConfiguration{
  14. DeviceID: id,
  15. Name: name,
  16. }
  17. util.SetDefaults(&d)
  18. d.prepare(nil)
  19. return d
  20. }
  21. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  22. c := cfg
  23. c.Addresses = make([]string, len(cfg.Addresses))
  24. copy(c.Addresses, cfg.Addresses)
  25. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  26. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  27. c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
  28. copy(c.IgnoredFolders, cfg.IgnoredFolders)
  29. c.PendingFolders = make([]ObservedFolder, len(cfg.PendingFolders))
  30. copy(c.PendingFolders, cfg.PendingFolders)
  31. return c
  32. }
  33. func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
  34. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  35. cfg.Addresses = []string{"dynamic"}
  36. }
  37. if len(cfg.AllowedNetworks) == 0 {
  38. cfg.AllowedNetworks = []string{}
  39. }
  40. ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
  41. pendingFolders := deduplicateObservedFoldersToMap(cfg.PendingFolders)
  42. for id := range ignoredFolders {
  43. delete(pendingFolders, id)
  44. }
  45. for _, sharedFolder := range sharedFolders {
  46. delete(ignoredFolders, sharedFolder)
  47. delete(pendingFolders, sharedFolder)
  48. }
  49. cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
  50. cfg.PendingFolders = sortedObservedFolderSlice(pendingFolders)
  51. }
  52. func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
  53. for _, ignoredFolder := range cfg.IgnoredFolders {
  54. if ignoredFolder.ID == folder {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
  61. output := make([]ObservedFolder, 0, len(input))
  62. for _, folder := range input {
  63. output = append(output, folder)
  64. }
  65. sort.Slice(output, func(i, j int) bool {
  66. return output[i].Time.Before(output[j].Time)
  67. })
  68. return output
  69. }
  70. func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
  71. output := make(map[string]ObservedFolder, len(input))
  72. for _, folder := range input {
  73. if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
  74. output[folder.ID] = folder
  75. }
  76. }
  77. return output
  78. }