rofolder.go 2.2 KB

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