basicfs_watch.go 3.3 KB

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