watcher_darwin.go 3.2 KB

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