wrapper.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "sync"
  12. "github.com/syncthing/protocol"
  13. "github.com/syncthing/syncthing/internal/events"
  14. "github.com/syncthing/syncthing/internal/osutil"
  15. )
  16. // An interface to handle configuration changes, and a wrapper type á la
  17. // http.Handler
  18. type Handler interface {
  19. Changed(Configuration) error
  20. }
  21. type HandlerFunc func(Configuration) error
  22. func (fn HandlerFunc) Changed(cfg Configuration) error {
  23. return fn(cfg)
  24. }
  25. // A wrapper around a Configuration that manages loads, saves and published
  26. // notifications of changes to registered Handlers
  27. type Wrapper struct {
  28. cfg Configuration
  29. path string
  30. deviceMap map[protocol.DeviceID]DeviceConfiguration
  31. folderMap map[string]FolderConfiguration
  32. replaces chan Configuration
  33. mut sync.Mutex
  34. subs []Handler
  35. sMut sync.Mutex
  36. }
  37. // Wrap wraps an existing Configuration structure and ties it to a file on
  38. // disk.
  39. func Wrap(path string, cfg Configuration) *Wrapper {
  40. w := &Wrapper{cfg: cfg, path: path}
  41. w.replaces = make(chan Configuration)
  42. go w.Serve()
  43. return w
  44. }
  45. // Load loads an existing file on disk and returns a new configuration
  46. // wrapper.
  47. func Load(path string, myID protocol.DeviceID) (*Wrapper, error) {
  48. fd, err := os.Open(path)
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer fd.Close()
  53. cfg, err := ReadXML(fd, myID)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return Wrap(path, cfg), nil
  58. }
  59. // Serve handles configuration replace events and calls any interested
  60. // handlers. It is started automatically by Wrap() and Load() and should not
  61. // be run manually.
  62. func (w *Wrapper) Serve() {
  63. for cfg := range w.replaces {
  64. w.sMut.Lock()
  65. subs := w.subs
  66. w.sMut.Unlock()
  67. for _, h := range subs {
  68. h.Changed(cfg)
  69. }
  70. }
  71. }
  72. // Stop stops the Serve() loop. Set and Replace operations will panic after a
  73. // Stop.
  74. func (w *Wrapper) Stop() {
  75. close(w.replaces)
  76. }
  77. // Subscribe registers the given handler to be called on any future
  78. // configuration changes.
  79. func (w *Wrapper) Subscribe(h Handler) {
  80. w.sMut.Lock()
  81. w.subs = append(w.subs, h)
  82. w.sMut.Unlock()
  83. }
  84. // Raw returns the currently wrapped Configuration object.
  85. func (w *Wrapper) Raw() Configuration {
  86. return w.cfg
  87. }
  88. // Replace swaps the current configuration object for the given one.
  89. func (w *Wrapper) Replace(cfg Configuration) {
  90. w.mut.Lock()
  91. defer w.mut.Unlock()
  92. w.cfg = cfg
  93. w.deviceMap = nil
  94. w.folderMap = nil
  95. w.replaces <- cfg.Copy()
  96. }
  97. // Devices returns a map of devices. Device structures should not be changed,
  98. // other than for the purpose of updating via SetDevice().
  99. func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
  100. w.mut.Lock()
  101. defer w.mut.Unlock()
  102. if w.deviceMap == nil {
  103. w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
  104. for _, dev := range w.cfg.Devices {
  105. w.deviceMap[dev.DeviceID] = dev
  106. }
  107. }
  108. return w.deviceMap
  109. }
  110. // SetDevice adds a new device to the configuration, or overwrites an existing
  111. // device with the same ID.
  112. func (w *Wrapper) SetDevice(dev DeviceConfiguration) {
  113. w.mut.Lock()
  114. defer w.mut.Unlock()
  115. w.deviceMap = nil
  116. for i := range w.cfg.Devices {
  117. if w.cfg.Devices[i].DeviceID == dev.DeviceID {
  118. w.cfg.Devices[i] = dev
  119. w.replaces <- w.cfg.Copy()
  120. return
  121. }
  122. }
  123. w.cfg.Devices = append(w.cfg.Devices, dev)
  124. w.replaces <- w.cfg.Copy()
  125. }
  126. // Devices returns a map of folders. Folder structures should not be changed,
  127. // other than for the purpose of updating via SetFolder().
  128. func (w *Wrapper) Folders() map[string]FolderConfiguration {
  129. w.mut.Lock()
  130. defer w.mut.Unlock()
  131. if w.folderMap == nil {
  132. w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
  133. for _, fld := range w.cfg.Folders {
  134. w.folderMap[fld.ID] = fld
  135. }
  136. }
  137. return w.folderMap
  138. }
  139. // SetFolder adds a new folder to the configuration, or overwrites an existing
  140. // folder with the same ID.
  141. func (w *Wrapper) SetFolder(fld FolderConfiguration) {
  142. w.mut.Lock()
  143. defer w.mut.Unlock()
  144. w.folderMap = nil
  145. for i := range w.cfg.Folders {
  146. if w.cfg.Folders[i].ID == fld.ID {
  147. w.cfg.Folders[i] = fld
  148. w.replaces <- w.cfg.Copy()
  149. return
  150. }
  151. }
  152. w.cfg.Folders = append(w.cfg.Folders, fld)
  153. w.replaces <- w.cfg.Copy()
  154. }
  155. // Options returns the current options configuration object.
  156. func (w *Wrapper) Options() OptionsConfiguration {
  157. w.mut.Lock()
  158. defer w.mut.Unlock()
  159. return w.cfg.Options
  160. }
  161. // SetOptions replaces the current options configuration object.
  162. func (w *Wrapper) SetOptions(opts OptionsConfiguration) {
  163. w.mut.Lock()
  164. defer w.mut.Unlock()
  165. w.cfg.Options = opts
  166. w.replaces <- w.cfg.Copy()
  167. }
  168. // GUI returns the current GUI configuration object.
  169. func (w *Wrapper) GUI() GUIConfiguration {
  170. w.mut.Lock()
  171. defer w.mut.Unlock()
  172. return w.cfg.GUI
  173. }
  174. // SetGUI replaces the current GUI configuration object.
  175. func (w *Wrapper) SetGUI(gui GUIConfiguration) {
  176. w.mut.Lock()
  177. defer w.mut.Unlock()
  178. w.cfg.GUI = gui
  179. w.replaces <- w.cfg.Copy()
  180. }
  181. // Sets the folder error state. Emits ConfigSaved to cause a GUI refresh.
  182. func (w *Wrapper) SetFolderError(id string, err error) {
  183. w.mut.Lock()
  184. defer w.mut.Unlock()
  185. w.folderMap = nil
  186. for i := range w.cfg.Folders {
  187. if w.cfg.Folders[i].ID == id {
  188. errstr := ""
  189. if err != nil {
  190. errstr = err.Error()
  191. }
  192. if errstr != w.cfg.Folders[i].Invalid {
  193. w.cfg.Folders[i].Invalid = errstr
  194. events.Default.Log(events.ConfigSaved, w.cfg)
  195. w.replaces <- w.cfg.Copy()
  196. }
  197. return
  198. }
  199. }
  200. }
  201. // Returns whether or not connection attempts from the given device should be
  202. // silently ignored.
  203. func (w *Wrapper) IgnoredDevice(id protocol.DeviceID) bool {
  204. w.mut.Lock()
  205. defer w.mut.Unlock()
  206. for _, device := range w.cfg.IgnoredDevices {
  207. if device == id {
  208. return true
  209. }
  210. }
  211. return false
  212. }
  213. // Save writes the configuration to disk, and generates a ConfigSaved event.
  214. func (w *Wrapper) Save() error {
  215. fd, err := ioutil.TempFile(filepath.Dir(w.path), "cfg")
  216. if err != nil {
  217. return err
  218. }
  219. defer os.Remove(fd.Name())
  220. err = w.cfg.WriteXML(fd)
  221. if err != nil {
  222. fd.Close()
  223. return err
  224. }
  225. err = fd.Close()
  226. if err != nil {
  227. return err
  228. }
  229. events.Default.Log(events.ConfigSaved, w.cfg)
  230. return osutil.Rename(fd.Name(), w.path)
  231. }