Replace os.Is* error checking functions with their errors.Is counterpart

Signed-off-by: Curd Becker <me@curd-becker.de>
This commit is contained in:
Curd Becker
2025-11-17 00:28:05 +01:00
parent e0adafb4ca
commit 536e183451
15 changed files with 40 additions and 36 deletions
+3 -3
View File
@@ -142,7 +142,7 @@ func security(config *configs.Config) error {
func namespaces(config *configs.Config) error { func namespaces(config *configs.Config) error {
if config.Namespaces.Contains(configs.NEWUSER) { if config.Namespaces.Contains(configs.NEWUSER) {
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/user"); errors.Is(err, os.ErrNotExist) {
return errors.New("user namespaces aren't enabled in the kernel") return errors.New("user namespaces aren't enabled in the kernel")
} }
hasPath := config.Namespaces.PathOf(configs.NEWUSER) != "" hasPath := config.Namespaces.PathOf(configs.NEWUSER) != ""
@@ -160,13 +160,13 @@ func namespaces(config *configs.Config) error {
} }
if config.Namespaces.Contains(configs.NEWCGROUP) { if config.Namespaces.Contains(configs.NEWCGROUP) {
if _, err := os.Stat("/proc/self/ns/cgroup"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/cgroup"); errors.Is(err, os.ErrNotExist) {
return errors.New("cgroup namespaces aren't enabled in the kernel") return errors.New("cgroup namespaces aren't enabled in the kernel")
} }
} }
if config.Namespaces.Contains(configs.NEWTIME) { if config.Namespaces.Contains(configs.NEWTIME) {
if _, err := os.Stat("/proc/self/timens_offsets"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/timens_offsets"); errors.Is(err, os.ErrNotExist) {
return errors.New("time namespaces aren't enabled in the kernel") return errors.New("time namespaces aren't enabled in the kernel")
} }
hasPath := config.Namespaces.PathOf(configs.NEWTIME) != "" hasPath := config.Namespaces.PathOf(configs.NEWTIME) != ""
@@ -1,6 +1,7 @@
package validate package validate
import ( import (
"errors"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -172,7 +173,7 @@ func TestValidateSecurityWithoutNEWNS(t *testing.T) {
} }
func TestValidateUserNamespace(t *testing.T) { func TestValidateUserNamespace(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/user"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires userns.") t.Skip("Test requires userns.")
} }
config := &configs.Config{ config := &configs.Config{
@@ -206,7 +207,7 @@ func TestValidateUsernsMappingWithoutNamespace(t *testing.T) {
} }
func TestValidateTimeNamespace(t *testing.T) { func TestValidateTimeNamespace(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/time"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/time"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires timens.") t.Skip("Test requires timens.")
} }
config := &configs.Config{ config := &configs.Config{
@@ -225,7 +226,7 @@ func TestValidateTimeNamespace(t *testing.T) {
} }
func TestValidateTimeNamespaceWithBothPathAndTimeOffset(t *testing.T) { func TestValidateTimeNamespaceWithBothPathAndTimeOffset(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/time"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/time"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires timens.") t.Skip("Test requires timens.")
} }
config := &configs.Config{ config := &configs.Config{
@@ -1020,7 +1021,7 @@ func TestValidateNetDevices(t *testing.T) {
} }
func TestValidateUserSysctlWithUserNamespace(t *testing.T) { func TestValidateUserSysctlWithUserNamespace(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/user"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires userns.") t.Skip("Test requires userns.")
} }
config := &configs.Config{ config := &configs.Config{
+1 -1
View File
@@ -295,7 +295,7 @@ func handleFifoResult(result openResult) error {
return err return err
} }
err := os.Remove(f.Name()) err := os.Remove(f.Name())
if err == nil || os.IsNotExist(err) { if err == nil || errors.Is(err, os.ErrNotExist) {
return nil return nil
} }
return err return err
+5 -5
View File
@@ -125,7 +125,7 @@ func (c *Container) addMaskPaths(req *criurpc.CriuReq) error {
for _, path := range c.config.MaskPaths { for _, path := range c.config.MaskPaths {
fi, err := os.Stat(fmt.Sprintf("/proc/%d/root/%s", c.initProcess.pid(), path)) fi, err := os.Stat(fmt.Sprintf("/proc/%d/root/%s", c.initProcess.pid(), path))
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
continue continue
} }
return err return err
@@ -318,7 +318,7 @@ func (c *Container) Checkpoint(criuOpts *CriuOpts) error {
// Since a container can be C/R'ed multiple times, // Since a container can be C/R'ed multiple times,
// the checkpoint directory may already exist. // the checkpoint directory may already exist.
if err := os.Mkdir(criuOpts.ImagesDirectory, 0o700); err != nil && !os.IsExist(err) { if err := os.Mkdir(criuOpts.ImagesDirectory, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
return err return err
} }
@@ -353,7 +353,7 @@ func (c *Container) Checkpoint(criuOpts *CriuOpts) error {
// if criuOpts.WorkDirectory is not set, criu default is used. // if criuOpts.WorkDirectory is not set, criu default is used.
if criuOpts.WorkDirectory != "" { if criuOpts.WorkDirectory != "" {
if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
return err return err
} }
workDir, err := os.Open(criuOpts.WorkDirectory) workDir, err := os.Open(criuOpts.WorkDirectory)
@@ -718,7 +718,7 @@ func (c *Container) Restore(process *Process, criuOpts *CriuOpts) error {
if criuOpts.WorkDirectory != "" { if criuOpts.WorkDirectory != "" {
// Since a container can be C/R'ed multiple times, // Since a container can be C/R'ed multiple times,
// the work directory may already exist. // the work directory may already exist.
if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
return err return err
} }
workDir, err := os.Open(criuOpts.WorkDirectory) workDir, err := os.Open(criuOpts.WorkDirectory)
@@ -1169,7 +1169,7 @@ func (c *Container) criuNotifications(resp *criurpc.CriuResp, process *Process,
return err return err
} }
if err := os.Remove(filepath.Join(c.stateDir, "checkpoint")); err != nil { if err := os.Remove(filepath.Join(c.stateDir, "checkpoint")); err != nil {
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
logrus.Error(err) logrus.Error(err)
} }
} }
+1 -1
View File
@@ -98,7 +98,7 @@ func GetDevices(path string) ([]*Device, error) {
if errors.Is(err, ErrNotADevice) { if errors.Is(err, ErrNotADevice) {
continue continue
} }
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
continue continue
} }
return nil, err return nil, err
+2 -2
View File
@@ -51,7 +51,7 @@ func Create(root, id string, config *configs.Config) (*Container, error) {
} }
if _, err := os.Stat(stateDir); err == nil { if _, err := os.Stat(stateDir); err == nil {
return nil, ErrExist return nil, ErrExist
} else if !os.IsNotExist(err) { } else if !errors.Is(err, os.ErrNotExist) {
return nil, err return nil, err
} }
@@ -154,7 +154,7 @@ func loadState(root string) (*State, error) {
} }
f, err := os.Open(stateFilePath) f, err := os.Open(stateFilePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil, ErrNotExist return nil, ErrNotExist
} }
return nil, err return nil, err
+2 -2
View File
@@ -314,7 +314,7 @@ func finalizeNamespace(config *initConfig) error {
switch { switch {
case err == nil: case err == nil:
doChdir = false doChdir = false
case os.IsPermission(err): case errors.Is(err, os.ErrPermission):
// If we hit an EPERM, we should attempt again after setting up user. // If we hit an EPERM, we should attempt again after setting up user.
// This will allow us to successfully chdir if the container user has access // This will allow us to successfully chdir if the container user has access
// to the directory, but the user running runc does not. // to the directory, but the user running runc does not.
@@ -480,7 +480,7 @@ func setupUser(config *initConfig) error {
setgroups, err = io.ReadAll(setgroupsFile) setgroups, err = io.ReadAll(setgroupsFile)
_ = setgroupsFile.Close() _ = setgroupsFile.Close()
} }
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return err return err
} }
+4 -3
View File
@@ -3,6 +3,7 @@ package integration
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@@ -1093,7 +1094,7 @@ func TestHook(t *testing.T) {
for _, hook := range []string{"prestart", "createRuntime", "poststart"} { for _, hook := range []string{"prestart", "createRuntime", "poststart"} {
fi, err := os.Stat(filepath.Join(config.Rootfs, hook)) fi, err := os.Stat(filepath.Join(config.Rootfs, hook))
if err == nil || !os.IsNotExist(err) { if err == nil || !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected file '%s to not exists, but it does", fi.Name()) t.Fatalf("expected file '%s to not exists, but it does", fi.Name())
} }
} }
@@ -1637,7 +1638,7 @@ func TestTmpfsCopyUp(t *testing.T) {
} }
func TestCGROUPPrivate(t *testing.T) { func TestCGROUPPrivate(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/cgroup"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/cgroup"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires cgroupns.") t.Skip("Test requires cgroupns.")
} }
if testing.Short() { if testing.Short() {
@@ -1657,7 +1658,7 @@ func TestCGROUPPrivate(t *testing.T) {
} }
func TestCGROUPHost(t *testing.T) { func TestCGROUPHost(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/cgroup"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/cgroup"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires cgroupns.") t.Skip("Test requires cgroupns.")
} }
if testing.Short() { if testing.Short() {
+4 -4
View File
@@ -460,7 +460,7 @@ func (m *Manager) Apply(pid int) (err error) {
} }
} }
if err := os.Mkdir(path, 0o755); err != nil && !os.IsExist(err) { if err := os.Mkdir(path, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
return newLastCmdError(err) return newLastCmdError(err)
} }
@@ -470,7 +470,7 @@ func (m *Manager) Apply(pid int) (err error) {
// Create MON group // Create MON group
if monPath := m.GetMonPath(); monPath != "" { if monPath := m.GetMonPath(); monPath != "" {
if err := os.Mkdir(monPath, 0o755); err != nil && !os.IsExist(err) { if err := os.Mkdir(monPath, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
return newLastCmdError(err) return newLastCmdError(err)
} }
if err := WriteIntelRdtTasks(monPath, pid); err != nil { if err := WriteIntelRdtTasks(monPath, pid); err != nil {
@@ -493,14 +493,14 @@ func (m *Manager) Destroy() error {
if m.config.IntelRdt.ClosID == "" { if m.config.IntelRdt.ClosID == "" {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
if err := os.Remove(m.GetPath()); err != nil && !os.IsNotExist(err) { if err := os.Remove(m.GetPath()); err != nil && !errors.Is(err, os.ErrNotExist) {
return err return err
} }
m.path = "" m.path = ""
} else if monPath := m.GetMonPath(); monPath != "" { } else if monPath := m.GetMonPath(); monPath != "" {
// If ClosID is not specified the possible monintoring group was // If ClosID is not specified the possible monintoring group was
// removed with the CLOS above. // removed with the CLOS above.
if err := os.Remove(monPath); err != nil && !os.IsNotExist(err) { if err := os.Remove(monPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return err return err
} }
} }
+1 -1
View File
@@ -51,7 +51,7 @@ func registerMemoryEvent(cgDir, evName, arg string) (<-chan struct{}, error) {
} }
// When a cgroup is destroyed, an event is sent to eventfd. // When a cgroup is destroyed, an event is sent to eventfd.
// So if the control path is gone, return instead of notifying. // So if the control path is gone, return instead of notifying.
if _, err := os.Lstat(eventControlPath); os.IsNotExist(err) { if _, err := os.Lstat(eventControlPath); errors.Is(err, os.ErrNotExist) {
return return
} }
ch <- struct{}{} ch <- struct{}{}
+1 -1
View File
@@ -1103,7 +1103,7 @@ func getPipeFds(pid int) ([]string, error) {
// Ignore permission errors, for rootless containers and other // Ignore permission errors, for rootless containers and other
// non-dumpable processes. if we can't get the fd for a particular // non-dumpable processes. if we can't get the fd for a particular
// file, there's not much we can do. // file, there's not much we can do.
if os.IsPermission(err) { if errors.Is(err, os.ErrPermission) {
continue continue
} }
return fds, err return fds, err
+4 -4
View File
@@ -364,7 +364,7 @@ func mountCgroupV1(m mountEntry, c *mountConfig) error {
// symlink(2) is very dumb, it will just shove the path into // symlink(2) is very dumb, it will just shove the path into
// the link and doesn't do any checks or relative path // the link and doesn't do any checks or relative path
// conversion. Also, don't error out if the cgroup already exists. // conversion. Also, don't error out if the cgroup already exists.
if err := os.Symlink(mc, filepath.Join(c.root, m.Destination, ss)); err != nil && !os.IsExist(err) { if err := os.Symlink(mc, filepath.Join(c.root, m.Destination, ss)); err != nil && !errors.Is(err, os.ErrExist) {
return err return err
} }
} }
@@ -602,7 +602,7 @@ func mountToRootfs(c *mountConfig, m mountEntry) error {
return err return err
} }
if fi, err := os.Lstat(dest); err != nil { if fi, err := os.Lstat(dest); err != nil {
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
return err return err
} }
} else if !fi.IsDir() { } else if !fi.IsDir() {
@@ -899,7 +899,7 @@ func setupDevSymlinks(rootfs string) error {
src = link[0] src = link[0]
dst = filepath.Join(rootfs, link[1]) dst = filepath.Join(rootfs, link[1])
) )
if err := os.Symlink(src, dst); err != nil && !os.IsExist(err) { if err := os.Symlink(src, dst); err != nil && !errors.Is(err, os.ErrExist) {
return err return err
} }
} }
@@ -1109,7 +1109,7 @@ func setReadonly() error {
func setupPtmx(config *configs.Config) error { func setupPtmx(config *configs.Config) error {
ptmx := filepath.Join(config.Rootfs, "dev/ptmx") ptmx := filepath.Join(config.Rootfs, "dev/ptmx")
if err := os.Remove(ptmx); err != nil && !os.IsNotExist(err) { if err := os.Remove(ptmx); err != nil && !errors.Is(err, os.ErrNotExist) {
return err return err
} }
if err := os.Symlink("pts/ptmx", ptmx); err != nil { if err := os.Symlink("pts/ptmx", ptmx); err != nil {
+3 -2
View File
@@ -1,6 +1,7 @@
package specconv package specconv
import ( import (
"errors"
"os" "os"
"strings" "strings"
"testing" "testing"
@@ -609,7 +610,7 @@ func TestDupNamespaces(t *testing.T) {
} }
func TestUserNamespaceMappingAndPath(t *testing.T) { func TestUserNamespaceMappingAndPath(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/user"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires userns.") t.Skip("Test requires userns.")
} }
@@ -643,7 +644,7 @@ func TestUserNamespaceMappingAndPath(t *testing.T) {
} }
func TestNonZeroEUIDCompatibleSpecconvValidate(t *testing.T) { func TestNonZeroEUIDCompatibleSpecconvValidate(t *testing.T) {
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/ns/user"); errors.Is(err, os.ErrNotExist) {
t.Skip("Test requires userns.") t.Skip("Test requires userns.")
} }
+2 -1
View File
@@ -1,6 +1,7 @@
package libcontainer package libcontainer
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -213,7 +214,7 @@ func (r *restoredState) transition(s containerState) error {
func (r *restoredState) destroy() error { func (r *restoredState) destroy() error {
if _, err := os.Stat(filepath.Join(r.c.stateDir, "checkpoint")); err != nil { if _, err := os.Stat(filepath.Join(r.c.stateDir, "checkpoint")); err != nil {
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
return err return err
} }
} }
+2 -2
View File
@@ -91,7 +91,7 @@ created by an unprivileged user.
if err == nil { if err == nil {
return fmt.Errorf("File %s exists. Remove it first", name) return fmt.Errorf("File %s exists. Remove it first", name)
} }
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
return err return err
} }
return nil return nil
@@ -117,7 +117,7 @@ created by an unprivileged user.
func loadSpec(cPath string) (spec *specs.Spec, err error) { func loadSpec(cPath string) (spec *specs.Spec, err error) {
cf, err := os.Open(cPath) cf, err := os.Open(cPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("JSON specification file %s not found", cPath) return nil, fmt.Errorf("JSON specification file %s not found", cPath)
} }
return nil, err return nil, err