2018-08-16 20:53:47 +02:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2019-07-13 20:13:24 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-05-15 16:34:39 +02:00
|
|
|
"github.com/tilt-dev/tilt/pkg/logger"
|
2019-04-01 22:51:43 +02:00
|
|
|
|
2020-05-15 16:34:39 +02:00
|
|
|
"github.com/tilt-dev/fsevents"
|
2018-08-16 20:53:47 +02:00
|
|
|
)
|
|
|
|
|
2019-01-02 17:18:33 +01:00
|
|
|
// A file watcher optimized for Darwin.
|
|
|
|
// Uses FSEvents to avoid the terrible perf characteristics of kqueue.
|
2018-08-16 20:53:47 +02:00
|
|
|
type darwinNotify struct {
|
|
|
|
stream *fsevents.EventStream
|
2018-08-22 21:33:06 +02:00
|
|
|
events chan FileEvent
|
2018-08-16 20:53:47 +02:00
|
|
|
errors chan error
|
|
|
|
stop chan struct{}
|
|
|
|
|
2018-10-18 16:44:07 +02:00
|
|
|
pathsWereWatching map[string]interface{}
|
2019-07-11 17:40:40 +02:00
|
|
|
ignore PathMatcher
|
|
|
|
logger logger.Logger
|
2018-10-18 16:44:07 +02:00
|
|
|
sawAnyHistoryDone bool
|
2018-08-16 20:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *darwinNotify) loop() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.stop:
|
|
|
|
return
|
|
|
|
case events, ok := <-d.stream.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
e.Path = filepath.Join("/", e.Path)
|
|
|
|
|
2018-10-18 16:44:07 +02:00
|
|
|
if e.Flags&fsevents.HistoryDone == fsevents.HistoryDone {
|
|
|
|
d.sawAnyHistoryDone = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We wait until we've seen the HistoryDone event for this watcher before processing any events
|
|
|
|
// so that we skip all of the "spurious" events that precede it.
|
|
|
|
if !d.sawAnyHistoryDone {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-23 19:24:47 +02:00
|
|
|
|
2018-10-18 16:44:07 +02:00
|
|
|
_, isPathWereWatching := d.pathsWereWatching[e.Path]
|
|
|
|
if e.Flags&fsevents.ItemIsDir == fsevents.ItemIsDir && e.Flags&fsevents.ItemCreated == fsevents.ItemCreated && isPathWereWatching {
|
|
|
|
// This is the first create for the path that we're watching. We always get exactly one of these
|
|
|
|
// even after we get the HistoryDone event. Skip it.
|
|
|
|
continue
|
2018-08-16 20:53:47 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:40:40 +02:00
|
|
|
ignore, err := d.ignore.Matches(e.Path)
|
|
|
|
if err != nil {
|
|
|
|
d.logger.Infof("Error matching path %q: %v", e.Path, err)
|
|
|
|
} else if ignore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-13 20:13:24 +02:00
|
|
|
d.events <- NewFileEvent(e.Path)
|
2018-08-16 20:53:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:40:40 +02:00
|
|
|
// Add a path to be watched. Should only be called during initialization.
|
|
|
|
func (d *darwinNotify) initAdd(name string) {
|
|
|
|
d.stream.Paths = append(d.stream.Paths, name)
|
2018-08-23 19:24:47 +02:00
|
|
|
|
2018-10-18 16:44:07 +02:00
|
|
|
if d.pathsWereWatching == nil {
|
|
|
|
d.pathsWereWatching = make(map[string]interface{})
|
2018-08-23 19:24:47 +02:00
|
|
|
}
|
2018-10-18 16:44:07 +02:00
|
|
|
d.pathsWereWatching[name] = struct{}{}
|
2019-07-11 17:40:40 +02:00
|
|
|
}
|
2018-08-23 19:24:47 +02:00
|
|
|
|
2019-07-11 17:40:40 +02:00
|
|
|
func (d *darwinNotify) Start() error {
|
2019-07-16 00:57:15 +02:00
|
|
|
if len(d.stream.Paths) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:54:50 +02:00
|
|
|
numberOfWatches.Add(int64(len(d.stream.Paths)))
|
|
|
|
|
2019-07-11 17:40:40 +02:00
|
|
|
d.stream.Start()
|
|
|
|
|
|
|
|
go d.loop()
|
2018-08-16 20:53:47 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *darwinNotify) Close() error {
|
2019-07-11 23:54:50 +02:00
|
|
|
numberOfWatches.Add(int64(-len(d.stream.Paths)))
|
|
|
|
|
2018-08-16 20:53:47 +02:00
|
|
|
d.stream.Stop()
|
|
|
|
close(d.errors)
|
|
|
|
close(d.stop)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-22 21:33:06 +02:00
|
|
|
func (d *darwinNotify) Events() chan FileEvent {
|
2018-08-16 20:53:47 +02:00
|
|
|
return d.events
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *darwinNotify) Errors() chan error {
|
|
|
|
return d.errors
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:40:40 +02:00
|
|
|
func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*darwinNotify, error) {
|
2018-08-16 20:53:47 +02:00
|
|
|
dw := &darwinNotify{
|
2019-07-11 17:40:40 +02:00
|
|
|
ignore: ignore,
|
|
|
|
logger: l,
|
2018-08-16 20:53:47 +02:00
|
|
|
stream: &fsevents.EventStream{
|
|
|
|
Latency: 1 * time.Millisecond,
|
|
|
|
Flags: fsevents.FileEvents,
|
2018-10-18 16:44:07 +02:00
|
|
|
// NOTE(dmiller): this corresponds to the `sinceWhen` parameter in FSEventStreamCreate
|
|
|
|
// https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate
|
|
|
|
EventID: fsevents.LatestEventID(),
|
2018-08-16 20:53:47 +02:00
|
|
|
},
|
2018-08-22 21:33:06 +02:00
|
|
|
events: make(chan FileEvent),
|
2018-08-16 20:53:47 +02:00
|
|
|
errors: make(chan error),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:45:34 +02:00
|
|
|
paths = dedupePathsForRecursiveWatcher(paths)
|
2019-07-11 17:40:40 +02:00
|
|
|
for _, path := range paths {
|
2019-07-13 20:13:24 +02:00
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "newWatcher")
|
|
|
|
}
|
2019-07-11 17:40:40 +02:00
|
|
|
dw.initAdd(path)
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:53:47 +02:00
|
|
|
return dw, nil
|
|
|
|
}
|
|
|
|
|
2018-08-22 21:33:06 +02:00
|
|
|
var _ Notify = &darwinNotify{}
|