libpod: use fileutils.(Le|E)xists

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2024-04-10 17:56:44 +02:00
parent 90304dd507
commit 5656ad40b1
11 changed files with 37 additions and 28 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/containers/common/libnetwork/types"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)
@ -86,7 +87,7 @@ func NewBoltState(path string, runtime *Runtime) (State, error) {
// To continue testing in CI, allow creation iff an undocumented env
// var is set.
if os.Getenv("CI_DESIRED_DATABASE") != "boltdb" {
if _, err := os.Stat(path); err != nil && errors.Is(err, fs.ErrNotExist) {
if err := fileutils.Exists(path); err != nil && errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("the BoltDB backend has been deprecated, no new BoltDB databases can be created: %w", define.ErrInvalidArg)
}
} else {

View File

@ -38,6 +38,7 @@ import (
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/chrootarchive"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/idmap"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/lockfile"
@ -204,7 +205,7 @@ func (c *Container) handleExitFile(exitFile string, fi os.FileInfo) error {
if err != nil {
return err
}
if _, err = os.Stat(oomFilePath); err == nil {
if err = fileutils.Exists(oomFilePath); err == nil {
c.state.OOMKilled = true
}
@ -1999,7 +2000,7 @@ func (c *Container) cleanup(ctx context.Context) error {
// cleanup host entry if it is shared
if c.config.NetNsCtr != "" {
if hoststFile, ok := c.state.BindMounts[config.DefaultHostsFile]; ok {
if _, err := os.Stat(hoststFile); err == nil {
if err := fileutils.Exists(hoststFile); err == nil {
// we cannot use the dependency container lock due ABBA deadlocks
if lock, err := lockfile.GetLockFile(hoststFile); err == nil {
lock.Lock()
@ -2220,7 +2221,7 @@ func (c *Container) saveSpec(spec *spec.Spec) error {
// Cannot guarantee some things, e.g. network namespaces, have the same
// paths
jsonPath := filepath.Join(c.bundlePath(), "config.json")
if _, err := os.Stat(jsonPath); err != nil {
if err := fileutils.Exists(jsonPath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("doing stat on container %s spec: %w", c.ID(), err)
}
@ -2363,8 +2364,7 @@ func (c *Container) checkReadyForRemoval() error {
// canWithPrevious return the stat of the preCheckPoint dir
func (c *Container) canWithPrevious() error {
_, err := os.Stat(c.PreCheckPointPath())
return err
return fileutils.Exists(c.PreCheckPointPath())
}
// prepareCheckpointExport writes the config and spec to

View File

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"math"
"net"
"os"
@ -44,6 +45,7 @@ import (
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/podman/v5/version"
"github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/lockfile"
"github.com/containers/storage/pkg/unshare"
@ -724,7 +726,7 @@ func (c *Container) isWorkDirSymlink(resolvedPath string) bool {
}
if resolvedSymlinkWorkdir != "" {
resolvedPath = resolvedSymlinkWorkdir
_, err := os.Stat(resolvedSymlinkWorkdir)
err := fileutils.Exists(resolvedSymlinkWorkdir)
if err == nil {
// Symlink resolved successfully and resolved path exists on container,
// this is a valid use-case so return nil.
@ -1422,7 +1424,7 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
// Let's try to stat() CRIU's inventory file. If it does not exist, it makes
// no sense to try a restore. This is a minimal check if a checkpoint exists.
if _, err := os.Stat(filepath.Join(c.CheckpointPath(), "inventory.img")); os.IsNotExist(err) {
if err := fileutils.Exists(filepath.Join(c.CheckpointPath(), "inventory.img")); errors.Is(err, fs.ErrNotExist) {
return nil, 0, fmt.Errorf("a complete checkpoint for this container cannot be found, cannot restore: %w", err)
}
@ -1632,7 +1634,7 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
// Restore /dev/shm content
if c.config.ShmDir != "" && c.state.BindMounts["/dev/shm"] == c.config.ShmDir {
shmDirTarFileFullPath := filepath.Join(c.bundlePath(), metadata.DevShmCheckpointTar)
if _, err := os.Stat(shmDirTarFileFullPath); err != nil {
if err := fileutils.Exists(shmDirTarFileFullPath); err != nil {
logrus.Debug("Container checkpoint doesn't contain dev/shm: ", err.Error())
} else {
shmDirTarFile, err := os.Open(shmDirTarFileFullPath)
@ -2678,13 +2680,13 @@ func (c *Container) generatePasswdAndGroup() (string, string, error) {
// do anything more.
if needPasswd {
passwdPath := filepath.Join(c.config.StaticDir, "passwd")
if _, err := os.Stat(passwdPath); err == nil {
if err := fileutils.Exists(passwdPath); err == nil {
needPasswd = false
}
}
if needGroup {
groupPath := filepath.Join(c.config.StaticDir, "group")
if _, err := os.Stat(groupPath); err == nil {
if err := fileutils.Exists(groupPath); err == nil {
needGroup = false
}
}
@ -2803,7 +2805,7 @@ func (c *Container) cleanupOverlayMounts() error {
// Creates and mounts an empty dir to mount secrets into, if it does not already exist
func (c *Container) createSecretMountDir(runPath string) error {
src := filepath.Join(c.state.RunDir, "/run/secrets")
_, err := os.Stat(src)
err := fileutils.Exists(src)
if os.IsNotExist(err) {
if err := umask.MkdirAllIgnoreUmask(src, os.FileMode(0o755)); err != nil {
return err

View File

@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
@ -14,6 +15,7 @@ import (
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
@ -428,7 +430,7 @@ func (c *Container) healthCheckLogPath() string {
// The caller should lock the container before this function is called.
func (c *Container) getHealthCheckLog() (define.HealthCheckResults, error) {
var healthCheck define.HealthCheckResults
if _, err := os.Stat(c.healthCheckLogPath()); os.IsNotExist(err) {
if err := fileutils.Exists(c.healthCheckLogPath()); errors.Is(err, fs.ErrNotExist) {
return healthCheck, nil
}
b, err := os.ReadFile(c.healthCheckLogPath())

View File

@ -7,6 +7,7 @@ import (
"strconv"
"syscall"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/lockfile"
"github.com/sirupsen/logrus"
)
@ -20,7 +21,7 @@ type FileLocks struct { //nolint:revive // struct name stutters
// CreateFileLock sets up a directory containing the various lock files.
func CreateFileLock(path string) (*FileLocks, error) {
_, err := os.Stat(path)
err := fileutils.Exists(path)
if err == nil {
return nil, fmt.Errorf("directory %s exists: %w", path, syscall.EEXIST)
}
@ -37,7 +38,7 @@ func CreateFileLock(path string) (*FileLocks, error) {
// OpenFileLock opens an existing directory with the lock files.
func OpenFileLock(path string) (*FileLocks, error) {
_, err := os.Stat(path)
err := fileutils.Exists(path)
if err != nil {
return nil, err
}

View File

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net"
"os"
"strings"
"syscall"
"time"
@ -23,6 +22,7 @@ import (
"github.com/containers/podman/v5/pkg/specgen"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/regexp"
"github.com/opencontainers/runtime-spec/specs-go"
@ -268,7 +268,7 @@ func WithStaticDir(dir string) RuntimeOption {
func WithRegistriesConf(path string) RuntimeOption {
logrus.Debugf("Setting custom registries.conf: %q", path)
return func(rt *Runtime) error {
if _, err := os.Stat(path); err != nil {
if err := fileutils.Exists(path); err != nil {
return fmt.Errorf("locating specified registries.conf: %w", err)
}
if rt.imageContext == nil {
@ -1329,7 +1329,7 @@ func WithRootFS(rootfs string, overlay bool, mapping *string) CtrCreateOption {
if ctr.valid {
return define.ErrCtrFinalized
}
if _, err := os.Stat(rootfs); err != nil {
if err := fileutils.Exists(rootfs); err != nil {
return err
}
ctr.config.Rootfs = rootfs

View File

@ -16,6 +16,7 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/storage/pkg/fileutils"
"github.com/docker/go-plugins-helpers/sdk"
"github.com/docker/go-plugins-helpers/volume"
jsoniter "github.com/json-iterator/go"
@ -188,7 +189,7 @@ func (p *VolumePlugin) getURI() string {
// Verify the plugin is still available.
// Does not actually ping the API, just verifies that the socket still exists.
func (p *VolumePlugin) verifyReachable() error {
if _, err := os.Stat(p.SocketPath); err != nil {
if err := fileutils.Exists(p.SocketPath); err != nil {
if os.IsNotExist(err) {
pluginsLock.Lock()
defer pluginsLock.Unlock()

View File

@ -35,6 +35,7 @@ import (
"github.com/containers/podman/v5/pkg/systemd"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/lockfile"
"github.com/containers/storage/pkg/unshare"
"github.com/docker/docker/pkg/namesgenerator"
@ -139,7 +140,7 @@ func SetXdgDirs() error {
if rootless.IsRootless() && os.Getenv("DBUS_SESSION_BUS_ADDRESS") == "" {
sessionAddr := filepath.Join(runtimeDir, "bus")
if _, err := os.Stat(sessionAddr); err == nil {
if err := fileutils.Exists(sessionAddr); err == nil {
os.Setenv("DBUS_SESSION_BUS_ADDRESS", fmt.Sprintf("unix:path=%s", sessionAddr))
}
}
@ -307,7 +308,7 @@ func getDBState(runtime *Runtime) (State, error) {
switch backend {
case config.DBBackendDefault:
// for backwards compatibility check if boltdb exists, if it does not we use sqlite
if _, err := os.Stat(boltDBPath); err != nil {
if err := fileutils.Exists(boltDBPath); err != nil {
if errors.Is(err, fs.ErrNotExist) {
// need to set DBBackend string so podman info will show the backend name correctly
runtime.config.Engine.DBBackend = config.DBBackendSQLite.String()
@ -543,7 +544,7 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
}
}()
_, err = os.Stat(runtimeAliveFile)
err = fileutils.Exists(runtimeAliveFile)
if err != nil {
// If we need to refresh, then it is safe to assume there are
// no containers running. Create immediately a namespace, as

View File

@ -16,6 +16,7 @@ import (
volplugin "github.com/containers/podman/v5/libpod/plugin"
"github.com/containers/storage"
"github.com/containers/storage/drivers/quota"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/stringid"
pluginapi "github.com/docker/go-plugins-helpers/volume"
@ -84,7 +85,7 @@ func (r *Runtime) newVolume(ctx context.Context, noCreatePluginVolume bool, opti
switch strings.ToLower(key) {
case "device":
if strings.ToLower(volume.config.Options["type"]) == define.TypeBind {
if _, err := os.Stat(val); err != nil {
if err := fileutils.Exists(val); err != nil {
return nil, fmt.Errorf("invalid volume option %s for driver 'local': %w", key, err)
}
}

View File

@ -20,6 +20,7 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/api/handlers/utils/apiutil"
"github.com/containers/storage/pkg/fileutils"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
@ -104,14 +105,14 @@ func DefaultSeccompPath() (string, error) {
return def.Containers.SeccompProfile, nil
}
_, err = os.Stat(config.SeccompOverridePath)
err = fileutils.Exists(config.SeccompOverridePath)
if err == nil {
return config.SeccompOverridePath, nil
}
if !os.IsNotExist(err) {
return "", err
}
if _, err := os.Stat(config.SeccompDefaultPath); err != nil {
if err := fileutils.Exists(config.SeccompDefaultPath); err != nil {
if !os.IsNotExist(err) {
return "", err
}

View File

@ -5,7 +5,6 @@ package libpod
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
@ -13,6 +12,7 @@ import (
"github.com/containers/common/pkg/cgroups"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/storage/pkg/fileutils"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
@ -27,8 +27,7 @@ func cgroupExist(path string) bool {
} else {
fullPath = filepath.Join("/sys/fs/cgroup/memory", path)
}
_, err := os.Stat(fullPath)
return err == nil
return fileutils.Exists(fullPath) == nil
}
// systemdSliceFromPath makes a new systemd slice under the given parent with