migrations.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "sort"
  13. "strings"
  14. "sync"
  15. "github.com/syncthing/syncthing/lib/build"
  16. "github.com/syncthing/syncthing/lib/fs"
  17. "github.com/syncthing/syncthing/lib/netutil"
  18. "github.com/syncthing/syncthing/lib/upgrade"
  19. )
  20. // migrations is the set of config migration functions, with their target
  21. // config version. The conversion function can be nil in which case we just
  22. // update the config version. The order of migrations doesn't matter here,
  23. // put the newest on top for readability.
  24. var (
  25. migrations = migrationSet{
  26. {37, migrateToConfigV37},
  27. {36, migrateToConfigV36},
  28. {35, migrateToConfigV35},
  29. {34, migrateToConfigV34},
  30. {33, migrateToConfigV33},
  31. {32, migrateToConfigV32},
  32. {31, migrateToConfigV31},
  33. {30, migrateToConfigV30},
  34. {29, migrateToConfigV29},
  35. {28, migrateToConfigV28},
  36. {27, migrateToConfigV27},
  37. {26, nil}, // triggers database update
  38. {25, migrateToConfigV25},
  39. {24, migrateToConfigV24},
  40. {23, migrateToConfigV23},
  41. {22, migrateToConfigV22},
  42. {21, migrateToConfigV21},
  43. {20, migrateToConfigV20},
  44. {19, nil}, // Triggers a database tweak
  45. {18, migrateToConfigV18},
  46. {17, nil}, // Fsync = true removed
  47. {16, nil}, // Triggers a database tweak
  48. {15, migrateToConfigV15},
  49. {14, migrateToConfigV14},
  50. {13, migrateToConfigV13},
  51. {12, migrateToConfigV12},
  52. {11, migrateToConfigV11},
  53. }
  54. migrationsMut = sync.Mutex{}
  55. )
  56. type migrationSet []migration
  57. // apply applies all the migrations in the set, as required by the current
  58. // version and target version, in the correct order.
  59. func (ms migrationSet) apply(cfg *Configuration) {
  60. // Make sure we apply the migrations in target version order regardless
  61. // of how it was defined.
  62. sort.Slice(ms, func(a, b int) bool {
  63. return ms[a].targetVersion < ms[b].targetVersion
  64. })
  65. // Apply all migrations.
  66. for _, m := range ms {
  67. m.apply(cfg)
  68. }
  69. }
  70. // A migration is a target config version and a function to do the needful
  71. // to reach that version. The function does not need to change the actual
  72. // cfg.Version field.
  73. type migration struct {
  74. targetVersion int
  75. convert func(cfg *Configuration)
  76. }
  77. // apply applies the conversion function if the current version is below the
  78. // target version and the function is not nil, and updates the current
  79. // version.
  80. func (m migration) apply(cfg *Configuration) {
  81. if cfg.Version >= m.targetVersion {
  82. return
  83. }
  84. if m.convert != nil {
  85. m.convert(cfg)
  86. }
  87. cfg.Version = m.targetVersion
  88. }
  89. func migrateToConfigV37(cfg *Configuration) {
  90. // "scan ownership" changed name to "send ownership"
  91. for i := range cfg.Folders {
  92. cfg.Folders[i].SendOwnership = cfg.Folders[i].DeprecatedScanOwnership
  93. cfg.Folders[i].DeprecatedScanOwnership = false
  94. }
  95. }
  96. func migrateToConfigV36(cfg *Configuration) {
  97. for i := range cfg.Folders {
  98. delete(cfg.Folders[i].Versioning.Params, "cleanInterval")
  99. }
  100. }
  101. func migrateToConfigV35(cfg *Configuration) {
  102. for i, fcfg := range cfg.Folders {
  103. params := fcfg.Versioning.Params
  104. if params["fsType"] != "" {
  105. var fsType FilesystemType
  106. _ = fsType.UnmarshalText([]byte(params["fsType"]))
  107. cfg.Folders[i].Versioning.FSType = fsType
  108. }
  109. if params["versionsPath"] != "" && params["fsPath"] == "" {
  110. params["fsPath"] = params["versionsPath"]
  111. }
  112. cfg.Folders[i].Versioning.FSPath = params["fsPath"]
  113. delete(cfg.Folders[i].Versioning.Params, "fsType")
  114. delete(cfg.Folders[i].Versioning.Params, "fsPath")
  115. delete(cfg.Folders[i].Versioning.Params, "versionsPath")
  116. }
  117. }
  118. func migrateToConfigV34(cfg *Configuration) {
  119. cfg.Defaults.Folder.Path = cfg.Options.DeprecatedDefaultFolderPath
  120. cfg.Options.DeprecatedDefaultFolderPath = ""
  121. }
  122. func migrateToConfigV33(cfg *Configuration) {
  123. for i := range cfg.Devices {
  124. cfg.Devices[i].DeprecatedPendingFolders = nil
  125. }
  126. cfg.DeprecatedPendingDevices = nil
  127. }
  128. func migrateToConfigV32(cfg *Configuration) {
  129. for i := range cfg.Folders {
  130. cfg.Folders[i].JunctionsAsDirs = true
  131. }
  132. }
  133. func migrateToConfigV31(cfg *Configuration) {
  134. // Show a notification about setting User and Password
  135. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "authenticationUserAndPassword")
  136. }
  137. func migrateToConfigV30(cfg *Configuration) {
  138. // The "max concurrent scans" option is now spelled "max folder concurrency"
  139. // to be more general.
  140. cfg.Options.RawMaxFolderConcurrency = cfg.Options.DeprecatedMaxConcurrentScans
  141. cfg.Options.DeprecatedMaxConcurrentScans = 0
  142. }
  143. func migrateToConfigV29(cfg *Configuration) {
  144. // The new crash reporting option should follow the state of global
  145. // discovery / usage reporting, and we should display an appropriate
  146. // notification.
  147. if cfg.Options.GlobalAnnEnabled || cfg.Options.URAccepted > 0 {
  148. cfg.Options.CREnabled = true
  149. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "crAutoEnabled")
  150. } else {
  151. cfg.Options.CREnabled = false
  152. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "crAutoDisabled")
  153. }
  154. }
  155. func migrateToConfigV28(cfg *Configuration) {
  156. // Show a notification about enabling filesystem watching
  157. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "fsWatcherNotification")
  158. }
  159. func migrateToConfigV27(cfg *Configuration) {
  160. for i := range cfg.Folders {
  161. f := &cfg.Folders[i]
  162. if f.DeprecatedPullers != 0 {
  163. f.PullerMaxPendingKiB = 128 * f.DeprecatedPullers
  164. f.DeprecatedPullers = 0
  165. }
  166. }
  167. }
  168. func migrateToConfigV25(cfg *Configuration) {
  169. for i := range cfg.Folders {
  170. cfg.Folders[i].FSWatcherDelayS = 10
  171. }
  172. }
  173. func migrateToConfigV24(cfg *Configuration) {
  174. cfg.Options.URSeen = 2
  175. }
  176. func migrateToConfigV23(cfg *Configuration) {
  177. permBits := fs.FileMode(0o777)
  178. if build.IsWindows {
  179. // Windows has no umask so we must chose a safer set of bits to
  180. // begin with.
  181. permBits = 0o700
  182. }
  183. // Upgrade code remains hardcoded for .stfolder despite configurable
  184. // marker name in later versions.
  185. for i := range cfg.Folders {
  186. fs := cfg.Folders[i].Filesystem(nil)
  187. // Invalid config posted, or tests.
  188. if fs == nil {
  189. continue
  190. }
  191. if stat, err := fs.Stat(DefaultMarkerName); err == nil && !stat.IsDir() {
  192. err = fs.Remove(DefaultMarkerName)
  193. if err == nil {
  194. err = fs.Mkdir(DefaultMarkerName, permBits)
  195. fs.Hide(DefaultMarkerName) // ignore error
  196. }
  197. if err != nil {
  198. l.Infoln("Failed to upgrade folder marker:", err)
  199. }
  200. }
  201. }
  202. }
  203. func migrateToConfigV22(cfg *Configuration) {
  204. for i := range cfg.Folders {
  205. cfg.Folders[i].FilesystemType = FilesystemTypeBasic
  206. // Migrate to templated external versioner commands
  207. if cfg.Folders[i].Versioning.Type == "external" {
  208. cfg.Folders[i].Versioning.Params["command"] += " %FOLDER_PATH% %FILE_PATH%"
  209. }
  210. }
  211. }
  212. func migrateToConfigV21(cfg *Configuration) {
  213. for _, folder := range cfg.Folders {
  214. if folder.FilesystemType != FilesystemTypeBasic {
  215. continue
  216. }
  217. switch folder.Versioning.Type {
  218. case "simple", "trashcan":
  219. // Clean out symlinks in the known place
  220. cleanSymlinks(folder.Filesystem(nil), ".stversions")
  221. case "staggered":
  222. versionDir := folder.Versioning.Params["versionsPath"]
  223. if versionDir == "" {
  224. // default place
  225. cleanSymlinks(folder.Filesystem(nil), ".stversions")
  226. } else if filepath.IsAbs(versionDir) {
  227. // absolute
  228. cleanSymlinks(fs.NewFilesystem(fs.FilesystemTypeBasic, versionDir), ".")
  229. } else {
  230. // relative to folder
  231. cleanSymlinks(folder.Filesystem(nil), versionDir)
  232. }
  233. }
  234. }
  235. }
  236. func migrateToConfigV20(cfg *Configuration) {
  237. cfg.Options.MinHomeDiskFree = Size{Value: cfg.Options.DeprecatedMinHomeDiskFreePct, Unit: "%"}
  238. cfg.Options.DeprecatedMinHomeDiskFreePct = 0
  239. for i := range cfg.Folders {
  240. cfg.Folders[i].MinDiskFree = Size{Value: cfg.Folders[i].DeprecatedMinDiskFreePct, Unit: "%"}
  241. cfg.Folders[i].DeprecatedMinDiskFreePct = 0
  242. }
  243. }
  244. func migrateToConfigV18(cfg *Configuration) {
  245. // Do channel selection for existing users. Those who have auto upgrades
  246. // and usage reporting on default to the candidate channel. Others get
  247. // stable.
  248. if cfg.Options.URAccepted > 0 && cfg.Options.AutoUpgradeEnabled() {
  249. cfg.Options.UpgradeToPreReleases = true
  250. }
  251. // Show a notification to explain what's going on, except if upgrades
  252. // are disabled by compilation or environment variable in which case
  253. // it's not relevant.
  254. if !upgrade.DisabledByCompilation && os.Getenv("STNOUPGRADE") == "" {
  255. cfg.Options.UnackedNotificationIDs = append(cfg.Options.UnackedNotificationIDs, "channelNotification")
  256. }
  257. }
  258. func migrateToConfigV15(cfg *Configuration) {
  259. // Undo v0.13.0 broken migration
  260. for i, addr := range cfg.Options.RawGlobalAnnServers {
  261. switch addr {
  262. case "default-v4v2/":
  263. cfg.Options.RawGlobalAnnServers[i] = "default-v4"
  264. case "default-v6v2/":
  265. cfg.Options.RawGlobalAnnServers[i] = "default-v6"
  266. }
  267. }
  268. }
  269. func migrateToConfigV14(cfg *Configuration) {
  270. // Not using the ignore cache is the new default. Disable it on existing
  271. // configurations.
  272. cfg.Options.CacheIgnoredFiles = false
  273. // Migrate UPnP -> NAT options
  274. cfg.Options.NATEnabled = cfg.Options.DeprecatedUPnPEnabled
  275. cfg.Options.DeprecatedUPnPEnabled = false
  276. cfg.Options.NATLeaseM = cfg.Options.DeprecatedUPnPLeaseM
  277. cfg.Options.DeprecatedUPnPLeaseM = 0
  278. cfg.Options.NATRenewalM = cfg.Options.DeprecatedUPnPRenewalM
  279. cfg.Options.DeprecatedUPnPRenewalM = 0
  280. cfg.Options.NATTimeoutS = cfg.Options.DeprecatedUPnPTimeoutS
  281. cfg.Options.DeprecatedUPnPTimeoutS = 0
  282. // Replace the default listen address "tcp://0.0.0.0:22000" with the
  283. // string "default", but only if we also have the default relay pool
  284. // among the relay servers as this is implied by the new "default"
  285. // entry.
  286. hasDefault := false
  287. for _, raddr := range cfg.Options.DeprecatedRelayServers {
  288. if raddr == "dynamic+https://relays.syncthing.net/endpoint" {
  289. for i, addr := range cfg.Options.RawListenAddresses {
  290. if addr == "tcp://0.0.0.0:22000" {
  291. cfg.Options.RawListenAddresses[i] = "default"
  292. hasDefault = true
  293. break
  294. }
  295. }
  296. break
  297. }
  298. }
  299. // Copy relay addresses into listen addresses.
  300. for _, addr := range cfg.Options.DeprecatedRelayServers {
  301. if hasDefault && addr == "dynamic+https://relays.syncthing.net/endpoint" {
  302. // Skip the default relay address if we already have the
  303. // "default" entry in the list.
  304. continue
  305. }
  306. if addr == "" {
  307. continue
  308. }
  309. cfg.Options.RawListenAddresses = append(cfg.Options.RawListenAddresses, addr)
  310. }
  311. cfg.Options.DeprecatedRelayServers = nil
  312. // For consistency
  313. sort.Strings(cfg.Options.RawListenAddresses)
  314. var newAddrs []string
  315. for _, addr := range cfg.Options.RawGlobalAnnServers {
  316. uri, err := url.Parse(addr)
  317. if err != nil {
  318. // That's odd. Skip the broken address.
  319. continue
  320. }
  321. if uri.Scheme == "https" {
  322. uri.Path = path.Join(uri.Path, "v2") + "/"
  323. addr = uri.String()
  324. }
  325. newAddrs = append(newAddrs, addr)
  326. }
  327. cfg.Options.RawGlobalAnnServers = newAddrs
  328. for i, fcfg := range cfg.Folders {
  329. if fcfg.DeprecatedReadOnly {
  330. cfg.Folders[i].Type = FolderTypeSendOnly
  331. } else {
  332. cfg.Folders[i].Type = FolderTypeSendReceive
  333. }
  334. cfg.Folders[i].DeprecatedReadOnly = false
  335. }
  336. // v0.13-beta already had config version 13 but did not get the new URL
  337. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  338. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  339. }
  340. }
  341. func migrateToConfigV13(cfg *Configuration) {
  342. if cfg.Options.ReleasesURL == "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" {
  343. cfg.Options.ReleasesURL = "https://upgrades.syncthing.net/meta.json"
  344. }
  345. }
  346. func migrateToConfigV12(cfg *Configuration) {
  347. // Change listen address schema
  348. for i, addr := range cfg.Options.RawListenAddresses {
  349. if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") {
  350. cfg.Options.RawListenAddresses[i] = netutil.AddressURL("tcp", addr)
  351. }
  352. }
  353. for i, device := range cfg.Devices {
  354. for j, addr := range device.Addresses {
  355. if addr != "dynamic" && addr != "" {
  356. cfg.Devices[i].Addresses[j] = netutil.AddressURL("tcp", addr)
  357. }
  358. }
  359. }
  360. // Use new discovery server
  361. var newDiscoServers []string
  362. var useDefault bool
  363. for _, addr := range cfg.Options.RawGlobalAnnServers {
  364. if addr == "udp4://announce.syncthing.net:22026" {
  365. useDefault = true
  366. } else if addr == "udp6://announce-v6.syncthing.net:22026" {
  367. useDefault = true
  368. } else {
  369. newDiscoServers = append(newDiscoServers, addr)
  370. }
  371. }
  372. if useDefault {
  373. newDiscoServers = append(newDiscoServers, "default")
  374. }
  375. cfg.Options.RawGlobalAnnServers = newDiscoServers
  376. // Use new multicast group
  377. if cfg.Options.LocalAnnMCAddr == "[ff32::5222]:21026" {
  378. cfg.Options.LocalAnnMCAddr = "[ff12::8384]:21027"
  379. }
  380. // Use new local discovery port
  381. if cfg.Options.LocalAnnPort == 21025 {
  382. cfg.Options.LocalAnnPort = 21027
  383. }
  384. // Set MaxConflicts to unlimited
  385. for i := range cfg.Folders {
  386. cfg.Folders[i].MaxConflicts = -1
  387. }
  388. }
  389. func migrateToConfigV11(cfg *Configuration) {
  390. // Set minimum disk free of existing folders to 1%
  391. for i := range cfg.Folders {
  392. cfg.Folders[i].DeprecatedMinDiskFreePct = 1
  393. }
  394. }