From 8bdd6a66d440a7a80c4cd95158c656bfddc6bbf1 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 11 Apr 2024 10:57:30 +0200 Subject: [PATCH] internal: use fileutils.(Le|E)xists Signed-off-by: Giuseppe Scrivano --- internal/mkcw/attest.go | 3 ++- internal/parse/parse.go | 4 ++-- internal/source/add.go | 3 ++- internal/source/create.go | 4 ++-- internal/source/pull.go | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/mkcw/attest.go b/internal/mkcw/attest.go index 91362d37e2d..400884a9968 100644 --- a/internal/mkcw/attest.go +++ b/internal/mkcw/attest.go @@ -15,6 +15,7 @@ import ( "strings" "github.com/containers/buildah/internal/mkcw/types" + "github.com/containers/storage/pkg/fileutils" "github.com/sirupsen/logrus" ) @@ -224,7 +225,7 @@ func GenerateMeasurement(workloadConfig WorkloadConfig, firmwareLibrary string) } } for _, candidate := range pathsToCheck { - if _, err := os.Lstat(candidate); err == nil { + if err := fileutils.Lexists(candidate); err == nil { var stdout, stderr bytes.Buffer logrus.Debugf("krunfw_measurement -c %s -m %s %s", cpuString, memoryString, candidate) cmd := exec.Command("krunfw_measurement", "-c", cpuString, "-m", memoryString, candidate) diff --git a/internal/parse/parse.go b/internal/parse/parse.go index 89ff7d399af..8a0fa48539f 100644 --- a/internal/parse/parse.go +++ b/internal/parse/parse.go @@ -2,11 +2,11 @@ package parse import ( "fmt" - "os" "path/filepath" "strings" "github.com/containers/common/pkg/parse" + "github.com/containers/storage/pkg/fileutils" specs "github.com/opencontainers/runtime-spec/specs-go" ) @@ -15,7 +15,7 @@ func ValidateVolumeMountHostDir(hostDir string) error { if !filepath.IsAbs(hostDir) { return fmt.Errorf("invalid host path, must be an absolute path %q", hostDir) } - if _, err := os.Stat(hostDir); err != nil { + if err := fileutils.Exists(hostDir); err != nil { return err } return nil diff --git a/internal/source/add.go b/internal/source/add.go index 64e944554c1..28506e58f1d 100644 --- a/internal/source/add.go +++ b/internal/source/add.go @@ -10,6 +10,7 @@ import ( "github.com/containers/image/v5/types" "github.com/containers/storage/pkg/archive" + "github.com/containers/storage/pkg/fileutils" specV1 "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -44,7 +45,7 @@ func (o *AddOptions) annotations() (map[string]string, error) { // tar ball. Add attempts to auto-tar and auto-compress only if necessary. func Add(ctx context.Context, sourcePath string, artifactPath string, options AddOptions) error { // Let's first make sure `sourcePath` exists and that we can access it. - if _, err := os.Stat(sourcePath); err != nil { + if err := fileutils.Exists(sourcePath); err != nil { return err } diff --git a/internal/source/create.go b/internal/source/create.go index c335cd0e566..55d4b83bf65 100644 --- a/internal/source/create.go +++ b/internal/source/create.go @@ -3,9 +3,9 @@ package source import ( "context" "fmt" - "os" "time" + "github.com/containers/storage/pkg/fileutils" spec "github.com/opencontainers/image-spec/specs-go" specV1 "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -32,7 +32,7 @@ func (o *CreateOptions) createdTime() *time.Time { // Create creates an empty source image at the specified `sourcePath`. Note // that `sourcePath` must not exist. func Create(ctx context.Context, sourcePath string, options CreateOptions) error { - if _, err := os.Stat(sourcePath); err == nil { + if err := fileutils.Exists(sourcePath); err == nil { return fmt.Errorf("creating source image: %q already exists", sourcePath) } diff --git a/internal/source/pull.go b/internal/source/pull.go index f8743ddf8f9..7e6f8ed53cd 100644 --- a/internal/source/pull.go +++ b/internal/source/pull.go @@ -12,6 +12,7 @@ import ( "github.com/containers/image/v5/signature" "github.com/containers/image/v5/transports/alltransports" "github.com/containers/image/v5/types" + "github.com/containers/storage/pkg/fileutils" ) // PullOptions includes data to alter certain knobs when pulling a source @@ -27,7 +28,7 @@ type PullOptions struct { // Pull `imageInput` from a container registry to `sourcePath`. func Pull(ctx context.Context, imageInput string, sourcePath string, options PullOptions) error { - if _, err := os.Stat(sourcePath); err == nil { + if err := fileutils.Exists(sourcePath); err == nil { return fmt.Errorf("%q already exists", sourcePath) }