wrapper.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. path, err := osutil.ExpandTilde(fld.Path)
  135. if err != nil {
  136. l.Warnln("home:", err)
  137. continue
  138. }
  139. fld.Path = path
  140. w.folderMap[fld.ID] = fld
  141. }
  142. }
  143. return w.folderMap
  144. }
  145. // SetFolder adds a new folder to the configuration, or overwrites an existing
  146. // folder with the same ID.
  147. func (w *Wrapper) SetFolder(fld FolderConfiguration) {
  148. w.mut.Lock()
  149. defer w.mut.Unlock()
  150. w.folderMap = nil
  151. for i := range w.cfg.Folders {
  152. if w.cfg.Folders[i].ID == fld.ID {
  153. w.cfg.Folders[i] = fld
  154. w.replaces <- w.cfg.Copy()
  155. return
  156. }
  157. }
  158. w.cfg.Folders = append(w.cfg.Folders, fld)
  159. w.replaces <- w.cfg.Copy()
  160. }
  161. // Options returns the current options configuration object.
  162. func (w *Wrapper) Options() OptionsConfiguration {
  163. w.mut.Lock()
  164. defer w.mut.Unlock()
  165. return w.cfg.Options
  166. }
  167. // SetOptions replaces the current options configuration object.
  168. func (w *Wrapper) SetOptions(opts OptionsConfiguration) {
  169. w.mut.Lock()
  170. defer w.mut.Unlock()
  171. w.cfg.Options = opts
  172. w.replaces <- w.cfg.Copy()
  173. }
  174. // GUI returns the current GUI configuration object.
  175. func (w *Wrapper) GUI() GUIConfiguration {
  176. w.mut.Lock()
  177. defer w.mut.Unlock()
  178. return w.cfg.GUI
  179. }
  180. // SetGUI replaces the current GUI configuration object.
  181. func (w *Wrapper) SetGUI(gui GUIConfiguration) {
  182. w.mut.Lock()
  183. defer w.mut.Unlock()
  184. w.cfg.GUI = gui
  185. w.replaces <- w.cfg.Copy()
  186. }
  187. // Sets the folder error state. Emits ConfigSaved to cause a GUI refresh.
  188. func (w *Wrapper) SetFolderError(id string, err error) {
  189. w.mut.Lock()
  190. defer w.mut.Unlock()
  191. w.folderMap = nil
  192. for i := range w.cfg.Folders {
  193. if w.cfg.Folders[i].ID == id {
  194. errstr := ""
  195. if err != nil {
  196. errstr = err.Error()
  197. }
  198. if errstr != w.cfg.Folders[i].Invalid {
  199. w.cfg.Folders[i].Invalid = errstr
  200. events.Default.Log(events.ConfigSaved, w.cfg)
  201. w.replaces <- w.cfg.Copy()
  202. }
  203. return
  204. }
  205. }
  206. }
  207. // Returns whether or not connection attempts from the given device should be
  208. // silently ignored.
  209. func (w *Wrapper) IgnoredDevice(id protocol.DeviceID) bool {
  210. w.mut.Lock()
  211. defer w.mut.Unlock()
  212. for _, device := range w.cfg.IgnoredDevices {
  213. if device == id {
  214. return true
  215. }
  216. }
  217. return false
  218. }
  219. // Save writes the configuration to disk, and generates a ConfigSaved event.
  220. func (w *Wrapper) Save() error {
  221. fd, err := ioutil.TempFile(filepath.Dir(w.path), "cfg")
  222. if err != nil {
  223. return err
  224. }
  225. defer os.Remove(fd.Name())
  226. err = w.cfg.WriteXML(fd)
  227. if err != nil {
  228. fd.Close()
  229. return err
  230. }
  231. err = fd.Close()
  232. if err != nil {
  233. return err
  234. }
  235. events.Default.Log(events.ConfigSaved, w.cfg)
  236. return osutil.Rename(fd.Name(), w.path)
  237. }