Skip to content

Commit 409e8c9

Browse files
committed
fix: dir ownership when type: dir
closes #954
1 parent ebcd6d2 commit 409e8c9

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

files/files.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func PrepareForPackager(
260260
return nil, contentCollisionError(content, presentContent)
261261
}
262262

263-
err := addParents(contentMap, content.Destination, mtime)
263+
err := addParents(contentMap, content.Destination, mtime, nil)
264264
if err != nil {
265265
return nil, err
266266
}
@@ -279,7 +279,7 @@ func PrepareForPackager(
279279
return nil, contentCollisionError(content, presentContent)
280280
}
281281

282-
err := addParents(contentMap, content.Destination, mtime)
282+
err := addParents(contentMap, content.Destination, mtime, nil)
283283
if err != nil {
284284
return nil, err
285285
}
@@ -345,7 +345,7 @@ func isRelevantForPackager(packager string, content *Content) bool {
345345
return true
346346
}
347347

348-
func addParents(contentMap map[string]*Content, path string, mtime time.Time) error {
348+
func addParents(contentMap map[string]*Content, path string, mtime time.Time, fileInfo *ContentFileInfo) error {
349349
for _, parent := range sortedParents(path) {
350350
parent = NormalizeAbsoluteDirPath(parent)
351351
// check for content collision and just overwrite previously created
@@ -364,12 +364,25 @@ func addParents(contentMap map[string]*Content, path string, mtime time.Time) er
364364
}, c)
365365
}
366366

367+
owner := "root"
368+
group := "root"
369+
370+
// Use provided ownership for directories that are not owned by the filesystem
371+
if fileInfo != nil && !ownedByFilesystem(parent) {
372+
if fileInfo.Owner != "" {
373+
owner = fileInfo.Owner
374+
}
375+
if fileInfo.Group != "" {
376+
group = fileInfo.Group
377+
}
378+
}
379+
367380
contentMap[parent] = &Content{
368381
Destination: parent,
369382
Type: TypeImplicitDir,
370383
FileInfo: &ContentFileInfo{
371-
Owner: "root",
372-
Group: "root",
384+
Owner: owner,
385+
Group: group,
373386
Mode: 0o755,
374387
MTime: mtime,
375388
},
@@ -415,7 +428,7 @@ func addGlobbedFiles(
415428
return contentCollisionError(&c, presentContent)
416429
}
417430

418-
if err := addParents(all, dst, mtime); err != nil {
431+
if err := addParents(all, dst, mtime, origFile.FileInfo); err != nil {
419432
return err
420433
}
421434

@@ -458,7 +471,7 @@ func addTree(
458471
}
459472
}
460473

461-
err := addParents(all, tree.Destination, mtime)
474+
err := addParents(all, tree.Destination, mtime, tree.FileInfo)
462475
if err != nil {
463476
return err
464477
}
@@ -478,10 +491,6 @@ func addTree(
478491
c := &Content{
479492
FileInfo: &ContentFileInfo{},
480493
}
481-
if tree.FileInfo != nil && !ownedByFilesystem(tree.Destination) {
482-
c.FileInfo.Owner = tree.FileInfo.Owner
483-
c.FileInfo.Group = tree.FileInfo.Group
484-
}
485494

486495
switch {
487496
case d.IsDir():
@@ -497,6 +506,10 @@ func addTree(
497506
if ownedByFilesystem(c.Destination) {
498507
c.Type = TypeImplicitDir
499508
}
509+
if tree.FileInfo != nil && !ownedByFilesystem(c.Destination) {
510+
c.FileInfo.Owner = tree.FileInfo.Owner
511+
c.FileInfo.Group = tree.FileInfo.Group
512+
}
500513
case d.Type()&os.ModeSymlink != 0:
501514
linkDestination, err := os.Readlink(path)
502515
if err != nil {
@@ -506,11 +519,19 @@ func addTree(
506519
c.Type = TypeSymlink
507520
c.Source = filepath.ToSlash(strings.TrimPrefix(linkDestination, filepath.VolumeName(linkDestination)))
508521
c.Destination = NormalizeAbsoluteFilePath(destination)
522+
if tree.FileInfo != nil {
523+
c.FileInfo.Owner = tree.FileInfo.Owner
524+
c.FileInfo.Group = tree.FileInfo.Group
525+
}
509526
default:
510527
c.Type = TypeFile
511528
c.Source = path
512529
c.Destination = NormalizeAbsoluteFilePath(destination)
513530
c.FileInfo.Mode = d.Type() &^ umask
531+
if tree.FileInfo != nil {
532+
c.FileInfo.Owner = tree.FileInfo.Owner
533+
c.FileInfo.Group = tree.FileInfo.Group
534+
}
514535
}
515536

516537
if tree.FileInfo != nil && tree.FileInfo.Mode != 0 && c.Type != TypeSymlink {

files/files_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,56 @@ func TestAsExplicitRelativePath(t *testing.T) {
975975
}
976976
}
977977

978+
func TestIssue954(t *testing.T) {
979+
tmpDir := t.TempDir()
980+
contentDir := filepath.Join(tmpDir, "content")
981+
require.NoError(t, os.MkdirAll(filepath.Join(contentDir, "subdir1"), 0o755))
982+
require.NoError(t, os.MkdirAll(filepath.Join(contentDir, "subdir2"), 0o755))
983+
require.NoError(t, os.WriteFile(filepath.Join(contentDir, "subdir1", "file1.txt"), []byte("test"), 0o644))
984+
require.NoError(t, os.WriteFile(filepath.Join(contentDir, "subdir2", "file2.txt"), []byte("test"), 0o644))
985+
986+
results, err := files.PrepareForPackager(
987+
files.Contents{
988+
{
989+
Source: contentDir + "/",
990+
Destination: "/srv/www",
991+
FileInfo: &files.ContentFileInfo{
992+
Owner: "www-data",
993+
Group: "www-data",
994+
Mode: 0o775,
995+
},
996+
},
997+
},
998+
0,
999+
"",
1000+
false,
1001+
mtime,
1002+
)
1003+
require.NoError(t, err)
1004+
1005+
found := []string{}
1006+
for _, f := range results {
1007+
found = append(found, f.Destination)
1008+
if f.Destination == "/srv/" {
1009+
require.Equal(t, "root", f.FileInfo.Owner, f.Destination)
1010+
require.Equal(t, "root", f.FileInfo.Group, f.Destination)
1011+
continue
1012+
}
1013+
require.Contains(t, f.Destination, "/srv/www")
1014+
require.Equal(t, "www-data", f.FileInfo.Owner, f.Destination)
1015+
require.Equal(t, "www-data", f.FileInfo.Group, f.Destination)
1016+
1017+
}
1018+
require.ElementsMatch(t, []string{
1019+
"/srv/",
1020+
"/srv/www/",
1021+
"/srv/www/subdir1/",
1022+
"/srv/www/subdir1/file1.txt",
1023+
"/srv/www/subdir2/",
1024+
"/srv/www/subdir2/file2.txt",
1025+
}, found)
1026+
}
1027+
9781028
func TestIssue829(t *testing.T) {
9791029
var config testStruct
9801030
dec := yaml.NewDecoder(strings.NewReader(`---

0 commit comments

Comments
 (0)