basicfs_watch.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. absShouldIgnore := func(absPath string) bool {
  26. return ignore.ShouldIgnore(f.unrootedChecked(absPath))
  27. }
  28. outChan := make(chan Event)
  29. backendChan := make(chan notify.EventInfo, backendBuffer)
  30. eventMask := subEventMask
  31. if !ignorePerms {
  32. eventMask |= permEventMask
  33. }
  34. if err := notify.WatchWithFilter(filepath.Join(absName, "..."), backendChan, absShouldIgnore, eventMask); err != nil {
  35. notify.Stop(backendChan)
  36. if reachedMaxUserWatches(err) {
  37. err = errors.New("failed to setup inotify handler. Please increase inotify limits, see https://docs.syncthing.net/users/faq.html#inotify-limits")
  38. }
  39. return nil, err
  40. }
  41. go f.watchLoop(name, backendChan, outChan, ignore, ctx)
  42. return outChan, nil
  43. }
  44. func (f *BasicFilesystem) watchLoop(name string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
  45. for {
  46. // Detect channel overflow
  47. if len(backendChan) == backendBuffer {
  48. outer:
  49. for {
  50. select {
  51. case <-backendChan:
  52. default:
  53. break outer
  54. }
  55. }
  56. // When next scheduling a scan, do it on the entire folder as events have been lost.
  57. outChan <- Event{Name: name, Type: NonRemove}
  58. l.Debugln(f.Type(), f.URI(), "Watch: Event overflow, send \".\"")
  59. }
  60. select {
  61. case ev := <-backendChan:
  62. relPath := f.unrootedChecked(ev.Path())
  63. if ignore.ShouldIgnore(relPath) {
  64. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  65. continue
  66. }
  67. evType := f.eventType(ev.Event())
  68. select {
  69. case outChan <- Event{Name: relPath, Type: evType}:
  70. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  71. case <-ctx.Done():
  72. notify.Stop(backendChan)
  73. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  74. return
  75. }
  76. case <-ctx.Done():
  77. notify.Stop(backendChan)
  78. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  79. return
  80. }
  81. }
  82. }
  83. func (f *BasicFilesystem) eventType(notifyType notify.Event) EventType {
  84. if notifyType&rmEventMask != 0 {
  85. return Remove
  86. }
  87. return NonRemove
  88. }
  89. // unrootedChecked returns the path relative to the folder root (same as
  90. // unrooted). It panics if the given path is not a subpath and handles the
  91. // special case when the given path is the folder root without a trailing
  92. // pathseparator.
  93. func (f *BasicFilesystem) unrootedChecked(absPath string) string {
  94. if absPath+string(PathSeparator) == f.rootSymlinkEvaluated {
  95. return "."
  96. }
  97. if !strings.HasPrefix(absPath, f.rootSymlinkEvaluated) {
  98. 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))
  99. }
  100. return f.unrootedSymlinkEvaluated(absPath)
  101. }