Skip to content

Commit

Permalink
cmd: Add check for newer config file and an option to override it (fixes
Browse files Browse the repository at this point in the history
 #4921) (#5597)

* Add check for newer config file and override option

* Expanded error message

* Polish previous commits

* Make it newER
  • Loading branch information
nekr0z authored and AudriusButkevicius committed Mar 12, 2019
1 parent 289a02e commit 04f05f1
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions cmd/syncthing/main.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand Down

0 comments on commit 04f05f1

Please sign in to comment.