migrations.go 11 KB

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