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
This commit is contained in:
Evgeny Kuznetsov 2019-03-12 10:12:08 +03:00 committed by Audrius Butkevicius
parent 289a02e994
commit 04f05f102d
1 changed files with 35 additions and 30 deletions

View File

@ -191,6 +191,7 @@ type RuntimeOptions struct {
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")