rofolder.go 2.0 KB

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