Skip to content

Commit e8a34c8

Browse files
nicksndeloof
authored andcommitted
watch: remove inotify-specific bits of watcher_linux (docker#890)
the tests on windows don't pass yet, but at least it compiles
1 parent 9e261c1 commit e8a34c8

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed

pkg/watch/watcher_darwin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/windmilleng/fsevents"
99
)
1010

11+
// A file watcher optimized for Darwin.
12+
// Uses FSEvents to avoid the terrible perf characteristics of kqueue.
1113
type darwinNotify struct {
1214
stream *fsevents.EventStream
1315
events chan FileEvent

pkg/watch/watcher_linux.go renamed to pkg/watch/watcher_naive.go

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1+
// +build !darwin
2+
13
package watch
24

35
import (
4-
"io/ioutil"
56
"log"
67
"os"
78
"path/filepath"
8-
"strconv"
9-
"strings"
109

1110
"github.com/pkg/errors"
1211
"github.com/windmilleng/fsnotify"
13-
"google.golang.org/grpc"
14-
"google.golang.org/grpc/codes"
1512
)
1613

17-
const enospc = "no space left on device"
18-
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"
19-
const inotifyMin = 8192
20-
21-
type linuxNotify struct {
14+
// A naive file watcher that uses the plain fsnotify API.
15+
// Used on all non-Darwin systems (including Windows & Linux).
16+
//
17+
// All OS-specific codepaths are handled by fsnotify.
18+
type naiveNotify struct {
2219
watcher *fsnotify.Watcher
2320
events chan fsnotify.Event
2421
wrappedEvents chan FileEvent
2522
errors chan error
2623
watchList map[string]bool
2724
}
2825

29-
func (d *linuxNotify) Add(name string) error {
26+
func (d *naiveNotify) Add(name string) error {
3027
fi, err := os.Stat(name)
3128
if err != nil && !os.IsNotExist(err) {
3229
return errors.Wrapf(err, "notify.Add(%q)", name)
@@ -57,7 +54,7 @@ func (d *linuxNotify) Add(name string) error {
5754
return nil
5855
}
5956

60-
func (d *linuxNotify) watchRecursively(dir string) error {
57+
func (d *naiveNotify) watchRecursively(dir string) error {
6158
return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
6259
if err != nil {
6360
return err
@@ -74,19 +71,19 @@ func (d *linuxNotify) watchRecursively(dir string) error {
7471
})
7572
}
7673

77-
func (d *linuxNotify) Close() error {
74+
func (d *naiveNotify) Close() error {
7875
return d.watcher.Close()
7976
}
8077

81-
func (d *linuxNotify) Events() chan FileEvent {
78+
func (d *naiveNotify) Events() chan FileEvent {
8279
return d.wrappedEvents
8380
}
8481

85-
func (d *linuxNotify) Errors() chan error {
82+
func (d *naiveNotify) Errors() chan error {
8683
return d.errors
8784
}
8885

89-
func (d *linuxNotify) loop() {
86+
func (d *naiveNotify) loop() {
9087
for e := range d.events {
9188
isCreateOp := e.Op&fsnotify.Create == fsnotify.Create
9289
shouldWalk := false
@@ -124,7 +121,7 @@ func (d *linuxNotify) loop() {
124121
}
125122
}
126123

127-
func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) {
124+
func (d *naiveNotify) sendEventIfWatched(e fsnotify.Event) {
128125
if _, ok := d.watchList[e.Name]; ok {
129126
d.wrappedEvents <- FileEvent{e.Name}
130127
} else {
@@ -138,15 +135,15 @@ func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) {
138135
}
139136
}
140137

141-
func NewWatcher() (*linuxNotify, error) {
138+
func NewWatcher() (*naiveNotify, error) {
142139
fsw, err := fsnotify.NewWatcher()
143140
if err != nil {
144141
return nil, err
145142
}
146143

147144
wrappedEvents := make(chan FileEvent)
148145

149-
wmw := &linuxNotify{
146+
wmw := &naiveNotify{
150147
watcher: fsw,
151148
events: fsw.Events,
152149
wrappedEvents: wrappedEvents,
@@ -169,30 +166,4 @@ func isDir(pth string) (bool, error) {
169166
return fi.IsDir(), nil
170167
}
171168

172-
func checkInotifyLimits() error {
173-
if !LimitChecksEnabled() {
174-
return nil
175-
}
176-
177-
data, err := ioutil.ReadFile("/proc/sys/fs/inotify/max_user_watches")
178-
if err != nil {
179-
return err
180-
}
181-
182-
i, err := strconv.Atoi(strings.TrimSpace(string(data)))
183-
if err != nil {
184-
return err
185-
}
186-
187-
if i < inotifyMin {
188-
return grpc.Errorf(
189-
codes.ResourceExhausted,
190-
"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",
191-
i,
192-
)
193-
}
194-
195-
return nil
196-
}
197-
198-
var _ Notify = &linuxNotify{}
169+
var _ Notify = &naiveNotify{}

0 commit comments

Comments
 (0)