wrapper.go 5.9 KB

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