basicfs_watch.go 3.5 KB

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