migrations.go 13 KB

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