wrapper.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package config
  16. import (
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "sync"
  21. "github.com/syncthing/syncthing/internal/events"
  22. "github.com/syncthing/syncthing/internal/osutil"
  23. "github.com/syncthing/syncthing/internal/protocol"
  24. )
  25. // An interface to handle configuration changes, and a wrapper type á la
  26. // http.Handler
  27. type Handler interface {
  28. Changed(Configuration) error
  29. }
  30. type HandlerFunc func(Configuration) error
  31. func (fn HandlerFunc) Changed(cfg Configuration) error {
  32. return fn(cfg)
  33. }
  34. // A wrapper around a Configuration that manages loads, saves and published
  35. // notifications of changes to registered Handlers
  36. type ConfigWrapper struct {
  37. cfg Configuration
  38. path string
  39. deviceMap map[protocol.DeviceID]DeviceConfiguration
  40. folderMap map[string]FolderConfiguration
  41. replaces chan Configuration
  42. subs []Handler
  43. mut sync.Mutex
  44. }
  45. // Wrap wraps an existing Configuration structure and ties it to a file on
  46. // disk.
  47. func Wrap(path string, cfg Configuration) *ConfigWrapper {
  48. w := &ConfigWrapper{cfg: cfg, path: path}
  49. w.replaces = make(chan Configuration)
  50. go w.Serve()
  51. return w
  52. }
  53. // Load loads an existing file on disk and returns a new configuration
  54. // wrapper.
  55. func Load(path string, myID protocol.DeviceID) (*ConfigWrapper, error) {
  56. fd, err := os.Open(path)
  57. if err != nil {
  58. return nil, err
  59. }
  60. defer fd.Close()
  61. cfg, err := ReadXML(fd, myID)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return Wrap(path, cfg), nil
  66. }
  67. // Serve handles configuration replace events and calls any interested
  68. // handlers. It is started automatically by Wrap() and Load() and should not
  69. // be run manually.
  70. func (w *ConfigWrapper) Serve() {
  71. for cfg := range w.replaces {
  72. w.mut.Lock()
  73. subs := w.subs
  74. w.mut.Unlock()
  75. for _, h := range subs {
  76. h.Changed(cfg)
  77. }
  78. }
  79. }
  80. // Stop stops the Serve() loop.
  81. func (w *ConfigWrapper) Stop() {
  82. close(w.replaces)
  83. }
  84. // Subscriber registers the given handler to be called on any future
  85. // configuration changes.
  86. func (w *ConfigWrapper) Subscribe(h Handler) {
  87. w.mut.Lock()
  88. w.subs = append(w.subs, h)
  89. w.mut.Unlock()
  90. }
  91. // Raw returns the currently wrapped Configuration object.
  92. func (w *ConfigWrapper) Raw() Configuration {
  93. return w.cfg
  94. }
  95. // Replace swaps the current configuration object for the given one.
  96. func (w *ConfigWrapper) Replace(cfg Configuration) {
  97. w.mut.Lock()
  98. defer w.mut.Unlock()
  99. w.cfg = cfg
  100. w.deviceMap = nil
  101. w.folderMap = nil
  102. w.replaces <- cfg
  103. }
  104. // Devices returns a map of devices. Device structures should not be changed,
  105. // other than for the purpose of updating via SetDevice().
  106. func (w *ConfigWrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
  107. w.mut.Lock()
  108. defer w.mut.Unlock()
  109. if w.deviceMap == nil {
  110. w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
  111. for _, dev := range w.cfg.Devices {
  112. w.deviceMap[dev.DeviceID] = dev
  113. }
  114. }
  115. return w.deviceMap
  116. }
  117. // SetDevice adds a new device to the configuration, or overwrites an existing
  118. // device with the same ID.
  119. func (w *ConfigWrapper) SetDevice(dev DeviceConfiguration) {
  120. w.mut.Lock()
  121. defer w.mut.Unlock()
  122. w.deviceMap = nil
  123. for i := range w.cfg.Devices {
  124. if w.cfg.Devices[i].DeviceID == dev.DeviceID {
  125. w.cfg.Devices[i] = dev
  126. w.replaces <- w.cfg
  127. return
  128. }
  129. }
  130. w.cfg.Devices = append(w.cfg.Devices, dev)
  131. w.replaces <- w.cfg
  132. }
  133. // Devices returns a map of folders. Folder structures should not be changed,
  134. // other than for the purpose of updating via SetFolder().
  135. func (w *ConfigWrapper) Folders() map[string]FolderConfiguration {
  136. w.mut.Lock()
  137. defer w.mut.Unlock()
  138. if w.folderMap == nil {
  139. w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
  140. for _, fld := range w.cfg.Folders {
  141. w.folderMap[fld.ID] = fld
  142. }
  143. }
  144. return w.folderMap
  145. }
  146. // SetFolder adds a new folder to the configuration, or overwrites an existing
  147. // folder with the same ID.
  148. func (w *ConfigWrapper) SetFolder(fld FolderConfiguration) {
  149. w.mut.Lock()
  150. defer w.mut.Unlock()
  151. w.folderMap = nil
  152. for i := range w.cfg.Folders {
  153. if w.cfg.Folders[i].ID == fld.ID {
  154. w.cfg.Folders[i] = fld
  155. w.replaces <- w.cfg
  156. return
  157. }
  158. }
  159. w.cfg.Folders = append(w.cfg.Folders, fld)
  160. w.replaces <- w.cfg
  161. }
  162. // Options returns the current options configuration object.
  163. func (w *ConfigWrapper) Options() OptionsConfiguration {
  164. w.mut.Lock()
  165. defer w.mut.Unlock()
  166. return w.cfg.Options
  167. }
  168. // SetOptions replaces the current options configuration object.
  169. func (w *ConfigWrapper) SetOptions(opts OptionsConfiguration) {
  170. w.mut.Lock()
  171. defer w.mut.Unlock()
  172. w.cfg.Options = opts
  173. w.replaces <- w.cfg
  174. }
  175. // GUI returns the current GUI configuration object.
  176. func (w *ConfigWrapper) GUI() GUIConfiguration {
  177. w.mut.Lock()
  178. defer w.mut.Unlock()
  179. return w.cfg.GUI
  180. }
  181. // SetGUI replaces the current GUI configuration object.
  182. func (w *ConfigWrapper) SetGUI(gui GUIConfiguration) {
  183. w.mut.Lock()
  184. defer w.mut.Unlock()
  185. w.cfg.GUI = gui
  186. w.replaces <- w.cfg
  187. }
  188. // InvalidateFolder sets the invalid marker on the given folder.
  189. func (w *ConfigWrapper) InvalidateFolder(id string, err string) {
  190. w.mut.Lock()
  191. defer w.mut.Unlock()
  192. w.folderMap = nil
  193. for i := range w.cfg.Folders {
  194. if w.cfg.Folders[i].ID == id {
  195. w.cfg.Folders[i].Invalid = err
  196. w.replaces <- w.cfg
  197. return
  198. }
  199. }
  200. }
  201. // Save writes the configuration to disk, and generates a ConfigSaved event.
  202. func (w *ConfigWrapper) Save() error {
  203. fd, err := ioutil.TempFile(filepath.Dir(w.path), "cfg")
  204. if err != nil {
  205. return err
  206. }
  207. err = w.cfg.WriteXML(fd)
  208. if err != nil {
  209. fd.Close()
  210. return err
  211. }
  212. err = fd.Close()
  213. if err != nil {
  214. return err
  215. }
  216. events.Default.Log(events.ConfigSaved, w.cfg)
  217. return osutil.Rename(fd.Name(), w.path)
  218. }