2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-06 07:25:45 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2014-10-06 07:25:45 +00:00
|
|
|
"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
|
|
|
|
|
2014-12-08 15:39:11 +00:00
|
|
|
type Wrapper struct {
|
2014-10-06 07:25:45 +00:00
|
|
|
cfg Configuration
|
|
|
|
path string
|
|
|
|
|
|
|
|
deviceMap map[protocol.DeviceID]DeviceConfiguration
|
|
|
|
folderMap map[string]FolderConfiguration
|
|
|
|
replaces chan Configuration
|
|
|
|
mut sync.Mutex
|
2014-10-07 08:34:53 +00:00
|
|
|
|
|
|
|
subs []Handler
|
|
|
|
sMut sync.Mutex
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap wraps an existing Configuration structure and ties it to a file on
|
|
|
|
// disk.
|
2014-12-08 15:39:11 +00:00
|
|
|
func Wrap(path string, cfg Configuration) *Wrapper {
|
|
|
|
w := &Wrapper{cfg: cfg, path: path}
|
2014-10-06 07:25:45 +00:00
|
|
|
w.replaces = make(chan Configuration)
|
|
|
|
go w.Serve()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads an existing file on disk and returns a new configuration
|
|
|
|
// wrapper.
|
2014-12-08 15:39:11 +00:00
|
|
|
func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
|
2014-10-06 07:25:45 +00:00
|
|
|
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.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Serve() {
|
2014-10-06 07:25:45 +00:00
|
|
|
for cfg := range w.replaces {
|
2014-10-07 08:34:53 +00:00
|
|
|
w.sMut.Lock()
|
2014-10-06 07:25:45 +00:00
|
|
|
subs := w.subs
|
2014-10-07 08:34:53 +00:00
|
|
|
w.sMut.Unlock()
|
2014-10-06 07:25:45 +00:00
|
|
|
for _, h := range subs {
|
|
|
|
h.Changed(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 08:34:53 +00:00
|
|
|
// Stop stops the Serve() loop. Set and Replace operations will panic after a
|
|
|
|
// Stop.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Stop() {
|
2014-10-06 07:25:45 +00:00
|
|
|
close(w.replaces)
|
|
|
|
}
|
|
|
|
|
2014-10-07 08:34:53 +00:00
|
|
|
// Subscribe registers the given handler to be called on any future
|
2014-10-06 07:25:45 +00:00
|
|
|
// configuration changes.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Subscribe(h Handler) {
|
2014-10-07 08:34:53 +00:00
|
|
|
w.sMut.Lock()
|
2014-10-06 07:25:45 +00:00
|
|
|
w.subs = append(w.subs, h)
|
2014-10-07 08:34:53 +00:00
|
|
|
w.sMut.Unlock()
|
2014-10-06 07:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns the currently wrapped Configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Raw() Configuration {
|
2014-10-06 07:25:45 +00:00
|
|
|
return w.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace swaps the current configuration object for the given one.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Replace(cfg Configuration) {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
|
|
|
w.cfg = cfg
|
|
|
|
w.deviceMap = nil
|
|
|
|
w.folderMap = nil
|
|
|
|
w.replaces <- cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Devices returns a map of devices. Device structures should not be changed,
|
|
|
|
// other than for the purpose of updating via SetDevice().
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
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.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) SetDevice(dev DeviceConfiguration) {
|
2014-10-06 07:25:45 +00:00
|
|
|
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
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.cfg.Devices = append(w.cfg.Devices, dev)
|
|
|
|
w.replaces <- w.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Devices returns a map of folders. Folder structures should not be changed,
|
|
|
|
// other than for the purpose of updating via SetFolder().
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Folders() map[string]FolderConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
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 {
|
2014-10-12 13:09:25 +00:00
|
|
|
path, err := osutil.ExpandTilde(fld.Path)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("home:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fld.Path = path
|
2014-10-06 07:25:45 +00:00
|
|
|
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.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) SetFolder(fld FolderConfiguration) {
|
2014-10-06 07:25:45 +00:00
|
|
|
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
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.cfg.Folders = append(w.cfg.Folders, fld)
|
|
|
|
w.replaces <- w.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns the current options configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Options() OptionsConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions replaces the current options configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) SetOptions(opts OptionsConfiguration) {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
w.cfg.Options = opts
|
|
|
|
w.replaces <- w.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// GUI returns the current GUI configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) GUI() GUIConfiguration {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
return w.cfg.GUI
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGUI replaces the current GUI configuration object.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) SetGUI(gui GUIConfiguration) {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
w.cfg.GUI = gui
|
|
|
|
w.replaces <- w.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidateFolder sets the invalid marker on the given folder.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) InvalidateFolder(id string, err string) {
|
2014-10-06 07:25:45 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
|
|
|
|
|
|
|
w.folderMap = nil
|
|
|
|
|
|
|
|
for i := range w.cfg.Folders {
|
|
|
|
if w.cfg.Folders[i].ID == id {
|
|
|
|
w.cfg.Folders[i].Invalid = err
|
|
|
|
w.replaces <- w.cfg
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 23:12:12 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
// Save writes the configuration to disk, and generates a ConfigSaved event.
|
2014-12-08 15:39:11 +00:00
|
|
|
func (w *Wrapper) Save() error {
|
2014-10-06 07:25:45 +00:00
|
|
|
fd, err := ioutil.TempFile(filepath.Dir(w.path), "cfg")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-14 20:33:12 +00:00
|
|
|
defer os.Remove(fd.Name())
|
2014-10-06 07:25:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|