migrations.go 11 KB

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