fix(files): check ownedByFilesystem after destination is set

Ownership check ran against empty c.Destination, so
ownedByFilesystem always returned false. Tree entries
with custom owner/group would incorrectly apply them
to filesystem-owned directories like /usr or /usr/local.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2026-04-11 10:36:28 -03:00
parent 1ecd69128d
commit 74c150997b
3 changed files with 49 additions and 4 deletions
+4 -4
View File
@@ -494,10 +494,6 @@ func addTree(
c := &Content{
FileInfo: &ContentFileInfo{},
}
if tree.FileInfo != nil && !ownedByFilesystem(c.Destination) {
c.FileInfo.Owner = tree.FileInfo.Owner
c.FileInfo.Group = tree.FileInfo.Group
}
switch {
case d.IsDir():
@@ -538,6 +534,10 @@ func addTree(
if tree.FileInfo != nil && tree.FileInfo.Mode != 0 && c.Type != TypeSymlink {
c.FileInfo.Mode = tree.FileInfo.Mode
}
if tree.FileInfo != nil && !ownedByFilesystem(c.Destination) {
c.FileInfo.Owner = tree.FileInfo.Owner
c.FileInfo.Group = tree.FileInfo.Group
}
all[c.Destination] = c.WithFileInfoDefaults(umask, mtime)
+44
View File
@@ -941,6 +941,50 @@ func withoutFileInfo(contents files.Contents) files.Contents {
return filtered
}
func TestTreeOwnerFsOwnedDirs(t *testing.T) {
// When a tree maps to a destination that contains filesystem-owned
// directories (like /usr, /usr/local), those dirs must not inherit
// the tree's custom owner/group.
results, err := files.PrepareForPackager(
files.Contents{
{
Source: filepath.Join("testdata", "treefs"),
Destination: "/",
Type: files.TypeTree,
FileInfo: &files.ContentFileInfo{
Owner: "custom",
Group: "custom",
},
},
},
0,
"",
false,
mtime,
)
require.NoError(t, err)
fsOwned := map[string]bool{
"/usr/": true,
"/usr/local/": true,
"/usr/local/bin/": true,
}
for _, f := range results {
if fsOwned[f.Destination] {
require.Equal(t, "root", f.FileInfo.Owner,
"%s should have root owner", f.Destination)
require.Equal(t, "root", f.FileInfo.Group,
"%s should have root group", f.Destination)
} else {
require.Equal(t, "custom", f.FileInfo.Owner,
"%s should have custom owner", f.Destination)
require.Equal(t, "custom", f.FileInfo.Group,
"%s should have custom group", f.Destination)
}
}
}
func TestTreeFileMode(t *testing.T) {
// Verify that regular files in a tree preserve their source permissions
// rather than getting a zero mode from d.Type().
+1
View File
@@ -0,0 +1 @@
test