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

@ -163,34 +163,35 @@ var (
) )
type RuntimeOptions struct { type RuntimeOptions struct {
confDir string confDir string
resetDatabase bool resetDatabase bool
resetDeltaIdxs bool resetDeltaIdxs bool
showVersion bool showVersion bool
showPaths bool showPaths bool
showDeviceId bool showDeviceId bool
doUpgrade bool doUpgrade bool
doUpgradeCheck bool doUpgradeCheck bool
upgradeTo string upgradeTo string
noBrowser bool noBrowser bool
browserOnly bool browserOnly bool
hideConsole bool hideConsole bool
logFile string logFile string
auditEnabled bool auditEnabled bool
auditFile string auditFile string
verbose bool verbose bool
paused bool paused bool
unpaused bool unpaused bool
guiAddress string guiAddress string
guiAPIKey string guiAPIKey string
generateDir string generateDir string
noRestart bool noRestart bool
profiler string profiler string
assetDir string assetDir string
cpuProfile bool cpuProfile bool
stRestarting bool stRestarting bool
logFlags int logFlags int
showHelp bool showHelp bool
allowNewerConfig bool
} }
func defaultRuntimeOptions() RuntimeOptions { func defaultRuntimeOptions() RuntimeOptions {
@ -244,6 +245,7 @@ func parseCommandLineOptions() RuntimeOptions {
flag.BoolVar(&options.unpaused, "unpaused", false, "Start with all devices and folders unpaused") 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.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.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" { if runtime.GOOS == "windows" {
// Allow user to hide the console window // Allow user to hide the console window
flag.BoolVar(&options.hideConsole, "no-console", false, "Hide console window") flag.BoolVar(&options.hideConsole, "no-console", false, "Hide console window")
@ -667,7 +669,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
"myID": myID.String(), "myID": myID.String(),
}) })
cfg, err := loadConfigAtStartup() cfg, err := loadConfigAtStartup(runtimeOptions.allowNewerConfig)
if err != nil { if err != nil {
l.Warnln("Failed to initialize config:", err) l.Warnln("Failed to initialize config:", err)
os.Exit(exitError) os.Exit(exitError)
@ -967,7 +969,7 @@ func loadOrDefaultConfig() (config.Wrapper, error) {
return cfg, err return cfg, err
} }
func loadConfigAtStartup() (config.Wrapper, error) { func loadConfigAtStartup(allowNewerConfig bool) (config.Wrapper, error) {
cfgFile := locations.Get(locations.ConfigFile) cfgFile := locations.Get(locations.ConfigFile)
cfg, err := config.Load(cfgFile, myID) cfg, err := config.Load(cfgFile, myID)
if os.IsNotExist(err) { 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 {
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) err = archiveAndSaveConfig(cfg)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "config archive") return nil, errors.Wrap(err, "config archive")