Fix fs error handling

This commit is contained in:
Daniel 2022-10-11 14:49:06 +02:00
parent b53b77e28c
commit 1144ac589b
8 changed files with 25 additions and 16 deletions

View file

@ -33,7 +33,7 @@ func checkAndCreateInstanceLock(path, name string, perUser bool) (pid int32, err
// read current pid file // read current pid file
data, err := os.ReadFile(lockFilePath) data, err := os.ReadFile(lockFilePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
// create new lock // create new lock
return 0, createInstanceLock(lockFilePath) return 0, createInstanceLock(lockFilePath)
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -79,7 +80,7 @@ func sign(cmd *cobra.Command, args []string) error {
// Check if there is an existing signature. // Check if there is an existing signature.
_, err := os.Stat(file.Path() + filesig.Extension) _, err := os.Stat(file.Path() + filesig.Extension)
switch { switch {
case err == nil || os.IsExist(err): case err == nil || errors.Is(err, fs.ErrExist):
// If the file exists, just verify. // If the file exists, just verify.
fileData, err := filesig.VerifyFile( fileData, err := filesig.VerifyFile(
file.Path(), file.Path(),
@ -97,7 +98,7 @@ func sign(cmd *cobra.Command, args []string) error {
verified++ verified++
} }
case os.IsNotExist(err): case errors.Is(err, fs.ErrNotExist):
// Attempt to sign file. // Attempt to sign file.
fileData, err := filesig.SignFile( fileData, err := filesig.SignFile(
file.Path(), file.Path(),
@ -123,10 +124,10 @@ func sign(cmd *cobra.Command, args []string) error {
} }
if verified > 0 { if verified > 0 {
fmt.Printf("[STAT] verified %d files", verified) fmt.Printf("[STAT] verified %d files\n", verified)
} }
if signed > 0 { if signed > 0 {
fmt.Printf("[STAT] signed %d files", signed) fmt.Printf("[STAT] signed %d files\n", signed)
} }
if fails > 0 { if fails > 0 {
return fmt.Errorf("signing or verification failed on %d files", fails) 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. // Check if there is an existing signature.
_, err := os.Stat(sigFile) _, err := os.Stat(sigFile)
switch { switch {
case err == nil || os.IsExist(err): case err == nil || errors.Is(err, fs.ErrExist):
// If the file exists, just verify. // If the file exists, just verify.
fileData, err := filesig.VerifyFile( fileData, err := filesig.VerifyFile(
file, file,
@ -189,7 +190,7 @@ func signIndex(cmd *cobra.Command, args []string) error {
} }
fallthrough fallthrough
case os.IsNotExist(err): case errors.Is(err, fs.ErrNotExist):
// Attempt to sign file. // Attempt to sign file.
fileData, err := filesig.SignFile( fileData, err := filesig.SignFile(
file, file,

View file

@ -1,6 +1,8 @@
package netenv package netenv
import ( import (
"errors"
"io/fs"
"os" "os"
"testing" "testing"
) )
@ -12,7 +14,7 @@ func TestDbus(t *testing.T) {
t.Skip("skipping test in short mode because it fails in the CI") 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) t.Logf("skipping dbus tests, as dbus does not seem to be installed: %s", err)
return return
} }

View file

@ -3,7 +3,9 @@
package proc package proc
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"time" "time"
@ -103,7 +105,7 @@ func findSocketFromPid(pid int, socketName string) bool {
for _, entry := range entries { for _, entry := range entries {
link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, entry)) link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, entry))
if err != nil { 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) log.Warningf("proc: failed to read link /proc/%d/fd/%s: %s", pid, entry, err)
} }
continue continue
@ -122,7 +124,7 @@ func findSocketFromPid(pid int, socketName string) bool {
func readDirNames(dir string) (names []string) { func readDirNames(dir string) (names []string) {
file, err := os.Open(dir) file, err := os.Open(dir)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !errors.Is(err, fs.ErrNotExist) {
log.Warningf("proc: could not open directory %s: %s", dir, err) log.Warningf("proc: could not open directory %s: %s", dir, err)
} }
return return

View file

@ -3,7 +3,9 @@
package proc package proc
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"strconv" "strconv"
"sync" "sync"
@ -50,7 +52,7 @@ func updatePids() {
statData, err := os.Stat(fmt.Sprintf("/proc/%d", pid)) statData, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !errors.Is(err, fs.ErrNotExist) {
log.Warningf("proc: could not stat /proc/%d: %s", pid, err) log.Warningf("proc: could not stat /proc/%d: %s", pid, err)
} }
continue entryLoop continue entryLoop

View file

@ -57,7 +57,7 @@ package profile
// lastError = fmt.Errorf("constructed path \"%s\" from framework is not absolute", buildPath) // lastError = fmt.Errorf("constructed path \"%s\" from framework is not absolute", buildPath)
// continue // 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) // lastError = fmt.Errorf("constructed path \"%s\" does not exist", buildPath)
// continue // continue
// } // }

View file

@ -4,9 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "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) { func ServeFileFromArchive(w http.ResponseWriter, r *http.Request, archiveName string, archiveFS *zipfs.FileSystem, path string) {
readCloser, err := archiveFS.Open(path) readCloser, err := archiveFS.Open(path)
if err != nil { 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. // Check if there is a base index.html file we can serve instead.
var indexErr error var indexErr error
path = "index.html" path = "index.html"

View file

@ -1,7 +1,9 @@
package helper package helper
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -106,13 +108,13 @@ func indexExists(registry *updater.ResourceRegistry, indexPath string) bool {
func deleteIndex(registry *updater.ResourceRegistry, indexPath string) error { func deleteIndex(registry *updater.ResourceRegistry, indexPath string) error {
// Remove index itself. // Remove index itself.
err := os.Remove(filepath.Join(registry.StorageDir().Path, indexPath)) 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 return err
} }
// Remove any accompanying signature. // Remove any accompanying signature.
err = os.Remove(filepath.Join(registry.StorageDir().Path, indexPath+filesig.Extension)) 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 return err
} }