basicfs_watch.go 3.3 KB

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