wrapper.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:generate counterfeiter -o mocks/mocked_wrapper.go --fake-name Wrapper . Wrapper
  7. package config
  8. import (
  9. "context"
  10. "errors"
  11. "os"
  12. "reflect"
  13. "sync/atomic"
  14. "time"
  15. "github.com/syncthing/syncthing/lib/events"
  16. "github.com/syncthing/syncthing/lib/osutil"
  17. "github.com/syncthing/syncthing/lib/protocol"
  18. "github.com/syncthing/syncthing/lib/sync"
  19. "github.com/thejerf/suture/v4"
  20. )
  21. const (
  22. maxModifications = 1000
  23. minSaveInterval = 5 * time.Second
  24. )
  25. var errTooManyModifications = errors.New("too many concurrent config modifications")
  26. // The Committer interface is implemented by objects that need to know about
  27. // or have a say in configuration changes.
  28. //
  29. // When the configuration is about to be changed, VerifyConfiguration() is
  30. // called for each subscribing object, with the old and new configuration. A
  31. // nil error is returned if the new configuration is acceptable (i.e. does not
  32. // contain any errors that would prevent it from being a valid config).
  33. // Otherwise an error describing the problem is returned.
  34. //
  35. // If any subscriber returns an error from VerifyConfiguration(), the
  36. // configuration change is not committed and an error is returned to whoever
  37. // tried to commit the broken config.
  38. //
  39. // If all verification calls returns nil, CommitConfiguration() is called for
  40. // each subscribing object. The callee returns true if the new configuration
  41. // has been successfully applied, otherwise false. Any Commit() call returning
  42. // false will result in a "restart needed" response to the API/user. Note that
  43. // the new configuration will still have been applied by those who were
  44. // capable of doing so.
  45. //
  46. // A Committer must take care not to hold any locks while changing the
  47. // configuration (e.g. calling Wrapper.SetFolder), that are also acquired in any
  48. // methods of the Committer interface.
  49. type Committer interface {
  50. VerifyConfiguration(from, to Configuration) error
  51. CommitConfiguration(from, to Configuration) (handled bool)
  52. String() string
  53. }
  54. // Waiter allows to wait for the given config operation to complete.
  55. type Waiter interface {
  56. Wait()
  57. }
  58. type noopWaiter struct{}
  59. func (noopWaiter) Wait() {}
  60. // ModifyFunction gets a pointer to a copy of the currently active configuration
  61. // for modification.
  62. type ModifyFunction func(*Configuration)
  63. // Wrapper handles a Configuration, i.e. it provides methods to access, change
  64. // and save the config, and notifies registered subscribers (Committer) of
  65. // changes.
  66. //
  67. // Modify allows changing the currently active configuration through the given
  68. // ModifyFunction. It can be called concurrently: All calls will be queued and
  69. // called in order.
  70. type Wrapper interface {
  71. ConfigPath() string
  72. MyID() protocol.DeviceID
  73. RawCopy() Configuration
  74. RequiresRestart() bool
  75. Save() error
  76. Modify(ModifyFunction) (Waiter, error)
  77. RemoveFolder(id string) (Waiter, error)
  78. RemoveDevice(id protocol.DeviceID) (Waiter, error)
  79. GUI() GUIConfiguration
  80. LDAP() LDAPConfiguration
  81. Options() OptionsConfiguration
  82. Folder(id string) (FolderConfiguration, bool)
  83. Folders() map[string]FolderConfiguration
  84. FolderList() []FolderConfiguration
  85. FolderPasswords(device protocol.DeviceID) map[string]string
  86. DefaultFolder() FolderConfiguration
  87. Device(id protocol.DeviceID) (DeviceConfiguration, bool)
  88. Devices() map[protocol.DeviceID]DeviceConfiguration
  89. DeviceList() []DeviceConfiguration
  90. DefaultDevice() DeviceConfiguration
  91. IgnoredDevices() []ObservedDevice
  92. IgnoredDevice(id protocol.DeviceID) bool
  93. IgnoredFolder(device protocol.DeviceID, folder string) bool
  94. Subscribe(c Committer) Configuration
  95. Unsubscribe(c Committer)
  96. suture.Service
  97. }
  98. type wrapper struct {
  99. cfg Configuration
  100. path string
  101. evLogger events.Logger
  102. myID protocol.DeviceID
  103. queue chan modifyEntry
  104. waiter Waiter // Latest ongoing config change
  105. subs []Committer
  106. mut sync.Mutex
  107. requiresRestart uint32 // an atomic bool
  108. }
  109. // Wrap wraps an existing Configuration structure and ties it to a file on
  110. // disk.
  111. // The returned Wrapper is a suture.Service, thus needs to be started (added to
  112. // a supervisor).
  113. func Wrap(path string, cfg Configuration, myID protocol.DeviceID, evLogger events.Logger) Wrapper {
  114. w := &wrapper{
  115. cfg: cfg,
  116. path: path,
  117. evLogger: evLogger,
  118. myID: myID,
  119. queue: make(chan modifyEntry, maxModifications),
  120. waiter: noopWaiter{}, // Noop until first config change
  121. mut: sync.NewMutex(),
  122. }
  123. return w
  124. }
  125. // Load loads an existing file on disk and returns a new configuration
  126. // wrapper.
  127. // The returned Wrapper is a suture.Service, thus needs to be started (added to
  128. // a supervisor).
  129. func Load(path string, myID protocol.DeviceID, evLogger events.Logger) (Wrapper, int, error) {
  130. fd, err := os.Open(path)
  131. if err != nil {
  132. return nil, 0, err
  133. }
  134. defer fd.Close()
  135. cfg, originalVersion, err := ReadXML(fd, myID)
  136. if err != nil {
  137. return nil, 0, err
  138. }
  139. return Wrap(path, cfg, myID, evLogger), originalVersion, nil
  140. }
  141. func (w *wrapper) ConfigPath() string {
  142. return w.path
  143. }
  144. func (w *wrapper) MyID() protocol.DeviceID {
  145. return w.myID
  146. }
  147. // Subscribe registers the given handler to be called on any future
  148. // configuration changes. It returns the config that is in effect while
  149. // subscribing, that can be used for initial setup.
  150. func (w *wrapper) Subscribe(c Committer) Configuration {
  151. w.mut.Lock()
  152. defer w.mut.Unlock()
  153. w.subs = append(w.subs, c)
  154. return w.cfg.Copy()
  155. }
  156. // Unsubscribe de-registers the given handler from any future calls to
  157. // configuration changes and only returns after a potential ongoing config
  158. // change is done.
  159. func (w *wrapper) Unsubscribe(c Committer) {
  160. w.mut.Lock()
  161. for i := range w.subs {
  162. if w.subs[i] == c {
  163. copy(w.subs[i:], w.subs[i+1:])
  164. w.subs[len(w.subs)-1] = nil
  165. w.subs = w.subs[:len(w.subs)-1]
  166. break
  167. }
  168. }
  169. waiter := w.waiter
  170. w.mut.Unlock()
  171. // Waiting mustn't be done under lock, as the goroutines in notifyListener
  172. // may dead-lock when trying to access lock on config read operations.
  173. waiter.Wait()
  174. }
  175. // RawCopy returns a copy of the currently wrapped Configuration object.
  176. func (w *wrapper) RawCopy() Configuration {
  177. w.mut.Lock()
  178. defer w.mut.Unlock()
  179. return w.cfg.Copy()
  180. }
  181. func (w *wrapper) Modify(fn ModifyFunction) (Waiter, error) {
  182. return w.modifyQueued(fn)
  183. }
  184. func (w *wrapper) modifyQueued(modifyFunc ModifyFunction) (Waiter, error) {
  185. e := modifyEntry{
  186. modifyFunc: modifyFunc,
  187. res: make(chan modifyResult),
  188. }
  189. select {
  190. case w.queue <- e:
  191. default:
  192. return noopWaiter{}, errTooManyModifications
  193. }
  194. res := <-e.res
  195. return res.w, res.err
  196. }
  197. func (w *wrapper) Serve(ctx context.Context) error {
  198. defer w.serveSave()
  199. var e modifyEntry
  200. saveTimer := time.NewTimer(0)
  201. <-saveTimer.C
  202. saveTimerRunning := false
  203. for {
  204. select {
  205. case e = <-w.queue:
  206. case <-saveTimer.C:
  207. w.serveSave()
  208. saveTimerRunning = false
  209. continue
  210. case <-ctx.Done():
  211. return ctx.Err()
  212. }
  213. var waiter Waiter = noopWaiter{}
  214. var err error
  215. // Let the caller modify the config.
  216. to := w.RawCopy()
  217. e.modifyFunc(&to)
  218. // Check if the config was actually changed at all.
  219. w.mut.Lock()
  220. if !reflect.DeepEqual(w.cfg, to) {
  221. waiter, err = w.replaceLocked(to)
  222. if !saveTimerRunning {
  223. saveTimer.Reset(minSaveInterval)
  224. saveTimerRunning = true
  225. }
  226. }
  227. w.mut.Unlock()
  228. e.res <- modifyResult{
  229. w: waiter,
  230. err: err,
  231. }
  232. // Wait for all subscriber to handle the config change before continuing
  233. // to process the next change.
  234. done := make(chan struct{})
  235. go func() {
  236. waiter.Wait()
  237. close(done)
  238. }()
  239. select {
  240. case <-done:
  241. case <-ctx.Done():
  242. return ctx.Err()
  243. }
  244. }
  245. }
  246. func (w *wrapper) serveSave() {
  247. if err := w.Save(); err != nil {
  248. l.Warnln("Failed to save config:", err)
  249. }
  250. }
  251. func (w *wrapper) replaceLocked(to Configuration) (Waiter, error) {
  252. from := w.cfg
  253. if err := to.prepare(w.myID); err != nil {
  254. return noopWaiter{}, err
  255. }
  256. for _, sub := range w.subs {
  257. l.Debugln(sub, "verifying configuration")
  258. if err := sub.VerifyConfiguration(from.Copy(), to.Copy()); err != nil {
  259. l.Debugln(sub, "rejected config:", err)
  260. return noopWaiter{}, err
  261. }
  262. }
  263. w.cfg = to
  264. w.waiter = w.notifyListeners(from.Copy(), to.Copy())
  265. return w.waiter, nil
  266. }
  267. func (w *wrapper) notifyListeners(from, to Configuration) Waiter {
  268. wg := sync.NewWaitGroup()
  269. wg.Add(len(w.subs))
  270. for _, sub := range w.subs {
  271. go func(commiter Committer) {
  272. w.notifyListener(commiter, from, to)
  273. wg.Done()
  274. }(sub)
  275. }
  276. return wg
  277. }
  278. func (w *wrapper) notifyListener(sub Committer, from, to Configuration) {
  279. l.Debugln(sub, "committing configuration")
  280. if !sub.CommitConfiguration(from, to) {
  281. l.Debugln(sub, "requires restart")
  282. w.setRequiresRestart()
  283. }
  284. }
  285. // Devices returns a map of devices.
  286. func (w *wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
  287. w.mut.Lock()
  288. defer w.mut.Unlock()
  289. deviceMap := make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
  290. for _, dev := range w.cfg.Devices {
  291. deviceMap[dev.DeviceID] = dev.Copy()
  292. }
  293. return deviceMap
  294. }
  295. // DeviceList returns a slice of devices.
  296. func (w *wrapper) DeviceList() []DeviceConfiguration {
  297. w.mut.Lock()
  298. defer w.mut.Unlock()
  299. return w.cfg.Copy().Devices
  300. }
  301. // RemoveDevice removes the device from the configuration
  302. func (w *wrapper) RemoveDevice(id protocol.DeviceID) (Waiter, error) {
  303. return w.modifyQueued(func(cfg *Configuration) {
  304. if _, i, ok := cfg.Device(id); ok {
  305. cfg.Devices = append(cfg.Devices[:i], cfg.Devices[i+1:]...)
  306. }
  307. })
  308. }
  309. func (w *wrapper) DefaultDevice() DeviceConfiguration {
  310. w.mut.Lock()
  311. defer w.mut.Unlock()
  312. return w.cfg.Defaults.Device.Copy()
  313. }
  314. // Folders returns a map of folders.
  315. func (w *wrapper) Folders() map[string]FolderConfiguration {
  316. w.mut.Lock()
  317. defer w.mut.Unlock()
  318. folderMap := make(map[string]FolderConfiguration, len(w.cfg.Folders))
  319. for _, fld := range w.cfg.Folders {
  320. folderMap[fld.ID] = fld.Copy()
  321. }
  322. return folderMap
  323. }
  324. // FolderList returns a slice of folders.
  325. func (w *wrapper) FolderList() []FolderConfiguration {
  326. w.mut.Lock()
  327. defer w.mut.Unlock()
  328. return w.cfg.Copy().Folders
  329. }
  330. // RemoveFolder removes the folder from the configuration
  331. func (w *wrapper) RemoveFolder(id string) (Waiter, error) {
  332. return w.modifyQueued(func(cfg *Configuration) {
  333. if _, i, ok := cfg.Folder(id); ok {
  334. cfg.Folders = append(cfg.Folders[:i], cfg.Folders[i+1:]...)
  335. }
  336. })
  337. }
  338. // FolderPasswords returns the folder passwords set for this device, for
  339. // folders that have an encryption password set.
  340. func (w *wrapper) FolderPasswords(device protocol.DeviceID) map[string]string {
  341. w.mut.Lock()
  342. defer w.mut.Unlock()
  343. return w.cfg.FolderPasswords(device)
  344. }
  345. func (w *wrapper) DefaultFolder() FolderConfiguration {
  346. w.mut.Lock()
  347. defer w.mut.Unlock()
  348. return w.cfg.Defaults.Folder.Copy()
  349. }
  350. // Options returns the current options configuration object.
  351. func (w *wrapper) Options() OptionsConfiguration {
  352. w.mut.Lock()
  353. defer w.mut.Unlock()
  354. return w.cfg.Options.Copy()
  355. }
  356. func (w *wrapper) LDAP() LDAPConfiguration {
  357. w.mut.Lock()
  358. defer w.mut.Unlock()
  359. return w.cfg.LDAP.Copy()
  360. }
  361. // GUI returns the current GUI configuration object.
  362. func (w *wrapper) GUI() GUIConfiguration {
  363. w.mut.Lock()
  364. defer w.mut.Unlock()
  365. return w.cfg.GUI.Copy()
  366. }
  367. // IgnoredDevice returns whether or not connection attempts from the given
  368. // device should be silently ignored.
  369. func (w *wrapper) IgnoredDevice(id protocol.DeviceID) bool {
  370. w.mut.Lock()
  371. defer w.mut.Unlock()
  372. for _, device := range w.cfg.IgnoredDevices {
  373. if device.ID == id {
  374. return true
  375. }
  376. }
  377. return false
  378. }
  379. // IgnoredDevices returns a slice of ignored devices.
  380. func (w *wrapper) IgnoredDevices() []ObservedDevice {
  381. w.mut.Lock()
  382. defer w.mut.Unlock()
  383. res := make([]ObservedDevice, len(w.cfg.IgnoredDevices))
  384. copy(res, w.cfg.IgnoredDevices)
  385. return res
  386. }
  387. // IgnoredFolder returns whether or not share attempts for the given
  388. // folder should be silently ignored.
  389. func (w *wrapper) IgnoredFolder(device protocol.DeviceID, folder string) bool {
  390. dev, ok := w.Device(device)
  391. if !ok {
  392. return false
  393. }
  394. return dev.IgnoredFolder(folder)
  395. }
  396. // Device returns the configuration for the given device and an "ok" bool.
  397. func (w *wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
  398. w.mut.Lock()
  399. defer w.mut.Unlock()
  400. device, _, ok := w.cfg.Device(id)
  401. if !ok {
  402. return DeviceConfiguration{}, false
  403. }
  404. return device.Copy(), ok
  405. }
  406. // Folder returns the configuration for the given folder and an "ok" bool.
  407. func (w *wrapper) Folder(id string) (FolderConfiguration, bool) {
  408. w.mut.Lock()
  409. defer w.mut.Unlock()
  410. fcfg, _, ok := w.cfg.Folder(id)
  411. if !ok {
  412. return FolderConfiguration{}, false
  413. }
  414. return fcfg.Copy(), ok
  415. }
  416. // Save writes the configuration to disk, and generates a ConfigSaved event.
  417. func (w *wrapper) Save() error {
  418. w.mut.Lock()
  419. defer w.mut.Unlock()
  420. fd, err := osutil.CreateAtomic(w.path)
  421. if err != nil {
  422. l.Debugln("CreateAtomic:", err)
  423. return err
  424. }
  425. if err := w.cfg.WriteXML(fd); err != nil {
  426. l.Debugln("WriteXML:", err)
  427. fd.Close()
  428. return err
  429. }
  430. if err := fd.Close(); err != nil {
  431. l.Debugln("Close:", err)
  432. return err
  433. }
  434. w.evLogger.Log(events.ConfigSaved, w.cfg)
  435. return nil
  436. }
  437. func (w *wrapper) RequiresRestart() bool {
  438. return atomic.LoadUint32(&w.requiresRestart) != 0
  439. }
  440. func (w *wrapper) setRequiresRestart() {
  441. atomic.StoreUint32(&w.requiresRestart, 1)
  442. }
  443. type modifyEntry struct {
  444. modifyFunc ModifyFunction
  445. res chan modifyResult
  446. }
  447. type modifyResult struct {
  448. w Waiter
  449. err error
  450. }