From 04f05f102de0628f29b56ef4cbcdd0d95e01b74b Mon Sep 17 00:00:00 2001 From: Evgeny Kuznetsov Date: Tue, 12 Mar 2019 10:12:08 +0300 Subject: [PATCH] cmd: Add check for newer config file and an option to override it (fixes #4921) (#5597) * Add check for newer config file and override option * Expanded error message * Polish previous commits * Make it newER --- cmd/syncthing/main.go | 65 +++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 368957b6cc5..a8b9adb4b23 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -163,34 +163,35 @@ var ( ) type RuntimeOptions struct { - confDir string - resetDatabase bool - resetDeltaIdxs bool - showVersion bool - showPaths bool - showDeviceId bool - doUpgrade bool - doUpgradeCheck bool - upgradeTo string - noBrowser bool - browserOnly bool - hideConsole bool - logFile string - auditEnabled bool - auditFile string - verbose bool - paused bool - unpaused bool - guiAddress string - guiAPIKey string - generateDir string - noRestart bool - profiler string - assetDir string - cpuProfile bool - stRestarting bool - logFlags int - showHelp bool + confDir string + resetDatabase bool + resetDeltaIdxs bool + showVersion bool + showPaths bool + showDeviceId bool + doUpgrade bool + doUpgradeCheck bool + upgradeTo string + noBrowser bool + browserOnly bool + hideConsole bool + logFile string + auditEnabled bool + auditFile string + verbose bool + paused bool + unpaused bool + guiAddress string + guiAPIKey string + generateDir string + noRestart bool + profiler string + assetDir string + cpuProfile bool + stRestarting bool + logFlags int + showHelp bool + allowNewerConfig bool } func defaultRuntimeOptions() RuntimeOptions { @@ -244,6 +245,7 @@ func parseCommandLineOptions() RuntimeOptions { flag.BoolVar(&options.unpaused, "unpaused", false, "Start with all devices and folders unpaused") flag.StringVar(&options.logFile, "logfile", options.logFile, "Log file name (still always logs to stdout). Cannot be used together with -no-restart/STNORESTART environment variable.") flag.StringVar(&options.auditFile, "auditfile", options.auditFile, "Specify audit file (use \"-\" for stdout, \"--\" for stderr)") + flag.BoolVar(&options.allowNewerConfig, "allow-newer-config", false, "Allow loading newer than current config version") if runtime.GOOS == "windows" { // Allow user to hide the console window flag.BoolVar(&options.hideConsole, "no-console", false, "Hide console window") @@ -667,7 +669,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) { "myID": myID.String(), }) - cfg, err := loadConfigAtStartup() + cfg, err := loadConfigAtStartup(runtimeOptions.allowNewerConfig) if err != nil { l.Warnln("Failed to initialize config:", err) os.Exit(exitError) @@ -967,7 +969,7 @@ func loadOrDefaultConfig() (config.Wrapper, error) { return cfg, err } -func loadConfigAtStartup() (config.Wrapper, error) { +func loadConfigAtStartup(allowNewerConfig bool) (config.Wrapper, error) { cfgFile := locations.Get(locations.ConfigFile) cfg, err := config.Load(cfgFile, myID) if os.IsNotExist(err) { @@ -987,6 +989,9 @@ func loadConfigAtStartup() (config.Wrapper, error) { } if cfg.RawCopy().OriginalVersion != config.CurrentVersion { + if cfg.RawCopy().OriginalVersion > config.CurrentVersion && !allowNewerConfig { + return nil, fmt.Errorf("Config file version (%d) is newer than supported version (%d). If this is expected, use -allow-newer-config to override.", cfg.RawCopy().OriginalVersion, config.CurrentVersion) + } err = archiveAndSaveConfig(cfg) if err != nil { return nil, errors.Wrap(err, "config archive")