rofolder.go 2.2 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 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. initialScanCompleted: make(chan struct{}),
  28. },
  29. FolderConfiguration: cfg,
  30. }
  31. }
  32. func (f *sendOnlyFolder) Serve() {
  33. l.Debugln(f, "starting")
  34. defer l.Debugln(f, "exiting")
  35. defer func() {
  36. f.scan.timer.Stop()
  37. }()
  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. select {
  59. case <-f.initialScanCompleted:
  60. default:
  61. l.Infoln("Completed initial scan (ro) of", f.Description())
  62. close(f.initialScanCompleted)
  63. }
  64. if f.scan.HasNoInterval() {
  65. continue
  66. }
  67. f.scan.Reschedule()
  68. case req := <-f.scan.now:
  69. req.err <- f.scanSubdirsIfHealthy(req.subdirs)
  70. case next := <-f.scan.delay:
  71. f.scan.timer.Reset(next)
  72. }
  73. }
  74. }
  75. func (f *sendOnlyFolder) String() string {
  76. return fmt.Sprintf("sendOnlyFolder/%s@%p", f.folderID, f)
  77. }