1
0

migrations.go 12 KB

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