From 2f62bc9d4627b19e12d6c7ef400f59f7f5d46b53 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 9 Apr 2024 23:17:31 +0200 Subject: [PATCH] pkg/config: use fileutils.(Le|E)xists Signed-off-by: Giuseppe Scrivano --- common/pkg/config/config.go | 7 ++++--- common/pkg/config/config_local.go | 3 ++- common/pkg/config/default.go | 5 +++-- common/pkg/config/modules.go | 6 +++--- common/pkg/config/new.go | 5 +++-- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/common/pkg/config/config.go b/common/pkg/config/config.go index dde4cf508c..d8e0646e67 100644 --- a/common/pkg/config/config.go +++ b/common/pkg/config/config.go @@ -12,6 +12,7 @@ import ( "github.com/containers/common/internal/attributedstring" "github.com/containers/common/libnetwork/types" "github.com/containers/common/pkg/capabilities" + "github.com/containers/storage/pkg/fileutils" "github.com/containers/storage/pkg/unshare" units "github.com/docker/go-units" selinux "github.com/opencontainers/selinux/go-selinux" @@ -715,7 +716,7 @@ func (c *Config) CheckCgroupsAndAdjustConfig() { if hasSession { for _, part := range strings.Split(session, ",") { if strings.HasPrefix(part, "unix:path=") { - _, err := os.Stat(strings.TrimPrefix(part, "unix:path=")) + err := fileutils.Exists(strings.TrimPrefix(part, "unix:path=")) hasSession = err == nil break } @@ -780,7 +781,7 @@ func (c *EngineConfig) findRuntime() string { // Search for crun first followed by runc, runj, kata, runsc, ocijail for _, name := range []string{"crun", "runc", "runj", "kata", "runsc", "ocijail"} { for _, v := range c.OCIRuntimes[name] { - if _, err := os.Stat(v); err == nil { + if err := fileutils.Exists(v); err == nil { return name } } @@ -1189,7 +1190,7 @@ func (c *Config) FindInitBinary() (string, error) { return c.Engine.InitPath, nil } // keep old default working to guarantee backwards compat - if _, err := os.Stat(DefaultInitPath); err == nil { + if err := fileutils.Exists(DefaultInitPath); err == nil { return DefaultInitPath, nil } return c.FindHelperBinary(defaultInitName, true) diff --git a/common/pkg/config/config_local.go b/common/pkg/config/config_local.go index e9826d62ca..3b8979ce2d 100644 --- a/common/pkg/config/config_local.go +++ b/common/pkg/config/config_local.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" + "github.com/containers/storage/pkg/fileutils" units "github.com/docker/go-units" "tags.cncf.io/container-device-interface/pkg/parser" ) @@ -83,7 +84,7 @@ func (c *ContainersConfig) validateTZ() error { for _, paths := range lookupPaths { zonePath := filepath.Join(paths, c.TZ) - if _, err := os.Stat(zonePath); err == nil { + if err := fileutils.Exists(zonePath); err == nil { // found zone information return nil } diff --git a/common/pkg/config/default.go b/common/pkg/config/default.go index 19c4bb6bfd..5faba8c9fd 100644 --- a/common/pkg/config/default.go +++ b/common/pkg/config/default.go @@ -13,6 +13,7 @@ import ( nettypes "github.com/containers/common/libnetwork/types" "github.com/containers/common/pkg/apparmor" "github.com/containers/common/pkg/cgroupv2" + "github.com/containers/storage/pkg/fileutils" "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/unshare" "github.com/containers/storage/types" @@ -206,8 +207,8 @@ func defaultConfig() (*Config, error) { } sigPath := filepath.Join(configHome, DefaultRootlessSignaturePolicyPath) defaultEngineConfig.SignaturePolicyPath = sigPath - if _, err := os.Stat(sigPath); err != nil { - if _, err := os.Stat(DefaultSignaturePolicyPath); err == nil { + if err := fileutils.Exists(sigPath); err != nil { + if err := fileutils.Exists(DefaultSignaturePolicyPath); err == nil { defaultEngineConfig.SignaturePolicyPath = DefaultSignaturePolicyPath } } diff --git a/common/pkg/config/modules.go b/common/pkg/config/modules.go index f21671f6b1..4f23694b77 100644 --- a/common/pkg/config/modules.go +++ b/common/pkg/config/modules.go @@ -2,9 +2,9 @@ package config import ( "fmt" - "os" "path/filepath" + "github.com/containers/storage/pkg/fileutils" "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/unshare" "github.com/hashicorp/go-multierror" @@ -76,7 +76,7 @@ func ModuleDirectories() ([]string, error) { // Public API for shell completions // Resolve the specified path to a module. func resolveModule(path string, dirs []string) (string, error) { if filepath.IsAbs(path) { - _, err := os.Stat(path) + err := fileutils.Exists(path) return path, err } @@ -85,7 +85,7 @@ func resolveModule(path string, dirs []string) (string, error) { var multiErr error for _, d := range dirs { candidate := filepath.Join(d, path) - _, err := os.Stat(candidate) + err := fileutils.Exists(candidate) if err == nil { return candidate, nil } diff --git a/common/pkg/config/new.go b/common/pkg/config/new.go index fb59473f00..51da47db1f 100644 --- a/common/pkg/config/new.go +++ b/common/pkg/config/new.go @@ -11,6 +11,7 @@ import ( "sync" "github.com/BurntSushi/toml" + "github.com/containers/storage/pkg/fileutils" "github.com/sirupsen/logrus" ) @@ -101,7 +102,7 @@ func newLocked(options *Options) (*Config, error) { // The _OVERRIDE variable _must_ always win. That's a contract we need // to honor (for the Podman CI). if path := os.Getenv(containersConfOverrideEnv); path != "" { - if _, err := os.Stat(path); err != nil { + if err := fileutils.Exists(path); err != nil { return nil, fmt.Errorf("%s file: %w", containersConfOverrideEnv, err) } options.additionalConfigs = append(options.additionalConfigs, path) @@ -152,7 +153,7 @@ func NewConfig(userConfigPath string) (*Config, error) { // file settings. func systemConfigs() (configs []string, finalErr error) { if path := os.Getenv(containersConfEnv); path != "" { - if _, err := os.Stat(path); err != nil { + if err := fileutils.Exists(path); err != nil { return nil, fmt.Errorf("%s file: %w", containersConfEnv, err) } return append(configs, path), nil