Skip to content

Commit

Permalink
Use common error when removing an unwatched file
Browse files Browse the repository at this point in the history
port of fsnotify/fsnotify#460

The errors returned by the various implementations of the watcher are all different
which makes handling them difficult. This PR follows the suggestion in:
fsnotify/fsnotify#455 (comment) by @mattn
to create a common error which is wrapped by the implementations.
  • Loading branch information
zeripath authored and shogo82148 committed Mar 6, 2024
1 parent 83fe815 commit 63cfc04
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion fsnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ func (e Event) String() string {

// Common errors that can be reported by a watcher
var (
ErrEventOverflow = errors.New("fsnotify queue overflow")
ErrNonExistentWatch = errors.New("fsnotify: can't remove non-existent watcher")
ErrEventOverflow = errors.New("fsnotify: queue overflow")
)
2 changes: 1 addition & 1 deletion inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (w *Watcher) Remove(name string) error {

// Remove it from inotify.
if !ok {
return fmt.Errorf("can't remove non-existent inotify watch for: %s", name)
return fmt.Errorf("%w: %s", ErrNonExistentWatch, name)
}

// We successfully removed the watch if InotifyRmWatch doesn't return an
Expand Down
5 changes: 4 additions & 1 deletion inotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package fsnotify

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -303,6 +304,8 @@ func TestInotifyRemoveTwice(t *testing.T) {
err = w.Remove(testFile)
if err == nil {
t.Fatalf("no error on removing invalid file")
} else if !errors.Is(err, ErrNonExistentWatch) {
t.Fatalf("unexpected error %v on removing invalid file", err)
}

w.mu.Lock()
Expand Down Expand Up @@ -384,7 +387,7 @@ func TestInotifyOverflow(t *testing.T) {
for dn := 0; dn < numDirs; dn++ {
testSubdir := fmt.Sprintf("%s/%d", testDir, dn)

err := os.Mkdir(testSubdir, 0777)
err := os.Mkdir(testSubdir, 0o777)
if err != nil {
t.Fatalf("Cannot create subdir: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions kqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (w *Watcher) Close() error {
w.isClosed = true

// copy paths to remove while locked
var pathsToRemove = make([]string, 0, len(w.watches))
pathsToRemove := make([]string, 0, len(w.watches))
for name := range w.watches {
pathsToRemove = append(pathsToRemove, name)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (w *Watcher) Remove(name string) error {
watchfd, ok := w.watches[name]
w.mu.Unlock()
if !ok {
return fmt.Errorf("can't remove non-existent kevent watch for: %s", name)
return fmt.Errorf("%w: %s", ErrNonExistentWatch, name)
}

const registerRemove = unix.EV_DELETE
Expand Down
2 changes: 1 addition & 1 deletion windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (w *Watcher) remWatch(pathname string) error {
watch := w.watches.get(ino)
w.mu.Unlock()
if watch == nil {
return fmt.Errorf("can't remove non-existent watch for: %s", pathname)
return fmt.Errorf("%w: %s", ErrNonExistentWatch, pathname)
}
if pathname == dir {
w.sendEvent(watch.path, watch.mask&sysFSIGNORED)
Expand Down

0 comments on commit 63cfc04

Please sign in to comment.