config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 implements reading and writing of the syncthing configuration file.
  7. package config
  8. import (
  9. "encoding/json"
  10. "encoding/xml"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "net"
  15. "net/url"
  16. "os"
  17. "path"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. "github.com/syncthing/syncthing/lib/protocol"
  22. "github.com/syncthing/syncthing/lib/rand"
  23. "github.com/syncthing/syncthing/lib/upgrade"
  24. "github.com/syncthing/syncthing/lib/util"
  25. )
  26. const (
  27. OldestHandledVersion = 10
  28. CurrentVersion = 20
  29. MaxRescanIntervalS = 365 * 24 * 60 * 60
  30. )
  31. var (
  32. // DefaultTCPPort defines default TCP port used if the URI does not specify one, for example tcp://0.0.0.0
  33. DefaultTCPPort = 22000
  34. // DefaultKCPPort defines default KCP (UDP) port used if the URI does not specify one, for example kcp://0.0.0.0
  35. DefaultKCPPort = 22020
  36. // DefaultListenAddresses should be substituted when the configuration
  37. // contains <listenAddress>default</listenAddress>. This is done by the
  38. // "consumer" of the configuration as we don't want these saved to the
  39. // config.
  40. DefaultListenAddresses = []string{
  41. util.Address("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultTCPPort))),
  42. "dynamic+https://relays.syncthing.net/endpoint",
  43. }
  44. // DefaultKCPListenAddress gets added to the default listen address set
  45. // when the appropriate feature flag is set. Feature flag stuff to be
  46. // removed later.
  47. DefaultKCPListenAddress = util.Address("kcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(DefaultKCPPort)))
  48. // DefaultDiscoveryServersV4 should be substituted when the configuration
  49. // contains <globalAnnounceServer>default-v4</globalAnnounceServer>.
  50. DefaultDiscoveryServersV4 = []string{
  51. "https://discovery-v4-2.syncthing.net/v2/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 45.55.230.38, USA
  52. "https://discovery-v4-3.syncthing.net/v2/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 128.199.95.124, Singapore
  53. "https://discovery-v4-4.syncthing.net/v2/?id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW", // 95.85.19.244, NL
  54. }
  55. // DefaultDiscoveryServersV6 should be substituted when the configuration
  56. // contains <globalAnnounceServer>default-v6</globalAnnounceServer>.
  57. DefaultDiscoveryServersV6 = []string{
  58. "https://discovery-v6-2.syncthing.net/v2/?id=DVU36WY-H3LVZHW-E6LLFRE-YAFN5EL-HILWRYP-OC2M47J-Z4PE62Y-ADIBDQC", // 2604:a880:800:10::182:a001, USA
  59. "https://discovery-v6-3.syncthing.net/v2/?id=VK6HNJ3-VVMM66S-HRVWSCR-IXEHL2H-U4AQ4MW-UCPQBWX-J2L2UBK-NVZRDQZ", // 2400:6180:0:d0::d9:d001, Singapore
  60. "https://discovery-v6-4.syncthing.net/v2/?id=LYXKCHX-VI3NYZR-ALCJBHF-WMZYSPK-QG6QJA3-MPFYMSO-U56GTUK-NA2MIAW", // 2a03:b0c0:0:1010::4ed:3001, NL
  61. }
  62. // DefaultDiscoveryServers should be substituted when the configuration
  63. // contains <globalAnnounceServer>default</globalAnnounceServer>.
  64. DefaultDiscoveryServers = append(DefaultDiscoveryServersV4, DefaultDiscoveryServersV6...)
  65. // DefaultStunServers should be substituted when the configuration
  66. // contains <stunServer>default</stunServer>.
  67. DefaultStunServers = []string{
  68. "stun.callwithus.com:3478",
  69. "stun.counterpath.com:3478",
  70. "stun.counterpath.net:3478",
  71. "stun.ekiga.net:3478",
  72. "stun.ideasip.com:3478",
  73. "stun.internetcalls.com:3478",
  74. "stun.schlund.de:3478",
  75. "stun.sipgate.net:10000",
  76. "stun.sipgate.net:3478",
  77. "stun.voip.aebc.com:3478",
  78. "stun.voiparound.com:3478",
  79. "stun.voipbuster.com:3478",
  80. "stun.voipstunt.com:3478",
  81. "stun.voxgratia.org:3478",
  82. "stun.xten.com:3478",
  83. }
  84. // DefaultTheme is the default and fallback theme for the web UI.
  85. DefaultTheme = "default"
  86. )
  87. func New(myID protocol.DeviceID) Configuration {
  88. var cfg Configuration
  89. cfg.Version = CurrentVersion
  90. cfg.OriginalVersion = CurrentVersion
  91. util.SetDefaults(&cfg)
  92. util.SetDefaults(&cfg.Options)
  93. util.SetDefaults(&cfg.GUI)
  94. // Can't happen.
  95. if err := cfg.prepare(myID); err != nil {
  96. panic("bug: error in preparing new folder: " + err.Error())
  97. }
  98. return cfg
  99. }
  100. func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  101. var cfg Configuration
  102. util.SetDefaults(&cfg)
  103. util.SetDefaults(&cfg.Options)
  104. util.SetDefaults(&cfg.GUI)
  105. if err := xml.NewDecoder(r).Decode(&cfg); err != nil {
  106. return Configuration{}, err
  107. }
  108. cfg.OriginalVersion = cfg.Version
  109. if err := cfg.prepare(myID); err != nil {
  110. return Configuration{}, err
  111. }
  112. return cfg, nil
  113. }
  114. func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  115. var cfg Configuration
  116. util.SetDefaults(&cfg)
  117. util.SetDefaults(&cfg.Options)
  118. util.SetDefaults(&cfg.GUI)
  119. bs, err := ioutil.ReadAll(r)
  120. if err != nil {
  121. return Configuration{}, err
  122. }
  123. if err := json.Unmarshal(bs, &cfg); err != nil {
  124. return Configuration{}, err
  125. }
  126. cfg.OriginalVersion = cfg.Version
  127. if err := cfg.prepare(myID); err != nil {
  128. return Configuration{}, err
  129. }
  130. return cfg, nil
  131. }
  132. type Configuration struct {
  133. Version int `xml:"version,attr" json:"version"`
  134. Folders []FolderConfiguration `xml:"folder" json:"folders"`
  135. Devices []DeviceConfiguration `xml:"device" json:"devices"`
  136. GUI GUIConfiguration `xml:"gui" json:"gui"`
  137. Options OptionsConfiguration `xml:"options" json:"options"`
  138. IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"`
  139. XMLName xml.Name `xml:"configuration" json:"-"`
  140. MyID protocol.DeviceID `xml:"-" json:"-"` // Provided by the instantiator.
  141. OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion
  142. }
  143. func (cfg Configuration) Copy() Configuration {
  144. newCfg := cfg
  145. // Deep copy FolderConfigurations
  146. newCfg.Folders = make([]FolderConfiguration, len(cfg.Folders))
  147. for i := range newCfg.Folders {
  148. newCfg.Folders[i] = cfg.Folders[i].Copy()
  149. }
  150. // Deep copy DeviceConfigurations
  151. newCfg.Devices = make([]DeviceConfiguration, len(cfg.Devices))
  152. for i := range newCfg.Devices {
  153. newCfg.Devices[i] = cfg.Devices[i].Copy()
  154. }
  155. newCfg.Options = cfg.Options.Copy()
  156. // DeviceIDs are values
  157. newCfg.IgnoredDevices = make([]protocol.DeviceID, len(cfg.IgnoredDevices))
  158. copy(newCfg.IgnoredDevices, cfg.IgnoredDevices)
  159. return newCfg
  160. }
  161. func (cfg *Configuration) WriteXML(w io.Writer) error {
  162. e := xml.NewEncoder(w)
  163. e.Indent("", " ")
  164. err := e.Encode(cfg)
  165. if err != nil {
  166. return err
  167. }
  168. _, err = w.Write([]byte("\n"))
  169. return err
  170. }
  171. func (cfg *Configuration) prepare(myID protocol.DeviceID) error {
  172. var myName string
  173. cfg.MyID = myID
  174. // Ensure this device is present in the config
  175. for _, device := range cfg.Devices {
  176. if device.DeviceID == myID {
  177. goto found
  178. }
  179. }
  180. myName, _ = os.Hostname()
  181. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  182. DeviceID: myID,
  183. Name: myName,
  184. })
  185. found:
  186. if err := cfg.clean(); err != nil {
  187. return err
  188. }
  189. // Ensure that we are part of the devices
  190. for i := range cfg.Folders {
  191. cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
  192. }
  193. return nil
  194. }
  195. func (cfg *Configuration) clean() error {
  196. util.FillNilSlices(&cfg.Options)
  197. // Initialize any empty slices
  198. if cfg.Folders == nil {
  199. cfg.Folders = []FolderConfiguration{}
  200. }
  201. if cfg.IgnoredDevices == nil {
  202. cfg.IgnoredDevices = []protocol.DeviceID{}
  203. }
  204. if cfg.Options.AlwaysLocalNets == nil {
  205. cfg.Options.AlwaysLocalNets = []string{}
  206. }
  207. if cfg.Options.UnackedNotificationIDs == nil {
  208. cfg.Options.UnackedNotificationIDs = []string{}
  209. }
  210. // Prepare folders and check for duplicates. Duplicates are bad and
  211. // dangerous, can't currently be resolved in the GUI, and shouldn't
  212. // happen when configured by the GUI. We return with an error in that
  213. // situation.
  214. seenFolders := make(map[string]struct{})
  215. for i := range cfg.Folders {
  216. folder := &cfg.Folders[i]
  217. folder.prepare()
  218. if _, ok := seenFolders[folder.ID]; ok {
  219. return fmt.Errorf("duplicate folder ID %q in configuration", folder.ID)
  220. }
  221. seenFolders[folder.ID] = struct{}{}
  222. }
  223. cfg.Options.ListenAddresses = util.UniqueStrings(cfg.Options.ListenAddresses)
  224. cfg.Options.GlobalAnnServers = util.UniqueStrings(cfg.Options.GlobalAnnServers)
  225. if cfg.Version > 0 && cfg.Version < OldestHandledVersion {
  226. l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
  227. }
  228. // Upgrade configuration versions as appropriate
  229. if cfg.Version <= 10 {
  230. convertV10V11(cfg)
  231. }
  232. if cfg.Version == 11 {
  233. convertV11V12(cfg)
  234. }
  235. if cfg.Version == 12 {
  236. convertV12V13(cfg)
  237. }
  238. if cfg.Version == 13 {
  239. convertV13V14(cfg)
  240. }
  241. if cfg.Version == 14 {
  242. convertV14V15(cfg)
  243. }
  244. if cfg.Version == 15 {
  245. convertV15V16(cfg)
  246. }
  247. if cfg.Version == 16 {
  248. convertV16V17(cfg)
  249. }
  250. if cfg.Version == 17 {
  251. convertV17V18(cfg)
  252. }
  253. if cfg.Version == 18 {
  254. convertV18V19(cfg)
  255. }
  256. if cfg.Version == 19 {
  257. convertV19V20(cfg)
  258. }
  259. // Build a list of available devices
  260. existingDevices := make(map[protocol.DeviceID]bool)
  261. for _, device := range cfg.Devices {
  262. existingDevices[device.DeviceID] = true
  263. }
  264. // Ensure that the device list is free from duplicates
  265. cfg.Devices = ensureNoDuplicateDevices(cfg.Devices)
  266. sort.Sort(DeviceConfigurationList(cfg.Devices))
  267. // Ensure that any loose devices are not present in the wrong places
  268. // Ensure that there are no duplicate devices
  269. // Ensure that the versioning configuration parameter map is not nil
  270. for i := range cfg.Folders {
  271. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  272. cfg.Folders[i].Devices = ensureNoDuplicateFolderDevices(cfg.Folders[i].Devices)
  273. if cfg.Folders[i].Versioning.Params == nil {
  274. cfg.Folders[i].Versioning.Params = map[string]string{}
  275. }
  276. sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
  277. }
  278. for i := range cfg.Devices {
  279. cfg.Devices[i].prepare()
  280. }
  281. // Very short reconnection intervals are annoying
  282. if cfg.Options.ReconnectIntervalS < 5 {
  283. cfg.Options.ReconnectIntervalS = 5
  284. }
  285. if cfg.GUI.APIKey == "" {
  286. cfg.GUI.APIKey = rand.String(32)
  287. }
  288. // The list of ignored devices should not contain any devices that have
  289. // been manually added to the config.
  290. newIgnoredDevices := []protocol.DeviceID{}
  291. for _, dev := range cfg.IgnoredDevices {
  292. if !existingDevices[dev] {
  293. newIgnoredDevices = append(newIgnoredDevices, dev)
  294. }
  295. }
  296. cfg.IgnoredDevices = newIgnoredDevices
  297. return nil
  298. }
  299. func convertV19V20(cfg *Configuration) {
  300. cfg.Options.MinHomeDiskFree = Size{Value: cfg.Options.DeprecatedMinHomeDiskFreePct, Unit: "%"}
  301. cfg.Options.DeprecatedMinHomeDiskFreePct = 0
  302. for i := range cfg.Folders {
  303. cfg.Folders[i].MinDiskFree = Size{Value: cfg.Folders[i].DeprecatedMinDiskFreePct, Unit: "%"}
  304. cfg.Folders[i].DeprecatedMinDiskFreePct = 0
  305. }
  306. cfg.Version = 20
  307. }
  308. func convertV18V19(cfg *Configuration) {
  309. // Triggers a database tweak
  310. cfg.Version = 19
  311. }
  312. func convertV17V18(cfg *Configuration) {
  313. // Do channel selection for existing users. Those who have auto upgrades
  314. // and usage reporting on default to the candidate channel. Others get
  315. // stable.
  316. if cfg.Options.URAccepted > 0 && cfg.Options.AutoUpgradeIntervalH > 0 {
  317. cfg.Options.UpgradeToPreReleases = true
  318. }
  319. // Show a notification to explain what's going on, except if upgrades
  320. // are disabled by compilation or environment variable in which case
  321. // it's not relevant.
  322. if !upgrade.DisabledByCompilation && os.Getenv("STNOUPGRADE") == "" {
  323. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "channelNotification")
  324. }
  325. cfg.Version = 18
  326. }
  327. func convertV16V17(cfg *Configuration) {
  328. for i := range cfg.Folders {
  329. cfg.Folders[i].Fsync = true
  330. }
  331. cfg.Version = 17
  332. }
  333. func convertV15V16(cfg *Configuration) {
  334. // Triggers a database tweak
  335. cfg.Version = 16
  336. }
  337. func convertV14V15(cfg *Configuration) {
  338. // Undo v0.13.0 broken migration
  339. for i, addr := range cfg.Options.GlobalAnnServers {
  340. switch addr {
  341. case "default-v4v2/":
  342. cfg.Options.GlobalAnnServers[i] = "default-v4"
  343. case "default-v6v2/":
  344. cfg.Options.GlobalAnnServers[i] = "default-v6"
  345. }
  346. }
  347. cfg.Version = 15
  348. }
  349. func convertV13V14(cfg *Configuration) {
  350. // Not using the ignore cache is the new default. Disable it on existing
  351. // configurations.
  352. cfg.Options.CacheIgnoredFiles = false
  353. // Migrate UPnP -> NAT options
  354. cfg.Options.NATEnabled = cfg.Options.DeprecatedUPnPEnabled
  355. cfg.Options.DeprecatedUPnPEnabled = false
  356. cfg.Options.NATLeaseM = cfg.Options.DeprecatedUPnPLeaseM
  357. cfg.Options.DeprecatedUPnPLeaseM = 0
  358. cfg.Options.NATRenewalM = cfg.Options.DeprecatedUPnPRenewalM
  359. cfg.Options.DeprecatedUPnPRenewalM = 0
  360. cfg.Options.NATTimeoutS = cfg.Options.DeprecatedUPnPTimeoutS
  361. cfg.Options.DeprecatedUPnPTimeoutS = 0
  362. // Replace the default listen address "tcp://0.0.0.0:22000" with the
  363. // string "default", but only if we also have the default relay pool
  364. // among the relay servers as this is implied by the new "default"
  365. // entry.
  366. hasDefault := false
  367. for _, raddr := range cfg.Options.DeprecatedRelayServers {
  368. if raddr == "dynamic+https://relays.syncthing.net/endpoint" {
  369. for i, addr := range cfg.Options.ListenAddresses {
  370. if addr == "tcp://0.0.0.0:22000" {
  371. cfg.Options.ListenAddresses[i] = "default"
  372. hasDefault = true
  373. break
  374. }
  375. }
  376. break
  377. }
  378. }
  379. // Copy relay addresses into listen addresses.
  380. for _, addr := range cfg.Options.DeprecatedRelayServers {
  381. if hasDefault && addr == "dynamic+https://relays.syncthing.net/endpoint" {
  382. // Skip the default relay address if we already have the
  383. // "default" entry in the list.
  384. continue
  385. }
  386. if addr == "" {
  387. continue
  388. }
  389. cfg.Options.ListenAddresses = append(cfg.Options.ListenAddresses, addr)
  390. }
  391. cfg.Options.DeprecatedRelayServers = nil
  392. // For consistency
  393. sort.Strings(cfg.Options.ListenAddresses)
  394. var newAddrs []string
  395. for _, addr := range cfg.Options.GlobalAnnServers {
  396. uri, err := url.Parse(addr)
  397. if err != nil {
  398. // That's odd. Skip the broken address.
  399. continue
  400. }
  401. if uri.Scheme == "https" {
  402. uri.Path = path.Join(uri.Path, "v2") + "/"
  403. addr = uri.String()
  404. }
  405. newAddrs = append(newAddrs, addr)
  406. }
  407. cfg.Options.GlobalAnnServers = newAddrs
  408. for i, fcfg := range cfg.Folders {
  409. if fcfg.DeprecatedReadOnly {
  410. cfg.Folders[i].Type = FolderTypeSendOnly
  411. } else {
  412. cfg.Folders[i].Type = FolderTypeSendReceive
  413. }
  414. cfg.Folders[i].DeprecatedReadOnly = false
  415. }
  416. // v0.13-beta already had config version 13 but did not get the new URL
  417. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  418. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  419. }
  420. cfg.Version = 14
  421. }
  422. func convertV12V13(cfg *Configuration) {
  423. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  424. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  425. }
  426. cfg.Version = 13
  427. }
  428. func convertV11V12(cfg *Configuration) {
  429. // Change listen address schema
  430. for i, addr := range cfg.Options.ListenAddresses {
  431. if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
  432. cfg.Options.ListenAddresses[i] = util.Address("tcp", addr)
  433. }
  434. }
  435. for i, device := range cfg.Devices {
  436. for j, addr := range device.Addresses {
  437. if addr != "dynamic" && addr != "" {
  438. cfg.Devices[i].Addresses[j] = util.Address("tcp", addr)
  439. }
  440. }
  441. }
  442. // Use new discovery server
  443. var newDiscoServers []string
  444. var useDefault bool
  445. for _, addr := range cfg.Options.GlobalAnnServers {
  446. if addr == "udp4://announce.syncthing.net:22026" {
  447. useDefault = true
  448. } else if addr == "udp6://announce-v6.syncthing.net:22026" {
  449. useDefault = true
  450. } else {
  451. newDiscoServers = append(newDiscoServers, addr)
  452. }
  453. }
  454. if useDefault {
  455. newDiscoServers = append(newDiscoServers, "default")
  456. }
  457. cfg.Options.GlobalAnnServers = newDiscoServers
  458. // Use new multicast group
  459. if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
  460. cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
  461. }
  462. // Use new local discovery port
  463. if cfg.Options.LocalAnnPort == 21025 {
  464. cfg.Options.LocalAnnPort = 21027
  465. }
  466. // Set MaxConflicts to unlimited
  467. for i := range cfg.Folders {
  468. cfg.Folders[i].MaxConflicts = -1
  469. }
  470. cfg.Version = 12
  471. }
  472. func convertV10V11(cfg *Configuration) {
  473. // Set minimum disk free of existing folders to 1%
  474. for i := range cfg.Folders {
  475. cfg.Folders[i].DeprecatedMinDiskFreePct = 1
  476. }
  477. cfg.Version = 11
  478. }
  479. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  480. for _, device := range devices {
  481. if device.DeviceID.Equals(myID) {
  482. return devices
  483. }
  484. }
  485. devices = append(devices, FolderDeviceConfiguration{
  486. DeviceID: myID,
  487. })
  488. return devices
  489. }
  490. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  491. count := len(devices)
  492. i := 0
  493. loop:
  494. for i < count {
  495. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  496. devices[i] = devices[count-1]
  497. count--
  498. continue loop
  499. }
  500. i++
  501. }
  502. return devices[0:count]
  503. }
  504. func ensureNoDuplicateFolderDevices(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  505. count := len(devices)
  506. i := 0
  507. seenDevices := make(map[protocol.DeviceID]bool)
  508. loop:
  509. for i < count {
  510. id := devices[i].DeviceID
  511. if _, ok := seenDevices[id]; ok {
  512. devices[i] = devices[count-1]
  513. count--
  514. continue loop
  515. }
  516. seenDevices[id] = true
  517. i++
  518. }
  519. return devices[0:count]
  520. }
  521. func ensureNoDuplicateDevices(devices []DeviceConfiguration) []DeviceConfiguration {
  522. count := len(devices)
  523. i := 0
  524. seenDevices := make(map[protocol.DeviceID]bool)
  525. loop:
  526. for i < count {
  527. id := devices[i].DeviceID
  528. if _, ok := seenDevices[id]; ok {
  529. devices[i] = devices[count-1]
  530. count--
  531. continue loop
  532. }
  533. seenDevices[id] = true
  534. i++
  535. }
  536. return devices[0:count]
  537. }