syncthing/lib/config/wrapper.go

528 lines
14 KiB
Go
Raw Permalink Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 20:36:35 +00:00
// 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 https://mozilla.org/MPL/2.0/.
//go:generate -command counterfeiter go run github.com/maxbrunsfeld/counterfeiter/v6
//go:generate counterfeiter -o mocks/mocked_wrapper.go --fake-name Wrapper . Wrapper
package config
import (
"context"
"errors"
"os"
"reflect"
"sync/atomic"
"time"
2015-08-06 09:29:25 +00:00
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/osutil"
2015-09-22 17:38:46 +00:00
"github.com/syncthing/syncthing/lib/protocol"
2015-08-06 09:29:25 +00:00
"github.com/syncthing/syncthing/lib/sync"
"github.com/thejerf/suture/v4"
)
const (
maxModifications = 1000
minSaveInterval = 5 * time.Second
)
var errTooManyModifications = errors.New("too many concurrent config modifications")
// The Committer interface is implemented by objects that need to know about
// or have a say in configuration changes.
//
// When the configuration is about to be changed, VerifyConfiguration() is
// called for each subscribing object, with the old and new configuration. A
// nil error is returned if the new configuration is acceptable (i.e. does not
// contain any errors that would prevent it from being a valid config).
// Otherwise an error describing the problem is returned.
//
// If any subscriber returns an error from VerifyConfiguration(), the
// configuration change is not committed and an error is returned to whoever
// tried to commit the broken config.
//
// If all verification calls returns nil, CommitConfiguration() is called for
// each subscribing object. The callee returns true if the new configuration
// has been successfully applied, otherwise false. Any Commit() call returning
2015-11-12 02:20:34 +00:00
// false will result in a "restart needed" response to the API/user. Note that
// the new configuration will still have been applied by those who were
// capable of doing so.
//
// A Committer must take care not to hold any locks while changing the
// configuration (e.g. calling Wrapper.SetFolder), that are also acquired in any
// methods of the Committer interface.
type Committer interface {
VerifyConfiguration(from, to Configuration) error
CommitConfiguration(from, to Configuration) (handled bool)
String() string
}
// Waiter allows to wait for the given config operation to complete.
type Waiter interface {
Wait()
}
type noopWaiter struct{}
func (noopWaiter) Wait() {}
// ModifyFunction gets a pointer to a copy of the currently active configuration
// for modification.
type ModifyFunction func(*Configuration)
// Wrapper handles a Configuration, i.e. it provides methods to access, change
// and save the config, and notifies registered subscribers (Committer) of
// changes.
//
// Modify allows changing the currently active configuration through the given
// ModifyFunction. It can be called concurrently: All calls will be queued and
// called in order.
type Wrapper interface {
ConfigPath() string
MyID() protocol.DeviceID
RawCopy() Configuration
RequiresRestart() bool
Save() error
Modify(ModifyFunction) (Waiter, error)
RemoveFolder(id string) (Waiter, error)
RemoveDevice(id protocol.DeviceID) (Waiter, error)
GUI() GUIConfiguration
LDAP() LDAPConfiguration
Options() OptionsConfiguration
Folder(id string) (FolderConfiguration, bool)
Folders() map[string]FolderConfiguration
FolderList() []FolderConfiguration
FolderPasswords(device protocol.DeviceID) map[string]string
DefaultFolder() FolderConfiguration
Device(id protocol.DeviceID) (DeviceConfiguration, bool)
Devices() map[protocol.DeviceID]DeviceConfiguration
DeviceList() []DeviceConfiguration
DefaultDevice() DeviceConfiguration
IgnoredDevices() []ObservedDevice
IgnoredDevice(id protocol.DeviceID) bool
IgnoredFolder(device protocol.DeviceID, folder string) bool
Subscribe(c Committer) Configuration
Unsubscribe(c Committer)
suture.Service
}
type wrapper struct {
cfg Configuration
path string
evLogger events.Logger
myID protocol.DeviceID
queue chan modifyEntry
waiter Waiter // Latest ongoing config change
subs []Committer
mut sync.Mutex
requiresRestart uint32 // an atomic bool
}
// Wrap wraps an existing Configuration structure and ties it to a file on
// disk.
// The returned Wrapper is a suture.Service, thus needs to be started (added to
// a supervisor).
func Wrap(path string, cfg Configuration, myID protocol.DeviceID, evLogger events.Logger) Wrapper {
w := &wrapper{
cfg: cfg,
path: path,
evLogger: evLogger,
myID: myID,
queue: make(chan modifyEntry, maxModifications),
waiter: noopWaiter{}, // Noop until first config change
mut: sync.NewMutex(),
2015-04-22 22:54:31 +00:00
}
return w
}
// Load loads an existing file on disk and returns a new configuration
// wrapper.
// The returned Wrapper is a suture.Service, thus needs to be started (added to
// a supervisor).
func Load(path string, myID protocol.DeviceID, evLogger events.Logger) (Wrapper, int, error) {
fd, err := os.Open(path)
if err != nil {
return nil, 0, err
}
defer fd.Close()
cfg, originalVersion, err := ReadXML(fd, myID)
if err != nil {
return nil, 0, err
}
return Wrap(path, cfg, myID, evLogger), originalVersion, nil
}
func (w *wrapper) ConfigPath() string {
return w.path
}
func (w *wrapper) MyID() protocol.DeviceID {
return w.myID
}
// Subscribe registers the given handler to be called on any future
// configuration changes. It returns the config that is in effect while
// subscribing, that can be used for initial setup.
func (w *wrapper) Subscribe(c Committer) Configuration {
w.mut.Lock()
defer w.mut.Unlock()
w.subs = append(w.subs, c)
return w.cfg.Copy()
}
2015-06-28 19:09:10 +00:00
// Unsubscribe de-registers the given handler from any future calls to
// configuration changes and only returns after a potential ongoing config
// change is done.
func (w *wrapper) Unsubscribe(c Committer) {
w.mut.Lock()
2015-06-28 19:09:10 +00:00
for i := range w.subs {
if w.subs[i] == c {
copy(w.subs[i:], w.subs[i+1:])
w.subs[len(w.subs)-1] = nil
w.subs = w.subs[:len(w.subs)-1]
break
}
}
waiter := w.waiter
w.mut.Unlock()
// Waiting mustn't be done under lock, as the goroutines in notifyListener
// may dead-lock when trying to access lock on config read operations.
waiter.Wait()
2015-06-28 19:09:10 +00:00
}
2016-11-12 09:34:18 +00:00
// RawCopy returns a copy of the currently wrapped Configuration object.
func (w *wrapper) RawCopy() Configuration {
2016-11-12 09:34:18 +00:00
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Copy()
}
func (w *wrapper) Modify(fn ModifyFunction) (Waiter, error) {
return w.modifyQueued(fn)
}
func (w *wrapper) modifyQueued(modifyFunc ModifyFunction) (Waiter, error) {
e := modifyEntry{
modifyFunc: modifyFunc,
res: make(chan modifyResult),
}
select {
case w.queue <- e:
default:
return noopWaiter{}, errTooManyModifications
}
res := <-e.res
return res.w, res.err
}
func (w *wrapper) Serve(ctx context.Context) error {
defer w.serveSave()
var e modifyEntry
saveTimer := time.NewTimer(0)
<-saveTimer.C
saveTimerRunning := false
for {
select {
case e = <-w.queue:
case <-saveTimer.C:
w.serveSave()
saveTimerRunning = false
continue
case <-ctx.Done():
return ctx.Err()
}
var waiter Waiter = noopWaiter{}
var err error
// Let the caller modify the config.
to := w.RawCopy()
e.modifyFunc(&to)
// Check if the config was actually changed at all.
w.mut.Lock()
if !reflect.DeepEqual(w.cfg, to) {
waiter, err = w.replaceLocked(to)
if !saveTimerRunning {
saveTimer.Reset(minSaveInterval)
saveTimerRunning = true
}
}
w.mut.Unlock()
e.res <- modifyResult{
w: waiter,
err: err,
}
// Wait for all subscriber to handle the config change before continuing
// to process the next change.
done := make(chan struct{})
go func() {
waiter.Wait()
close(done)
}()
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
}
}
func (w *wrapper) serveSave() {
if err := w.Save(); err != nil {
l.Warnln("Failed to save config:", err)
}
}
func (w *wrapper) replaceLocked(to Configuration) (Waiter, error) {
from := w.cfg
if err := to.prepare(w.myID); err != nil {
return noopWaiter{}, err
}
for _, sub := range w.subs {
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln(sub, "verifying configuration")
if err := sub.VerifyConfiguration(from.Copy(), to.Copy()); err != nil {
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
2015-10-03 15:25:21 +00:00
l.Debugln(sub, "rejected config:", err)
return noopWaiter{}, err
}
}
w.cfg = to
w.waiter = w.notifyListeners(from.Copy(), to.Copy())
return w.waiter, nil
}
func (w *wrapper) notifyListeners(from, to Configuration) Waiter {
wg := sync.NewWaitGroup()
wg.Add(len(w.subs))
for _, sub := range w.subs {
go func(commiter Committer) {
w.notifyListener(commiter, from, to)
wg.Done()
}(sub)
}
return wg
}
func (w *wrapper) notifyListener(sub Committer, from, to Configuration) {
l.Debugln(sub, "committing configuration")
if !sub.CommitConfiguration(from, to) {
l.Debugln(sub, "requires restart")
w.setRequiresRestart()
}
}
// Devices returns a map of devices.
func (w *wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
deviceMap := make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
for _, dev := range w.cfg.Devices {
deviceMap[dev.DeviceID] = dev.Copy()
}
return deviceMap
}
// DeviceList returns a slice of devices.
func (w *wrapper) DeviceList() []DeviceConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Copy().Devices
}
// RemoveDevice removes the device from the configuration
func (w *wrapper) RemoveDevice(id protocol.DeviceID) (Waiter, error) {
return w.modifyQueued(func(cfg *Configuration) {
if _, i, ok := cfg.Device(id); ok {
cfg.Devices = append(cfg.Devices[:i], cfg.Devices[i+1:]...)
}
})
}
func (w *wrapper) DefaultDevice() DeviceConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Defaults.Device.Copy()
}
// Folders returns a map of folders.
func (w *wrapper) Folders() map[string]FolderConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
folderMap := make(map[string]FolderConfiguration, len(w.cfg.Folders))
for _, fld := range w.cfg.Folders {
folderMap[fld.ID] = fld.Copy()
}
return folderMap
}
// FolderList returns a slice of folders.
func (w *wrapper) FolderList() []FolderConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Copy().Folders
}
// RemoveFolder removes the folder from the configuration
func (w *wrapper) RemoveFolder(id string) (Waiter, error) {
return w.modifyQueued(func(cfg *Configuration) {
if _, i, ok := cfg.Folder(id); ok {
cfg.Folders = append(cfg.Folders[:i], cfg.Folders[i+1:]...)
}
})
}
// FolderPasswords returns the folder passwords set for this device, for
// folders that have an encryption password set.
func (w *wrapper) FolderPasswords(device protocol.DeviceID) map[string]string {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.FolderPasswords(device)
}
func (w *wrapper) DefaultFolder() FolderConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Defaults.Folder.Copy()
}
// Options returns the current options configuration object.
func (w *wrapper) Options() OptionsConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Options.Copy()
}
func (w *wrapper) LDAP() LDAPConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.LDAP.Copy()
}
// GUI returns the current GUI configuration object.
func (w *wrapper) GUI() GUIConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.GUI.Copy()
}
2015-04-28 20:32:10 +00:00
// IgnoredDevice 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 == id {
return true
}
}
return false
}
// IgnoredDevices returns a slice of ignored devices.
func (w *wrapper) IgnoredDevices() []ObservedDevice {
w.mut.Lock()
defer w.mut.Unlock()
res := make([]ObservedDevice, len(w.cfg.IgnoredDevices))
copy(res, w.cfg.IgnoredDevices)
return res
}
// IgnoredFolder returns whether or not share attempts for the given
// folder should be silently ignored.
func (w *wrapper) IgnoredFolder(device protocol.DeviceID, folder string) bool {
dev, ok := w.Device(device)
if !ok {
return false
}
return dev.IgnoredFolder(folder)
}
// Device returns the configuration for the given device and an "ok" bool.
func (w *wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
w.mut.Lock()
defer w.mut.Unlock()
device, _, ok := w.cfg.Device(id)
if !ok {
return DeviceConfiguration{}, false
}
return device.Copy(), ok
}
// Folder returns the configuration for the given folder and an "ok" bool.
func (w *wrapper) Folder(id string) (FolderConfiguration, bool) {
w.mut.Lock()
defer w.mut.Unlock()
fcfg, _, ok := w.cfg.Folder(id)
if !ok {
return FolderConfiguration{}, false
}
return fcfg.Copy(), ok
}
// Save writes the configuration to disk, and generates a ConfigSaved event.
func (w *wrapper) Save() error {
w.mut.Lock()
defer w.mut.Unlock()
fd, err := osutil.CreateAtomic(w.path)
if err != nil {
l.Debugln("CreateAtomic:", err)
return err
}
if err := w.cfg.WriteXML(fd); err != nil {
l.Debugln("WriteXML:", err)
fd.Close()
return err
}
if err := fd.Close(); err != nil {
l.Debugln("Close:", err)
return err
}
w.evLogger.Log(events.ConfigSaved, w.cfg)
return nil
}
func (w *wrapper) RequiresRestart() bool {
return atomic.LoadUint32(&w.requiresRestart) != 0
}
func (w *wrapper) setRequiresRestart() {
atomic.StoreUint32(&w.requiresRestart, 1)
}
type modifyEntry struct {
modifyFunc ModifyFunction
res chan modifyResult
}
type modifyResult struct {
w Waiter
err error
}