basicfs_watch.go 3.2 KB

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