wrapper.go 6.4 KB

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