rofolder.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 model
  7. import (
  8. "fmt"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. )
  13. type roFolder struct {
  14. folder
  15. }
  16. func newROFolder(model *Model, cfg config.FolderConfiguration) *roFolder {
  17. return &roFolder{
  18. folder: folder{
  19. stateTracker: stateTracker{
  20. folderID: cfg.ID,
  21. mut: sync.NewMutex(),
  22. },
  23. scan: folderscan{
  24. interval: time.Duration(cfg.RescanIntervalS) * time.Second,
  25. timer: time.NewTimer(time.Millisecond),
  26. now: make(chan rescanRequest),
  27. delay: make(chan time.Duration),
  28. },
  29. stop: make(chan struct{}),
  30. model: model,
  31. },
  32. }
  33. }
  34. func (f *roFolder) Serve() {
  35. l.Debugln(f, "starting")
  36. defer l.Debugln(f, "exiting")
  37. defer func() {
  38. f.scan.timer.Stop()
  39. }()
  40. initialScanCompleted := false
  41. for {
  42. select {
  43. case <-f.stop:
  44. return
  45. case <-f.scan.timer.C:
  46. if err := f.model.CheckFolderHealth(f.folderID); err != nil {
  47. l.Infoln("Skipping folder", f.folderID, "scan due to folder error:", err)
  48. f.scan.reschedule()
  49. continue
  50. }
  51. l.Debugln(f, "rescan")
  52. if err := f.model.internalScanFolderSubdirs(f.folderID, nil); err != nil {
  53. // Potentially sets the error twice, once in the scanner just
  54. // by doing a check, and once here, if the error returned is
  55. // the same one as returned by CheckFolderHealth, though
  56. // duplicate set is handled by setError.
  57. f.setError(err)
  58. f.scan.reschedule()
  59. continue
  60. }
  61. if !initialScanCompleted {
  62. l.Infoln("Completed initial scan (ro) of folder", f.folderID)
  63. initialScanCompleted = true
  64. }
  65. if f.scan.interval == 0 {
  66. continue
  67. }
  68. f.scan.reschedule()
  69. case req := <-f.scan.now:
  70. req.err <- f.scanSubdirsIfHealthy(req.subdirs)
  71. case next := <-f.scan.delay:
  72. f.scan.timer.Reset(next)
  73. }
  74. }
  75. }
  76. func (f *roFolder) String() string {
  77. return fmt.Sprintf("roFolder/%s@%p", f.folderID, f)
  78. }