notify.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "errors"
  16. "expvar"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strconv"
  22. "github.com/tilt-dev/fsnotify"
  23. )
  24. var numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
  25. type FileEvent string
  26. func NewFileEvent(p string) FileEvent {
  27. if !filepath.IsAbs(p) {
  28. panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
  29. }
  30. return FileEvent(p)
  31. }
  32. type Notify interface {
  33. // Start watching the paths set at init time
  34. Start() error
  35. // Stop watching and close all channels
  36. Close() error
  37. // A channel to read off incoming file changes
  38. Events() chan FileEvent
  39. // A channel to read off show-stopping errors
  40. Errors() chan error
  41. }
  42. // When we specify directories to watch, we often want to
  43. // ignore some subset of the files under those directories.
  44. //
  45. // For example:
  46. // - Watch /src/repo, but ignore /src/repo/.git
  47. // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
  48. //
  49. // The PathMatcher interface helps us manage these ignores.
  50. type PathMatcher interface {
  51. Matches(file string) (bool, error)
  52. // If this matches the entire dir, we can often optimize filetree walks a bit.
  53. MatchesEntireDir(file string) (bool, error)
  54. }
  55. // AnyMatcher is a PathMatcher to match any path
  56. type AnyMatcher struct{}
  57. func (AnyMatcher) Matches(f string) (bool, error) { return true, nil }
  58. func (AnyMatcher) MatchesEntireDir(f string) (bool, error) { return true, nil }
  59. var _ PathMatcher = AnyMatcher{}
  60. // EmptyMatcher is a PathMatcher to match no path
  61. type EmptyMatcher struct{}
  62. func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
  63. func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
  64. var _ PathMatcher = EmptyMatcher{}
  65. func NewWatcher(paths []string) (Notify, error) {
  66. return newWatcher(paths)
  67. }
  68. const WindowsBufferSizeEnvVar = "COMPOSE_WATCH_WINDOWS_BUFFER_SIZE"
  69. const defaultBufferSize int = 65536
  70. func DesiredWindowsBufferSize() int {
  71. envVar := os.Getenv(WindowsBufferSizeEnvVar)
  72. if envVar != "" {
  73. size, err := strconv.Atoi(envVar)
  74. if err == nil {
  75. return size
  76. }
  77. }
  78. return defaultBufferSize
  79. }
  80. func IsWindowsShortReadError(err error) bool {
  81. return runtime.GOOS == "windows" && !errors.Is(err, fsnotify.ErrEventOverflow)
  82. }
  83. type CompositePathMatcher struct {
  84. Matchers []PathMatcher
  85. }
  86. func NewCompositeMatcher(matchers ...PathMatcher) PathMatcher {
  87. if len(matchers) == 0 {
  88. return EmptyMatcher{}
  89. }
  90. return CompositePathMatcher{Matchers: matchers}
  91. }
  92. func (c CompositePathMatcher) Matches(f string) (bool, error) {
  93. for _, t := range c.Matchers {
  94. ret, err := t.Matches(f)
  95. if err != nil {
  96. return false, err
  97. }
  98. if ret {
  99. return true, nil
  100. }
  101. }
  102. return false, nil
  103. }
  104. func (c CompositePathMatcher) MatchesEntireDir(f string) (bool, error) {
  105. for _, t := range c.Matchers {
  106. matches, err := t.MatchesEntireDir(f)
  107. if matches || err != nil {
  108. return matches, err
  109. }
  110. }
  111. return false, nil
  112. }
  113. var _ PathMatcher = CompositePathMatcher{}