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

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2024-04-10 18:34:23 +02:00
parent aab06ac445
commit 1991990d5a
10 changed files with 25 additions and 22 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage/pkg/fileutils"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
@ -133,7 +134,7 @@ func addDevice(g *generate.Generator, device string) error {
return fmt.Errorf("%s is not a valid device: %w", src, err)
}
if rootless.IsRootless() {
if _, err := os.Stat(src); err != nil {
if err := fileutils.Exists(src); err != nil {
return err
}
perm := "ro"

View File

@ -5,12 +5,14 @@ package kube
import (
"errors"
"fmt"
"io/fs"
"os"
"github.com/containers/common/pkg/parse"
"github.com/containers/common/pkg/secrets"
"github.com/containers/podman/v5/libpod"
v1 "github.com/containers/podman/v5/pkg/k8s.io/api/core/v1"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
@ -69,7 +71,7 @@ func VolumeFromHostPath(hostPath *v1.HostPathVolumeSource, mountLabel string) (*
return nil, fmt.Errorf("giving %s a label: %w", hostPath.Path, err)
}
case v1.HostPathFileOrCreate:
if _, err := os.Stat(hostPath.Path); os.IsNotExist(err) {
if err := fileutils.Exists(hostPath.Path); errors.Is(err, fs.ErrNotExist) {
f, err := os.OpenFile(hostPath.Path, os.O_RDONLY|os.O_CREATE, kubeFilePermission)
if err != nil {
return nil, fmt.Errorf("creating HostPath: %w", err)

View File

@ -9,6 +9,7 @@ import (
"github.com/containers/podman/v5/libpod"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/specgen"
"github.com/containers/storage/pkg/fileutils"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sirupsen/logrus"
@ -18,7 +19,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
// PID
switch s.PidNS.NSMode {
case specgen.Path:
if _, err := os.Stat(s.PidNS.Value); err != nil {
if err := fileutils.Exists(s.PidNS.Value); err != nil {
return fmt.Errorf("cannot find specified PID namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.PIDNamespace), s.PidNS.Value); err != nil {
@ -37,7 +38,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
// IPC
switch s.IpcNS.NSMode {
case specgen.Path:
if _, err := os.Stat(s.IpcNS.Value); err != nil {
if err := fileutils.Exists(s.IpcNS.Value); err != nil {
return fmt.Errorf("cannot find specified IPC namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.IPCNamespace), s.IpcNS.Value); err != nil {
@ -56,7 +57,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
// UTS
switch s.UtsNS.NSMode {
case specgen.Path:
if _, err := os.Stat(s.UtsNS.Value); err != nil {
if err := fileutils.Exists(s.UtsNS.Value); err != nil {
return fmt.Errorf("cannot find specified UTS namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.UTSNamespace), s.UtsNS.Value); err != nil {
@ -114,7 +115,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
// Cgroup
switch s.CgroupNS.NSMode {
case specgen.Path:
if _, err := os.Stat(s.CgroupNS.Value); err != nil {
if err := fileutils.Exists(s.CgroupNS.Value); err != nil {
return fmt.Errorf("cannot find specified cgroup namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), s.CgroupNS.Value); err != nil {
@ -133,7 +134,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
// Net
switch s.NetNS.NSMode {
case specgen.Path:
if _, err := os.Stat(s.NetNS.Value); err != nil {
if err := fileutils.Exists(s.NetNS.Value); err != nil {
return fmt.Errorf("cannot find specified network namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), s.NetNS.Value); err != nil {

View File

@ -6,7 +6,7 @@ import (
"context"
"errors"
"fmt"
"os"
"io/fs"
"path"
"path/filepath"
"strings"
@ -18,6 +18,7 @@ import (
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/specgen"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage/pkg/fileutils"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
@ -391,7 +392,7 @@ func addContainerInitBinary(s *specgen.SpecGenerator, path string) (spec.Mount,
if s.Systemd == "always" {
return mount, errors.New("cannot use container-init binary with systemd=always")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := fileutils.Exists(path); errors.Is(err, fs.ErrNotExist) {
return mount, fmt.Errorf("container-init binary not found on the host: %w", err)
}
return mount, nil

View File

@ -13,6 +13,7 @@ import (
"github.com/containers/common/pkg/sysinfo"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/podman/v5/pkg/specgen"
"github.com/containers/storage/pkg/fileutils"
"github.com/opencontainers/runtime-spec/specs-go"
)
@ -191,8 +192,8 @@ func verifyContainerResourcesCgroupV2(s *specgen.SpecGenerator) ([]string, error
memoryMax := filepath.Join("/sys/fs/cgroup", own, "memory.max")
memorySwapMax := filepath.Join("/sys/fs/cgroup", own, "memory.swap.max")
_, errMemoryMax := os.Stat(memoryMax)
_, errMemorySwapMax := os.Stat(memorySwapMax)
errMemoryMax := fileutils.Exists(memoryMax)
errMemorySwapMax := fileutils.Exists(memorySwapMax)
// Differently than cgroup v1, the memory.*max files are not present in the
// root directory, so we cannot query directly that, so as best effort use
// the current cgroup.

View File

@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net"
"os"
"strings"
"github.com/containers/common/libnetwork/types"
@ -13,6 +12,7 @@ import (
"github.com/containers/podman/v5/pkg/namespaces"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage/pkg/fileutils"
storageTypes "github.com/containers/storage/types"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
@ -483,7 +483,7 @@ func SetupUserNS(idmappings *storageTypes.IDMappingOptions, userns Namespace, g
var user string
switch userns.NSMode {
case Path:
if _, err := os.Stat(userns.Value); err != nil {
if err := fileutils.Exists(userns.Value); err != nil {
return user, fmt.Errorf("cannot find specified user namespace path: %w", err)
}
if err := g.AddOrReplaceLinuxNamespace(string(spec.UserNamespace), userns.Value); err != nil {

View File

@ -1,9 +1,8 @@
package specgen
import (
"os"
"github.com/containers/common/pkg/machine"
"github.com/containers/storage/pkg/fileutils"
)
func shouldResolveWinPaths() bool {
@ -11,8 +10,7 @@ func shouldResolveWinPaths() bool {
}
func shouldResolveUnixWinVariant(path string) bool {
_, err := os.Stat(path)
return err != nil
return fileutils.Exists(path) != nil
}
func resolveRelativeOnWindows(path string) string {

View File

@ -1,9 +1,9 @@
package specgen
import (
"os"
"path/filepath"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -26,6 +26,5 @@ func resolveRelativeOnWindows(path string) string {
}
func winPathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
return fileutils.Exists(path) == nil
}

View File

@ -981,7 +981,7 @@ EXPOSE 2004-2005/tcp`, ALPINE)
session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxy", ALPINE, "wget", "www.redhat.com"})
session.Wait(90)
Expect(session).To(ExitWithError())
Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory"))
Expect(session.ErrorToString()).To(ContainSubstring("faccessat /run/netns/xxy: no such file or directory"))
})
It("podman run in custom CNI network with --static-ip", func() {

View File

@ -419,7 +419,7 @@ EOF
myvolume=myvol$(random_string)
run_podman 125 volume create -o type=bind -o device=/bogus $myvolume
is "$output" "Error: invalid volume option device for driver 'local': stat /bogus: no such file or directory" "should fail with bogus directory not existing"
is "$output" "Error: invalid volume option device for driver 'local': faccessat /bogus: no such file or directory" "should fail with bogus directory not existing"
run_podman volume create -o type=bind -o device=/$myvoldir $myvolume
is "$output" "$myvolume" "should successfully create myvolume"