notify.go 3.4 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. "expvar"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "strconv"
  21. "github.com/pkg/errors"
  22. "github.com/tilt-dev/fsnotify"
  23. )
  24. var (
  25. numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
  26. )
  27. type FileEvent struct {
  28. path string
  29. }
  30. func NewFileEvent(p string) FileEvent {
  31. if !filepath.IsAbs(p) {
  32. panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
  33. }
  34. return FileEvent{path: p}
  35. }
  36. func (e FileEvent) Path() string {
  37. return e.path
  38. }
  39. type Notify interface {
  40. // Start watching the paths set at init time
  41. Start() error
  42. // Stop watching and close all channels
  43. Close() error
  44. // A channel to read off incoming file changes
  45. Events() chan FileEvent
  46. // A channel to read off show-stopping errors
  47. Errors() chan error
  48. }
  49. // When we specify directories to watch, we often want to
  50. // ignore some subset of the files under those directories.
  51. //
  52. // For example:
  53. // - Watch /src/repo, but ignore /src/repo/.git
  54. // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
  55. //
  56. // The PathMatcher inteface helps us manage these ignores.
  57. type PathMatcher interface {
  58. Matches(file string) (bool, error)
  59. // If this matches the entire dir, we can often optimize filetree walks a bit.
  60. MatchesEntireDir(file string) (bool, error)
  61. }
  62. type EmptyMatcher struct {
  63. }
  64. func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
  65. func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
  66. var _ PathMatcher = EmptyMatcher{}
  67. func NewWatcher(paths []string, ignore PathMatcher) (Notify, error) {
  68. return newWatcher(paths, ignore)
  69. }
  70. const WindowsBufferSizeEnvVar = "COMPOSE_WATCH_WINDOWS_BUFFER_SIZE"
  71. const defaultBufferSize int = 65536
  72. func DesiredWindowsBufferSize() int {
  73. envVar := os.Getenv(WindowsBufferSizeEnvVar)
  74. if envVar != "" {
  75. size, err := strconv.Atoi(envVar)
  76. if err == nil {
  77. return size
  78. }
  79. }
  80. return defaultBufferSize
  81. }
  82. func IsWindowsShortReadError(err error) bool {
  83. return runtime.GOOS == "windows" && !errors.Is(err, fsnotify.ErrEventOverflow)
  84. }
  85. type CompositePathMatcher struct {
  86. Matchers []PathMatcher
  87. }
  88. func NewCompositeMatcher(matchers ...PathMatcher) PathMatcher {
  89. if len(matchers) == 0 {
  90. return EmptyMatcher{}
  91. }
  92. return CompositePathMatcher{Matchers: matchers}
  93. }
  94. func (c CompositePathMatcher) Matches(f string) (bool, error) {
  95. for _, t := range c.Matchers {
  96. ret, err := t.Matches(f)
  97. if err != nil {
  98. return false, err
  99. }
  100. if ret {
  101. return true, nil
  102. }
  103. }
  104. return false, nil
  105. }
  106. func (c CompositePathMatcher) MatchesEntireDir(f string) (bool, error) {
  107. for _, t := range c.Matchers {
  108. matches, err := t.MatchesEntireDir(f)
  109. if matches || err != nil {
  110. return matches, err
  111. }
  112. }
  113. return false, nil
  114. }
  115. var _ PathMatcher = CompositePathMatcher{}