@@ -459,6 +459,7 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
459459 return "" , fmt .Errorf ("%w\n %s" , err , out .String ())
460460 }
461461
462+ // Fix permissions so files are accessible from the host.
462463 if err = buildEnv .FixPermissions (fsFn ); err != nil {
463464 return "" , err
464465 }
@@ -476,22 +477,31 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
476477 return string (data ), nil
477478}
478479
479- // concatFiles appends the contents of file b to a.
480- func concatFiles (a , b string ) error {
481- df , err := os . OpenFile ( a , os . O_APPEND | os . O_CREATE | os . O_WRONLY , 0o644 )
480+ // concatFiles appends the contents of file b to a using the given build environment .
481+ func concatFiles (buildEnv env. ExecEnv , a , b string ) error {
482+ aEnv , err := buildEnv . PathToEnv ( a )
482483 if err != nil {
483- return err
484+ return fmt . Errorf ( "failed to translate path: %w" , err )
484485 }
485- defer df .Close ()
486-
487- sf , err := os .Open (b )
486+ bEnv , err := buildEnv .PathToEnv (b )
488487 if err != nil {
489- return err
488+ return fmt . Errorf ( "failed to translate path: %w" , err )
490489 }
491- defer sf .Close ()
492490
493- _ , err = io .Copy (df , sf )
494- return err
491+ // Use shell to append file b to file a instead of os/io packages. This ensures
492+ // the operation works correctly with containerized builds where the host may not
493+ // have write permissions to container-created files.
494+ cmd := exec .Command ("sh" , "-c" , fmt .Sprintf ("cat %q >> %q" , bEnv , aEnv )) //nolint:gosec
495+ var out strings.Builder
496+ cmd .Stderr = & out
497+ cmd .Stdout = & out
498+ if err = buildEnv .WrapCommand (cmd ); err != nil {
499+ return err
500+ }
501+ if err = cmd .Run (); err != nil {
502+ return fmt .Errorf ("%w\n %s" , err , out .String ())
503+ }
504+ return nil
495505}
496506
497507// padWithEmptySpace pads the given file with empty space to make it the given size. See
0 commit comments