scanner.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "fmt"
  18. "time"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. )
  21. type Scanner struct {
  22. folder string
  23. intv time.Duration
  24. model *Model
  25. stop chan struct{}
  26. }
  27. func (s *Scanner) Serve() {
  28. if debug {
  29. l.Debugln(s, "starting")
  30. defer l.Debugln(s, "exiting")
  31. }
  32. timer := time.NewTimer(time.Millisecond)
  33. defer timer.Stop()
  34. initialScanCompleted := false
  35. for {
  36. select {
  37. case <-s.stop:
  38. return
  39. case <-timer.C:
  40. if debug {
  41. l.Debugln(s, "rescan")
  42. }
  43. s.model.setState(s.folder, FolderScanning)
  44. if err := s.model.ScanFolder(s.folder); err != nil {
  45. s.model.cfg.InvalidateFolder(s.folder, err.Error())
  46. return
  47. }
  48. s.model.setState(s.folder, FolderIdle)
  49. if !initialScanCompleted {
  50. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  51. initialScanCompleted = true
  52. }
  53. if s.intv == 0 {
  54. return
  55. }
  56. timer.Reset(s.intv)
  57. }
  58. }
  59. }
  60. func (s *Scanner) Stop() {
  61. close(s.stop)
  62. }
  63. func (s *Scanner) String() string {
  64. return fmt.Sprintf("scanner/%s@%p", s.folder, s)
  65. }
  66. func (s *Scanner) Bump(string) {}
  67. func (s *Scanner) Jobs() ([]protocol.FileInfoTruncated, []protocol.FileInfoTruncated) {
  68. return nil, nil
  69. }