migrations.go 11 KB

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