Skip to content

Commit

Permalink
set custom scripts to executable, but custom files retain their exist…
Browse files Browse the repository at this point in the history
…ing permissions (#431)

* set custom scripts to executable (but not files)

* Update pkg/combustion/custom.go

Co-authored-by: Atanas Dinov <[email protected]>

---------

Co-authored-by: Atanas Dinov <[email protected]>
  • Loading branch information
jdob and atanasdinov authored May 14, 2024
1 parent e8abdaf commit 5f6e022
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions pkg/combustion/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/combustion/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5f6e022

Please sign in to comment.