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
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)
}

View file

@ -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,

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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
// }

View file

@ -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"

View file

@ -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
}