migrations.go 12 KB

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