From 7990ffcc6099509e1fc34c53c343d7a075af4e71 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sat, 3 Sep 2016 21:29:32 +0000 Subject: [PATCH] cmd/syncthing: Copy config on upgrade, instead of renaming (fixes #3525) GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3560 --- cmd/syncthing/main.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index d3cded6ff..d40d4078c 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -889,19 +889,32 @@ func loadOrCreateConfig() *config.Wrapper { } func archiveAndSaveConfig(cfg *config.Wrapper) error { - // To prevent previous config from being cleaned up, quickly touch it too - now := time.Now() - _ = os.Chtimes(cfg.ConfigPath(), now, now) // May return error on Android etc; no worries - + // Copy the existing config to an archive copy archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion) l.Infoln("Archiving a copy of old config file format at:", archivePath) - if err := osutil.Rename(cfg.ConfigPath(), archivePath); err != nil { + if err := copyFile(cfg.ConfigPath(), archivePath); err != nil { return err } + // Do a regular atomic config sve return cfg.Save() } +func copyFile(src, dst string) error { + bs, err := ioutil.ReadFile(src) + if err != nil { + return err + } + + if err := ioutil.WriteFile(dst, bs, 0600); err != nil { + // Attempt to clean up + os.Remove(dst) + return err + } + + return nil +} + func startAuditing(mainService *suture.Supervisor) { auditFile := timestampedLoc(locAuditLog) fd, err := os.OpenFile(auditFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)