From cd33bf119b2714d31a5472226c8d688bb305b9af Mon Sep 17 00:00:00 2001 From: Alexander Jung Date: Tue, 19 Nov 2024 15:31:10 +0100 Subject: [PATCH] fix(initrd): Use `stat` as a more robust mechanic for checking a file This would also pass for other types, especially directories, which is problematic, as this is also an initrd-provider. Signed-off-by: Alexander Jung --- initrd/file.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/initrd/file.go b/initrd/file.go index e982fb0fc..3c5f79dc4 100644 --- a/initrd/file.go +++ b/initrd/file.go @@ -6,6 +6,7 @@ package initrd import ( "context" + "fmt" "os" ) @@ -17,11 +18,13 @@ type file struct { // NewFromFile accepts an input file which already represents a CPIO archive and // is provided as a mechanism for satisfying the Initrd interface. func NewFromFile(_ context.Context, path string, opts ...InitrdOption) (Initrd, error) { - fi, err := os.Open(path) + stat, err := os.Stat(path) if err != nil { return nil, err } - defer fi.Close() + if stat.IsDir() { + return nil, fmt.Errorf("path %s is a directory, not a file", path) + } initrd := file{ opts: InitrdOptions{},