watcher_darwin.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //go:build darwin
  2. // +build darwin
  3. /*
  4. Copyright 2020 Docker Compose CLI authors
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package watch
  16. import (
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "time"
  21. pathutil "github.com/docker/compose/v2/internal/paths"
  22. "github.com/fsnotify/fsevents"
  23. "github.com/sirupsen/logrus"
  24. )
  25. // A file watcher optimized for Darwin.
  26. // Uses FSEvents to avoid the terrible perf characteristics of kqueue. Requires CGO
  27. type fseventNotify struct {
  28. stream *fsevents.EventStream
  29. events chan FileEvent
  30. errors chan error
  31. stop chan struct{}
  32. pathsWereWatching map[string]interface{}
  33. ignore PathMatcher
  34. }
  35. func (d *fseventNotify) loop() {
  36. for {
  37. select {
  38. case <-d.stop:
  39. return
  40. case events, ok := <-d.stream.Events:
  41. if !ok {
  42. return
  43. }
  44. for _, e := range events {
  45. e.Path = filepath.Join(string(os.PathSeparator), e.Path)
  46. _, isPathWereWatching := d.pathsWereWatching[e.Path]
  47. if e.Flags&fsevents.ItemIsDir == fsevents.ItemIsDir && e.Flags&fsevents.ItemCreated == fsevents.ItemCreated && isPathWereWatching {
  48. // This is the first create for the path that we're watching. We always get exactly one of these
  49. // even after we get the HistoryDone event. Skip it.
  50. continue
  51. }
  52. ignore, err := d.ignore.Matches(e.Path)
  53. if err != nil {
  54. logrus.Infof("Error matching path %q: %v", e.Path, err)
  55. } else if ignore {
  56. logrus.Tracef("Ignoring event for path: %v", e.Path)
  57. continue
  58. }
  59. d.events <- NewFileEvent(e.Path)
  60. }
  61. }
  62. }
  63. }
  64. // Add a path to be watched. Should only be called during initialization.
  65. func (d *fseventNotify) initAdd(name string) {
  66. d.stream.Paths = append(d.stream.Paths, name)
  67. if d.pathsWereWatching == nil {
  68. d.pathsWereWatching = make(map[string]interface{})
  69. }
  70. d.pathsWereWatching[name] = struct{}{}
  71. }
  72. func (d *fseventNotify) Start() error {
  73. if len(d.stream.Paths) == 0 {
  74. return nil
  75. }
  76. numberOfWatches.Add(int64(len(d.stream.Paths)))
  77. d.stream.Start()
  78. go d.loop()
  79. return nil
  80. }
  81. func (d *fseventNotify) Close() error {
  82. numberOfWatches.Add(int64(-len(d.stream.Paths)))
  83. d.stream.Stop()
  84. close(d.errors)
  85. close(d.stop)
  86. return nil
  87. }
  88. func (d *fseventNotify) Events() chan FileEvent {
  89. return d.events
  90. }
  91. func (d *fseventNotify) Errors() chan error {
  92. return d.errors
  93. }
  94. func newWatcher(paths []string, ignore PathMatcher) (Notify, error) {
  95. dw := &fseventNotify{
  96. ignore: ignore,
  97. stream: &fsevents.EventStream{
  98. Latency: 50 * time.Millisecond,
  99. Flags: fsevents.FileEvents | fsevents.IgnoreSelf,
  100. // NOTE(dmiller): this corresponds to the `sinceWhen` parameter in FSEventStreamCreate
  101. // https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate
  102. EventID: fsevents.LatestEventID(),
  103. },
  104. events: make(chan FileEvent),
  105. errors: make(chan error),
  106. stop: make(chan struct{}),
  107. }
  108. paths = pathutil.EncompassingPaths(paths)
  109. for _, path := range paths {
  110. path, err := filepath.Abs(path)
  111. if err != nil {
  112. return nil, fmt.Errorf("newWatcher: %w", err)
  113. }
  114. dw.initAdd(path)
  115. }
  116. return dw, nil
  117. }
  118. var _ Notify = &fseventNotify{}