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"
|
|
|
|
|
2024-03-20 19:41:24 +01:00
|
|
|
pathutil "github.com/docker/compose/v2/internal/paths"
|
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)))
|
|
|
|
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
d.stream.Start() //nolint:errcheck // FIXME(thaJeztah): should this return an error?
|
2019-07-11 17:40:40 +02:00
|
|
|
|
|
|
|
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{}),
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:41:24 +01:00
|
|
|
paths = pathutil.EncompassingPaths(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{}
|