basicfs_watch.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "runtime"
  14. "strings"
  15. "github.com/syncthing/notify"
  16. )
  17. // Notify does not block on sending to channel, so the channel must be buffered.
  18. // The actual number is magic.
  19. // Not meant to be changed, but must be changeable for tests
  20. var backendBuffer = 500
  21. func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error) {
  22. evalRoot, err := filepath.EvalSymlinks(f.root)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if runtime.GOOS == "windows" {
  27. evalRoot = longFilenameSupport(evalRoot)
  28. }
  29. absName, err := rooted(name, evalRoot)
  30. if err != nil {
  31. return nil, err
  32. }
  33. outChan := make(chan Event)
  34. backendChan := make(chan notify.EventInfo, backendBuffer)
  35. eventMask := subEventMask
  36. if !ignorePerms {
  37. eventMask |= permEventMask
  38. }
  39. if ignore.SkipIgnoredDirs() {
  40. absShouldIgnore := func(absPath string) bool {
  41. return ignore.ShouldIgnore(f.unrootedChecked(absPath, evalRoot))
  42. }
  43. err = notify.WatchWithFilter(filepath.Join(absName, "..."), backendChan, absShouldIgnore, eventMask)
  44. } else {
  45. err = notify.Watch(filepath.Join(absName, "..."), backendChan, eventMask)
  46. }
  47. if err != nil {
  48. notify.Stop(backendChan)
  49. if reachedMaxUserWatches(err) {
  50. err = errors.New("failed to setup inotify handler. Please increase inotify limits, see https://docs.syncthing.net/users/faq.html#inotify-limits")
  51. }
  52. return nil, err
  53. }
  54. go f.watchLoop(name, evalRoot, backendChan, outChan, ignore, ctx)
  55. return outChan, nil
  56. }
  57. func (f *BasicFilesystem) watchLoop(name, evalRoot string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
  58. for {
  59. // Detect channel overflow
  60. if len(backendChan) == backendBuffer {
  61. outer:
  62. for {
  63. select {
  64. case <-backendChan:
  65. default:
  66. break outer
  67. }
  68. }
  69. // When next scheduling a scan, do it on the entire folder as events have been lost.
  70. outChan <- Event{Name: name, Type: NonRemove}
  71. l.Debugln(f.Type(), f.URI(), "Watch: Event overflow, send \".\"")
  72. }
  73. select {
  74. case ev := <-backendChan:
  75. relPath := f.unrootedChecked(ev.Path(), evalRoot)
  76. if ignore.ShouldIgnore(relPath) {
  77. l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
  78. continue
  79. }
  80. evType := f.eventType(ev.Event())
  81. select {
  82. case outChan <- Event{Name: relPath, Type: evType}:
  83. l.Debugln(f.Type(), f.URI(), "Watch: Sending", relPath, evType)
  84. case <-ctx.Done():
  85. notify.Stop(backendChan)
  86. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  87. return
  88. }
  89. case <-ctx.Done():
  90. notify.Stop(backendChan)
  91. l.Debugln(f.Type(), f.URI(), "Watch: Stopped")
  92. return
  93. }
  94. }
  95. }
  96. func (f *BasicFilesystem) eventType(notifyType notify.Event) EventType {
  97. if notifyType&rmEventMask != 0 {
  98. return Remove
  99. }
  100. return NonRemove
  101. }
  102. // unrootedChecked returns the path relative to the folder root (same as
  103. // unrooted). It panics if the given path is not a subpath and handles the
  104. // special case when the given path is the folder root without a trailing
  105. // pathseparator.
  106. func (f *BasicFilesystem) unrootedChecked(absPath, root string) string {
  107. absPath = f.resolveWin83(absPath)
  108. if absPath+string(PathSeparator) == root {
  109. return "."
  110. }
  111. if !strings.HasPrefix(absPath, root) {
  112. panic(fmt.Sprintf("bug: Notify backend is processing a change outside of the filesystem root: f.root==%v, root==%v, path==%v", f.root, root, absPath))
  113. }
  114. return rel(absPath, root)
  115. }