Skip to content

Commit 5656ad4

Browse files
committed
libpod: use fileutils.(Le|E)xists
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
1 parent 90304dd commit 5656ad4

File tree

11 files changed

+37
-28
lines changed

11 files changed

+37
-28
lines changed

libpod/boltdb_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

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

libpod/container_internal.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/containers/podman/v5/pkg/util"
3939
"github.com/containers/storage"
4040
"github.com/containers/storage/pkg/chrootarchive"
41+
"github.com/containers/storage/pkg/fileutils"
4142
"github.com/containers/storage/pkg/idmap"
4243
"github.com/containers/storage/pkg/idtools"
4344
"github.com/containers/storage/pkg/lockfile"
@@ -204,7 +205,7 @@ func (c *Container) handleExitFile(exitFile string, fi os.FileInfo) error {
204205
if err != nil {
205206
return err
206207
}
207-
if _, err = os.Stat(oomFilePath); err == nil {
208+
if err = fileutils.Exists(oomFilePath); err == nil {
208209
c.state.OOMKilled = true
209210
}
210211

@@ -1999,7 +2000,7 @@ func (c *Container) cleanup(ctx context.Context) error {
19992000
// cleanup host entry if it is shared
20002001
if c.config.NetNsCtr != "" {
20012002
if hoststFile, ok := c.state.BindMounts[config.DefaultHostsFile]; ok {
2002-
if _, err := os.Stat(hoststFile); err == nil {
2003+
if err := fileutils.Exists(hoststFile); err == nil {
20032004
// we cannot use the dependency container lock due ABBA deadlocks
20042005
if lock, err := lockfile.GetLockFile(hoststFile); err == nil {
20052006
lock.Lock()
@@ -2220,7 +2221,7 @@ func (c *Container) saveSpec(spec *spec.Spec) error {
22202221
// Cannot guarantee some things, e.g. network namespaces, have the same
22212222
// paths
22222223
jsonPath := filepath.Join(c.bundlePath(), "config.json")
2223-
if _, err := os.Stat(jsonPath); err != nil {
2224+
if err := fileutils.Exists(jsonPath); err != nil {
22242225
if !os.IsNotExist(err) {
22252226
return fmt.Errorf("doing stat on container %s spec: %w", c.ID(), err)
22262227
}
@@ -2363,8 +2364,7 @@ func (c *Container) checkReadyForRemoval() error {
23632364

23642365
// canWithPrevious return the stat of the preCheckPoint dir
23652366
func (c *Container) canWithPrevious() error {
2366-
_, err := os.Stat(c.PreCheckPointPath())
2367-
return err
2367+
return fileutils.Exists(c.PreCheckPointPath())
23682368
}
23692369

23702370
// prepareCheckpointExport writes the config and spec to

libpod/container_internal_common.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"io/fs"
1011
"math"
1112
"net"
1213
"os"
@@ -44,6 +45,7 @@ import (
4445
"github.com/containers/podman/v5/pkg/util"
4546
"github.com/containers/podman/v5/version"
4647
"github.com/containers/storage/pkg/archive"
48+
"github.com/containers/storage/pkg/fileutils"
4749
"github.com/containers/storage/pkg/idtools"
4850
"github.com/containers/storage/pkg/lockfile"
4951
"github.com/containers/storage/pkg/unshare"
@@ -724,7 +726,7 @@ func (c *Container) isWorkDirSymlink(resolvedPath string) bool {
724726
}
725727
if resolvedSymlinkWorkdir != "" {
726728
resolvedPath = resolvedSymlinkWorkdir
727-
_, err := os.Stat(resolvedSymlinkWorkdir)
729+
err := fileutils.Exists(resolvedSymlinkWorkdir)
728730
if err == nil {
729731
// Symlink resolved successfully and resolved path exists on container,
730732
// this is a valid use-case so return nil.
@@ -1422,7 +1424,7 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
14221424

14231425
// Let's try to stat() CRIU's inventory file. If it does not exist, it makes
14241426
// no sense to try a restore. This is a minimal check if a checkpoint exists.
1425-
if _, err := os.Stat(filepath.Join(c.CheckpointPath(), "inventory.img")); os.IsNotExist(err) {
1427+
if err := fileutils.Exists(filepath.Join(c.CheckpointPath(), "inventory.img")); errors.Is(err, fs.ErrNotExist) {
14261428
return nil, 0, fmt.Errorf("a complete checkpoint for this container cannot be found, cannot restore: %w", err)
14271429
}
14281430

@@ -1632,7 +1634,7 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
16321634
// Restore /dev/shm content
16331635
if c.config.ShmDir != "" && c.state.BindMounts["/dev/shm"] == c.config.ShmDir {
16341636
shmDirTarFileFullPath := filepath.Join(c.bundlePath(), metadata.DevShmCheckpointTar)
1635-
if _, err := os.Stat(shmDirTarFileFullPath); err != nil {
1637+
if err := fileutils.Exists(shmDirTarFileFullPath); err != nil {
16361638
logrus.Debug("Container checkpoint doesn't contain dev/shm: ", err.Error())
16371639
} else {
16381640
shmDirTarFile, err := os.Open(shmDirTarFileFullPath)
@@ -2678,13 +2680,13 @@ func (c *Container) generatePasswdAndGroup() (string, string, error) {
26782680
// do anything more.
26792681
if needPasswd {
26802682
passwdPath := filepath.Join(c.config.StaticDir, "passwd")
2681-
if _, err := os.Stat(passwdPath); err == nil {
2683+
if err := fileutils.Exists(passwdPath); err == nil {
26822684
needPasswd = false
26832685
}
26842686
}
26852687
if needGroup {
26862688
groupPath := filepath.Join(c.config.StaticDir, "group")
2687-
if _, err := os.Stat(groupPath); err == nil {
2689+
if err := fileutils.Exists(groupPath); err == nil {
26882690
needGroup = false
26892691
}
26902692
}
@@ -2803,7 +2805,7 @@ func (c *Container) cleanupOverlayMounts() error {
28032805
// Creates and mounts an empty dir to mount secrets into, if it does not already exist
28042806
func (c *Container) createSecretMountDir(runPath string) error {
28052807
src := filepath.Join(c.state.RunDir, "/run/secrets")
2806-
_, err := os.Stat(src)
2808+
err := fileutils.Exists(src)
28072809
if os.IsNotExist(err) {
28082810
if err := umask.MkdirAllIgnoreUmask(src, os.FileMode(0o755)); err != nil {
28092811
return err

libpod/healthcheck.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"io/fs"
1011
"os"
1112
"path/filepath"
1213
"strings"
1314
"time"
1415

1516
"github.com/containers/podman/v5/libpod/define"
1617
"github.com/containers/podman/v5/libpod/events"
18+
"github.com/containers/storage/pkg/fileutils"
1719
"github.com/sirupsen/logrus"
1820
"golang.org/x/sys/unix"
1921
)
@@ -428,7 +430,7 @@ func (c *Container) healthCheckLogPath() string {
428430
// The caller should lock the container before this function is called.
429431
func (c *Container) getHealthCheckLog() (define.HealthCheckResults, error) {
430432
var healthCheck define.HealthCheckResults
431-
if _, err := os.Stat(c.healthCheckLogPath()); os.IsNotExist(err) {
433+
if err := fileutils.Exists(c.healthCheckLogPath()); errors.Is(err, fs.ErrNotExist) {
432434
return healthCheck, nil
433435
}
434436
b, err := os.ReadFile(c.healthCheckLogPath())

libpod/lock/file/file_lock.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"syscall"
99

10+
"github.com/containers/storage/pkg/fileutils"
1011
"github.com/containers/storage/pkg/lockfile"
1112
"github.com/sirupsen/logrus"
1213
)
@@ -20,7 +21,7 @@ type FileLocks struct { //nolint:revive // struct name stutters
2021

2122
// CreateFileLock sets up a directory containing the various lock files.
2223
func CreateFileLock(path string) (*FileLocks, error) {
23-
_, err := os.Stat(path)
24+
err := fileutils.Exists(path)
2425
if err == nil {
2526
return nil, fmt.Errorf("directory %s exists: %w", path, syscall.EEXIST)
2627
}
@@ -37,7 +38,7 @@ func CreateFileLock(path string) (*FileLocks, error) {
3738

3839
// OpenFileLock opens an existing directory with the lock files.
3940
func OpenFileLock(path string) (*FileLocks, error) {
40-
_, err := os.Stat(path)
41+
err := fileutils.Exists(path)
4142
if err != nil {
4243
return nil, err
4344
}

libpod/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"net"
9-
"os"
109
"strings"
1110
"syscall"
1211
"time"
@@ -23,6 +22,7 @@ import (
2322
"github.com/containers/podman/v5/pkg/specgen"
2423
"github.com/containers/podman/v5/pkg/util"
2524
"github.com/containers/storage"
25+
"github.com/containers/storage/pkg/fileutils"
2626
"github.com/containers/storage/pkg/idtools"
2727
"github.com/containers/storage/pkg/regexp"
2828
"github.com/opencontainers/runtime-spec/specs-go"
@@ -268,7 +268,7 @@ func WithStaticDir(dir string) RuntimeOption {
268268
func WithRegistriesConf(path string) RuntimeOption {
269269
logrus.Debugf("Setting custom registries.conf: %q", path)
270270
return func(rt *Runtime) error {
271-
if _, err := os.Stat(path); err != nil {
271+
if err := fileutils.Exists(path); err != nil {
272272
return fmt.Errorf("locating specified registries.conf: %w", err)
273273
}
274274
if rt.imageContext == nil {
@@ -1329,7 +1329,7 @@ func WithRootFS(rootfs string, overlay bool, mapping *string) CtrCreateOption {
13291329
if ctr.valid {
13301330
return define.ErrCtrFinalized
13311331
}
1332-
if _, err := os.Stat(rootfs); err != nil {
1332+
if err := fileutils.Exists(rootfs); err != nil {
13331333
return err
13341334
}
13351335
ctr.config.Rootfs = rootfs

libpod/plugin/volume_api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

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

libpod/runtime.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containers/podman/v5/pkg/systemd"
3636
"github.com/containers/podman/v5/pkg/util"
3737
"github.com/containers/storage"
38+
"github.com/containers/storage/pkg/fileutils"
3839
"github.com/containers/storage/pkg/lockfile"
3940
"github.com/containers/storage/pkg/unshare"
4041
"github.com/docker/docker/pkg/namesgenerator"
@@ -139,7 +140,7 @@ func SetXdgDirs() error {
139140

140141
if rootless.IsRootless() && os.Getenv("DBUS_SESSION_BUS_ADDRESS") == "" {
141142
sessionAddr := filepath.Join(runtimeDir, "bus")
142-
if _, err := os.Stat(sessionAddr); err == nil {
143+
if err := fileutils.Exists(sessionAddr); err == nil {
143144
os.Setenv("DBUS_SESSION_BUS_ADDRESS", fmt.Sprintf("unix:path=%s", sessionAddr))
144145
}
145146
}
@@ -307,7 +308,7 @@ func getDBState(runtime *Runtime) (State, error) {
307308
switch backend {
308309
case config.DBBackendDefault:
309310
// for backwards compatibility check if boltdb exists, if it does not we use sqlite
310-
if _, err := os.Stat(boltDBPath); err != nil {
311+
if err := fileutils.Exists(boltDBPath); err != nil {
311312
if errors.Is(err, fs.ErrNotExist) {
312313
// need to set DBBackend string so podman info will show the backend name correctly
313314
runtime.config.Engine.DBBackend = config.DBBackendSQLite.String()
@@ -543,7 +544,7 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
543544
}
544545
}()
545546

546-
_, err = os.Stat(runtimeAliveFile)
547+
err = fileutils.Exists(runtimeAliveFile)
547548
if err != nil {
548549
// If we need to refresh, then it is safe to assume there are
549550
// no containers running. Create immediately a namespace, as

libpod/runtime_volume_common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
volplugin "github.com/containers/podman/v5/libpod/plugin"
1717
"github.com/containers/storage"
1818
"github.com/containers/storage/drivers/quota"
19+
"github.com/containers/storage/pkg/fileutils"
1920
"github.com/containers/storage/pkg/idtools"
2021
"github.com/containers/storage/pkg/stringid"
2122
pluginapi "github.com/docker/go-plugins-helpers/volume"
@@ -84,7 +85,7 @@ func (r *Runtime) newVolume(ctx context.Context, noCreatePluginVolume bool, opti
8485
switch strings.ToLower(key) {
8586
case "device":
8687
if strings.ToLower(volume.config.Options["type"]) == define.TypeBind {
87-
if _, err := os.Stat(val); err != nil {
88+
if err := fileutils.Exists(val); err != nil {
8889
return nil, fmt.Errorf("invalid volume option %s for driver 'local': %w", key, err)
8990
}
9091
}

libpod/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/containers/common/pkg/config"
2121
"github.com/containers/podman/v5/libpod/define"
2222
"github.com/containers/podman/v5/pkg/api/handlers/utils/apiutil"
23+
"github.com/containers/storage/pkg/fileutils"
2324
spec "github.com/opencontainers/runtime-spec/specs-go"
2425
"github.com/opencontainers/selinux/go-selinux/label"
2526
"github.com/sirupsen/logrus"
@@ -104,14 +105,14 @@ func DefaultSeccompPath() (string, error) {
104105
return def.Containers.SeccompProfile, nil
105106
}
106107

107-
_, err = os.Stat(config.SeccompOverridePath)
108+
err = fileutils.Exists(config.SeccompOverridePath)
108109
if err == nil {
109110
return config.SeccompOverridePath, nil
110111
}
111112
if !os.IsNotExist(err) {
112113
return "", err
113114
}
114-
if _, err := os.Stat(config.SeccompDefaultPath); err != nil {
115+
if err := fileutils.Exists(config.SeccompDefaultPath); err != nil {
115116
if !os.IsNotExist(err) {
116117
return "", err
117118
}

libpod/util_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ package libpod
55
import (
66
"errors"
77
"fmt"
8-
"os"
98
"path/filepath"
109
"strings"
1110
"syscall"
1211

1312
"github.com/containers/common/pkg/cgroups"
1413
"github.com/containers/podman/v5/libpod/define"
1514
"github.com/containers/podman/v5/pkg/rootless"
15+
"github.com/containers/storage/pkg/fileutils"
1616
spec "github.com/opencontainers/runtime-spec/specs-go"
1717
"github.com/opencontainers/selinux/go-selinux/label"
1818
"github.com/sirupsen/logrus"
@@ -27,8 +27,7 @@ func cgroupExist(path string) bool {
2727
} else {
2828
fullPath = filepath.Join("/sys/fs/cgroup/memory", path)
2929
}
30-
_, err := os.Stat(fullPath)
31-
return err == nil
30+
return fileutils.Exists(fullPath) == nil
3231
}
3332

3433
// systemdSliceFromPath makes a new systemd slice under the given parent with

0 commit comments

Comments
 (0)