migrations.go 11 KB

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