2023-01-30 11:58:55 +01:00
|
|
|
//go:build darwin
|
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-08-16 20:53:47 +02:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2023-09-26 00:57:12 +02:00
|
|
|
"fmt"
|
2023-04-04 21:10:01 +02:00
|
|
|
"os"
|
2018-08-16 20:53:47 +02:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
"github.com/fsnotify/fsevents"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-16 20:53:47 +02:00
|
|
|
)
|
|
|
|
|
2019-01-02 17:18:33 +01:00
|
|
|
// A file watcher optimized for Darwin.
|
2023-01-30 11:58:55 +01:00
|
|
|
// Uses FSEvents to avoid the terrible perf characteristics of kqueue. Requires CGO
|
|
|
|
type fseventNotify struct {
|
2018-08-16 20:53:47 +02:00
|
|
|
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
|
2018-08-16 20:53:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) loop() {
|
2018-08-16 20:53:47 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.stop:
|
|
|
|
return
|
|
|
|
case events, ok := <-d.stream.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range events {
|
2023-04-04 21:10:01 +02:00
|
|
|
e.Path = filepath.Join(string(os.PathSeparator), e.Path)
|
2018-08-16 20:53: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 {
|
2023-01-30 11:58:55 +01:00
|
|
|
logrus.Infof("Error matching path %q: %v", e.Path, err)
|
2019-07-11 17:40:40 +02:00
|
|
|
} else if ignore {
|
2023-02-24 17:10:15 +01:00
|
|
|
logrus.Tracef("Ignoring event for path: %v", e.Path)
|
2019-07-11 17:40:40 +02:00
|
|
|
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.
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) initAdd(name string) {
|
2019-07-11 17:40:40 +02:00
|
|
|
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
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) 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
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) 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
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) Events() chan FileEvent {
|
2018-08-16 20:53:47 +02:00
|
|
|
return d.events
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
func (d *fseventNotify) Errors() chan error {
|
2018-08-16 20:53:47 +02:00
|
|
|
return d.errors
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:57:45 +01:00
|
|
|
func newWatcher(paths []string, ignore PathMatcher) (Notify, error) {
|
2023-01-30 11:58:55 +01:00
|
|
|
dw := &fseventNotify{
|
2019-07-11 17:40:40 +02:00
|
|
|
ignore: ignore,
|
2018-08-16 20:53:47 +02:00
|
|
|
stream: &fsevents.EventStream{
|
2023-08-01 20:39:08 +02:00
|
|
|
Latency: 50 * time.Millisecond,
|
|
|
|
Flags: fsevents.FileEvents | fsevents.IgnoreSelf,
|
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 {
|
2023-09-26 00:57:12 +02:00
|
|
|
return nil, fmt.Errorf("newWatcher: %w", err)
|
2019-07-13 20:13:24 +02:00
|
|
|
}
|
2019-07-11 17:40:40 +02:00
|
|
|
dw.initAdd(path)
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:53:47 +02:00
|
|
|
return dw, nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:58:55 +01:00
|
|
|
var _ Notify = &fseventNotify{}
|