wrapper.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 http://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "os"
  9. "github.com/syncthing/protocol"
  10. "github.com/syncthing/syncthing/lib/events"
  11. "github.com/syncthing/syncthing/lib/osutil"
  12. "github.com/syncthing/syncthing/lib/sync"
  13. )
  14. // The Committer interface is implemented by objects that need to know about
  15. // or have a say in configuration changes.
  16. //
  17. // When the configuration is about to be changed, VerifyConfiguration() is
  18. // called for each subscribing object, with the old and new configuration. A
  19. // nil error is returned if the new configuration is acceptable (i.e. does not
  20. // contain any errors that would prevent it from being a valid config).
  21. // Otherwise an error describing the problem is returned.
  22. //
  23. // If any subscriber returns an error from VerifyConfiguration(), the
  24. // configuration change is not committed and an error is returned to whoever
  25. // tried to commit the broken config.
  26. //
  27. // If all verification calls returns nil, CommitConfiguration() is called for
  28. // each subscribing object. The callee returns true if the new configuration
  29. // has been successfully applied, otherwise false. Any Commit() call returning
  30. // false will result in a "restart needed" respone to the API/user. Note that
  31. // the new configuration will still have been applied by those who were
  32. // capable of doing so.
  33. type Committer interface {
  34. VerifyConfiguration(from, to Configuration) error
  35. CommitConfiguration(from, to Configuration) (handled bool)
  36. String() string
  37. }
  38. type CommitResponse struct {
  39. ValidationError error
  40. RequiresRestart bool
  41. }
  42. var ResponseNoRestart = CommitResponse{
  43. ValidationError: nil,
  44. RequiresRestart: false,
  45. }
  46. // A wrapper around a Configuration that manages loads, saves and published
  47. // notifications of changes to registered Handlers
  48. type Wrapper struct {
  49. cfg Configuration
  50. path string
  51. deviceMap map[protocol.DeviceID]DeviceConfiguration
  52. folderMap map[string]FolderConfiguration
  53. replaces chan Configuration
  54. mut sync.Mutex
  55. subs []Committer
  56. sMut sync.Mutex
  57. }
  58. // Wrap wraps an existing Configuration structure and ties it to a file on
  59. // disk.
  60. func Wrap(path string, cfg Configuration) *Wrapper {
  61. w := &Wrapper{
  62. cfg: cfg,
  63. path: path,
  64. mut: sync.NewMutex(),
  65. sMut: sync.NewMutex(),
  66. }
  67. w.replaces = make(chan Configuration)
  68. return w
  69. }
  70. // Load loads an existing file on disk and returns a new configuration
  71. // wrapper.
  72. func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
  73. fd, err := os.Open(path)
  74. if err != nil {
  75. return nil, err
  76. }
  77. defer fd.Close()
  78. cfg, err := ReadXML(fd, myID)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return Wrap(path, cfg), nil
  83. }
  84. func (w *Wrapper) ConfigPath() string {
  85. return w.path
  86. }
  87. // Stop stops the Serve() loop. Set and Replace operations will panic after a
  88. // Stop.
  89. func (w *Wrapper) Stop() {
  90. close(w.replaces)
  91. }
  92. // Subscribe registers the given handler to be called on any future
  93. // configuration changes.
  94. func (w *Wrapper) Subscribe(c Committer) {
  95. w.sMut.Lock()
  96. w.subs = append(w.subs, c)
  97. w.sMut.Unlock()
  98. }
  99. // Raw returns the currently wrapped Configuration object.
  100. func (w *Wrapper) Raw() Configuration {
  101. return w.cfg
  102. }
  103. // Replace swaps the current configuration object for the given one.
  104. func (w *Wrapper) Replace(cfg Configuration) CommitResponse {
  105. w.mut.Lock()
  106. defer w.mut.Unlock()
  107. return w.replaceLocked(cfg)
  108. }
  109. func (w *Wrapper) replaceLocked(to Configuration) CommitResponse {
  110. from := w.cfg
  111. for _, sub := range w.subs {
  112. if debug {
  113. l.Debugln(sub, "verifying configuration")
  114. }
  115. if err := sub.VerifyConfiguration(from, to); err != nil {
  116. if debug {
  117. l.Debugln(sub, "rejected config:", err)
  118. }
  119. return CommitResponse{
  120. ValidationError: err,
  121. }
  122. }
  123. }
  124. allOk := true
  125. for _, sub := range w.subs {
  126. if debug {
  127. l.Debugln(sub, "committing configuration")
  128. }
  129. ok := sub.CommitConfiguration(from, to)
  130. if !ok {
  131. if debug {
  132. l.Debugln(sub, "requires restart")
  133. }
  134. allOk = false
  135. }
  136. }
  137. w.cfg = to
  138. w.deviceMap = nil
  139. w.folderMap = nil
  140. return CommitResponse{
  141. RequiresRestart: !allOk,
  142. }
  143. }
  144. // Devices returns a map of devices. Device structures should not be changed,
  145. // other than for the purpose of updating via SetDevice().
  146. func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
  147. w.mut.Lock()
  148. defer w.mut.Unlock()
  149. if w.deviceMap == nil {
  150. w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
  151. for _, dev := range w.cfg.Devices {
  152. w.deviceMap[dev.DeviceID] = dev
  153. }
  154. }
  155. return w.deviceMap
  156. }
  157. // SetDevice adds a new device to the configuration, or overwrites an existing
  158. // device with the same ID.
  159. func (w *Wrapper) SetDevice(dev DeviceConfiguration) CommitResponse {
  160. w.mut.Lock()
  161. defer w.mut.Unlock()
  162. newCfg := w.cfg.Copy()
  163. replaced := false
  164. for i := range newCfg.Devices {
  165. if newCfg.Devices[i].DeviceID == dev.DeviceID {
  166. newCfg.Devices[i] = dev
  167. replaced = true
  168. break
  169. }
  170. }
  171. if !replaced {
  172. newCfg.Devices = append(w.cfg.Devices, dev)
  173. }
  174. return w.replaceLocked(newCfg)
  175. }
  176. // Folders returns a map of folders. Folder structures should not be changed,
  177. // other than for the purpose of updating via SetFolder().
  178. func (w *Wrapper) Folders() map[string]FolderConfiguration {
  179. w.mut.Lock()
  180. defer w.mut.Unlock()
  181. if w.folderMap == nil {
  182. w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
  183. for _, fld := range w.cfg.Folders {
  184. w.folderMap[fld.ID] = fld
  185. }
  186. }
  187. return w.folderMap
  188. }
  189. // SetFolder adds a new folder to the configuration, or overwrites an existing
  190. // folder with the same ID.
  191. func (w *Wrapper) SetFolder(fld FolderConfiguration) CommitResponse {
  192. w.mut.Lock()
  193. defer w.mut.Unlock()
  194. newCfg := w.cfg.Copy()
  195. replaced := false
  196. for i := range newCfg.Folders {
  197. if newCfg.Folders[i].ID == fld.ID {
  198. newCfg.Folders[i] = fld
  199. replaced = true
  200. break
  201. }
  202. }
  203. if !replaced {
  204. newCfg.Folders = append(w.cfg.Folders, fld)
  205. }
  206. return w.replaceLocked(newCfg)
  207. }
  208. // Options returns the current options configuration object.
  209. func (w *Wrapper) Options() OptionsConfiguration {
  210. w.mut.Lock()
  211. defer w.mut.Unlock()
  212. return w.cfg.Options
  213. }
  214. // SetOptions replaces the current options configuration object.
  215. func (w *Wrapper) SetOptions(opts OptionsConfiguration) CommitResponse {
  216. w.mut.Lock()
  217. defer w.mut.Unlock()
  218. newCfg := w.cfg.Copy()
  219. newCfg.Options = opts
  220. return w.replaceLocked(newCfg)
  221. }
  222. // GUI returns the current GUI configuration object.
  223. func (w *Wrapper) GUI() GUIConfiguration {
  224. w.mut.Lock()
  225. defer w.mut.Unlock()
  226. return w.cfg.GUI
  227. }
  228. // SetGUI replaces the current GUI configuration object.
  229. func (w *Wrapper) SetGUI(gui GUIConfiguration) CommitResponse {
  230. w.mut.Lock()
  231. defer w.mut.Unlock()
  232. newCfg := w.cfg.Copy()
  233. newCfg.GUI = gui
  234. return w.replaceLocked(newCfg)
  235. }
  236. // IgnoredDevice returns whether or not connection attempts from the given
  237. // device should be silently ignored.
  238. func (w *Wrapper) IgnoredDevice(id protocol.DeviceID) bool {
  239. w.mut.Lock()
  240. defer w.mut.Unlock()
  241. for _, device := range w.cfg.IgnoredDevices {
  242. if device == id {
  243. return true
  244. }
  245. }
  246. return false
  247. }
  248. // Save writes the configuration to disk, and generates a ConfigSaved event.
  249. func (w *Wrapper) Save() error {
  250. fd, err := osutil.CreateAtomic(w.path, 0600)
  251. if err != nil {
  252. return err
  253. }
  254. if err := w.cfg.WriteXML(fd); err != nil {
  255. fd.Close()
  256. return err
  257. }
  258. if err := fd.Close(); err != nil {
  259. return err
  260. }
  261. events.Default.Log(events.ConfigSaved, w.cfg)
  262. return nil
  263. }