@@ -179,7 +179,7 @@ func handleCp(ctx context.Context, cmd *cli.Command) error {
179179 if srcIsRemote {
180180 // Copy from instance to local (or stdout if dstPath is "-")
181181 if dstPath == "-" {
182- return copyFromInstanceToStdout (ctx , baseURL , apiKey , instanceID , srcPath , followLinks )
182+ return copyFromInstanceToStdout (ctx , baseURL , apiKey , instanceID , srcPath , followLinks , archive )
183183 }
184184 return copyFromInstance (ctx , baseURL , apiKey , instanceID , srcPath , dstPath , followLinks , quiet , archive )
185185 } else {
@@ -851,7 +851,7 @@ func copyTarFileToInstance(ctx context.Context, baseURL, apiKey, instanceID stri
851851}
852852
853853// copyFromInstanceToStdout copies files from the instance and writes a tar archive to stdout
854- func copyFromInstanceToStdout (ctx context.Context , baseURL , apiKey , instanceID , srcPath string , followLinks bool ) error {
854+ func copyFromInstanceToStdout (ctx context.Context , baseURL , apiKey , instanceID , srcPath string , followLinks , archive bool ) error {
855855 wsURL , err := buildCpWsURL (baseURL , instanceID )
856856 if err != nil {
857857 return err
@@ -912,7 +912,7 @@ func copyFromInstanceToStdout(ctx context.Context, baseURL, apiKey, instanceID,
912912 case "header" :
913913 // Flush previous file if any (but not symlinks - they have no data)
914914 if currentHeader != nil && ! currentHeader .IsDir && ! currentHeader .IsSymlink {
915- if err := writeTarEntry (tw , currentHeader , fileData ); err != nil {
915+ if err := writeTarEntry (tw , currentHeader , fileData , archive ); err != nil {
916916 return err
917917 }
918918 fileData = nil
@@ -931,8 +931,11 @@ func copyFromInstanceToStdout(ctx context.Context, baseURL, apiKey, instanceID,
931931 Name : header .Path + "/" ,
932932 Mode : int64 (header .Mode ),
933933 ModTime : time .Unix (header .Mtime , 0 ),
934- Uid : int (header .Uid ),
935- Gid : int (header .Gid ),
934+ }
935+ // Only preserve UID/GID in archive mode
936+ if archive {
937+ tarHeader .Uid = int (header .Uid )
938+ tarHeader .Gid = int (header .Gid )
936939 }
937940 if err := tw .WriteHeader (tarHeader ); err != nil {
938941 return fmt .Errorf ("write tar dir header: %w" , err )
@@ -945,8 +948,11 @@ func copyFromInstanceToStdout(ctx context.Context, baseURL, apiKey, instanceID,
945948 Linkname : header .LinkTarget ,
946949 Mode : int64 (header .Mode ),
947950 ModTime : time .Unix (header .Mtime , 0 ),
948- Uid : int (header .Uid ),
949- Gid : int (header .Gid ),
951+ }
952+ // Only preserve UID/GID in archive mode
953+ if archive {
954+ tarHeader .Uid = int (header .Uid )
955+ tarHeader .Gid = int (header .Gid )
950956 }
951957 if err := tw .WriteHeader (tarHeader ); err != nil {
952958 return fmt .Errorf ("write tar symlink header: %w" , err )
@@ -956,7 +962,7 @@ func copyFromInstanceToStdout(ctx context.Context, baseURL, apiKey, instanceID,
956962 case "end" :
957963 // Write final file data if any
958964 if currentHeader != nil && ! currentHeader .IsDir && ! currentHeader .IsSymlink {
959- if err := writeTarEntry (tw , currentHeader , fileData ); err != nil {
965+ if err := writeTarEntry (tw , currentHeader , fileData , archive ); err != nil {
960966 return err
961967 }
962968 fileData = nil
@@ -989,15 +995,18 @@ func copyFromInstanceToStdout(ctx context.Context, baseURL, apiKey, instanceID,
989995}
990996
991997// writeTarEntry writes a file entry to the tar archive
992- func writeTarEntry (tw * tar.Writer , header * cpFileHeader , data []byte ) error {
998+ func writeTarEntry (tw * tar.Writer , header * cpFileHeader , data []byte , archive bool ) error {
993999 tarHeader := & tar.Header {
9941000 Typeflag : tar .TypeReg ,
9951001 Name : header .Path ,
9961002 Size : int64 (len (data )),
9971003 Mode : int64 (header .Mode ),
9981004 ModTime : time .Unix (header .Mtime , 0 ),
999- Uid : int (header .Uid ),
1000- Gid : int (header .Gid ),
1005+ }
1006+ // Only preserve UID/GID in archive mode
1007+ if archive {
1008+ tarHeader .Uid = int (header .Uid )
1009+ tarHeader .Gid = int (header .Gid )
10011010 }
10021011 if err := tw .WriteHeader (tarHeader ); err != nil {
10031012 return fmt .Errorf ("write tar header: %w" , err )
0 commit comments