migrations.go 13 KB

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