1
0

basicfs_watch.go 3.5 KB

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