pkg/config: use fileutils.(Le|E)xists

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2024-04-09 23:17:31 +02:00
parent 7bf4b98cd3
commit 2f62bc9d46
5 changed files with 15 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/containers/common/internal/attributedstring" "github.com/containers/common/internal/attributedstring"
"github.com/containers/common/libnetwork/types" "github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/capabilities" "github.com/containers/common/pkg/capabilities"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/unshare" "github.com/containers/storage/pkg/unshare"
units "github.com/docker/go-units" units "github.com/docker/go-units"
selinux "github.com/opencontainers/selinux/go-selinux" selinux "github.com/opencontainers/selinux/go-selinux"
@ -715,7 +716,7 @@ func (c *Config) CheckCgroupsAndAdjustConfig() {
if hasSession { if hasSession {
for _, part := range strings.Split(session, ",") { for _, part := range strings.Split(session, ",") {
if strings.HasPrefix(part, "unix:path=") { 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 hasSession = err == nil
break break
} }
@ -780,7 +781,7 @@ func (c *EngineConfig) findRuntime() string {
// Search for crun first followed by runc, runj, kata, runsc, ocijail // Search for crun first followed by runc, runj, kata, runsc, ocijail
for _, name := range []string{"crun", "runc", "runj", "kata", "runsc", "ocijail"} { for _, name := range []string{"crun", "runc", "runj", "kata", "runsc", "ocijail"} {
for _, v := range c.OCIRuntimes[name] { for _, v := range c.OCIRuntimes[name] {
if _, err := os.Stat(v); err == nil { if err := fileutils.Exists(v); err == nil {
return name return name
} }
} }
@ -1189,7 +1190,7 @@ func (c *Config) FindInitBinary() (string, error) {
return c.Engine.InitPath, nil return c.Engine.InitPath, nil
} }
// keep old default working to guarantee backwards compat // 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 DefaultInitPath, nil
} }
return c.FindHelperBinary(defaultInitName, true) return c.FindHelperBinary(defaultInitName, true)

View File

@ -9,6 +9,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/containers/storage/pkg/fileutils"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"tags.cncf.io/container-device-interface/pkg/parser" "tags.cncf.io/container-device-interface/pkg/parser"
) )
@ -83,7 +84,7 @@ func (c *ContainersConfig) validateTZ() error {
for _, paths := range lookupPaths { for _, paths := range lookupPaths {
zonePath := filepath.Join(paths, c.TZ) zonePath := filepath.Join(paths, c.TZ)
if _, err := os.Stat(zonePath); err == nil { if err := fileutils.Exists(zonePath); err == nil {
// found zone information // found zone information
return nil return nil
} }

View File

@ -13,6 +13,7 @@ import (
nettypes "github.com/containers/common/libnetwork/types" nettypes "github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/apparmor" "github.com/containers/common/pkg/apparmor"
"github.com/containers/common/pkg/cgroupv2" "github.com/containers/common/pkg/cgroupv2"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/unshare" "github.com/containers/storage/pkg/unshare"
"github.com/containers/storage/types" "github.com/containers/storage/types"
@ -206,8 +207,8 @@ func defaultConfig() (*Config, error) {
} }
sigPath := filepath.Join(configHome, DefaultRootlessSignaturePolicyPath) sigPath := filepath.Join(configHome, DefaultRootlessSignaturePolicyPath)
defaultEngineConfig.SignaturePolicyPath = sigPath defaultEngineConfig.SignaturePolicyPath = sigPath
if _, err := os.Stat(sigPath); err != nil { if err := fileutils.Exists(sigPath); err != nil {
if _, err := os.Stat(DefaultSignaturePolicyPath); err == nil { if err := fileutils.Exists(DefaultSignaturePolicyPath); err == nil {
defaultEngineConfig.SignaturePolicyPath = DefaultSignaturePolicyPath defaultEngineConfig.SignaturePolicyPath = DefaultSignaturePolicyPath
} }
} }

View File

@ -2,9 +2,9 @@ package config
import ( import (
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/unshare" "github.com/containers/storage/pkg/unshare"
"github.com/hashicorp/go-multierror" "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. // Resolve the specified path to a module.
func resolveModule(path string, dirs []string) (string, error) { func resolveModule(path string, dirs []string) (string, error) {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
_, err := os.Stat(path) err := fileutils.Exists(path)
return path, err return path, err
} }
@ -85,7 +85,7 @@ func resolveModule(path string, dirs []string) (string, error) {
var multiErr error var multiErr error
for _, d := range dirs { for _, d := range dirs {
candidate := filepath.Join(d, path) candidate := filepath.Join(d, path)
_, err := os.Stat(candidate) err := fileutils.Exists(candidate)
if err == nil { if err == nil {
return candidate, nil return candidate, nil
} }

View File

@ -11,6 +11,7 @@ import (
"sync" "sync"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus" "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 // The _OVERRIDE variable _must_ always win. That's a contract we need
// to honor (for the Podman CI). // to honor (for the Podman CI).
if path := os.Getenv(containersConfOverrideEnv); path != "" { 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) return nil, fmt.Errorf("%s file: %w", containersConfOverrideEnv, err)
} }
options.additionalConfigs = append(options.additionalConfigs, path) options.additionalConfigs = append(options.additionalConfigs, path)
@ -152,7 +153,7 @@ func NewConfig(userConfigPath string) (*Config, error) {
// file settings. // file settings.
func systemConfigs() (configs []string, finalErr error) { func systemConfigs() (configs []string, finalErr error) {
if path := os.Getenv(containersConfEnv); path != "" { 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 nil, fmt.Errorf("%s file: %w", containersConfEnv, err)
} }
return append(configs, path), nil return append(configs, path), nil