From 5f6e022707b78379772d47a113b92f38ba87fef2 Mon Sep 17 00:00:00 2001 From: Jason Dobies Date: Tue, 14 May 2024 08:52:08 -0400 Subject: [PATCH] set custom scripts to executable, but custom files retain their existing permissions (#431) * set custom scripts to executable (but not files) * Update pkg/combustion/custom.go Co-authored-by: Atanas Dinov --------- Co-authored-by: Atanas Dinov --- RELEASE_NOTES.md | 2 ++ pkg/combustion/custom.go | 21 ++++++++++++++------- pkg/combustion/custom_test.go | 11 ++++++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 187e1677..1240752d 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -12,6 +12,8 @@ ## Bug Fixes +* [#429](https://github.com/suse-edge/edge-image-builder/issues/429) - Automatically set execute bit on scripts + --- # v1.0.1 diff --git a/pkg/combustion/custom.go b/pkg/combustion/custom.go index 4ce53b64..9a3fef6f 100644 --- a/pkg/combustion/custom.go +++ b/pkg/combustion/custom.go @@ -41,17 +41,18 @@ func configureCustomFiles(ctx *image.Context) ([]string, error) { func handleCustomFiles(ctx *image.Context) error { fullFilesDir := generateComponentPath(ctx, filepath.Join(customDir, customFilesDir)) - _, err := copyCustomFiles(fullFilesDir, ctx.CombustionDir) + _, err := copyCustomFiles(fullFilesDir, ctx.CombustionDir, nil) return err } func handleCustomScripts(ctx *image.Context) ([]string, error) { fullScriptsDir := generateComponentPath(ctx, filepath.Join(customDir, customScriptsDir)) - scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir) + executablePerms := fileio.ExecutablePerms + scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir, &executablePerms) return scripts, err } -func copyCustomFiles(fromDir, toDir string) ([]string, error) { +func copyCustomFiles(fromDir, toDir string, filePermissions *os.FileMode) ([]string, error) { if _, err := os.Stat(fromDir); os.IsNotExist(err) { return nil, nil } @@ -72,12 +73,18 @@ func copyCustomFiles(fromDir, toDir string) ([]string, error) { copyMe := filepath.Join(fromDir, entry.Name()) copyTo := filepath.Join(toDir, entry.Name()) - info, err := entry.Info() - if err != nil { - return nil, fmt.Errorf("reading file info: %w", err) + var mode os.FileMode + if filePermissions == nil { + info, infoErr := entry.Info() + if infoErr != nil { + return nil, fmt.Errorf("reading file info: %w", infoErr) + } + mode = info.Mode() + } else { + mode = *filePermissions } - if err = fileio.CopyFile(copyMe, copyTo, info.Mode()); err != nil { + if err = fileio.CopyFile(copyMe, copyTo, mode); err != nil { return nil, fmt.Errorf("copying file to %s: %w", copyTo, err) } diff --git a/pkg/combustion/custom_test.go b/pkg/combustion/custom_test.go index e69b0219..931829ae 100644 --- a/pkg/combustion/custom_test.go +++ b/pkg/combustion/custom_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/suse-edge/edge-image-builder/pkg/fileio" ) func TestConfigureCustomFiles(t *testing.T) { @@ -75,7 +76,11 @@ func TestConfigureCustomFiles(t *testing.T) { stats, err := os.Stat(entryPath) require.NoError(t, err) - assert.Equal(t, file.perms, stats.Mode()) + if files[entry.Name()].isScript { + assert.Equal(t, fileio.ExecutablePerms, stats.Mode()) + } else { + assert.Equal(t, file.perms, stats.Mode()) + } } // - make sure only script entries were added to the combustion scripts list @@ -103,7 +108,7 @@ func TestCopyCustomFiles_MissingFromDir(t *testing.T) { defer teardown() // Test - files, err := copyCustomFiles("missing", ctx.CombustionDir) + files, err := copyCustomFiles("missing", ctx.CombustionDir, nil) // Verify assert.Nil(t, files) @@ -121,7 +126,7 @@ func TestCopyCustomFiles_EmptyFromDir(t *testing.T) { require.NoError(t, err) // Test - scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir) + scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir, nil) // Verify require.Error(t, err)