From 584e792a5a20617e54420e86b3928aa65781a649 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Thu, 24 Nov 2022 10:13:21 +0100 Subject: [PATCH] Ensure output folder exists for CDI spec Signed-off-by: Evan Lezar --- cmd/nvidia-ctk/cdi/generate/generate.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/nvidia-ctk/cdi/generate/generate.go b/cmd/nvidia-ctk/cdi/generate/generate.go index 17a83d77..ea534245 100644 --- a/cmd/nvidia-ctk/cdi/generate/generate.go +++ b/cmd/nvidia-ctk/cdi/generate/generate.go @@ -117,6 +117,10 @@ func (m command) run(c *cli.Context, cfg *config) error { if cfg.output == "" { outputTo = os.Stdout } else { + err := createParentDirsIfRequired(cfg.output) + if err != nil { + return fmt.Errorf("failed to create parent folders for output file: %v", err) + } outputFile, err := os.Create(cfg.output) if err != nil { return fmt.Errorf("failed to create output file: %v", err) @@ -594,3 +598,14 @@ func (m command) generateDeviceFolderPermissionHooks(nvidiaCTKPath string, devic return hooks, nil } + +// createParentDirsIfRequired creates the parent folders of the specified path if requried. +// Note that MkdirAll does not specifically check whether the specified path is non-empty and raises an error if it is. +// The path will be empty if filename in the current folder is specified, for example +func createParentDirsIfRequired(filename string) error { + dir := filepath.Dir(filename) + if dir == "" { + return nil + } + return os.MkdirAll(dir, 0755) +}