basicfs_watch.go 3.5 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. // +build !solaris,!darwin solaris,cgo darwin,cgo
  7. package fs
  8. import (
  9. "context"
  10. "errors"
  11. "fmt"
  12. "path/filepath"
  13. "strings"
  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, error) {
  21. absName, err := f.rootedSymlinkEvaluated(name)
  22. if err != nil {
  23. return 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. return ignore.ShouldIgnore(f.unrootedChecked(absPath))
  34. }
  35. err = notify.WatchWithFilter(filepath.Join(absName, "..."), backendChan, absShouldIgnore, eventMask)
  36. } else {
  37. err = notify.Watch(filepath.Join(absName, "..."), backendChan, eventMask)
  38. }
  39. if err != nil {
  40. notify.Stop(backendChan)
  41. if reachedMaxUserWatches(err) {
  42. err = errors.New("failed to setup inotify handler. Please increase inotify limits, see https://docs.syncthing.net/users/faq.html#inotify-limits")
  43. }
  44. return nil, err
  45. }
  46. go f.watchLoop(name, backendChan, outChan, ignore, ctx)
  47. return outChan, nil
  48. }
  49. func (f *BasicFilesystem) watchLoop(name string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
  50. for {
  51. // Detect channel overflow
  52. if len(backendChan) == backendBuffer {
  53. outer:
  54. for {
  55. select {
  56. case <-backendChan:
  57. default:
  58. break outer
  59. }
  60. }
  61. // When next scheduling a scan, do it on the entire folder as events have been lost.
  62. outChan <- Event{Name: name, Type: NonRemove}
  63. l.Debugln(f.Type(), f.URI(), "Watch: Event overflow, send \".\"")
  64. }
  65. select {
  66. case ev := <-backendChan:
  67. relPath := f.unrootedChecked(ev.Path())
  68. if ignore.ShouldIgnore(relPath) {
  69. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  70. continue
  71. }
  72. evType := f.eventType(ev.Event())
  73. select {
  74. case outChan <- Event{Name: relPath, Type: evType}:
  75. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  76. case <-ctx.Done():
  77. notify.Stop(backendChan)
  78. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  79. return
  80. }
  81. case <-ctx.Done():
  82. notify.Stop(backendChan)
  83. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  84. return
  85. }
  86. }
  87. }
  88. func (f *BasicFilesystem) eventType(notifyType notify.Event) EventType {
  89. if notifyType&rmEventMask != 0 {
  90. return Remove
  91. }
  92. return NonRemove
  93. }
  94. // unrootedChecked returns the path relative to the folder root (same as
  95. // unrooted). It panics if the given path is not a subpath and handles the
  96. // special case when the given path is the folder root without a trailing
  97. // pathseparator.
  98. func (f *BasicFilesystem) unrootedChecked(absPath string) string {
  99. if absPath+string(PathSeparator) == f.rootSymlinkEvaluated {
  100. return "."
  101. }
  102. if !strings.HasPrefix(absPath, f.rootSymlinkEvaluated) {
  103. panic(fmt.Sprintf("bug: Notify backend is processing a change outside of the filesystem root: root==%v, rootSymEval==%v, path==%v", f.root, f.rootSymlinkEvaluated, absPath))
  104. }
  105. return f.unrootedSymlinkEvaluated(absPath)
  106. }