Skip to content

Commit bdad985

Browse files
committed
fix(cp): preserve directory modification times during copy
The mtime was only being applied when currentFile != nil, which is only true for regular files. For directories, currentFile is never set since they are created with os.MkdirAll without opening a file handle. This fix moves the mtime logic outside of the currentFile != nil block so it applies to both files and directories.
1 parent 61f0469 commit bdad985

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/guest/client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,17 @@ func CopyFromInstance(ctx context.Context, vsockSocketPath string, opts CopyFrom
559559
case *CopyFromGuestResponse_End:
560560
if currentFile != nil {
561561
currentFile.Close()
562-
// Set modification time
563-
if currentHeader != nil && currentHeader.Mtime > 0 {
564-
targetPath, err := securejoin.SecureJoin(opts.DstPath, currentHeader.Path)
565-
if err == nil {
566-
mtime := time.Unix(currentHeader.Mtime, 0)
567-
os.Chtimes(targetPath, mtime, mtime)
568-
}
569-
}
570562
currentFile = nil
571-
currentHeader = nil
572563
}
564+
// Set modification time for files and directories
565+
if currentHeader != nil && currentHeader.Mtime > 0 {
566+
targetPath, err := securejoin.SecureJoin(opts.DstPath, currentHeader.Path)
567+
if err == nil {
568+
mtime := time.Unix(currentHeader.Mtime, 0)
569+
os.Chtimes(targetPath, mtime, mtime)
570+
}
571+
}
572+
currentHeader = nil
573573
if r.End.Final {
574574
receivedFinal = true
575575
return nil

0 commit comments

Comments
 (0)