diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index b45e6b36e..c5b67c9e4 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -104,6 +104,7 @@ type configIntf interface { Folders() map[string]config.FolderConfiguration Devices() map[protocol.DeviceID]config.DeviceConfiguration SetDevice(config.DeviceConfiguration) error + SetDevices([]config.DeviceConfiguration) error Save() error ListenAddresses() []string RequiresRestart() bool @@ -269,8 +270,8 @@ func (s *apiService) Serve() { postRestMux.HandleFunc("/rest/system/restart", s.postSystemRestart) // - postRestMux.HandleFunc("/rest/system/shutdown", s.postSystemShutdown) // - postRestMux.HandleFunc("/rest/system/upgrade", s.postSystemUpgrade) // - - postRestMux.HandleFunc("/rest/system/pause", s.makeDevicePauseHandler(true)) // device - postRestMux.HandleFunc("/rest/system/resume", s.makeDevicePauseHandler(false)) // device + postRestMux.HandleFunc("/rest/system/pause", s.makeDevicePauseHandler(true)) // [device] + postRestMux.HandleFunc("/rest/system/resume", s.makeDevicePauseHandler(false)) // [device] postRestMux.HandleFunc("/rest/system/debug", s.postSystemDebug) // [enable] [disable] // Debug endpoints, not for general use @@ -1118,19 +1119,31 @@ func (s *apiService) makeDevicePauseHandler(paused bool) http.HandlerFunc { var qs = r.URL.Query() var deviceStr = qs.Get("device") - device, err := protocol.DeviceIDFromString(deviceStr) - if err != nil { - http.Error(w, err.Error(), 500) - return + var cfgs []config.DeviceConfiguration + + if deviceStr == "" { + for _, cfg := range s.cfg.Devices() { + cfg.Paused = paused + cfgs = append(cfgs, cfg) + } + } else { + device, err := protocol.DeviceIDFromString(deviceStr) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + cfg, ok := s.cfg.Devices()[device] + if !ok { + http.Error(w, "not found", http.StatusNotFound) + return + } + + cfg.Paused = paused + cfgs = append(cfgs, cfg) } - cfg, ok := s.cfg.Devices()[device] - if !ok { - http.Error(w, "not found", http.StatusNotFound) - } - - cfg.Paused = paused - if err := s.cfg.SetDevice(cfg); err != nil { + if err := s.cfg.SetDevices(cfgs); err != nil { http.Error(w, err.Error(), 500) } } diff --git a/cmd/syncthing/mocked_config_test.go b/cmd/syncthing/mocked_config_test.go index d869ce612..bc825d409 100644 --- a/cmd/syncthing/mocked_config_test.go +++ b/cmd/syncthing/mocked_config_test.go @@ -52,6 +52,10 @@ func (c *mockedConfig) SetDevice(config.DeviceConfiguration) error { return nil } +func (c *mockedConfig) SetDevices([]config.DeviceConfiguration) error { + return nil +} + func (c *mockedConfig) Save() error { return nil } diff --git a/lib/config/wrapper.go b/lib/config/wrapper.go index 9df03f698..f3a3f19d8 100644 --- a/lib/config/wrapper.go +++ b/lib/config/wrapper.go @@ -187,28 +187,37 @@ func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration { return w.deviceMap } -// SetDevice adds a new device to the configuration, or overwrites an existing -// device with the same ID. -func (w *Wrapper) SetDevice(dev DeviceConfiguration) error { +// SetDevices adds new devices to the configuration, or overwrites existing +// devices with the same ID. +func (w *Wrapper) SetDevices(devs []DeviceConfiguration) error { w.mut.Lock() defer w.mut.Unlock() newCfg := w.cfg.Copy() - replaced := false - for i := range newCfg.Devices { - if newCfg.Devices[i].DeviceID == dev.DeviceID { - newCfg.Devices[i] = dev - replaced = true - break + var replaced bool + for oldIndex := range devs { + replaced = false + for newIndex := range newCfg.Devices { + if newCfg.Devices[newIndex].DeviceID == devs[oldIndex].DeviceID { + newCfg.Devices[newIndex] = devs[oldIndex] + replaced = true + break + } + } + if !replaced { + newCfg.Devices = append(newCfg.Devices, devs[oldIndex]) } - } - if !replaced { - newCfg.Devices = append(w.cfg.Devices, dev) } return w.replaceLocked(newCfg) } +// SetDevice adds a new device to the configuration, or overwrites an existing +// device with the same ID. +func (w *Wrapper) SetDevice(dev DeviceConfiguration) error { + return w.SetDevices([]DeviceConfiguration{dev}) +} + // RemoveDevice removes the device from the configuration func (w *Wrapper) RemoveDevice(id protocol.DeviceID) error { w.mut.Lock()