| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- // Copyright (C) 2014 The Syncthing Authors.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at http://mozilla.org/MPL/2.0/.
- package config
- import (
- "io/ioutil"
- "os"
- "path/filepath"
- "sync"
- "github.com/syncthing/protocol"
- "github.com/syncthing/syncthing/internal/events"
- "github.com/syncthing/syncthing/internal/osutil"
- )
- // An interface to handle configuration changes, and a wrapper type á la
- // http.Handler
- type Handler interface {
- Changed(Configuration) error
- }
- type HandlerFunc func(Configuration) error
- func (fn HandlerFunc) Changed(cfg Configuration) error {
- return fn(cfg)
- }
- // A wrapper around a Configuration that manages loads, saves and published
- // notifications of changes to registered Handlers
- type Wrapper struct {
- cfg Configuration
- path string
- deviceMap map[protocol.DeviceID]DeviceConfiguration
- folderMap map[string]FolderConfiguration
- replaces chan Configuration
- mut sync.Mutex
- subs []Handler
- sMut sync.Mutex
- }
- // Wrap wraps an existing Configuration structure and ties it to a file on
- // disk.
- func Wrap(path string, cfg Configuration) *Wrapper {
- w := &Wrapper{cfg: cfg, path: path}
- w.replaces = make(chan Configuration)
- go w.Serve()
- return w
- }
- // Load loads an existing file on disk and returns a new configuration
- // wrapper.
- func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
- fd, err := os.Open(path)
- if err != nil {
- return nil, err
- }
- defer fd.Close()
- cfg, err := ReadXML(fd, myID)
- if err != nil {
- return nil, err
- }
- return Wrap(path, cfg), nil
- }
- // Serve handles configuration replace events and calls any interested
- // handlers. It is started automatically by Wrap() and Load() and should not
- // be run manually.
- func (w *Wrapper) Serve() {
- for cfg := range w.replaces {
- w.sMut.Lock()
- subs := w.subs
- w.sMut.Unlock()
- for _, h := range subs {
- h.Changed(cfg)
- }
- }
- }
- // Stop stops the Serve() loop. Set and Replace operations will panic after a
- // Stop.
- func (w *Wrapper) Stop() {
- close(w.replaces)
- }
- // Subscribe registers the given handler to be called on any future
- // configuration changes.
- func (w *Wrapper) Subscribe(h Handler) {
- w.sMut.Lock()
- w.subs = append(w.subs, h)
- w.sMut.Unlock()
- }
- // Raw returns the currently wrapped Configuration object.
- func (w *Wrapper) Raw() Configuration {
- return w.cfg
- }
- // Replace swaps the current configuration object for the given one.
- func (w *Wrapper) Replace(cfg Configuration) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.cfg = cfg
- w.deviceMap = nil
- w.folderMap = nil
- w.replaces <- cfg.Copy()
- }
- // Devices returns a map of devices. Device structures should not be changed,
- // other than for the purpose of updating via SetDevice().
- func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
- w.mut.Lock()
- defer w.mut.Unlock()
- if w.deviceMap == nil {
- w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
- for _, dev := range w.cfg.Devices {
- w.deviceMap[dev.DeviceID] = dev
- }
- }
- 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) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.deviceMap = nil
- for i := range w.cfg.Devices {
- if w.cfg.Devices[i].DeviceID == dev.DeviceID {
- w.cfg.Devices[i] = dev
- w.replaces <- w.cfg.Copy()
- return
- }
- }
- w.cfg.Devices = append(w.cfg.Devices, dev)
- w.replaces <- w.cfg.Copy()
- }
- // Devices returns a map of folders. Folder structures should not be changed,
- // other than for the purpose of updating via SetFolder().
- func (w *Wrapper) Folders() map[string]FolderConfiguration {
- w.mut.Lock()
- defer w.mut.Unlock()
- if w.folderMap == nil {
- w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
- for _, fld := range w.cfg.Folders {
- w.folderMap[fld.ID] = fld
- }
- }
- return w.folderMap
- }
- // SetFolder adds a new folder to the configuration, or overwrites an existing
- // folder with the same ID.
- func (w *Wrapper) SetFolder(fld FolderConfiguration) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.folderMap = nil
- for i := range w.cfg.Folders {
- if w.cfg.Folders[i].ID == fld.ID {
- w.cfg.Folders[i] = fld
- w.replaces <- w.cfg.Copy()
- return
- }
- }
- w.cfg.Folders = append(w.cfg.Folders, fld)
- w.replaces <- w.cfg.Copy()
- }
- // Options returns the current options configuration object.
- func (w *Wrapper) Options() OptionsConfiguration {
- w.mut.Lock()
- defer w.mut.Unlock()
- return w.cfg.Options
- }
- // SetOptions replaces the current options configuration object.
- func (w *Wrapper) SetOptions(opts OptionsConfiguration) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.cfg.Options = opts
- w.replaces <- w.cfg.Copy()
- }
- // GUI returns the current GUI configuration object.
- func (w *Wrapper) GUI() GUIConfiguration {
- w.mut.Lock()
- defer w.mut.Unlock()
- return w.cfg.GUI
- }
- // SetGUI replaces the current GUI configuration object.
- func (w *Wrapper) SetGUI(gui GUIConfiguration) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.cfg.GUI = gui
- w.replaces <- w.cfg.Copy()
- }
- // Sets the folder error state. Emits ConfigSaved to cause a GUI refresh.
- func (w *Wrapper) SetFolderError(id string, err error) {
- w.mut.Lock()
- defer w.mut.Unlock()
- w.folderMap = nil
- for i := range w.cfg.Folders {
- if w.cfg.Folders[i].ID == id {
- errstr := ""
- if err != nil {
- errstr = err.Error()
- }
- if errstr != w.cfg.Folders[i].Invalid {
- w.cfg.Folders[i].Invalid = errstr
- events.Default.Log(events.ConfigSaved, w.cfg)
- w.replaces <- w.cfg.Copy()
- }
- return
- }
- }
- }
- // Returns whether or not connection attempts from the given device should be
- // silently ignored.
- func (w *Wrapper) IgnoredDevice(id protocol.DeviceID) bool {
- w.mut.Lock()
- defer w.mut.Unlock()
- for _, device := range w.cfg.IgnoredDevices {
- if device == id {
- return true
- }
- }
- return false
- }
- // Save writes the configuration to disk, and generates a ConfigSaved event.
- func (w *Wrapper) Save() error {
- fd, err := ioutil.TempFile(filepath.Dir(w.path), "cfg")
- if err != nil {
- return err
- }
- defer os.Remove(fd.Name())
- err = w.cfg.WriteXML(fd)
- if err != nil {
- fd.Close()
- return err
- }
- err = fd.Close()
- if err != nil {
- return err
- }
- events.Default.Log(events.ConfigSaved, w.cfg)
- return osutil.Rename(fd.Name(), w.path)
- }
|