config.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package config implements reading and writing of the syncthing configuration file.
  16. package config
  17. import (
  18. "encoding/xml"
  19. "fmt"
  20. "os"
  21. "reflect"
  22. "sort"
  23. "strconv"
  24. "code.google.com/p/go.crypto/bcrypt"
  25. "github.com/syncthing/syncthing/internal/events"
  26. "github.com/syncthing/syncthing/internal/logger"
  27. "github.com/syncthing/syncthing/internal/osutil"
  28. "github.com/syncthing/syncthing/internal/protocol"
  29. )
  30. var l = logger.DefaultLogger
  31. type Configuration struct {
  32. Location string `xml:"-" json:"-"`
  33. Version int `xml:"version,attr" default:"5"`
  34. Folders []FolderConfiguration `xml:"folder"`
  35. Devices []DeviceConfiguration `xml:"device"`
  36. GUI GUIConfiguration `xml:"gui"`
  37. Options OptionsConfiguration `xml:"options"`
  38. XMLName xml.Name `xml:"configuration" json:"-"`
  39. Deprecated_Repositories []FolderConfiguration `xml:"repository" json:"-"`
  40. Deprecated_Nodes []DeviceConfiguration `xml:"node" json:"-"`
  41. }
  42. type FolderConfiguration struct {
  43. ID string `xml:"id,attr"`
  44. Path string `xml:"path,attr"`
  45. Devices []FolderDeviceConfiguration `xml:"device"`
  46. ReadOnly bool `xml:"ro,attr"`
  47. RescanIntervalS int `xml:"rescanIntervalS,attr" default:"60"`
  48. IgnorePerms bool `xml:"ignorePerms,attr"`
  49. Invalid string `xml:"-"` // Set at runtime when there is an error, not saved
  50. Versioning VersioningConfiguration `xml:"versioning"`
  51. deviceIDs []protocol.DeviceID
  52. Deprecated_Directory string `xml:"directory,omitempty,attr" json:"-"`
  53. Deprecated_Nodes []FolderDeviceConfiguration `xml:"node" json:"-"`
  54. }
  55. type VersioningConfiguration struct {
  56. Type string `xml:"type,attr"`
  57. Params map[string]string
  58. }
  59. type InternalVersioningConfiguration struct {
  60. Type string `xml:"type,attr,omitempty"`
  61. Params []InternalParam `xml:"param"`
  62. }
  63. type InternalParam struct {
  64. Key string `xml:"key,attr"`
  65. Val string `xml:"val,attr"`
  66. }
  67. func (c *VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  68. var tmp InternalVersioningConfiguration
  69. tmp.Type = c.Type
  70. for k, v := range c.Params {
  71. tmp.Params = append(tmp.Params, InternalParam{k, v})
  72. }
  73. return e.EncodeElement(tmp, start)
  74. }
  75. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  76. var tmp InternalVersioningConfiguration
  77. err := d.DecodeElement(&tmp, &start)
  78. if err != nil {
  79. return err
  80. }
  81. c.Type = tmp.Type
  82. c.Params = make(map[string]string, len(tmp.Params))
  83. for _, p := range tmp.Params {
  84. c.Params[p.Key] = p.Val
  85. }
  86. return nil
  87. }
  88. func (r *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
  89. if r.deviceIDs == nil {
  90. for _, n := range r.Devices {
  91. r.deviceIDs = append(r.deviceIDs, n.DeviceID)
  92. }
  93. }
  94. return r.deviceIDs
  95. }
  96. type DeviceConfiguration struct {
  97. DeviceID protocol.DeviceID `xml:"id,attr"`
  98. Name string `xml:"name,attr,omitempty"`
  99. Addresses []string `xml:"address,omitempty"`
  100. Compression bool `xml:"compression,attr"`
  101. CertName string `xml:"certName,attr,omitempty"`
  102. Introducer bool `xml:"introducer,attr"`
  103. }
  104. type FolderDeviceConfiguration struct {
  105. DeviceID protocol.DeviceID `xml:"id,attr"`
  106. Deprecated_Name string `xml:"name,attr,omitempty" json:"-"`
  107. Deprecated_Addresses []string `xml:"address,omitempty" json:"-"`
  108. }
  109. type OptionsConfiguration struct {
  110. ListenAddress []string `xml:"listenAddress" default:"0.0.0.0:22000"`
  111. GlobalAnnServer string `xml:"globalAnnounceServer" default:"announce.syncthing.net:22026"`
  112. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" default:"true"`
  113. LocalAnnEnabled bool `xml:"localAnnounceEnabled" default:"true"`
  114. LocalAnnPort int `xml:"localAnnouncePort" default:"21025"`
  115. LocalAnnMCAddr string `xml:"localAnnounceMCAddr" default:"[ff32::5222]:21026"`
  116. MaxSendKbps int `xml:"maxSendKbps"`
  117. MaxRecvKbps int `xml:"maxRecvKbps"`
  118. ReconnectIntervalS int `xml:"reconnectionIntervalS" default:"60"`
  119. StartBrowser bool `xml:"startBrowser" default:"true"`
  120. UPnPEnabled bool `xml:"upnpEnabled" default:"true"`
  121. UPnPLease int `xml:"upnpLeaseMinutes" default:"0"`
  122. UPnPRenewal int `xml:"upnpRenewalMinutes" default:"30"`
  123. URAccepted int `xml:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
  124. RestartOnWakeup bool `xml:"restartOnWakeup" default:"true"`
  125. AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" default:"12"` // 0 for off
  126. KeepTemporariesH int `xml:"keepTemporariesH" default:"24"` // 0 for off
  127. Deprecated_RescanIntervalS int `xml:"rescanIntervalS,omitempty" json:"-"`
  128. Deprecated_UREnabled bool `xml:"urEnabled,omitempty" json:"-"`
  129. Deprecated_URDeclined bool `xml:"urDeclined,omitempty" json:"-"`
  130. Deprecated_ReadOnly bool `xml:"readOnly,omitempty" json:"-"`
  131. Deprecated_GUIEnabled bool `xml:"guiEnabled,omitempty" json:"-"`
  132. Deprecated_GUIAddress string `xml:"guiAddress,omitempty" json:"-"`
  133. }
  134. type GUIConfiguration struct {
  135. Enabled bool `xml:"enabled,attr" default:"true"`
  136. Address string `xml:"address" default:"127.0.0.1:8080"`
  137. User string `xml:"user,omitempty"`
  138. Password string `xml:"password,omitempty"`
  139. UseTLS bool `xml:"tls,attr"`
  140. APIKey string `xml:"apikey,omitempty"`
  141. }
  142. func (cfg *Configuration) DeviceMap() map[protocol.DeviceID]DeviceConfiguration {
  143. m := make(map[protocol.DeviceID]DeviceConfiguration, len(cfg.Devices))
  144. for _, n := range cfg.Devices {
  145. m[n.DeviceID] = n
  146. }
  147. return m
  148. }
  149. func (cfg *Configuration) GetDeviceConfiguration(deviceID protocol.DeviceID) *DeviceConfiguration {
  150. for i, device := range cfg.Devices {
  151. if device.DeviceID == deviceID {
  152. return &cfg.Devices[i]
  153. }
  154. }
  155. return nil
  156. }
  157. func (cfg *Configuration) GetFolderConfiguration(folderID string) *FolderConfiguration {
  158. for i, folder := range cfg.Folders {
  159. if folder.ID == folderID {
  160. return &cfg.Folders[i]
  161. }
  162. }
  163. return nil
  164. }
  165. func (cfg *Configuration) FolderMap() map[string]FolderConfiguration {
  166. m := make(map[string]FolderConfiguration, len(cfg.Folders))
  167. for _, r := range cfg.Folders {
  168. m[r.ID] = r
  169. }
  170. return m
  171. }
  172. func setDefaults(data interface{}) error {
  173. s := reflect.ValueOf(data).Elem()
  174. t := s.Type()
  175. for i := 0; i < s.NumField(); i++ {
  176. f := s.Field(i)
  177. tag := t.Field(i).Tag
  178. v := tag.Get("default")
  179. if len(v) > 0 {
  180. switch f.Interface().(type) {
  181. case string:
  182. f.SetString(v)
  183. case int:
  184. i, err := strconv.ParseInt(v, 10, 64)
  185. if err != nil {
  186. return err
  187. }
  188. f.SetInt(i)
  189. case bool:
  190. f.SetBool(v == "true")
  191. case []string:
  192. // We don't do anything with string slices here. Any default
  193. // we set will be appended to by the XML decoder, so we fill
  194. // those after decoding.
  195. default:
  196. panic(f.Type())
  197. }
  198. }
  199. }
  200. return nil
  201. }
  202. // fillNilSlices sets default value on slices that are still nil.
  203. func fillNilSlices(data interface{}) error {
  204. s := reflect.ValueOf(data).Elem()
  205. t := s.Type()
  206. for i := 0; i < s.NumField(); i++ {
  207. f := s.Field(i)
  208. tag := t.Field(i).Tag
  209. v := tag.Get("default")
  210. if len(v) > 0 {
  211. switch f.Interface().(type) {
  212. case []string:
  213. if f.IsNil() {
  214. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), 1, 1)
  215. rv.Index(0).SetString(v)
  216. f.Set(rv)
  217. }
  218. }
  219. }
  220. }
  221. return nil
  222. }
  223. func (cfg *Configuration) Save() error {
  224. fd, err := os.Create(cfg.Location + ".tmp")
  225. if err != nil {
  226. l.Warnln("Saving config:", err)
  227. return err
  228. }
  229. e := xml.NewEncoder(fd)
  230. e.Indent("", " ")
  231. err = e.Encode(cfg)
  232. if err != nil {
  233. fd.Close()
  234. return err
  235. }
  236. _, err = fd.Write([]byte("\n"))
  237. if err != nil {
  238. l.Warnln("Saving config:", err)
  239. fd.Close()
  240. return err
  241. }
  242. err = fd.Close()
  243. if err != nil {
  244. l.Warnln("Saving config:", err)
  245. return err
  246. }
  247. err = osutil.Rename(cfg.Location+".tmp", cfg.Location)
  248. if err != nil {
  249. l.Warnln("Saving config:", err)
  250. }
  251. events.Default.Log(events.ConfigSaved, cfg)
  252. return err
  253. }
  254. func uniqueStrings(ss []string) []string {
  255. var m = make(map[string]bool, len(ss))
  256. for _, s := range ss {
  257. m[s] = true
  258. }
  259. var us = make([]string, 0, len(m))
  260. for k := range m {
  261. us = append(us, k)
  262. }
  263. return us
  264. }
  265. func (cfg *Configuration) prepare(myID protocol.DeviceID) {
  266. fillNilSlices(&cfg.Options)
  267. cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
  268. // Initialize an empty slice for folders if the config has none
  269. if cfg.Folders == nil {
  270. cfg.Folders = []FolderConfiguration{}
  271. }
  272. // Check for missing, bad or duplicate folder ID:s
  273. var seenFolders = map[string]*FolderConfiguration{}
  274. var uniqueCounter int
  275. for i := range cfg.Folders {
  276. folder := &cfg.Folders[i]
  277. if len(folder.Path) == 0 {
  278. folder.Invalid = "no directory configured"
  279. continue
  280. }
  281. if folder.ID == "" {
  282. folder.ID = "default"
  283. }
  284. if seen, ok := seenFolders[folder.ID]; ok {
  285. l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
  286. seen.Invalid = "duplicate folder ID"
  287. if seen.ID == folder.ID {
  288. uniqueCounter++
  289. seen.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
  290. }
  291. folder.Invalid = "duplicate folder ID"
  292. uniqueCounter++
  293. folder.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
  294. } else {
  295. seenFolders[folder.ID] = folder
  296. }
  297. }
  298. if cfg.Options.Deprecated_URDeclined {
  299. cfg.Options.URAccepted = -1
  300. }
  301. cfg.Options.Deprecated_URDeclined = false
  302. cfg.Options.Deprecated_UREnabled = false
  303. // Upgrade to v2 configuration if appropriate
  304. if cfg.Version == 1 {
  305. convertV1V2(cfg)
  306. }
  307. // Upgrade to v3 configuration if appropriate
  308. if cfg.Version == 2 {
  309. convertV2V3(cfg)
  310. }
  311. // Upgrade to v4 configuration if appropriate
  312. if cfg.Version == 3 {
  313. convertV3V4(cfg)
  314. }
  315. // Upgrade to v5 configuration if appropriate
  316. if cfg.Version == 4 {
  317. convertV4V5(cfg)
  318. }
  319. // Hash old cleartext passwords
  320. if len(cfg.GUI.Password) > 0 && cfg.GUI.Password[0] != '$' {
  321. hash, err := bcrypt.GenerateFromPassword([]byte(cfg.GUI.Password), 0)
  322. if err != nil {
  323. l.Warnln("bcrypting password:", err)
  324. } else {
  325. cfg.GUI.Password = string(hash)
  326. }
  327. }
  328. // Build a list of available devices
  329. existingDevices := make(map[protocol.DeviceID]bool)
  330. existingDevices[myID] = true
  331. for _, device := range cfg.Devices {
  332. existingDevices[device.DeviceID] = true
  333. }
  334. // Ensure this device is present in all relevant places
  335. me := cfg.GetDeviceConfiguration(myID)
  336. if me == nil {
  337. myName, _ := os.Hostname()
  338. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  339. DeviceID: myID,
  340. Name: myName,
  341. })
  342. }
  343. sort.Sort(DeviceConfigurationList(cfg.Devices))
  344. // Ensure that any loose devices are not present in the wrong places
  345. // Ensure that there are no duplicate devices
  346. for i := range cfg.Folders {
  347. cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
  348. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  349. cfg.Folders[i].Devices = ensureNoDuplicates(cfg.Folders[i].Devices)
  350. sort.Sort(FolderDeviceConfigurationList(cfg.Folders[i].Devices))
  351. }
  352. // An empty address list is equivalent to a single "dynamic" entry
  353. for i := range cfg.Devices {
  354. n := &cfg.Devices[i]
  355. if len(n.Addresses) == 0 || len(n.Addresses) == 1 && n.Addresses[0] == "" {
  356. n.Addresses = []string{"dynamic"}
  357. }
  358. }
  359. }
  360. func New(location string, myID protocol.DeviceID) Configuration {
  361. var cfg Configuration
  362. cfg.Location = location
  363. setDefaults(&cfg)
  364. setDefaults(&cfg.Options)
  365. setDefaults(&cfg.GUI)
  366. cfg.prepare(myID)
  367. return cfg
  368. }
  369. func Load(location string, myID protocol.DeviceID) (Configuration, error) {
  370. var cfg Configuration
  371. cfg.Location = location
  372. setDefaults(&cfg)
  373. setDefaults(&cfg.Options)
  374. setDefaults(&cfg.GUI)
  375. fd, err := os.Open(location)
  376. if err != nil {
  377. return Configuration{}, err
  378. }
  379. err = xml.NewDecoder(fd).Decode(&cfg)
  380. fd.Close()
  381. cfg.prepare(myID)
  382. return cfg, err
  383. }
  384. // ChangeRequiresRestart returns true if updating the configuration requires a
  385. // complete restart.
  386. func ChangeRequiresRestart(from, to Configuration) bool {
  387. // Adding, removing or changing folders requires restart
  388. if len(from.Folders) != len(to.Folders) {
  389. return true
  390. }
  391. fromFolders := from.FolderMap()
  392. toFolders := to.FolderMap()
  393. for id := range fromFolders {
  394. if !reflect.DeepEqual(fromFolders[id], toFolders[id]) {
  395. return true
  396. }
  397. }
  398. // Removing a device requires a restart. Adding one does not. Changing
  399. // address or name does not.
  400. fromDevices := from.DeviceMap()
  401. toDevices := to.DeviceMap()
  402. for deviceID := range fromDevices {
  403. if _, ok := toDevices[deviceID]; !ok {
  404. return true
  405. }
  406. }
  407. // All of the generic options require restart
  408. if !reflect.DeepEqual(from.Options, to.Options) || !reflect.DeepEqual(from.GUI, to.GUI) {
  409. return true
  410. }
  411. return false
  412. }
  413. func convertV4V5(cfg *Configuration) {
  414. // Renamed a bunch of fields in the structs.
  415. if cfg.Deprecated_Nodes == nil {
  416. cfg.Deprecated_Nodes = []DeviceConfiguration{}
  417. }
  418. if cfg.Deprecated_Repositories == nil {
  419. cfg.Deprecated_Repositories = []FolderConfiguration{}
  420. }
  421. cfg.Devices = cfg.Deprecated_Nodes
  422. cfg.Folders = cfg.Deprecated_Repositories
  423. for i := range cfg.Folders {
  424. cfg.Folders[i].Path = cfg.Folders[i].Deprecated_Directory
  425. cfg.Folders[i].Deprecated_Directory = ""
  426. cfg.Folders[i].Devices = cfg.Folders[i].Deprecated_Nodes
  427. cfg.Folders[i].Deprecated_Nodes = nil
  428. }
  429. cfg.Deprecated_Nodes = nil
  430. cfg.Deprecated_Repositories = nil
  431. cfg.Version = 5
  432. }
  433. func convertV3V4(cfg *Configuration) {
  434. // In previous versions, rescan interval was common for each folder.
  435. // From now, it can be set independently. We have to make sure, that after upgrade
  436. // the individual rescan interval will be defined for every existing folder.
  437. for i := range cfg.Deprecated_Repositories {
  438. cfg.Deprecated_Repositories[i].RescanIntervalS = cfg.Options.Deprecated_RescanIntervalS
  439. }
  440. cfg.Options.Deprecated_RescanIntervalS = 0
  441. // In previous versions, folders held full device configurations.
  442. // Since that's the only place where device configs were in V1, we still have
  443. // to define the deprecated fields to be able to upgrade from V1 to V4.
  444. for i, folder := range cfg.Deprecated_Repositories {
  445. for j := range folder.Deprecated_Nodes {
  446. rncfg := cfg.Deprecated_Repositories[i].Deprecated_Nodes[j]
  447. rncfg.Deprecated_Name = ""
  448. rncfg.Deprecated_Addresses = nil
  449. }
  450. }
  451. cfg.Version = 4
  452. }
  453. func convertV2V3(cfg *Configuration) {
  454. // In previous versions, compression was always on. When upgrading, enable
  455. // compression on all existing new. New devices will get compression on by
  456. // default by the GUI.
  457. for i := range cfg.Deprecated_Nodes {
  458. cfg.Deprecated_Nodes[i].Compression = true
  459. }
  460. // The global discovery format and port number changed in v0.9. Having the
  461. // default announce server but old port number is guaranteed to be legacy.
  462. if cfg.Options.GlobalAnnServer == "announce.syncthing.net:22025" {
  463. cfg.Options.GlobalAnnServer = "announce.syncthing.net:22026"
  464. }
  465. cfg.Version = 3
  466. }
  467. func convertV1V2(cfg *Configuration) {
  468. // Collect the list of devices.
  469. // Replace device configs inside folders with only a reference to the
  470. // device ID. Set all folders to read only if the global read only flag is
  471. // set.
  472. var devices = map[string]FolderDeviceConfiguration{}
  473. for i, folder := range cfg.Deprecated_Repositories {
  474. cfg.Deprecated_Repositories[i].ReadOnly = cfg.Options.Deprecated_ReadOnly
  475. for j, device := range folder.Deprecated_Nodes {
  476. id := device.DeviceID.String()
  477. if _, ok := devices[id]; !ok {
  478. devices[id] = device
  479. }
  480. cfg.Deprecated_Repositories[i].Deprecated_Nodes[j] = FolderDeviceConfiguration{DeviceID: device.DeviceID}
  481. }
  482. }
  483. cfg.Options.Deprecated_ReadOnly = false
  484. // Set and sort the list of devices.
  485. for _, device := range devices {
  486. cfg.Deprecated_Nodes = append(cfg.Deprecated_Nodes, DeviceConfiguration{
  487. DeviceID: device.DeviceID,
  488. Name: device.Deprecated_Name,
  489. Addresses: device.Deprecated_Addresses,
  490. })
  491. }
  492. sort.Sort(DeviceConfigurationList(cfg.Deprecated_Nodes))
  493. // GUI
  494. cfg.GUI.Address = cfg.Options.Deprecated_GUIAddress
  495. cfg.GUI.Enabled = cfg.Options.Deprecated_GUIEnabled
  496. cfg.Options.Deprecated_GUIEnabled = false
  497. cfg.Options.Deprecated_GUIAddress = ""
  498. cfg.Version = 2
  499. }
  500. type DeviceConfigurationList []DeviceConfiguration
  501. func (l DeviceConfigurationList) Less(a, b int) bool {
  502. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  503. }
  504. func (l DeviceConfigurationList) Swap(a, b int) {
  505. l[a], l[b] = l[b], l[a]
  506. }
  507. func (l DeviceConfigurationList) Len() int {
  508. return len(l)
  509. }
  510. type FolderDeviceConfigurationList []FolderDeviceConfiguration
  511. func (l FolderDeviceConfigurationList) Less(a, b int) bool {
  512. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  513. }
  514. func (l FolderDeviceConfigurationList) Swap(a, b int) {
  515. l[a], l[b] = l[b], l[a]
  516. }
  517. func (l FolderDeviceConfigurationList) Len() int {
  518. return len(l)
  519. }
  520. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  521. for _, device := range devices {
  522. if device.DeviceID.Equals(myID) {
  523. return devices
  524. }
  525. }
  526. devices = append(devices, FolderDeviceConfiguration{
  527. DeviceID: myID,
  528. })
  529. return devices
  530. }
  531. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  532. count := len(devices)
  533. i := 0
  534. loop:
  535. for i < count {
  536. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  537. devices[i] = devices[count-1]
  538. count--
  539. continue loop
  540. }
  541. i++
  542. }
  543. return devices[0:count]
  544. }
  545. func ensureNoDuplicates(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  546. count := len(devices)
  547. i := 0
  548. seenDevices := make(map[protocol.DeviceID]bool)
  549. loop:
  550. for i < count {
  551. id := devices[i].DeviceID
  552. if _, ok := seenDevices[id]; ok {
  553. devices[i] = devices[count-1]
  554. count--
  555. continue loop
  556. }
  557. seenDevices[id] = true
  558. i++
  559. }
  560. return devices[0:count]
  561. }