basicfs_watch.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "path/filepath"
  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, error) {
  19. evalRoot, err := evalSymlinks(f.root)
  20. if err != nil {
  21. return nil, err
  22. }
  23. absName, err := rooted(name, evalRoot)
  24. if err != nil {
  25. return 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. if ignore.SkipIgnoredDirs() {
  34. absShouldIgnore := func(absPath string) bool {
  35. return ignore.ShouldIgnore(f.unrootedChecked(absPath, evalRoot))
  36. }
  37. err = notify.WatchWithFilter(filepath.Join(absName, "..."), backendChan, absShouldIgnore, eventMask)
  38. } else {
  39. err = notify.Watch(filepath.Join(absName, "..."), 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, err
  47. }
  48. go f.watchLoop(name, evalRoot, backendChan, outChan, ignore, ctx)
  49. return outChan, nil
  50. }
  51. func (f *BasicFilesystem) watchLoop(name, evalRoot string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
  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 := f.unrootedChecked(ev.Path(), evalRoot)
  70. if ignore.ShouldIgnore(relPath) {
  71. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  72. continue
  73. }
  74. evType := f.eventType(ev.Event())
  75. select {
  76. case outChan <- Event{Name: relPath, Type: evType}:
  77. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  78. case <-ctx.Done():
  79. notify.Stop(backendChan)
  80. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  81. return
  82. }
  83. case <-ctx.Done():
  84. notify.Stop(backendChan)
  85. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  86. return
  87. }
  88. }
  89. }
  90. func (f *BasicFilesystem) eventType(notifyType notify.Event) EventType {
  91. if notifyType&rmEventMask != 0 {
  92. return Remove
  93. }
  94. return NonRemove
  95. }