From 1144ac589b9c7e22b4f3fafc31e37472bf87060c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 11 Oct 2022 14:49:06 +0200 Subject: [PATCH] Fix fs error handling --- cmds/portmaster-start/lock.go | 2 +- cmds/updatemgr/sign.go | 13 +++++++------ netenv/dbus_linux_test.go | 4 +++- network/proc/findpid.go | 6 ++++-- network/proc/pids_by_user.go | 4 +++- profile/framework.go | 2 +- ui/serve.go | 4 ++-- updates/helper/indexes.go | 6 ++++-- 8 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cmds/portmaster-start/lock.go b/cmds/portmaster-start/lock.go index c09e8731..33ba8d8e 100644 --- a/cmds/portmaster-start/lock.go +++ b/cmds/portmaster-start/lock.go @@ -33,7 +33,7 @@ func checkAndCreateInstanceLock(path, name string, perUser bool) (pid int32, err // read current pid file data, err := os.ReadFile(lockFilePath) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { // create new lock return 0, createInstanceLock(lockFilePath) } diff --git a/cmds/updatemgr/sign.go b/cmds/updatemgr/sign.go index b0011356..02cfb15d 100644 --- a/cmds/updatemgr/sign.go +++ b/cmds/updatemgr/sign.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -79,7 +80,7 @@ func sign(cmd *cobra.Command, args []string) error { // Check if there is an existing signature. _, err := os.Stat(file.Path() + filesig.Extension) switch { - case err == nil || os.IsExist(err): + case err == nil || errors.Is(err, fs.ErrExist): // If the file exists, just verify. fileData, err := filesig.VerifyFile( file.Path(), @@ -97,7 +98,7 @@ func sign(cmd *cobra.Command, args []string) error { verified++ } - case os.IsNotExist(err): + case errors.Is(err, fs.ErrNotExist): // Attempt to sign file. fileData, err := filesig.SignFile( file.Path(), @@ -123,10 +124,10 @@ func sign(cmd *cobra.Command, args []string) error { } if verified > 0 { - fmt.Printf("[STAT] verified %d files", verified) + fmt.Printf("[STAT] verified %d files\n", verified) } if signed > 0 { - fmt.Printf("[STAT] signed %d files", signed) + fmt.Printf("[STAT] signed %d files\n", signed) } if fails > 0 { return fmt.Errorf("signing or verification failed on %d files", fails) @@ -170,7 +171,7 @@ func signIndex(cmd *cobra.Command, args []string) error { // Check if there is an existing signature. _, err := os.Stat(sigFile) switch { - case err == nil || os.IsExist(err): + case err == nil || errors.Is(err, fs.ErrExist): // If the file exists, just verify. fileData, err := filesig.VerifyFile( file, @@ -189,7 +190,7 @@ func signIndex(cmd *cobra.Command, args []string) error { } fallthrough - case os.IsNotExist(err): + case errors.Is(err, fs.ErrNotExist): // Attempt to sign file. fileData, err := filesig.SignFile( file, diff --git a/netenv/dbus_linux_test.go b/netenv/dbus_linux_test.go index 160d4c06..65abac07 100644 --- a/netenv/dbus_linux_test.go +++ b/netenv/dbus_linux_test.go @@ -1,6 +1,8 @@ package netenv import ( + "errors" + "io/fs" "os" "testing" ) @@ -12,7 +14,7 @@ func TestDbus(t *testing.T) { t.Skip("skipping test in short mode because it fails in the CI") } - if _, err := os.Stat("/var/run/dbus/system_bus_socket"); os.IsNotExist(err) { + if _, err := os.Stat("/var/run/dbus/system_bus_socket"); errors.Is(err, fs.ErrNotExist) { t.Logf("skipping dbus tests, as dbus does not seem to be installed: %s", err) return } diff --git a/network/proc/findpid.go b/network/proc/findpid.go index 693fb72f..2c89da29 100644 --- a/network/proc/findpid.go +++ b/network/proc/findpid.go @@ -3,7 +3,9 @@ package proc import ( + "errors" "fmt" + "io/fs" "os" "time" @@ -103,7 +105,7 @@ func findSocketFromPid(pid int, socketName string) bool { for _, entry := range entries { link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, entry)) if err != nil { - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { log.Warningf("proc: failed to read link /proc/%d/fd/%s: %s", pid, entry, err) } continue @@ -122,7 +124,7 @@ func findSocketFromPid(pid int, socketName string) bool { func readDirNames(dir string) (names []string) { file, err := os.Open(dir) if err != nil { - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { log.Warningf("proc: could not open directory %s: %s", dir, err) } return diff --git a/network/proc/pids_by_user.go b/network/proc/pids_by_user.go index 662d2a07..95d6122c 100644 --- a/network/proc/pids_by_user.go +++ b/network/proc/pids_by_user.go @@ -3,7 +3,9 @@ package proc import ( + "errors" "fmt" + "io/fs" "os" "strconv" "sync" @@ -50,7 +52,7 @@ func updatePids() { statData, err := os.Stat(fmt.Sprintf("/proc/%d", pid)) if err != nil { - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { log.Warningf("proc: could not stat /proc/%d: %s", pid, err) } continue entryLoop diff --git a/profile/framework.go b/profile/framework.go index dc203098..4eae57c7 100644 --- a/profile/framework.go +++ b/profile/framework.go @@ -57,7 +57,7 @@ package profile // lastError = fmt.Errorf("constructed path \"%s\" from framework is not absolute", buildPath) // continue // } -// if _, err := os.Stat(buildPath); os.IsNotExist(err) { +// if _, err := os.Stat(buildPath); errors.Is(err, fs.ErrNotExist) { // lastError = fmt.Errorf("constructed path \"%s\" does not exist", buildPath) // continue // } diff --git a/ui/serve.go b/ui/serve.go index 1d80a059..811be7df 100644 --- a/ui/serve.go +++ b/ui/serve.go @@ -4,9 +4,9 @@ import ( "errors" "fmt" "io" + "io/fs" "net/http" "net/url" - "os" "path/filepath" "strings" "sync" @@ -125,7 +125,7 @@ func (bs *archiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { func ServeFileFromArchive(w http.ResponseWriter, r *http.Request, archiveName string, archiveFS *zipfs.FileSystem, path string) { readCloser, err := archiveFS.Open(path) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { // Check if there is a base index.html file we can serve instead. var indexErr error path = "index.html" diff --git a/updates/helper/indexes.go b/updates/helper/indexes.go index d0722f34..2995e168 100644 --- a/updates/helper/indexes.go +++ b/updates/helper/indexes.go @@ -1,7 +1,9 @@ package helper import ( + "errors" "fmt" + "io/fs" "os" "path/filepath" @@ -106,13 +108,13 @@ func indexExists(registry *updater.ResourceRegistry, indexPath string) bool { func deleteIndex(registry *updater.ResourceRegistry, indexPath string) error { // Remove index itself. err := os.Remove(filepath.Join(registry.StorageDir().Path, indexPath)) - if err != nil && !os.IsNotExist(err) { + if err != nil && !errors.Is(err, fs.ErrNotExist) { return err } // Remove any accompanying signature. err = os.Remove(filepath.Join(registry.StorageDir().Path, indexPath+filesig.Extension)) - if err != nil && !os.IsNotExist(err) { + if err != nil && !errors.Is(err, fs.ErrNotExist) { return err }