folder.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) scanSubdirs(subDirs []string) error {
  50. if err := f.model.internalScanFolderSubdirs(f.ctx, f.folderID, subDirs); err != nil {
  51. // Potentially sets the error twice, once in the scanner just
  52. // by doing a check, and once here, if the error returned is
  53. // the same one as returned by CheckFolderHealth, though
  54. // duplicate set is handled by setError.
  55. f.setError(err)
  56. return err
  57. }
  58. return nil
  59. }
  60. func (f *folder) scanTimerFired() {
  61. err := f.scanSubdirs(nil)
  62. select {
  63. case <-f.initialScanFinished:
  64. default:
  65. status := "Completed"
  66. if err != nil {
  67. status = "Failed"
  68. }
  69. l.Infoln(status, "initial scan of", f.Type.String(), "folder", f.Description())
  70. close(f.initialScanFinished)
  71. }
  72. f.scan.Reschedule()
  73. }