From 15b347db92f11d4aa0841d601e3f612c74df23b6 Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Sat, 5 Aug 2023 22:31:39 +0300 Subject: [PATCH] filesystem: refactor: use `errors.Is` instead of `os.IsNotExist` --- internal/gopsutil/net/net_linux.go | 3 ++- internal/gopsutil/process/process_posix.go | 4 +++- middleware/filesystem/filesystem.go | 15 ++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/gopsutil/net/net_linux.go b/internal/gopsutil/net/net_linux.go index de8a3a7e80..964b053c73 100644 --- a/internal/gopsutil/net/net_linux.go +++ b/internal/gopsutil/net/net_linux.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "io" + "io/fs" "net" "os" "strconv" @@ -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 diff --git a/internal/gopsutil/process/process_posix.go b/internal/gopsutil/process/process_posix.go index 1b6c6fc2d7..ee09574c74 100644 --- a/internal/gopsutil/process/process_posix.go +++ b/internal/gopsutil/process/process_posix.go @@ -5,7 +5,9 @@ package process import ( "context" + "errors" "fmt" + "io/fs" "os" "os/user" "path/filepath" @@ -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 diff --git a/middleware/filesystem/filesystem.go b/middleware/filesystem/filesystem.go index 4b5b6c2928..42276a9b5b 100644 --- a/middleware/filesystem/filesystem.go +++ b/middleware/filesystem/filesystem.go @@ -1,9 +1,10 @@ package filesystem import ( + "errors" "fmt" + "io/fs" "net/http" - "os" "strconv" "strings" "sync" @@ -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) @@ -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) @@ -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 {