From 8eb06874077a93f9633841b871a338471598e3bb Mon Sep 17 00:00:00 2001 From: Anderson Mesquita Date: Wed, 23 Dec 2015 19:12:24 -0500 Subject: [PATCH] Update mtime of config file before upgrading (fixes #2509) This updates the modified time of the config file before archiving it during an update so that the clean up routine doesn't delete it if it's too old, preventing the user from being able to rollback after an upgrade. --- cmd/syncthing/main.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 3e59d69d..59e8fee3 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -922,16 +922,33 @@ func loadOrCreateConfig() *config.Wrapper { } if cfg.Raw().OriginalVersion != config.CurrentVersion { - // Archive previous version and save new one - archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion) - l.Infoln("Archiving a copy of old config file format at:", archivePath) - osutil.Rename(cfg.ConfigPath(), archivePath) - cfg.Save() + err = archiveAndSaveConfig(cfg) + if err != nil { + l.Fatalln("Config archive:", err) + } } return cfg } +func archiveAndSaveConfig(cfg *config.Wrapper) error { + // To prevent previous config from being cleaned up, quickly touch it too + now := time.Now() + err := os.Chtimes(cfg.ConfigPath(), now, now) + if err != nil { + return err + } + + archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion) + l.Infoln("Archiving a copy of old config file format at:", archivePath) + err = osutil.Rename(cfg.ConfigPath(), archivePath) + if err != nil { + return err + } + + return cfg.Save() +} + func startAuditing(mainService *suture.Supervisor) { auditFile := timestampedLoc(locAuditLog) fd, err := os.OpenFile(auditFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)