config.go 22 KB

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