watcher_darwin.go 3.2 KB

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