]> go.fuhry.dev Git - fsnotify.git/commitdiff
A few staticcheck fixes (#539)
authorMartin Tournoij <martin@arp242.net>
Wed, 16 Nov 2022 04:17:00 +0000 (05:17 +0100)
committerGitHub <noreply@github.com>
Wed, 16 Nov 2022 04:17:00 +0000 (05:17 +0100)
This fixes a few small staticcheck issues; turns out that staticcheck
only compiles the files for the current GOOS:
https://staticcheck.io/docs/running-staticcheck/cli/build-tags/

You can run all systems with:

staticcheck -matrix <<EOF
windows: GOOS=windows
linux: GOOS=linux
freebsd: GOOS=freebsd
openbsd: GOOS=openbsd
netbsd: GOOS=netbsd
darwin: GOOS=darwin
illumos: GOOS=illumos
EOF

The CI runner doesn't really support that, but it's little more than a
small wrapper around "go install" with some variables, so just use that
directly instead of the action.

.github/workflows/staticcheck.yml
CHANGELOG.md
backend_kqueue.go
backend_windows.go
internal/freebsd.go
internal/internal.go [new file with mode: 0644]
internal/unix.go

index 2906aa96d69baac2f908830a15636f5c48f9d08c..f3caf4def786d89a21cb83f2e1bdd80320a5470f 100644 (file)
@@ -10,9 +10,28 @@ jobs:
     name:    'staticcheck'
     runs-on: ubuntu-latest
     steps:
-    - uses: actions/checkout@v3
-      with:
-        fetch-depth: 1
-    - uses: dominikh/staticcheck-action@v1.2.0
-      with:
-        version: '2022.1'
+      - id: install_go
+        uses: WillAbides/setup-go-faster@v1.7.0
+        with:
+          go-version: "1.19.x"
+
+      - uses: actions/cache@v3
+        with:
+          key: ${{ runner.os }}-staticcheck
+          path: |
+            ${{ runner.temp }}/staticcheck
+            ${{ steps.install_go.outputs.GOCACHE || '' }}
+
+      - run: |
+          export STATICCHECK_CACHE="${{ runner.temp }}/staticcheck"
+          go install honnef.co/go/tools/cmd/staticcheck@latest
+
+          $(go env GOPATH)/bin/staticcheck -matrix <<EOF
+          windows: GOOS=windows
+          linux: GOOS=linux
+          freebsd: GOOS=freebsd
+          openbsd: GOOS=openbsd
+          netbsd: GOOS=netbsd
+          darwin: GOOS=darwin
+          illumos: GOOS=illumos
+          EOF
index 3b7f9e1e4ea7e8f361a9eea46f0a93d899660f76..31fa1a47d61e197c7bc5982e7912a07fc932823c 100644 (file)
@@ -40,10 +40,14 @@ Unreleased
 
 - kqueue: deal with `rm -rf watched-dir` better ([#526], [#537])
 
-- Add `Watcher.Errors` and `Watcher.Events` to the no-op `Watcher` in
+- 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
   WASM, AIX, etc. ([#528])
 
+- other: use the backend_other.go no-op if the `appengine` build tag is set;
+  Google AppEngine forbids usage of the unsafe package so the inotify backend
+  won't work there.
+
 
 [#371]: https://github.com/fsnotify/fsnotify/pull/371
 [#516]: https://github.com/fsnotify/fsnotify/pull/516
index b6f5121a08e266fb131a94baabffb2a6d000682b..e48225b276a4dfb3e87c529caea19ab644969f87 100644 (file)
@@ -6,7 +6,6 @@ package fsnotify
 import (
        "errors"
        "fmt"
-       "io/ioutil"
        "os"
        "path/filepath"
        "sync"
@@ -570,8 +569,8 @@ func (w *Watcher) readEvents() {
                                        }
                                } else {
                                        filePath := filepath.Clean(event.Name)
-                                       if fileInfo, err := os.Lstat(filePath); err == nil {
-                                               err := w.sendFileCreatedEventIfNew(filePath, fileInfo)
+                                       if fi, err := os.Lstat(filePath); err == nil {
+                                               err := w.sendFileCreatedEventIfNew(filePath, fi)
                                                if err != nil {
                                                        if !w.sendError(err) {
                                                                closed = true
@@ -610,15 +609,20 @@ func (w *Watcher) newEvent(name string, mask uint32) Event {
 // watchDirectoryFiles to mimic inotify when adding a watch on a directory
 func (w *Watcher) watchDirectoryFiles(dirPath string) error {
        // Get all files
-       files, err := ioutil.ReadDir(dirPath)
+       files, err := os.ReadDir(dirPath)
        if err != nil {
                return err
        }
 
-       for _, fileInfo := range files {
-               path := filepath.Join(dirPath, fileInfo.Name())
+       for _, f := range files {
+               path := filepath.Join(dirPath, f.Name())
 
-               cleanPath, err := w.internalWatch(path, fileInfo)
+               fi, err := f.Info()
+               if err != nil {
+                       return fmt.Errorf("%q: %w", filepath.Join(dirPath, fi.Name()), err)
+               }
+
+               cleanPath, err := w.internalWatch(path, fi)
                if err != nil {
                        // No permission to read the file; that's not a problem: just skip.
                        // But do add it to w.fileExists to prevent it from being picked up
@@ -628,7 +632,7 @@ func (w *Watcher) watchDirectoryFiles(dirPath string) error {
                        case errors.Is(err, unix.EACCES) || errors.Is(err, unix.EPERM):
                                cleanPath = filepath.Clean(path)
                        default:
-                               return fmt.Errorf("%q: %w", filepath.Join(dirPath, fileInfo.Name()), err)
+                               return fmt.Errorf("%q: %w", filepath.Join(dirPath, fi.Name()), err)
                        }
                }
 
@@ -645,7 +649,7 @@ func (w *Watcher) watchDirectoryFiles(dirPath string) error {
 // This functionality is to have the BSD watcher match the inotify, which sends
 // a create event for files created in a watched directory.
 func (w *Watcher) sendDirectoryChangeEvents(dir string) error {
-       files, err := ioutil.ReadDir(dir)
+       files, err := os.ReadDir(dir)
        if err != nil {
                // Directory no longer exists: we can ignore this safely. kqueue will
                // still give us the correct events.
@@ -655,8 +659,13 @@ func (w *Watcher) sendDirectoryChangeEvents(dir string) error {
                return fmt.Errorf("fsnotify.sendDirectoryChangeEvents: %w", err)
        }
 
-       for _, fi := range files {
-               err := w.sendFileCreatedEventIfNew(filepath.Join(dir, fi.Name()), fi)
+       for _, f := range files {
+               fi, err := f.Info()
+               if err != nil {
+                       return fmt.Errorf("fsnotify.sendDirectoryChangeEvents: %w", err)
+               }
+
+               err = w.sendFileCreatedEventIfNew(filepath.Join(dir, fi.Name()), fi)
                if err != nil {
                        // Don't need to send an error if this file isn't readable.
                        if errors.Is(err, unix.EACCES) || errors.Is(err, unix.EPERM) {
@@ -669,7 +678,7 @@ func (w *Watcher) sendDirectoryChangeEvents(dir string) error {
 }
 
 // sendFileCreatedEvent sends a create event if the file isn't already being tracked.
-func (w *Watcher) sendFileCreatedEventIfNew(filePath string, fileInfo os.FileInfo) (err error) {
+func (w *Watcher) sendFileCreatedEventIfNew(filePath string, fi os.FileInfo) (err error) {
        w.mu.Lock()
        _, doesExist := w.fileExists[filePath]
        w.mu.Unlock()
@@ -680,7 +689,7 @@ func (w *Watcher) sendFileCreatedEventIfNew(filePath string, fileInfo os.FileInf
        }
 
        // like watchDirectoryFiles (but without doing another ReadDir)
-       filePath, err = w.internalWatch(filePath, fileInfo)
+       filePath, err = w.internalWatch(filePath, fi)
        if err != nil {
                return err
        }
@@ -692,8 +701,8 @@ func (w *Watcher) sendFileCreatedEventIfNew(filePath string, fileInfo os.FileInf
        return nil
 }
 
-func (w *Watcher) internalWatch(name string, fileInfo os.FileInfo) (string, error) {
-       if fileInfo.IsDir() {
+func (w *Watcher) internalWatch(name string, fi os.FileInfo) (string, error) {
+       if fi.IsDir() {
                // mimic Linux providing delete events for subdirectories, but preserve
                // the flags used if currently watching subdirectory
                w.mu.Lock()
index e99139e7980b02b3543fd8ef74553381f2bda636..86a320a0402eaf6f6f572df92c1afc7a7fda7929 100644 (file)
@@ -635,6 +635,8 @@ func (w *Watcher) readEvents() {
                }
 
                switch qErr {
+               case nil:
+                       // No error
                case windows.ERROR_MORE_DATA:
                        if watch == nil {
                                w.sendError(errors.New("ERROR_MORE_DATA has unexpectedly null lpOverlapped buffer"))
@@ -656,7 +658,6 @@ func (w *Watcher) readEvents() {
                default:
                        w.sendError(os.NewSyscallError("GetQueuedCompletionPort", qErr))
                        continue
-               case nil:
                }
 
                var offset uint32
@@ -733,8 +734,9 @@ func (w *Watcher) readEvents() {
 
                        // Error!
                        if offset >= n {
+                               //lint:ignore ST1005 Windows should be capitalized
                                w.sendError(errors.New(
-                                       "Windows system assumed buffer larger than it is, events have likely been missed."))
+                                       "Windows system assumed buffer larger than it is, events have likely been missed"))
                                break
                        }
                }
index 4128482390909bc736d0b8ab74ae80325438fe83..285c54b79caba31c1691ccaa75669c5b61600fcd 100644 (file)
@@ -16,8 +16,8 @@ var (
 
 var maxfiles uint64
 
-// Go 1.19 will do this automatically: https://go-review.googlesource.com/c/go/+/393354/
 func SetRlimit() {
+       // Go 1.19 will do this automatically: https://go-review.googlesource.com/c/go/+/393354/
        var l syscall.Rlimit
        err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &l)
        if err == nil && l.Cur != l.Max {
diff --git a/internal/internal.go b/internal/internal.go
new file mode 100644 (file)
index 0000000..7daa45e
--- /dev/null
@@ -0,0 +1,2 @@
+// Package internal contains some helpers.
+package internal
index 301b242e6ebe3c09f6599c75c0f2bf5582ef5166..e006de4daf3e19639d61ea1a738054c7884ed130 100644 (file)
@@ -16,8 +16,8 @@ var (
 
 var maxfiles uint64
 
-// Go 1.19 will do this automatically: https://go-review.googlesource.com/c/go/+/393354/
 func SetRlimit() {
+       // Go 1.19 will do this automatically: https://go-review.googlesource.com/c/go/+/393354/
        var l syscall.Rlimit
        err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &l)
        if err == nil && l.Cur != l.Max {