rofolder.go 2.3 KB

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