watcher_darwin.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "os"
  18. "path/filepath"
  19. "time"
  20. "github.com/fsnotify/fsevents"
  21. "github.com/pkg/errors"
  22. "github.com/sirupsen/logrus"
  23. )
  24. // A file watcher optimized for Darwin.
  25. // Uses FSEvents to avoid the terrible perf characteristics of kqueue. Requires CGO
  26. type fseventNotify struct {
  27. stream *fsevents.EventStream
  28. events chan FileEvent
  29. errors chan error
  30. stop chan struct{}
  31. pathsWereWatching map[string]interface{}
  32. ignore PathMatcher
  33. }
  34. func (d *fseventNotify) loop() {
  35. for {
  36. select {
  37. case <-d.stop:
  38. return
  39. case events, ok := <-d.stream.Events:
  40. if !ok {
  41. return
  42. }
  43. for _, e := range events {
  44. e.Path = filepath.Join(string(os.PathSeparator), e.Path)
  45. _, isPathWereWatching := d.pathsWereWatching[e.Path]
  46. if e.Flags&fsevents.ItemIsDir == fsevents.ItemIsDir && e.Flags&fsevents.ItemCreated == fsevents.ItemCreated && isPathWereWatching {
  47. // This is the first create for the path that we're watching. We always get exactly one of these
  48. // even after we get the HistoryDone event. Skip it.
  49. continue
  50. }
  51. ignore, err := d.ignore.Matches(e.Path)
  52. if err != nil {
  53. logrus.Infof("Error matching path %q: %v", e.Path, err)
  54. } else if ignore {
  55. logrus.Tracef("Ignoring event for path: %v", e.Path)
  56. continue
  57. }
  58. d.events <- NewFileEvent(e.Path)
  59. }
  60. }
  61. }
  62. }
  63. // Add a path to be watched. Should only be called during initialization.
  64. func (d *fseventNotify) initAdd(name string) {
  65. d.stream.Paths = append(d.stream.Paths, name)
  66. if d.pathsWereWatching == nil {
  67. d.pathsWereWatching = make(map[string]interface{})
  68. }
  69. d.pathsWereWatching[name] = struct{}{}
  70. }
  71. func (d *fseventNotify) Start() error {
  72. if len(d.stream.Paths) == 0 {
  73. return nil
  74. }
  75. numberOfWatches.Add(int64(len(d.stream.Paths)))
  76. d.stream.Start()
  77. go d.loop()
  78. return nil
  79. }
  80. func (d *fseventNotify) Close() error {
  81. numberOfWatches.Add(int64(-len(d.stream.Paths)))
  82. d.stream.Stop()
  83. close(d.errors)
  84. close(d.stop)
  85. return nil
  86. }
  87. func (d *fseventNotify) Events() chan FileEvent {
  88. return d.events
  89. }
  90. func (d *fseventNotify) Errors() chan error {
  91. return d.errors
  92. }
  93. func newWatcher(paths []string, ignore PathMatcher) (Notify, error) {
  94. dw := &fseventNotify{
  95. ignore: ignore,
  96. stream: &fsevents.EventStream{
  97. Latency: 50 * time.Millisecond,
  98. Flags: fsevents.FileEvents | fsevents.IgnoreSelf,
  99. // NOTE(dmiller): this corresponds to the `sinceWhen` parameter in FSEventStreamCreate
  100. // https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate
  101. EventID: fsevents.LatestEventID(),
  102. },
  103. events: make(chan FileEvent),
  104. errors: make(chan error),
  105. stop: make(chan struct{}),
  106. }
  107. paths = dedupePathsForRecursiveWatcher(paths)
  108. for _, path := range paths {
  109. path, err := filepath.Abs(path)
  110. if err != nil {
  111. return nil, errors.Wrap(err, "newWatcher")
  112. }
  113. dw.initAdd(path)
  114. }
  115. return dw, nil
  116. }
  117. var _ Notify = &fseventNotify{}