folder.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "time"
  10. "github.com/syncthing/syncthing/lib/config"
  11. )
  12. type folder struct {
  13. stateTracker
  14. config.FolderConfiguration
  15. scan folderScanner
  16. model *Model
  17. ctx context.Context
  18. cancel context.CancelFunc
  19. initialScanFinished chan struct{}
  20. }
  21. func newFolder(model *Model, cfg config.FolderConfiguration) folder {
  22. ctx, cancel := context.WithCancel(context.Background())
  23. return folder{
  24. stateTracker: newStateTracker(cfg.ID),
  25. FolderConfiguration: cfg,
  26. scan: newFolderScanner(cfg),
  27. ctx: ctx,
  28. cancel: cancel,
  29. model: model,
  30. initialScanFinished: make(chan struct{}),
  31. }
  32. }
  33. func (f *folder) IndexUpdated() {
  34. }
  35. func (f *folder) DelayScan(next time.Duration) {
  36. f.scan.Delay(next)
  37. }
  38. func (f *folder) Scan(subdirs []string) error {
  39. <-f.initialScanFinished
  40. return f.scan.Scan(subdirs)
  41. }
  42. func (f *folder) Stop() {
  43. f.cancel()
  44. }
  45. func (f *folder) Jobs() ([]string, []string) {
  46. return nil, nil
  47. }
  48. func (f *folder) BringToFront(string) {}
  49. func (f *folder) BlockStats() map[string]int {
  50. return nil
  51. }
  52. func (f *folder) scanSubdirs(subDirs []string) error {
  53. if err := f.model.internalScanFolderSubdirs(f.ctx, f.folderID, subDirs); err != nil {
  54. // Potentially sets the error twice, once in the scanner just
  55. // by doing a check, and once here, if the error returned is
  56. // the same one as returned by CheckFolderHealth, though
  57. // duplicate set is handled by setError.
  58. f.setError(err)
  59. return err
  60. }
  61. return nil
  62. }
  63. func (f *folder) scanTimerFired() {
  64. err := f.scanSubdirs(nil)
  65. select {
  66. case <-f.initialScanFinished:
  67. default:
  68. status := "Completed"
  69. if err != nil {
  70. status = "Failed"
  71. }
  72. l.Infoln(status, "initial scan of", f.Type.String(), "folder", f.Description())
  73. close(f.initialScanFinished)
  74. }
  75. f.scan.Reschedule()
  76. }