scanner.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. )
  20. type Scanner struct {
  21. folder string
  22. intv time.Duration
  23. model *Model
  24. stop chan struct{}
  25. }
  26. func (s *Scanner) Serve() {
  27. if debug {
  28. l.Debugln(s, "starting")
  29. defer l.Debugln(s, "exiting")
  30. }
  31. timer := time.NewTimer(time.Millisecond)
  32. defer timer.Stop()
  33. initialScanCompleted := false
  34. for {
  35. select {
  36. case <-s.stop:
  37. return
  38. case <-timer.C:
  39. if debug {
  40. l.Debugln(s, "rescan")
  41. }
  42. s.model.setState(s.folder, FolderScanning)
  43. if err := s.model.ScanFolder(s.folder); err != nil {
  44. s.model.cfg.InvalidateFolder(s.folder, err.Error())
  45. return
  46. }
  47. s.model.setState(s.folder, FolderIdle)
  48. if !initialScanCompleted {
  49. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  50. initialScanCompleted = true
  51. }
  52. if s.intv == 0 {
  53. return
  54. }
  55. timer.Reset(s.intv)
  56. }
  57. }
  58. }
  59. func (s *Scanner) Stop() {
  60. close(s.stop)
  61. }
  62. func (s *Scanner) String() string {
  63. return fmt.Sprintf("scanner/%s@%p", s.folder, s)
  64. }