]> go.fuhry.dev Git - fsnotify.git/commitdiff
Use common error when removing an unwatched file (#460)
authorMartin Tournoij <martin@arp242.net>
Thu, 21 Jul 2022 03:06:57 +0000 (05:06 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Jul 2022 03:06:57 +0000 (05:06 +0200)
The errors returned by the various implementations of the watcher are all different
which makes handling them difficult. This PR follows the suggestion in:
https://github.com/fsnotify/fsnotify/pull/455#issuecomment-1146037157 by @mattn
to create a common error which is wrapped by the implementations.

Replaces: https://github.com/fsnotify/fsnotify/pull/455
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Andrew Thornton <art27@cantab.net>
fsnotify.go
inotify.go
inotify_test.go
kqueue.go
windows.go

index 0f4ee52e8aa202bc8c6d89eaa2c5f4efd554f69e..b474b5263a55f29cd755ad1d22cb68c21c017f1e 100644 (file)
@@ -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("can't remove non-existent watcher")
+       ErrEventOverflow    = errors.New("fsnotify queue overflow")
 )
index a6d0e0ec8c10e1720ab8cf1ef5bee16b6efef4ee..e6abddfdd04c390bf9f9ea5fbb03c8b9f95a57d4 100644 (file)
@@ -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
index bb6db097234fc4f361a72f5f29233d5e7c6ef953..728f5c218ff752c7a61dbf337774463967c67dc4 100644 (file)
@@ -8,6 +8,7 @@
 package fsnotify
 
 import (
+       "errors"
        "fmt"
        "os"
        "path/filepath"
@@ -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()
@@ -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)
                }
index 6fb8d8532e7f365dfcebf460fbe5e96367a14140..1b46acbd4e37fd10c008b8292b0fd15e86256365 100644 (file)
--- a/kqueue.go
+++ b/kqueue.go
@@ -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)
        }
@@ -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
@@ -442,7 +442,6 @@ func (w *Watcher) sendDirectoryChangeEvents(dirPath string) {
        for _, fileInfo := range files {
                filePath := filepath.Join(dirPath, fileInfo.Name())
                err := w.sendFileCreatedEventIfNew(filePath, fileInfo)
-
                if err != nil {
                        return
                }
index 7ff70093a1fa744e8bc46639736e142e49ee3941..1c1fe412d028abed99a4b47d359eda5b92008978 100644 (file)
@@ -196,8 +196,10 @@ type watch struct {
        buf    [4096]byte
 }
 
-type indexMap map[uint64]*watch
-type watchMap map[uint32]indexMap
+type (
+       indexMap map[uint64]*watch
+       watchMap map[uint32]indexMap
+)
 
 func (w *Watcher) wakeupReader() error {
        e := syscall.PostQueuedCompletionStatus(w.port, 0, 0, nil)
@@ -324,7 +326,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)