Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filesystem: refactor: use errors.Is instead of os.IsNotExist #2558

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/gopsutil/net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -673,7 +674,7 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
t, err := getProcInodes(root, pid, max)
if err != nil {
// skip if permission error or no longer exists
if os.IsPermission(err) || os.IsNotExist(err) || err == io.EOF {
if os.IsPermission(err) || errors.Is(err, fs.ErrNotExist) || err == io.EOF {
continue
}
return ret, err
Expand Down
4 changes: 3 additions & 1 deletion internal/gopsutil/process/process_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package process

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -86,7 +88,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
// This covers the case when running inside container with a different process namespace (by default)

_, err := os.Stat(common.HostProc(strconv.Itoa(int(pid))))
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return err == nil, err
Expand Down
15 changes: 8 additions & 7 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package filesystem

import (
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -143,11 +144,11 @@ func New(config ...Config) fiber.Handler {
path = utils.TrimRight(path, '/')
}
file, err := cfg.Root.Open(path)
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
if err != nil && errors.Is(err, fs.ErrNotExist) && cfg.NotFoundFile != "" {
file, err = cfg.Root.Open(cfg.NotFoundFile)
}
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return c.Status(fiber.StatusNotFound).Next()
}
return fmt.Errorf("failed to open: %w", err)
Expand Down Expand Up @@ -217,10 +218,10 @@ func New(config ...Config) fiber.Handler {
}

// SendFile ...
func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) error {
file, err := fs.Open(path)
func SendFile(c *fiber.Ctx, filesystem http.FileSystem, path string) error {
file, err := filesystem.Open(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return fiber.ErrNotFound
}
return fmt.Errorf("failed to open: %w", err)
Expand All @@ -234,7 +235,7 @@ func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) error {
// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + ConfigDefault.Index
index, err := fs.Open(indexPath)
index, err := filesystem.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
if err == nil {
Expand Down