config.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 http://mozilla.org/MPL/2.0/.
  6. // Package config implements reading and writing of the syncthing configuration file.
  7. package config
  8. import (
  9. "encoding/xml"
  10. "fmt"
  11. "io"
  12. "math/rand"
  13. "os"
  14. "path/filepath"
  15. "reflect"
  16. "runtime"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "github.com/calmh/logger"
  21. "github.com/syncthing/protocol"
  22. "github.com/syncthing/syncthing/internal/osutil"
  23. "golang.org/x/crypto/bcrypt"
  24. )
  25. var l = logger.DefaultLogger
  26. const (
  27. OldestHandledVersion = 5
  28. CurrentVersion = 10
  29. )
  30. type Configuration struct {
  31. Version int `xml:"version,attr" json:"version"`
  32. Folders []FolderConfiguration `xml:"folder" json:"folders"`
  33. Devices []DeviceConfiguration `xml:"device" json:"devices"`
  34. GUI GUIConfiguration `xml:"gui" json:"gui"`
  35. Options OptionsConfiguration `xml:"options" json:"options"`
  36. IgnoredDevices []protocol.DeviceID `xml:"ignoredDevice" json:"ignoredDevices"`
  37. XMLName xml.Name `xml:"configuration" json:"-"`
  38. OriginalVersion int `xml:"-" json:"-"` // The version we read from disk, before any conversion
  39. }
  40. func (orig Configuration) Copy() Configuration {
  41. c := orig
  42. // Deep copy FolderConfigurations
  43. c.Folders = make([]FolderConfiguration, len(orig.Folders))
  44. for i := range c.Folders {
  45. c.Folders[i] = orig.Folders[i].Copy()
  46. }
  47. // Deep copy DeviceConfigurations
  48. c.Devices = make([]DeviceConfiguration, len(orig.Devices))
  49. for i := range c.Devices {
  50. c.Devices[i] = orig.Devices[i].Copy()
  51. }
  52. c.Options = orig.Options.Copy()
  53. // DeviceIDs are values
  54. c.IgnoredDevices = make([]protocol.DeviceID, len(orig.IgnoredDevices))
  55. copy(c.IgnoredDevices, orig.IgnoredDevices)
  56. return c
  57. }
  58. type FolderConfiguration struct {
  59. ID string `xml:"id,attr" json:"id"`
  60. RawPath string `xml:"path,attr" json:"path"`
  61. Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
  62. ReadOnly bool `xml:"ro,attr" json:"readOnly"`
  63. RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
  64. IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
  65. AutoNormalize bool `xml:"autoNormalize,attr" json:"autoNormalize"`
  66. Versioning VersioningConfiguration `xml:"versioning" json:"versioning"`
  67. LenientMtimes bool `xml:"lenientMtimes" json:"lenientMTimes"`
  68. Copiers int `xml:"copiers" json:"copiers"` // This defines how many files are handled concurrently.
  69. Pullers int `xml:"pullers" json:"pullers"` // Defines how many blocks are fetched at the same time, possibly between separate copier routines.
  70. Hashers int `xml:"hashers" json:"hashers"` // Less than one sets the value to the number of cores. These are CPU bound due to hashing.
  71. Order PullOrder `xml:"order" json:"order"`
  72. Invalid string `xml:"-" json:"invalid"` // Set at runtime when there is an error, not saved
  73. deviceIDs []protocol.DeviceID
  74. }
  75. func (orig FolderConfiguration) Copy() FolderConfiguration {
  76. c := orig
  77. c.Devices = make([]FolderDeviceConfiguration, len(orig.Devices))
  78. copy(c.Devices, orig.Devices)
  79. return c
  80. }
  81. func (f FolderConfiguration) Path() string {
  82. // This is intentionally not a pointer method, because things like
  83. // cfg.Folders["default"].Path() should be valid.
  84. // Attempt tilde expansion; leave unchanged in case of error
  85. if path, err := osutil.ExpandTilde(f.RawPath); err == nil {
  86. f.RawPath = path
  87. }
  88. // Attempt absolutification; leave unchanged in case of error
  89. if !filepath.IsAbs(f.RawPath) {
  90. // Abs() looks like a fairly expensive syscall on Windows, while
  91. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  92. // somewhat faster in the general case, hence the outer if...
  93. if path, err := filepath.Abs(f.RawPath); err == nil {
  94. f.RawPath = path
  95. }
  96. }
  97. // Attempt to enable long filename support on Windows. We may still not
  98. // have an absolute path here if the previous steps failed.
  99. if runtime.GOOS == "windows" && filepath.IsAbs(f.RawPath) && !strings.HasPrefix(f.RawPath, `\\`) {
  100. return `\\?\` + f.RawPath
  101. }
  102. return f.RawPath
  103. }
  104. func (f *FolderConfiguration) CreateMarker() error {
  105. if !f.HasMarker() {
  106. marker := filepath.Join(f.Path(), ".stfolder")
  107. fd, err := os.Create(marker)
  108. if err != nil {
  109. return err
  110. }
  111. fd.Close()
  112. osutil.HideFile(marker)
  113. }
  114. return nil
  115. }
  116. func (f *FolderConfiguration) HasMarker() bool {
  117. _, err := os.Stat(filepath.Join(f.Path(), ".stfolder"))
  118. if err != nil {
  119. return false
  120. }
  121. return true
  122. }
  123. func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
  124. if f.deviceIDs == nil {
  125. for _, n := range f.Devices {
  126. f.deviceIDs = append(f.deviceIDs, n.DeviceID)
  127. }
  128. }
  129. return f.deviceIDs
  130. }
  131. type VersioningConfiguration struct {
  132. Type string `xml:"type,attr" json:"type"`
  133. Params map[string]string `json:"params"`
  134. }
  135. type InternalVersioningConfiguration struct {
  136. Type string `xml:"type,attr,omitempty"`
  137. Params []InternalParam `xml:"param"`
  138. }
  139. type InternalParam struct {
  140. Key string `xml:"key,attr"`
  141. Val string `xml:"val,attr"`
  142. }
  143. func (c *VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  144. var tmp InternalVersioningConfiguration
  145. tmp.Type = c.Type
  146. for k, v := range c.Params {
  147. tmp.Params = append(tmp.Params, InternalParam{k, v})
  148. }
  149. return e.EncodeElement(tmp, start)
  150. }
  151. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  152. var tmp InternalVersioningConfiguration
  153. err := d.DecodeElement(&tmp, &start)
  154. if err != nil {
  155. return err
  156. }
  157. c.Type = tmp.Type
  158. c.Params = make(map[string]string, len(tmp.Params))
  159. for _, p := range tmp.Params {
  160. c.Params[p.Key] = p.Val
  161. }
  162. return nil
  163. }
  164. type DeviceConfiguration struct {
  165. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  166. Name string `xml:"name,attr,omitempty" json:"name"`
  167. Addresses []string `xml:"address,omitempty" json:"addresses"`
  168. Compression protocol.Compression `xml:"compression,attr" json:"compression"`
  169. CertName string `xml:"certName,attr,omitempty" json:"certName"`
  170. Introducer bool `xml:"introducer,attr" json:"introducer"`
  171. }
  172. func (orig DeviceConfiguration) Copy() DeviceConfiguration {
  173. c := orig
  174. c.Addresses = make([]string, len(orig.Addresses))
  175. copy(c.Addresses, orig.Addresses)
  176. return c
  177. }
  178. type FolderDeviceConfiguration struct {
  179. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  180. }
  181. type OptionsConfiguration struct {
  182. ListenAddress []string `xml:"listenAddress" json:"listenAddress" default:"0.0.0.0:22000"`
  183. GlobalAnnServers []string `xml:"globalAnnounceServer" json:"globalAnnounceServers" json:"globalAnnounceServer" default:"udp4://announce.syncthing.net:22026, udp6://announce-v6.syncthing.net:22026"`
  184. GlobalAnnEnabled bool `xml:"globalAnnounceEnabled" json:"globalAnnounceEnabled" default:"true"`
  185. LocalAnnEnabled bool `xml:"localAnnounceEnabled" json:"localAnnounceEnabled" default:"true"`
  186. LocalAnnPort int `xml:"localAnnouncePort" json:"localAnnouncePort" default:"21025"`
  187. LocalAnnMCAddr string `xml:"localAnnounceMCAddr" json:"localAnnounceMCAddr" default:"[ff32::5222]:21026"`
  188. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  189. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  190. ReconnectIntervalS int `xml:"reconnectionIntervalS" json:"reconnectionIntervalS" default:"60"`
  191. StartBrowser bool `xml:"startBrowser" json:"startBrowser" default:"true"`
  192. UPnPEnabled bool `xml:"upnpEnabled" json:"upnpEnabled" default:"true"`
  193. UPnPLeaseM int `xml:"upnpLeaseMinutes" json:"upnpLeaseMinutes" default:"0"`
  194. UPnPRenewalM int `xml:"upnpRenewalMinutes" json:"upnpRenewalMinutes" default:"30"`
  195. UPnPTimeoutS int `xml:"upnpTimeoutSeconds" json:"upnpTimeoutSeconds" default:"3"`
  196. URAccepted int `xml:"urAccepted" json:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
  197. URUniqueID string `xml:"urUniqueID" json:"urUniqueId"` // Unique ID for reporting purposes, regenerated when UR is turned on.
  198. RestartOnWakeup bool `xml:"restartOnWakeup" json:"restartOnWakeup" default:"true"`
  199. AutoUpgradeIntervalH int `xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12"` // 0 for off
  200. KeepTemporariesH int `xml:"keepTemporariesH" json:"keepTemporariesH" default:"24"` // 0 for off
  201. CacheIgnoredFiles bool `xml:"cacheIgnoredFiles" json:"cacheIgnoredFiles" default:"true"`
  202. ProgressUpdateIntervalS int `xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5"`
  203. SymlinksEnabled bool `xml:"symlinksEnabled" json:"symlinksEnabled" default:"true"`
  204. LimitBandwidthInLan bool `xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false"`
  205. }
  206. func (orig OptionsConfiguration) Copy() OptionsConfiguration {
  207. c := orig
  208. c.ListenAddress = make([]string, len(orig.ListenAddress))
  209. copy(c.ListenAddress, orig.ListenAddress)
  210. c.GlobalAnnServers = make([]string, len(orig.GlobalAnnServers))
  211. copy(c.GlobalAnnServers, orig.GlobalAnnServers)
  212. return c
  213. }
  214. type GUIConfiguration struct {
  215. Enabled bool `xml:"enabled,attr" json:"enabled" default:"true"`
  216. Address string `xml:"address" json:"address" default:"127.0.0.1:8384"`
  217. User string `xml:"user,omitempty" json:"user"`
  218. Password string `xml:"password,omitempty" json:"password"`
  219. UseTLS bool `xml:"tls,attr" json:"useTLS"`
  220. APIKey string `xml:"apikey,omitempty" json:"apiKey"`
  221. }
  222. func New(myID protocol.DeviceID) Configuration {
  223. var cfg Configuration
  224. cfg.Version = CurrentVersion
  225. cfg.OriginalVersion = CurrentVersion
  226. setDefaults(&cfg)
  227. setDefaults(&cfg.Options)
  228. setDefaults(&cfg.GUI)
  229. cfg.prepare(myID)
  230. return cfg
  231. }
  232. func ReadXML(r io.Reader, myID protocol.DeviceID) (Configuration, error) {
  233. var cfg Configuration
  234. setDefaults(&cfg)
  235. setDefaults(&cfg.Options)
  236. setDefaults(&cfg.GUI)
  237. err := xml.NewDecoder(r).Decode(&cfg)
  238. cfg.OriginalVersion = cfg.Version
  239. cfg.prepare(myID)
  240. return cfg, err
  241. }
  242. func (cfg *Configuration) WriteXML(w io.Writer) error {
  243. e := xml.NewEncoder(w)
  244. e.Indent("", " ")
  245. err := e.Encode(cfg)
  246. if err != nil {
  247. return err
  248. }
  249. _, err = w.Write([]byte("\n"))
  250. return err
  251. }
  252. func (cfg *Configuration) prepare(myID protocol.DeviceID) {
  253. fillNilSlices(&cfg.Options)
  254. // Initialize an empty slices
  255. if cfg.Folders == nil {
  256. cfg.Folders = []FolderConfiguration{}
  257. }
  258. if cfg.IgnoredDevices == nil {
  259. cfg.IgnoredDevices = []protocol.DeviceID{}
  260. }
  261. // Check for missing, bad or duplicate folder ID:s
  262. var seenFolders = map[string]*FolderConfiguration{}
  263. var uniqueCounter int
  264. for i := range cfg.Folders {
  265. folder := &cfg.Folders[i]
  266. if len(folder.RawPath) == 0 {
  267. folder.Invalid = "no directory configured"
  268. continue
  269. }
  270. // The reason it's done like this:
  271. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  272. // C:\somedir -> C:\somedir\ -> C:\somedir
  273. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  274. // This way in the tests, we get away without OS specific separators
  275. // in the test configs.
  276. folder.RawPath = filepath.Dir(folder.RawPath + string(filepath.Separator))
  277. if folder.ID == "" {
  278. folder.ID = "default"
  279. }
  280. if seen, ok := seenFolders[folder.ID]; ok {
  281. l.Warnf("Multiple folders with ID %q; disabling", folder.ID)
  282. seen.Invalid = "duplicate folder ID"
  283. if seen.ID == folder.ID {
  284. uniqueCounter++
  285. seen.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
  286. }
  287. folder.Invalid = "duplicate folder ID"
  288. uniqueCounter++
  289. folder.ID = fmt.Sprintf("%s~%d", folder.ID, uniqueCounter)
  290. } else {
  291. seenFolders[folder.ID] = folder
  292. }
  293. }
  294. if cfg.Version < OldestHandledVersion {
  295. l.Warnf("Configuration version %d is deprecated. Attempting best effort conversion, but please verify manually.", cfg.Version)
  296. }
  297. // Upgrade configuration versions as appropriate
  298. if cfg.Version <= 5 {
  299. convertV5V6(cfg)
  300. }
  301. if cfg.Version == 6 {
  302. convertV6V7(cfg)
  303. }
  304. if cfg.Version == 7 {
  305. convertV7V8(cfg)
  306. }
  307. if cfg.Version == 8 {
  308. convertV8V9(cfg)
  309. }
  310. if cfg.Version == 9 {
  311. convertV9V10(cfg)
  312. }
  313. // Hash old cleartext passwords
  314. if len(cfg.GUI.Password) > 0 && cfg.GUI.Password[0] != '$' {
  315. hash, err := bcrypt.GenerateFromPassword([]byte(cfg.GUI.Password), 0)
  316. if err != nil {
  317. l.Warnln("bcrypting password:", err)
  318. } else {
  319. cfg.GUI.Password = string(hash)
  320. }
  321. }
  322. // Build a list of available devices
  323. existingDevices := make(map[protocol.DeviceID]bool)
  324. for _, device := range cfg.Devices {
  325. existingDevices[device.DeviceID] = true
  326. }
  327. // Ensure this device is present in the config
  328. if !existingDevices[myID] {
  329. myName, _ := os.Hostname()
  330. cfg.Devices = append(cfg.Devices, DeviceConfiguration{
  331. DeviceID: myID,
  332. Name: myName,
  333. })
  334. existingDevices[myID] = true
  335. }
  336. sort.Sort(DeviceConfigurationList(cfg.Devices))
  337. // Ensure that any loose devices are not present in the wrong places
  338. // Ensure that there are no duplicate devices
  339. // Ensure that puller settings are sane
  340. for i := range cfg.Folders {
  341. cfg.Folders[i].Devices = ensureDevicePresent(cfg.Folders[i].Devices, myID)
  342. cfg.Folders[i].Devices = ensureExistingDevices(cfg.Folders[i].Devices, existingDevices)
  343. cfg.Folders[i].Devices = ensureNoDuplicates(cfg.Folders[i].Devices)
  344. if cfg.Folders[i].Copiers == 0 {
  345. cfg.Folders[i].Copiers = 1
  346. }
  347. if cfg.Folders[i].Pullers == 0 {
  348. cfg.Folders[i].Pullers = 16
  349. }
  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. // Very short reconnection intervals are annoying
  360. if cfg.Options.ReconnectIntervalS < 5 {
  361. cfg.Options.ReconnectIntervalS = 5
  362. }
  363. cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
  364. cfg.Options.GlobalAnnServers = uniqueStrings(cfg.Options.GlobalAnnServers)
  365. if cfg.GUI.APIKey == "" {
  366. cfg.GUI.APIKey = randomString(32)
  367. }
  368. }
  369. // ChangeRequiresRestart returns true if updating the configuration requires a
  370. // complete restart.
  371. func ChangeRequiresRestart(from, to Configuration) bool {
  372. // Adding, removing or changing folders requires restart
  373. if !reflect.DeepEqual(from.Folders, to.Folders) {
  374. return true
  375. }
  376. // Removing a device requres restart
  377. toDevs := make(map[protocol.DeviceID]bool, len(from.Devices))
  378. for _, dev := range to.Devices {
  379. toDevs[dev.DeviceID] = true
  380. }
  381. for _, dev := range from.Devices {
  382. if _, ok := toDevs[dev.DeviceID]; !ok {
  383. return true
  384. }
  385. }
  386. // Changing usage reporting to on or off does not require a restart.
  387. to.Options.URAccepted = from.Options.URAccepted
  388. to.Options.URUniqueID = from.Options.URUniqueID
  389. // All of the generic options require restart
  390. if !reflect.DeepEqual(from.Options, to.Options) || !reflect.DeepEqual(from.GUI, to.GUI) {
  391. return true
  392. }
  393. return false
  394. }
  395. func convertV9V10(cfg *Configuration) {
  396. // Enable auto normalization on existing folders.
  397. for i := range cfg.Folders {
  398. cfg.Folders[i].AutoNormalize = true
  399. }
  400. cfg.Version = 10
  401. }
  402. func convertV8V9(cfg *Configuration) {
  403. // Compression is interpreted and serialized differently, but no enforced
  404. // changes. Still need a new version number since the compression stuff
  405. // isn't understandable by earlier versions.
  406. cfg.Version = 9
  407. }
  408. func convertV7V8(cfg *Configuration) {
  409. // Add IPv6 announce server
  410. if len(cfg.Options.GlobalAnnServers) == 1 && cfg.Options.GlobalAnnServers[0] == "udp4://announce.syncthing.net:22026" {
  411. cfg.Options.GlobalAnnServers = append(cfg.Options.GlobalAnnServers, "udp6://announce-v6.syncthing.net:22026")
  412. }
  413. cfg.Version = 8
  414. }
  415. func convertV6V7(cfg *Configuration) {
  416. // Migrate announce server addresses to the new URL based format
  417. for i := range cfg.Options.GlobalAnnServers {
  418. cfg.Options.GlobalAnnServers[i] = "udp4://" + cfg.Options.GlobalAnnServers[i]
  419. }
  420. cfg.Version = 7
  421. }
  422. func convertV5V6(cfg *Configuration) {
  423. // Added ".stfolder" file at folder roots to identify mount issues
  424. // Doesn't affect the config itself, but uses config migrations to identify
  425. // the migration point.
  426. for _, folder := range Wrap("", *cfg).Folders() {
  427. // Best attempt, if it fails, it fails, the user will have to fix
  428. // it up manually, as the repo will not get started.
  429. folder.CreateMarker()
  430. }
  431. cfg.Version = 6
  432. }
  433. func setDefaults(data interface{}) error {
  434. s := reflect.ValueOf(data).Elem()
  435. t := s.Type()
  436. for i := 0; i < s.NumField(); i++ {
  437. f := s.Field(i)
  438. tag := t.Field(i).Tag
  439. v := tag.Get("default")
  440. if len(v) > 0 {
  441. switch f.Interface().(type) {
  442. case string:
  443. f.SetString(v)
  444. case int:
  445. i, err := strconv.ParseInt(v, 10, 64)
  446. if err != nil {
  447. return err
  448. }
  449. f.SetInt(i)
  450. case bool:
  451. f.SetBool(v == "true")
  452. case []string:
  453. // We don't do anything with string slices here. Any default
  454. // we set will be appended to by the XML decoder, so we fill
  455. // those after decoding.
  456. default:
  457. panic(f.Type())
  458. }
  459. }
  460. }
  461. return nil
  462. }
  463. // fillNilSlices sets default value on slices that are still nil.
  464. func fillNilSlices(data interface{}) error {
  465. s := reflect.ValueOf(data).Elem()
  466. t := s.Type()
  467. for i := 0; i < s.NumField(); i++ {
  468. f := s.Field(i)
  469. tag := t.Field(i).Tag
  470. v := tag.Get("default")
  471. if len(v) > 0 {
  472. switch f.Interface().(type) {
  473. case []string:
  474. if f.IsNil() {
  475. // Treat the default as a comma separated slice
  476. vs := strings.Split(v, ",")
  477. for i := range vs {
  478. vs[i] = strings.TrimSpace(vs[i])
  479. }
  480. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  481. for i, v := range vs {
  482. rv.Index(i).SetString(v)
  483. }
  484. f.Set(rv)
  485. }
  486. }
  487. }
  488. }
  489. return nil
  490. }
  491. func uniqueStrings(ss []string) []string {
  492. var m = make(map[string]bool, len(ss))
  493. for _, s := range ss {
  494. m[s] = true
  495. }
  496. var us = make([]string, 0, len(m))
  497. for k := range m {
  498. us = append(us, k)
  499. }
  500. sort.Strings(us)
  501. return us
  502. }
  503. func ensureDevicePresent(devices []FolderDeviceConfiguration, myID protocol.DeviceID) []FolderDeviceConfiguration {
  504. for _, device := range devices {
  505. if device.DeviceID.Equals(myID) {
  506. return devices
  507. }
  508. }
  509. devices = append(devices, FolderDeviceConfiguration{
  510. DeviceID: myID,
  511. })
  512. return devices
  513. }
  514. func ensureExistingDevices(devices []FolderDeviceConfiguration, existingDevices map[protocol.DeviceID]bool) []FolderDeviceConfiguration {
  515. count := len(devices)
  516. i := 0
  517. loop:
  518. for i < count {
  519. if _, ok := existingDevices[devices[i].DeviceID]; !ok {
  520. devices[i] = devices[count-1]
  521. count--
  522. continue loop
  523. }
  524. i++
  525. }
  526. return devices[0:count]
  527. }
  528. func ensureNoDuplicates(devices []FolderDeviceConfiguration) []FolderDeviceConfiguration {
  529. count := len(devices)
  530. i := 0
  531. seenDevices := make(map[protocol.DeviceID]bool)
  532. loop:
  533. for i < count {
  534. id := devices[i].DeviceID
  535. if _, ok := seenDevices[id]; ok {
  536. devices[i] = devices[count-1]
  537. count--
  538. continue loop
  539. }
  540. seenDevices[id] = true
  541. i++
  542. }
  543. return devices[0:count]
  544. }
  545. type DeviceConfigurationList []DeviceConfiguration
  546. func (l DeviceConfigurationList) Less(a, b int) bool {
  547. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  548. }
  549. func (l DeviceConfigurationList) Swap(a, b int) {
  550. l[a], l[b] = l[b], l[a]
  551. }
  552. func (l DeviceConfigurationList) Len() int {
  553. return len(l)
  554. }
  555. type FolderDeviceConfigurationList []FolderDeviceConfiguration
  556. func (l FolderDeviceConfigurationList) Less(a, b int) bool {
  557. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  558. }
  559. func (l FolderDeviceConfigurationList) Swap(a, b int) {
  560. l[a], l[b] = l[b], l[a]
  561. }
  562. func (l FolderDeviceConfigurationList) Len() int {
  563. return len(l)
  564. }
  565. // randomCharset contains the characters that can make up a randomString().
  566. const randomCharset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
  567. // randomString returns a string of random characters (taken from
  568. // randomCharset) of the specified length.
  569. func randomString(l int) string {
  570. bs := make([]byte, l)
  571. for i := range bs {
  572. bs[i] = randomCharset[rand.Intn(len(randomCharset))]
  573. }
  574. return string(bs)
  575. }
  576. type PullOrder int
  577. const (
  578. OrderRandom PullOrder = iota // default is random
  579. OrderAlphabetic
  580. OrderSmallestFirst
  581. OrderLargestFirst
  582. OrderOldestFirst
  583. OrderNewestFirst
  584. )
  585. func (o PullOrder) String() string {
  586. switch o {
  587. case OrderRandom:
  588. return "random"
  589. case OrderAlphabetic:
  590. return "alphabetic"
  591. case OrderSmallestFirst:
  592. return "smallestFirst"
  593. case OrderLargestFirst:
  594. return "largestFirst"
  595. case OrderOldestFirst:
  596. return "oldestFirst"
  597. case OrderNewestFirst:
  598. return "newestFirst"
  599. default:
  600. return "unknown"
  601. }
  602. }
  603. func (o PullOrder) MarshalText() ([]byte, error) {
  604. return []byte(o.String()), nil
  605. }
  606. func (o *PullOrder) UnmarshalText(bs []byte) error {
  607. switch string(bs) {
  608. case "random":
  609. *o = OrderRandom
  610. case "alphabetic":
  611. *o = OrderAlphabetic
  612. case "smallestFirst":
  613. *o = OrderSmallestFirst
  614. case "largestFirst":
  615. *o = OrderLargestFirst
  616. case "oldestFirst":
  617. *o = OrderOldestFirst
  618. case "newestFirst":
  619. *o = OrderNewestFirst
  620. default:
  621. *o = OrderRandom
  622. }
  623. return nil
  624. }