deviceconfiguration.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. )
  10. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  11. c := cfg
  12. c.Addresses = make([]string, len(cfg.Addresses))
  13. copy(c.Addresses, cfg.Addresses)
  14. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  15. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  16. c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
  17. copy(c.IgnoredFolders, cfg.IgnoredFolders)
  18. return c
  19. }
  20. func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
  21. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  22. cfg.Addresses = []string{"dynamic"}
  23. }
  24. ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
  25. for _, sharedFolder := range sharedFolders {
  26. delete(ignoredFolders, sharedFolder)
  27. }
  28. cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
  29. }
  30. func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
  31. for _, ignoredFolder := range cfg.IgnoredFolders {
  32. if ignoredFolder.ID == folder {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
  39. output := make([]ObservedFolder, 0, len(input))
  40. for _, folder := range input {
  41. output = append(output, folder)
  42. }
  43. sort.Slice(output, func(i, j int) bool {
  44. return output[i].Time.Before(output[j].Time)
  45. })
  46. return output
  47. }
  48. func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
  49. output := make(map[string]ObservedFolder, len(input))
  50. for _, folder := range input {
  51. if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
  52. output[folder.ID] = folder
  53. }
  54. }
  55. return output
  56. }