basicfs_watch.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2016 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 http://mozilla.org/MPL/2.0/.
  6. //go:build !(solaris && !cgo) && !(darwin && !cgo) && !(android && amd64)
  7. // +build !solaris cgo
  8. // +build !darwin cgo
  9. // +build !android !amd64
  10. package fs
  11. import (
  12. "context"
  13. "errors"
  14. "unicode/utf8"
  15. "github.com/syncthing/notify"
  16. )
  17. // Notify does not block on sending to channel, so the channel must be buffered.
  18. // The actual number is magic.
  19. // Not meant to be changed, but must be changeable for tests
  20. var backendBuffer = 500
  21. func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
  22. watchPath, roots, err := f.watchPaths(name)
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. outChan := make(chan Event)
  27. backendChan := make(chan notify.EventInfo, backendBuffer)
  28. eventMask := subEventMask
  29. if !ignorePerms {
  30. eventMask |= permEventMask
  31. }
  32. absShouldIgnore := func(absPath string) bool {
  33. if !utf8.ValidString(absPath) {
  34. return true
  35. }
  36. rel, err := f.unrootedChecked(absPath, roots)
  37. if err != nil {
  38. return true
  39. }
  40. return ignore.Match(rel).CanSkipDir()
  41. }
  42. err = notify.WatchWithFilter(watchPath, backendChan, absShouldIgnore, eventMask)
  43. if err != nil {
  44. notify.Stop(backendChan)
  45. if reachedMaxUserWatches(err) {
  46. err = errors.New("failed to setup inotify handler. Please increase inotify limits, see https://docs.syncthing.net/users/faq.html#inotify-limits")
  47. }
  48. return nil, nil, err
  49. }
  50. errChan := make(chan error)
  51. go f.watchLoop(ctx, name, roots, backendChan, outChan, errChan, ignore)
  52. return outChan, errChan, nil
  53. }
  54. func (f *BasicFilesystem) watchLoop(ctx context.Context, name string, roots []string, backendChan chan notify.EventInfo, outChan chan<- Event, errChan chan<- error, ignore Matcher) {
  55. for {
  56. // Detect channel overflow
  57. if len(backendChan) == backendBuffer {
  58. outer:
  59. for {
  60. select {
  61. case <-backendChan:
  62. default:
  63. break outer
  64. }
  65. }
  66. // When next scheduling a scan, do it on the entire folder as events have been lost.
  67. outChan <- Event{Name: name, Type: NonRemove}
  68. l.Debugln(f.Type(), f.URI(), "Watch: Event overflow, send \".\"")
  69. }
  70. select {
  71. case ev := <-backendChan:
  72. evPath := ev.Path()
  73. if !utf8.ValidString(evPath) {
  74. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring invalid UTF-8")
  75. continue
  76. }
  77. relPath, err := f.unrootedChecked(evPath, roots)
  78. if err != nil {
  79. select {
  80. case errChan <- err:
  81. l.Debugln(f.Type(), f.URI(), "Watch: Sending error", err)
  82. case <-ctx.Done():
  83. }
  84. notify.Stop(backendChan)
  85. l.Debugln(f.Type(), f.URI(), "Watch: Stopped due to", err)
  86. return
  87. }
  88. if ignore.Match(relPath).IsIgnored() {
  89. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  90. continue
  91. }
  92. evType := f.eventType(ev.Event())
  93. select {
  94. case outChan <- Event{Name: relPath, Type: evType}:
  95. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  96. case <-ctx.Done():
  97. notify.Stop(backendChan)
  98. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  99. return
  100. }
  101. case <-ctx.Done():
  102. notify.Stop(backendChan)
  103. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  104. return
  105. }
  106. }
  107. }
  108. func (*BasicFilesystem) eventType(notifyType notify.Event) EventType {
  109. if notifyType&rmEventMask != 0 {
  110. return Remove
  111. }
  112. return NonRemove
  113. }