compose/pkg/watch/watcher_naive.go

194 lines
4.0 KiB
Go
Raw Normal View History

// +build !darwin
2018-08-16 20:53:47 +02:00
package watch
import (
"fmt"
2018-08-16 20:53:47 +02:00
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
2018-08-16 20:53:47 +02:00
"github.com/windmilleng/fsnotify"
)
// A naive file watcher that uses the plain fsnotify API.
// Used on all non-Darwin systems (including Windows & Linux).
//
// All OS-specific codepaths are handled by fsnotify.
type naiveNotify struct {
2018-08-16 20:53:47 +02:00
watcher *fsnotify.Watcher
events chan fsnotify.Event
wrappedEvents chan FileEvent
2018-08-16 20:53:47 +02:00
errors chan error
// Paths that we're watching that should be passed up to the caller.
// Note that we may have to watch ancestors of these paths
// in order to fulfill the API promise.
notifyList map[string]bool
2018-08-16 20:53:47 +02:00
}
func (d *naiveNotify) Add(name string) error {
2018-08-16 20:53:47 +02:00
fi, err := os.Stat(name)
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "notify.Add(%q)", name)
2018-08-16 20:53:47 +02:00
}
2018-09-14 23:13:36 +02:00
// if it's a file that doesn't exist, watch its parent
2018-08-16 20:53:47 +02:00
if os.IsNotExist(err) {
err = d.watchAncestorOfMissingPath(name)
2018-08-16 20:53:47 +02:00
if err != nil {
return errors.Wrapf(err, "watchAncestorOfMissingPath(%q)", name)
2018-08-16 20:53:47 +02:00
}
} else if fi.IsDir() {
err = d.watchRecursively(name)
if err != nil {
return errors.Wrapf(err, "notify.Add(%q)", name)
2018-08-16 20:53:47 +02:00
}
} else {
err = d.watcher.Add(name)
if err != nil {
return errors.Wrapf(err, "notify.Add(%q)", name)
2018-08-16 20:53:47 +02:00
}
}
d.notifyList[name] = true
2018-08-16 20:53:47 +02:00
return nil
}
func (d *naiveNotify) watchRecursively(dir string) error {
2018-08-16 20:53:47 +02:00
return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
if err != nil {
return err
}
2018-09-14 23:13:36 +02:00
err = d.watcher.Add(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return errors.Wrapf(err, "watcher.Add(%q)", path)
2018-09-14 23:13:36 +02:00
}
return nil
2018-08-16 20:53:47 +02:00
})
}
func (d *naiveNotify) watchAncestorOfMissingPath(path string) error {
if path == string(filepath.Separator) {
return fmt.Errorf("cannot watch root directory")
}
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "os.Stat(%q)", path)
}
if os.IsNotExist(err) {
parent := filepath.Dir(path)
return d.watchAncestorOfMissingPath(parent)
}
return d.watcher.Add(path)
}
func (d *naiveNotify) Close() error {
2018-08-16 20:53:47 +02:00
return d.watcher.Close()
}
func (d *naiveNotify) Events() chan FileEvent {
2018-08-16 20:53:47 +02:00
return d.wrappedEvents
}
func (d *naiveNotify) Errors() chan error {
2018-08-16 20:53:47 +02:00
return d.errors
}
func (d *naiveNotify) loop() {
2018-08-16 20:53:47 +02:00
for e := range d.events {
2018-08-22 21:59:46 +02:00
isCreateOp := e.Op&fsnotify.Create == fsnotify.Create
shouldWalk := false
if isCreateOp {
isDir, err := isDir(e.Name)
if err != nil {
log.Printf("Error stat-ing file %s: %s", e.Name, err)
continue
}
shouldWalk = isDir
}
if shouldWalk {
2018-08-16 20:53:47 +02:00
err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error {
if err != nil {
return err
}
newE := fsnotify.Event{
Op: fsnotify.Create,
Name: path,
}
if d.shouldNotify(newE) {
d.wrappedEvents <- FileEvent{newE.Name}
// TODO(dmiller): symlinks 😭
err = d.Add(path)
if err != nil {
log.Printf("Error watching path %s: %s", e.Name, err)
}
2018-08-16 20:53:47 +02:00
}
return nil
})
if err != nil {
log.Printf("Error walking directory %s: %s", e.Name, err)
}
} else if d.shouldNotify(e) {
d.wrappedEvents <- FileEvent{e.Name}
2018-08-16 20:53:47 +02:00
}
}
}
func (d *naiveNotify) shouldNotify(e fsnotify.Event) bool {
if _, ok := d.notifyList[e.Name]; ok {
return true
2018-08-16 20:53:47 +02:00
} else {
// TODO(dmiller): maybe use a prefix tree here?
for path := range d.notifyList {
2018-08-16 20:53:47 +02:00
if pathIsChildOf(e.Name, path) {
return true
2018-08-16 20:53:47 +02:00
}
}
}
return false
2018-08-16 20:53:47 +02:00
}
func NewWatcher() (*naiveNotify, error) {
2018-08-16 20:53:47 +02:00
fsw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
wrappedEvents := make(chan FileEvent)
2018-08-16 20:53:47 +02:00
wmw := &naiveNotify{
2018-08-16 20:53:47 +02:00
watcher: fsw,
events: fsw.Events,
wrappedEvents: wrappedEvents,
errors: fsw.Errors,
notifyList: map[string]bool{},
2018-08-16 20:53:47 +02:00
}
go wmw.loop()
return wmw, nil
}
2018-08-22 21:59:46 +02:00
func isDir(pth string) (bool, error) {
fi, err := os.Lstat(pth)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return fi.IsDir(), nil
2018-08-16 20:53:47 +02:00
}
var _ Notify = &naiveNotify{}