@@ -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 {
0 commit comments