Repository: syncthing/syncthing
File: cmd/syncthing/main.go (Go)

Code before:
			// Set the option into the config as well, as the auto upgrade
			// loop expects to read a valid interval from there.
			cfg.SetOptions(opts)
			cfg.Save()
		}
		// We don't tweak the user's choice of upgrading to pre-releases or
		// not, as otherwise they cannot step off the candidate channel.
	}

	shouldAutoUpgrade := false

	if opts := cfg.Options(); opts.AutoUpgradeIntervalH > 0 {
		if runtimeOptions.NoUpgrade {
			l.Infof("No automatic upgrades; STNOUPGRADE environment variable defined.")
		} else {
			release, err := checkUpgrade()
			if err == nil {
				if err = performUpgradeDirect(release); err == nil {
					l.Infof("Upgraded to %q, exiting now.", release.Tag)
					os.Exit(syncthing.ExitUpgrade.AsInt())
				}
			}
			if err != upgrade.ErrUpgradeUnsupported {
				shouldAutoUpgrade = true
			} else if err != nil {
				if _, ok := err.(errNoUpgrade); !ok {
					l.Infoln("Initial automatic upgrade:", err)
				}
			}
		}
	}

	if runtimeOptions.unpaused {
		setPauseState(cfg, false)
	} else if runtimeOptions.paused {
		setPauseState(cfg, true)
	}

	dbFile := locations.Get(locations.Database)
	ldb, err := syncthing.OpenDBBackend(dbFile, cfg.Options().DatabaseTuning)
	if err != nil {
		l.Warnln("Error opening database:", err)
		os.Exit(1)
	}

	appOpts := runtimeOptions.Options
	if runtimeOptions.auditEnabled {
		appOpts.AuditWriter = auditWriter(runtimeOptions.auditFile)
	}
	if t := os.Getenv("STDEADLOCKTIMEOUT"); t != "" {
		secs, _ := strconv.Atoi(t)

Review comment (suggestion):
This branch will only be taken if err == ErrUpgradeUnsupported, or my double negation logic parser is bad (in which case this also needs simplification I think, as I've stared at these if clauses now for like half a minute)

Code after:
		// not, as otherwise they cannot step off the candidate channel.
	}

	// Check if auto-upgrades should be done and if yes, do an initial
	// upgrade immedately. The auto-upgrade routine can only be started
	// later after App is initialised.

	shouldAutoUpgrade := shouldUpgrade(cfg, runtimeOptions)
	if shouldAutoUpgrade {
		// Try to do upgrade directly
		release, err := checkUpgrade()
		if err == nil {
			if err = performUpgradeDirect(release); err == nil {
				l.Infof("Upgraded to %q, exiting now.", release.Tag)
				os.Exit(syncthing.ExitUpgrade.AsInt())
			}
		}
		// Log the error if relevant.
		if err != nil {
			if _, ok := err.(errNoUpgrade); !ok {
				l.Infoln("Initial automatic upgrade:", err)
			}
		}
	}

	if runtimeOptions.unpaused {
		setPauseState(cfg, false)
	} else if runtimeOptions.paused {
		setPauseState(cfg, true)
	}

	dbFile := locations.Get(locations.Database)
	ldb, err := syncthing.OpenDBBackend(dbFile, cfg.Options().DatabaseTuning)
	if err != nil {
		l.Warnln("Error opening database:", err)
		os.Exit(1)
	}

	appOpts := runtimeOptions.Options
	if runtimeOptions.auditEnabled {
		appOpts.AuditWriter = auditWriter(runtimeOptions.auditFile)
	}
	if t := os.Getenv("STDEADLOCKTIMEOUT"); t != "" {
		secs, _ := strconv.Atoi(t)
		appOpts.DeadlockTimeoutS = secs
	}

	app := syncthing.New(cfg, ldb, evLogger, cert, appOpts)

	if shouldAutoUpgrade {
		go autoUpgrade(cfg, app, evLogger)