rofolder.go 2.1 KB

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