rofolder.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "context"
  9. "fmt"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/versioner"
  13. )
  14. func init() {
  15. folderFactories[config.FolderTypeSendOnly] = newSendOnlyFolder
  16. }
  17. type sendOnlyFolder struct {
  18. folder
  19. config.FolderConfiguration
  20. }
  21. func newSendOnlyFolder(model *Model, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem) service {
  22. ctx, cancel := context.WithCancel(context.Background())
  23. return &sendOnlyFolder{
  24. folder: folder{
  25. stateTracker: newStateTracker(cfg.ID),
  26. scan: newFolderScanner(cfg),
  27. ctx: ctx,
  28. cancel: cancel,
  29. model: model,
  30. initialScanFinished: make(chan struct{}),
  31. },
  32. FolderConfiguration: cfg,
  33. }
  34. }
  35. func (f *sendOnlyFolder) Serve() {
  36. l.Debugln(f, "starting")
  37. defer l.Debugln(f, "exiting")
  38. defer func() {
  39. f.scan.timer.Stop()
  40. }()
  41. for {
  42. select {
  43. case <-f.ctx.Done():
  44. return
  45. case <-f.scan.timer.C:
  46. l.Debugln(f, "Scanning subdirectories")
  47. err := f.scanSubdirs(nil)
  48. select {
  49. case <-f.initialScanFinished:
  50. default:
  51. status := "Completed"
  52. if err != nil {
  53. status = "Failed"
  54. }
  55. l.Infoln(status, "initial scan (ro) of", f.Description())
  56. close(f.initialScanFinished)
  57. }
  58. if f.scan.HasNoInterval() {
  59. continue
  60. }
  61. f.scan.Reschedule()
  62. case req := <-f.scan.now:
  63. req.err <- f.scanSubdirs(req.subdirs)
  64. case next := <-f.scan.delay:
  65. f.scan.timer.Reset(next)
  66. }
  67. }
  68. }
  69. func (f *sendOnlyFolder) String() string {
  70. return fmt.Sprintf("sendOnlyFolder/%s@%p", f.folderID, f)
  71. }