Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Nov 16, 2022
1 parent afa21f9 commit 76959bc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Expand Up @@ -36,9 +36,17 @@ Unreleased
Before it would merely return "short read", making it hard to detect this
error.

- all: return `ErrClosed` on `Add()` when the watcher is closed ([#516])
- kqueue: make sure events for all files are delivered properly when removing a
watched directory ([#526])

- kqueue: deal with `rm -rf watched-dir` better ([#526], [#537])
Previously they would get sent with "" or "." as the path name.

- kqueue: don't emit spurious Create events for symbolic links ([#524])

The link would get resolved but kqueue would "forget" it already saw the link
itself, resulting on a Create for every Write event for the directory.

- all: return ErrClosed on Add() when the watcher is closed ([#516])

- other: add `Watcher.Errors` and `Watcher.Events` to the no-op `Watcher` in
`backend_other.go`, making it easier to use on unsupported platforms such as
Expand Down
9 changes: 4 additions & 5 deletions backend_kqueue.go
Expand Up @@ -160,6 +160,7 @@ func NewWatcher() (*Watcher, error) {
// all.
func newKqueue() (kq int, closepipe [2]int, err error) {
kq, err = unix.Kqueue()
// fmt.Println("OPEN", kq, err)
if kq == -1 {
return kq, closepipe, err
}
Expand Down Expand Up @@ -500,13 +501,10 @@ func (w *Watcher) addWatch(name string, flags uint32) (string, error) {
// Event values that it sends down the Events channel.
func (w *Watcher) readEvents() {
defer func() {
err := unix.Close(w.kq)
if err != nil {
w.Errors <- err
}
unix.Close(w.closepipe[0])
close(w.Events)
close(w.Errors)
_ = unix.Close(w.kq)
unix.Close(w.closepipe[0])
}()

eventBuffer := make([]unix.Kevent_t, 10)
Expand Down Expand Up @@ -742,6 +740,7 @@ func (w *Watcher) register(fds []int, flags int, fflags uint32) error {
// read retrieves pending events, or waits until an event occurs.
func (w *Watcher) read(events []unix.Kevent_t) ([]unix.Kevent_t, error) {
n, err := unix.Kevent(w.kq, nil, events, nil)
// fmt.Printf("kevent(%d) -> %d %v\n", w.kq, n, err)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion fsnotify.go
Expand Up @@ -63,7 +63,7 @@ const (

// Common errors that can be reported.
var (
ErrNonExistentWatch = errors.New("fsnotify: can't remove non-existent watcher")
ErrNonExistentWatch = errors.New("fsnotify: can't remove non-existent watch")
ErrEventOverflow = errors.New("fsnotify: queue or buffer overflow")
ErrClosed = errors.New("fsnotify: watcher already closed")
)
Expand Down
22 changes: 15 additions & 7 deletions fsnotify_test.go
Expand Up @@ -531,6 +531,16 @@ func TestWatchSymlink(t *testing.T) {

// Bug #277
{"277", func(t *testing.T, w *Watcher, tmp string) {
// TODO: there is some strange fuckery going on if I use go test
// -count=2; the second test run has unix.Kqueue() in newKqueue()
// return 0, which is a very odd fd number, but the first event does
// work (create /foo). After that we get EBADF (Bad file
// descriptor).
//
// This is *only* for this test, and *only* if we have the symlinks
// below. kqueue(2) doesn't document returning fd 0.
//
// This happens on both FreeBSD and NetBSD.
touch(t, tmp, "file1")
touch(t, tmp, "file2")
symlink(t, join(tmp, "file1"), tmp, "link1")
Expand All @@ -551,15 +561,13 @@ func TestWatchSymlink(t *testing.T) {
remove /pear # rm -r pear
kqueue:
create /foo # touch foo
remove /foo # rm foo
create /apple # mkdir apple
create /pear # mv apple pear
remove|rename /apple
remove /pear # rm -r pear
create /foo # touch foo
remove /foo # rm foo
create /apple # mkdir apple
rename /apple # mv apple pear
# TODO: why no create /pear and remove /pear?
`},
}

for _, tt := range tests {
tt := tt
tt.run(t)
Expand Down

0 comments on commit 76959bc

Please sign in to comment.