From f8ccc9df94eb9a4c91c3a1c78d4759a65f156731 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 31 Jan 2024 14:51:47 -0300 Subject: [PATCH] fix: file mode when type: tree (#779) closes #777 Signed-off-by: Carlos Alexandro Becker --- files/files.go | 4 ++++ files/files_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/files/files.go b/files/files.go index c8f462a2..776949d6 100644 --- a/files/files.go +++ b/files/files.go @@ -506,6 +506,10 @@ func addTree( c.FileInfo.Mode = d.Type() &^ umask } + if tree.FileInfo != nil && tree.FileInfo.Mode != 0 && c.Type != TypeSymlink { + c.FileInfo.Mode = tree.FileInfo.Mode + } + all[c.Destination] = c.WithFileInfoDefaults(umask, mtime) return nil diff --git a/files/files_test.go b/files/files_test.go index d6c45dce..678307c3 100644 --- a/files/files_test.go +++ b/files/files_test.go @@ -2,6 +2,7 @@ package files_test import ( "fmt" + "io/fs" "os" "path/filepath" "runtime" @@ -896,6 +897,34 @@ func TestTree(t *testing.T) { }, withoutFileInfo(results)) } +func TestTreeMode(t *testing.T) { + results, err := files.PrepareForPackager( + files.Contents{ + { + Source: filepath.Join("testdata", "tree"), + Destination: "/usr/share/foo", + Type: files.TypeTree, + FileInfo: &files.ContentFileInfo{ + Mode: 0o777, + }, + }, + }, + 0, + "", + false, + mtime, + ) + require.NoError(t, err) + + for _, f := range results { + if f.Destination == "/usr/" || f.Destination == "/usr/share/" || f.Type == files.TypeSymlink { + require.NotEqual(t, fs.FileMode(0o777).String(), f.FileInfo.Mode.String(), f.Destination) + continue + } + require.Equal(t, fs.FileMode(0o777).String(), f.FileInfo.Mode.String(), "%s: %s", f.Destination, f.Type) + } +} + func withoutFileInfo(contents files.Contents) files.Contents { filtered := make(files.Contents, 0, len(contents))