diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index a5ababd17..675881953 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -570,26 +570,26 @@ func (s *apiSvc) postSystemRestart(w http.ResponseWriter, r *http.Request) { func (s *apiSvc) postSystemReset(w http.ResponseWriter, r *http.Request) { var qs = r.URL.Query() folder := qs.Get("folder") - var err error - if len(folder) == 0 { - for folder := range cfg.Folders() { - err = s.model.ResetFolder(folder) - if err != nil { - break - } + + if len(folder) > 0 { + if _, ok := cfg.Folders()[folder]; !ok { + http.Error(w, "Invalid folder ID", 500) + return } - } else { - err = s.model.ResetFolder(folder) - } - if err != nil { - http.Error(w, err.Error(), 500) - return } + if len(folder) == 0 { + // Reset all folders. + for folder := range cfg.Folders() { + s.model.ResetFolder(folder) + } s.flushResponse(`{"ok": "resetting database"}`, w) } else { + // Reset a specific folder, assuming it's supposed to exist. + s.model.ResetFolder(folder) s.flushResponse(`{"ok": "resetting folder `+folder+`"}`, w) } + go restart() } diff --git a/internal/model/model.go b/internal/model/model.go index 2de748c42..e66fdb5cb 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1732,15 +1732,9 @@ func (m *Model) CheckFolderHealth(id string) error { return err } -func (m *Model) ResetFolder(folder string) error { - for _, f := range db.ListFolders(m.db) { - if f == folder { - l.Infof("Cleaning data for folder %q", folder) - db.DropFolder(m.db, folder) - return nil - } - } - return fmt.Errorf("Unknown folder %q", folder) +func (m *Model) ResetFolder(folder string) { + l.Infof("Cleaning data for folder %q", folder) + db.DropFolder(m.db, folder) } func (m *Model) String() string { diff --git a/test/reset_test.go b/test/reset_test.go index 1067c533a..081fc2b35 100644 --- a/test/reset_test.go +++ b/test/reset_test.go @@ -9,6 +9,7 @@ package integration import ( + "bytes" "log" "os" "path/filepath" @@ -67,17 +68,17 @@ func TestReset(t *testing.T) { // Reset indexes of the default folder log.Println("Reset indexes of default folder") - _, err = p.Post("/rest/system/reset?folder=default", nil) + bs, err := p.Post("/rest/system/reset?folder=default", nil) if err != nil { - t.Fatal("Failed to reset indexes of the default folder:", err) + t.Fatalf("Failed to reset indexes (default): %v (%s)", err, bytes.TrimSpace(bs)) } // Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So // we wait for it to exit, then do a stop so the rc.Process is happy and // restart it again. time.Sleep(time.Second) - checkedStop(t, p) p = startInstance(t, 1) + defer checkedStop(t, p) m, err = p.Model("default") if err != nil { @@ -108,17 +109,14 @@ func TestReset(t *testing.T) { // Reset all indexes log.Println("Reset DB...") - _, err = p.Post("/rest/system/reset?folder=default", nil) + bs, err = p.Post("/rest/system/reset", nil) if err != nil { - t.Fatalf("Failed to reset indexes", err) + t.Fatalf("Failed to reset indexes (all): %v (%s)", err, bytes.TrimSpace(bs)) } // Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So - // we wait for it to exit, then do a stop so the rc.Process is happy and - // restart it again. + // we wait for it to exit, then restart it again. time.Sleep(time.Second) - checkedStop(t, p) - p = startInstance(t, 1) defer checkedStop(t, p)