watch: remove inotify-specific bits of watcher_linux (#890)

the tests on windows don't pass yet, but at least it compiles
This commit is contained in:
Nick Santos 2019-01-02 11:18:33 -05:00 committed by Nicolas De loof
parent 9e261c18b3
commit e8a34c8d1e
2 changed files with 19 additions and 46 deletions

View File

@ -8,6 +8,8 @@ import (
"github.com/windmilleng/fsevents" "github.com/windmilleng/fsevents"
) )
// A file watcher optimized for Darwin.
// Uses FSEvents to avoid the terrible perf characteristics of kqueue.
type darwinNotify struct { type darwinNotify struct {
stream *fsevents.EventStream stream *fsevents.EventStream
events chan FileEvent events chan FileEvent

View File

@ -1,24 +1,21 @@
// +build !darwin
package watch package watch
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/windmilleng/fsnotify" "github.com/windmilleng/fsnotify"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
) )
const enospc = "no space left on device" // A naive file watcher that uses the plain fsnotify API.
const inotifyErrMsg = "The user limit on the total number of inotify watches was reached; increase the fs.inotify.max_user_watches sysctl. See here for more information: https://facebook.github.io/watchman/docs/install.html#linux-inotify-limits" // Used on all non-Darwin systems (including Windows & Linux).
const inotifyMin = 8192 //
// All OS-specific codepaths are handled by fsnotify.
type linuxNotify struct { type naiveNotify struct {
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
events chan fsnotify.Event events chan fsnotify.Event
wrappedEvents chan FileEvent wrappedEvents chan FileEvent
@ -26,7 +23,7 @@ type linuxNotify struct {
watchList map[string]bool watchList map[string]bool
} }
func (d *linuxNotify) Add(name string) error { func (d *naiveNotify) Add(name string) error {
fi, err := os.Stat(name) fi, err := os.Stat(name)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "notify.Add(%q)", name) return errors.Wrapf(err, "notify.Add(%q)", name)
@ -57,7 +54,7 @@ func (d *linuxNotify) Add(name string) error {
return nil return nil
} }
func (d *linuxNotify) watchRecursively(dir string) error { func (d *naiveNotify) watchRecursively(dir string) error {
return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error { return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -74,19 +71,19 @@ func (d *linuxNotify) watchRecursively(dir string) error {
}) })
} }
func (d *linuxNotify) Close() error { func (d *naiveNotify) Close() error {
return d.watcher.Close() return d.watcher.Close()
} }
func (d *linuxNotify) Events() chan FileEvent { func (d *naiveNotify) Events() chan FileEvent {
return d.wrappedEvents return d.wrappedEvents
} }
func (d *linuxNotify) Errors() chan error { func (d *naiveNotify) Errors() chan error {
return d.errors return d.errors
} }
func (d *linuxNotify) loop() { func (d *naiveNotify) loop() {
for e := range d.events { for e := range d.events {
isCreateOp := e.Op&fsnotify.Create == fsnotify.Create isCreateOp := e.Op&fsnotify.Create == fsnotify.Create
shouldWalk := false shouldWalk := false
@ -124,7 +121,7 @@ func (d *linuxNotify) loop() {
} }
} }
func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) { func (d *naiveNotify) sendEventIfWatched(e fsnotify.Event) {
if _, ok := d.watchList[e.Name]; ok { if _, ok := d.watchList[e.Name]; ok {
d.wrappedEvents <- FileEvent{e.Name} d.wrappedEvents <- FileEvent{e.Name}
} else { } else {
@ -138,7 +135,7 @@ func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) {
} }
} }
func NewWatcher() (*linuxNotify, error) { func NewWatcher() (*naiveNotify, error) {
fsw, err := fsnotify.NewWatcher() fsw, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
return nil, err return nil, err
@ -146,7 +143,7 @@ func NewWatcher() (*linuxNotify, error) {
wrappedEvents := make(chan FileEvent) wrappedEvents := make(chan FileEvent)
wmw := &linuxNotify{ wmw := &naiveNotify{
watcher: fsw, watcher: fsw,
events: fsw.Events, events: fsw.Events,
wrappedEvents: wrappedEvents, wrappedEvents: wrappedEvents,
@ -169,30 +166,4 @@ func isDir(pth string) (bool, error) {
return fi.IsDir(), nil return fi.IsDir(), nil
} }
func checkInotifyLimits() error { var _ Notify = &naiveNotify{}
if !LimitChecksEnabled() {
return nil
}
data, err := ioutil.ReadFile("/proc/sys/fs/inotify/max_user_watches")
if err != nil {
return err
}
i, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
return err
}
if i < inotifyMin {
return grpc.Errorf(
codes.ResourceExhausted,
"The user limit on the total number of inotify watches is too low (%d); increase the fs.inotify.max_user_watches sysctl. See here for more information: https://facebook.github.io/watchman/docs/install.html#linux-inotify-limits",
i,
)
}
return nil
}
var _ Notify = &linuxNotify{}