From 06f2cf7945d20470a853d837ee420fbe883875ba Mon Sep 17 00:00:00 2001 From: sealos-ci-robot <109538726+sealos-ci-robot@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:32:47 +0800 Subject: [PATCH] fix: scp files (#4041) (#4042) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: scp files * fix: scp files * fix: scp files * fix: scp files Co-authored-by: 榴莲榴莲 <78798447@qq.com> --- pkg/ssh/scp.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/ssh/scp.go b/pkg/ssh/scp.go index a20f36588f4..bbbf90e5ea2 100644 --- a/pkg/ssh/scp.go +++ b/pkg/ssh/scp.go @@ -218,16 +218,27 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } defer lf.Close() - dstfp, err := client.Create(dest) - if err != nil { - return fmt.Errorf("failed to create: %v", err) - } - if err = dstfp.Chmod(lfp.Mode()); err != nil { - return fmt.Errorf("failed to Chmod dst: %v", err) + destTmp := dest + ".tmp" + if err = func(tmpName string) error { + dstfp, err := client.Create(tmpName) + if err != nil { + return fmt.Errorf("failed to create: %v", err) + } + defer dstfp.Close() + + if err = dstfp.Chmod(lfp.Mode()); err != nil { + return fmt.Errorf("failed to Chmod dst: %v", err) + } + if _, err = io.Copy(dstfp, lf); err != nil { + return fmt.Errorf("failed to Copy: %v", err) + } + return nil + }(destTmp); err != nil { + return err } - defer dstfp.Close() - if _, err = io.Copy(dstfp, lf); err != nil { - return fmt.Errorf("failed to Copy: %v", err) + + if err = client.PosixRename(destTmp, dest); err != nil { + return fmt.Errorf("failed to rename %s to %s: %v", destTmp, dest, err) } if isCheckFileMD5() { dh := c.RemoteSha256Sum(host, dest)