basicfs_watch.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "github.com/syncthing/notify"
  15. )
  16. // Notify does not block on sending to channel, so the channel must be buffered.
  17. // The actual number is magic.
  18. // Not meant to be changed, but must be changeable for tests
  19. var backendBuffer = 500
  20. func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
  21. watchPath, roots, err := f.watchPaths(name)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. outChan := make(chan Event)
  26. backendChan := make(chan notify.EventInfo, backendBuffer)
  27. eventMask := subEventMask
  28. if !ignorePerms {
  29. eventMask |= permEventMask
  30. }
  31. if ignore.SkipIgnoredDirs() {
  32. absShouldIgnore := func(absPath string) bool {
  33. rel, err := f.unrootedChecked(absPath, roots)
  34. if err != nil {
  35. return true
  36. }
  37. return ignore.ShouldIgnore(rel)
  38. }
  39. err = notify.WatchWithFilter(watchPath, backendChan, absShouldIgnore, eventMask)
  40. } else {
  41. err = notify.Watch(watchPath, backendChan, eventMask)
  42. }
  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. relPath, err := f.unrootedChecked(ev.Path(), roots)
  73. if err != nil {
  74. select {
  75. case errChan <- err:
  76. l.Debugln(f.Type(), f.URI(), "Watch: Sending error", err)
  77. case <-ctx.Done():
  78. }
  79. notify.Stop(backendChan)
  80. l.Debugln(f.Type(), f.URI(), "Watch: Stopped due to", err)
  81. return
  82. }
  83. if ignore.ShouldIgnore(relPath) {
  84. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  85. continue
  86. }
  87. evType := f.eventType(ev.Event())
  88. select {
  89. case outChan <- Event{Name: relPath, Type: evType}:
  90. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  91. case <-ctx.Done():
  92. notify.Stop(backendChan)
  93. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  94. return
  95. }
  96. case <-ctx.Done():
  97. notify.Stop(backendChan)
  98. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  99. return
  100. }
  101. }
  102. }
  103. func (*BasicFilesystem) eventType(notifyType notify.Event) EventType {
  104. if notifyType&rmEventMask != 0 {
  105. return Remove
  106. }
  107. return NonRemove
  108. }