basicfs_watch.go 3.2 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. "path/filepath"
  12. "runtime"
  13. "github.com/syncthing/notify"
  14. )
  15. // Notify does not block on sending to channel, so the channel must be buffered.
  16. // The actual number is magic.
  17. // Not meant to be changed, but must be changeable for tests
  18. var backendBuffer = 500
  19. func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error) {
  20. evalRoot, err := evalSymlinks(f.root)
  21. if err != nil {
  22. return nil, err
  23. }
  24. absName, err := rooted(name, evalRoot)
  25. if err != nil {
  26. return nil, err
  27. }
  28. // Remove `\\?\` prefix if the path is just a drive letter as a dirty
  29. // fix for https://github.com/syncthing/syncthing/issues/5578
  30. if runtime.GOOS == "windows" && len(absName) <= 7 && len(absName) > 4 && absName[:4] == `\\?\` {
  31. absName = absName[4:]
  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. }