watcher_darwin.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package watch
  2. import (
  3. "path/filepath"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/windmilleng/tilt/internal/logger"
  7. "github.com/windmilleng/tilt/internal/ospath"
  8. "github.com/windmilleng/fsevents"
  9. )
  10. // A file watcher optimized for Darwin.
  11. // Uses FSEvents to avoid the terrible perf characteristics of kqueue.
  12. type darwinNotify struct {
  13. stream *fsevents.EventStream
  14. events chan FileEvent
  15. errors chan error
  16. stop chan struct{}
  17. pathsWereWatching map[string]interface{}
  18. ignore PathMatcher
  19. logger logger.Logger
  20. sawAnyHistoryDone bool
  21. }
  22. func (d *darwinNotify) loop() {
  23. for {
  24. select {
  25. case <-d.stop:
  26. return
  27. case events, ok := <-d.stream.Events:
  28. if !ok {
  29. return
  30. }
  31. for _, e := range events {
  32. e.Path = filepath.Join("/", e.Path)
  33. if e.Flags&fsevents.HistoryDone == fsevents.HistoryDone {
  34. d.sawAnyHistoryDone = true
  35. continue
  36. }
  37. // We wait until we've seen the HistoryDone event for this watcher before processing any events
  38. // so that we skip all of the "spurious" events that precede it.
  39. if !d.sawAnyHistoryDone {
  40. continue
  41. }
  42. _, isPathWereWatching := d.pathsWereWatching[e.Path]
  43. if e.Flags&fsevents.ItemIsDir == fsevents.ItemIsDir && e.Flags&fsevents.ItemCreated == fsevents.ItemCreated && isPathWereWatching {
  44. // This is the first create for the path that we're watching. We always get exactly one of these
  45. // even after we get the HistoryDone event. Skip it.
  46. continue
  47. }
  48. ignore, err := d.ignore.Matches(e.Path)
  49. if err != nil {
  50. d.logger.Infof("Error matching path %q: %v", e.Path, err)
  51. } else if ignore {
  52. continue
  53. }
  54. d.events <- NewFileEvent(e.Path)
  55. }
  56. }
  57. }
  58. }
  59. // Add a path to be watched. Should only be called during initialization.
  60. func (d *darwinNotify) initAdd(name string) {
  61. // Check if this is a subdirectory of any of the paths
  62. // we're already watching.
  63. for _, parent := range d.stream.Paths {
  64. if ospath.IsChild(parent, name) {
  65. return
  66. }
  67. }
  68. d.stream.Paths = append(d.stream.Paths, name)
  69. if d.pathsWereWatching == nil {
  70. d.pathsWereWatching = make(map[string]interface{})
  71. }
  72. d.pathsWereWatching[name] = struct{}{}
  73. }
  74. func (d *darwinNotify) Start() error {
  75. if len(d.stream.Paths) == 0 {
  76. return nil
  77. }
  78. numberOfWatches.Add(int64(len(d.stream.Paths)))
  79. d.stream.Start()
  80. go d.loop()
  81. return nil
  82. }
  83. func (d *darwinNotify) Close() error {
  84. numberOfWatches.Add(int64(-len(d.stream.Paths)))
  85. d.stream.Stop()
  86. close(d.errors)
  87. close(d.stop)
  88. return nil
  89. }
  90. func (d *darwinNotify) Events() chan FileEvent {
  91. return d.events
  92. }
  93. func (d *darwinNotify) Errors() chan error {
  94. return d.errors
  95. }
  96. func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*darwinNotify, error) {
  97. dw := &darwinNotify{
  98. ignore: ignore,
  99. logger: l,
  100. stream: &fsevents.EventStream{
  101. Latency: 1 * time.Millisecond,
  102. Flags: fsevents.FileEvents,
  103. // NOTE(dmiller): this corresponds to the `sinceWhen` parameter in FSEventStreamCreate
  104. // https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate
  105. EventID: fsevents.LatestEventID(),
  106. },
  107. events: make(chan FileEvent),
  108. errors: make(chan error),
  109. stop: make(chan struct{}),
  110. }
  111. for _, path := range paths {
  112. path, err := filepath.Abs(path)
  113. if err != nil {
  114. return nil, errors.Wrap(err, "newWatcher")
  115. }
  116. dw.initAdd(path)
  117. }
  118. return dw, nil
  119. }
  120. var _ Notify = &darwinNotify{}