folder.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "context"
  9. "errors"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/config"
  12. "github.com/syncthing/syncthing/lib/ignore"
  13. "github.com/syncthing/syncthing/lib/sync"
  14. "github.com/syncthing/syncthing/lib/watchaggregator"
  15. )
  16. var errWatchNotStarted error = errors.New("not started")
  17. type folder struct {
  18. stateTracker
  19. config.FolderConfiguration
  20. scan folderScanner
  21. model *Model
  22. ctx context.Context
  23. cancel context.CancelFunc
  24. initialScanFinished chan struct{}
  25. watchCancel context.CancelFunc
  26. watchChan chan []string
  27. restartWatchChan chan struct{}
  28. watchErr error
  29. watchErrMut sync.Mutex
  30. }
  31. func newFolder(model *Model, cfg config.FolderConfiguration) folder {
  32. ctx, cancel := context.WithCancel(context.Background())
  33. return folder{
  34. stateTracker: newStateTracker(cfg.ID),
  35. FolderConfiguration: cfg,
  36. scan: newFolderScanner(cfg),
  37. ctx: ctx,
  38. cancel: cancel,
  39. model: model,
  40. initialScanFinished: make(chan struct{}),
  41. watchCancel: func() {},
  42. watchErr: errWatchNotStarted,
  43. watchErrMut: sync.NewMutex(),
  44. }
  45. }
  46. func (f *folder) BringToFront(string) {}
  47. func (f *folder) DelayScan(next time.Duration) {
  48. f.scan.Delay(next)
  49. }
  50. func (f *folder) IgnoresUpdated() {
  51. if f.FSWatcherEnabled {
  52. f.scheduleWatchRestart()
  53. }
  54. }
  55. func (f *folder) SchedulePull() {}
  56. func (f *folder) Jobs() ([]string, []string) {
  57. return nil, nil
  58. }
  59. func (f *folder) Scan(subdirs []string) error {
  60. <-f.initialScanFinished
  61. return f.scan.Scan(subdirs)
  62. }
  63. func (f *folder) Stop() {
  64. f.cancel()
  65. }
  66. // CheckHealth checks the folder for common errors, updates the folder state
  67. // and returns the current folder error, or nil if the folder is healthy.
  68. func (f *folder) CheckHealth() error {
  69. err := f.getHealthError()
  70. f.setError(err)
  71. return err
  72. }
  73. func (f *folder) getHealthError() error {
  74. // Check for folder errors, with the most serious and specific first and
  75. // generic ones like out of space on the home disk later.
  76. if err := f.CheckPath(); err != nil {
  77. return err
  78. }
  79. if err := f.CheckFreeSpace(); err != nil {
  80. return err
  81. }
  82. if err := f.model.cfg.CheckHomeFreeSpace(); err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func (f *folder) scanSubdirs(subDirs []string) error {
  88. if err := f.model.internalScanFolderSubdirs(f.ctx, f.folderID, subDirs); err != nil {
  89. // Potentially sets the error twice, once in the scanner just
  90. // by doing a check, and once here, if the error returned is
  91. // the same one as returned by CheckHealth, though
  92. // duplicate set is handled by setError.
  93. f.setError(err)
  94. return err
  95. }
  96. return nil
  97. }
  98. func (f *folder) scanTimerFired() {
  99. err := f.scanSubdirs(nil)
  100. select {
  101. case <-f.initialScanFinished:
  102. default:
  103. status := "Completed"
  104. if err != nil {
  105. status = "Failed"
  106. }
  107. l.Infoln(status, "initial scan of", f.Type.String(), "folder", f.Description())
  108. close(f.initialScanFinished)
  109. }
  110. f.scan.Reschedule()
  111. }
  112. func (f *folder) WatchError() error {
  113. f.watchErrMut.Lock()
  114. defer f.watchErrMut.Unlock()
  115. return f.watchErr
  116. }
  117. // stopWatch immediately aborts watching and may be called asynchronously
  118. func (f *folder) stopWatch() {
  119. f.watchCancel()
  120. f.watchErrMut.Lock()
  121. f.watchErr = errWatchNotStarted
  122. f.watchErrMut.Unlock()
  123. }
  124. // scheduleWatchRestart makes sure watching is restarted from the main for loop
  125. // in a folder's Serve and thus may be called asynchronously (e.g. when ignores change).
  126. func (f *folder) scheduleWatchRestart() {
  127. select {
  128. case f.restartWatchChan <- struct{}{}:
  129. default:
  130. // We might be busy doing a pull and thus not reading from this
  131. // channel. The channel is 1-buffered, so one notification will be
  132. // queued to ensure we recheck after the pull.
  133. }
  134. }
  135. // restartWatch should only ever be called synchronously. If you want to use
  136. // this asynchronously, you should probably use scheduleWatchRestart instead.
  137. func (f *folder) restartWatch() {
  138. f.stopWatch()
  139. f.startWatch()
  140. f.Scan(nil)
  141. }
  142. // startWatch should only ever be called synchronously. If you want to use
  143. // this asynchronously, you should probably use scheduleWatchRestart instead.
  144. func (f *folder) startWatch() {
  145. ctx, cancel := context.WithCancel(f.ctx)
  146. f.model.fmut.RLock()
  147. ignores := f.model.folderIgnores[f.folderID]
  148. f.model.fmut.RUnlock()
  149. f.watchChan = make(chan []string)
  150. f.watchCancel = cancel
  151. go f.startWatchAsync(ctx, ignores)
  152. }
  153. // startWatchAsync tries to start the filesystem watching and retries every minute on failure.
  154. // It is a convenience function that should not be used except in startWatch.
  155. func (f *folder) startWatchAsync(ctx context.Context, ignores *ignore.Matcher) {
  156. timer := time.NewTimer(0)
  157. for {
  158. select {
  159. case <-timer.C:
  160. eventChan, err := f.Filesystem().Watch(".", ignores, ctx, f.IgnorePerms)
  161. f.watchErrMut.Lock()
  162. prevErr := f.watchErr
  163. f.watchErr = err
  164. f.watchErrMut.Unlock()
  165. if err != nil {
  166. if prevErr == errWatchNotStarted {
  167. l.Warnf("Failed to start filesystem watcher for folder %s: %v", f.Description(), err)
  168. } else {
  169. l.Debugf("Failed to start filesystem watcher for folder %s again: %v", f.Description(), err)
  170. }
  171. timer.Reset(time.Minute)
  172. continue
  173. }
  174. watchaggregator.Aggregate(eventChan, f.watchChan, f.FolderConfiguration, f.model.cfg, ctx)
  175. l.Debugln("Started filesystem watcher for folder", f.Description())
  176. return
  177. case <-ctx.Done():
  178. return
  179. }
  180. }
  181. }
  182. func (f *folder) setError(err error) {
  183. _, _, oldErr := f.getState()
  184. if (err != nil && oldErr != nil && oldErr.Error() == err.Error()) || (err == nil && oldErr == nil) {
  185. return
  186. }
  187. if err != nil {
  188. if oldErr == nil {
  189. l.Warnf("Error on folder %s: %v", f.Description(), err)
  190. } else {
  191. l.Infof("Error on folder %s changed: %q -> %q", f.Description(), oldErr, err)
  192. }
  193. } else {
  194. l.Infoln("Cleared error on folder", f.Description())
  195. }
  196. if f.FSWatcherEnabled {
  197. if err != nil {
  198. f.stopWatch()
  199. } else {
  200. f.scheduleWatchRestart()
  201. }
  202. }
  203. f.stateTracker.setError(err)
  204. }