migrations.go 10 KB

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