From d30a286f38ebc2a84dc0c82e89247a5ab89fc7b1 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 3 Apr 2014 22:10:51 +0200 Subject: [PATCH] Command line flag and REST command to reset and resync (fixes #85) Still needs implemention in GUI --- cmd/syncthing/gui.go | 8 +++++++- cmd/syncthing/main.go | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index a9e739993..4ca6285ae 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -38,6 +38,7 @@ func startGUI(addr string, m *Model) { router.Post("/rest/config", restPostConfig) router.Post("/rest/restart", restPostRestart) + router.Post("/rest/reset", restPostReset) router.Post("/rest/error", restPostError) go func() { @@ -112,7 +113,12 @@ func restGetConfigInSync(w http.ResponseWriter) { } func restPostRestart(req *http.Request) { - restart() + go restart() +} + +func restPostReset(req *http.Request) { + resetRepositories() + go restart() } type guiFile scanner.File diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index da152295c..445dc3ac0 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -59,8 +59,10 @@ const ( ) func main() { + var reset bool var showVersion bool flag.StringVar(&confDir, "home", getDefaultConfDir(), "Set configuration directory") + flag.BoolVar(&reset, "reset", false, "Prepare to resync from cluster") flag.BoolVar(&showVersion, "version", false, "Show version") flag.Usage = usageFor(flag.CommandLine, usage, extraUsage) flag.Parse() @@ -163,6 +165,11 @@ func main() { infof("Edit %s to taste or use the GUI\n", cfgFile) } + if reset { + resetRepositories() + os.Exit(0) + } + if profiler := os.Getenv("STPROFILER"); len(profiler) > 0 { go func() { dlog.Println("Starting profiler on", profiler) @@ -260,6 +267,25 @@ func main() { select {} } +func resetRepositories() { + suffix := fmt.Sprintf(".syncthing-reset-%d", time.Now().UnixNano()) + for _, repo := range cfg.Repositories { + if _, err := os.Stat(repo.Directory); err == nil { + infof("Reset: Moving %s -> %s", repo.Directory, repo.Directory+suffix) + os.Rename(repo.Directory, repo.Directory+suffix) + } + } + + pat := filepath.Join(confDir, "*.idx.gz") + idxs, err := filepath.Glob(pat) + if err == nil { + for _, idx := range idxs { + infof("Reset: Removing %s", idx) + os.Remove(idx) + } + } +} + func restart() { infoln("Restarting") if os.Getenv("SMF_FMRI") != "" || os.Getenv("STNORESTART") != "" {