From 73ac87a50fbd13c72da1260fde453e500f3a9526 Mon Sep 17 00:00:00 2001 From: ghostloda <78798447@qq.com> Date: Sat, 7 Oct 2023 21:42:20 +0800 Subject: [PATCH 1/4] fix: scp files --- pkg/ssh/scp.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/ssh/scp.go b/pkg/ssh/scp.go index a20f36588f4..61a0c2d2f80 100644 --- a/pkg/ssh/scp.go +++ b/pkg/ssh/scp.go @@ -198,14 +198,11 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } } } else { - fn := func(host string, name string) bool { - exists, err := checkIfRemoteFileExists(client, name) - if err != nil { - logger.Error("failed to detect remote file exists: %v", err) - } - return exists + exists, err := checkIfRemoteFileExists(client, dest) + if err != nil { + logger.Error("failed to detect remote file exists: %v", err) } - if isCheckFileMD5() && fn(host, dest) { + if isCheckFileMD5() && exists { rfp, _ := client.Stat(dest) if lfp.Size() == rfp.Size() && hash.FileDigest(src) == c.RemoteSha256Sum(host, dest) { logger.Debug("remote dst %s already exists and is the latest version, skip copying process", dest) @@ -218,7 +215,8 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } defer lf.Close() - dstfp, err := client.Create(dest) + destTmp := fmt.Sprintf("%s.%s", dest, "tmp") + dstfp, err := client.Create(destTmp) if err != nil { return fmt.Errorf("failed to create: %v", err) } @@ -240,6 +238,14 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre return fmt.Errorf("sha256 sum not match %s(%s) != %s(%s), maybe network corruption?", src, sh, dest, dh) } } + if exists { + if err = client.Remove(dest); err != nil { + return fmt.Errorf("failed to Remove %s: %v", dest, err) + } + } + if err = client.Rename(destTmp, dest); err != nil { + return fmt.Errorf("failed to Rename %s to %s: %v", destTmp, dest, err) + } _ = epu.Add(1) } return nil From c55f90fcbd3dd30739a530d9464f4f392a617e96 Mon Sep 17 00:00:00 2001 From: ghostloda <78798447@qq.com> Date: Sun, 8 Oct 2023 10:25:06 +0800 Subject: [PATCH 2/4] fix: scp files --- pkg/ssh/scp.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pkg/ssh/scp.go b/pkg/ssh/scp.go index 61a0c2d2f80..27f4a269094 100644 --- a/pkg/ssh/scp.go +++ b/pkg/ssh/scp.go @@ -198,11 +198,14 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } } } else { - exists, err := checkIfRemoteFileExists(client, dest) - if err != nil { - logger.Error("failed to detect remote file exists: %v", err) + fn := func(host string, name string) bool { + exists, err := checkIfRemoteFileExists(client, name) + if err != nil { + logger.Error("failed to detect remote file exists: %v", err) + } + return exists } - if isCheckFileMD5() && exists { + if isCheckFileMD5() && fn(host, dest) { rfp, _ := client.Stat(dest) if lfp.Size() == rfp.Size() && hash.FileDigest(src) == c.RemoteSha256Sum(host, dest) { logger.Debug("remote dst %s already exists and is the latest version, skip copying process", dest) @@ -215,7 +218,7 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } defer lf.Close() - destTmp := fmt.Sprintf("%s.%s", dest, "tmp") + destTmp := dest + ".tmp" dstfp, err := client.Create(destTmp) if err != nil { return fmt.Errorf("failed to create: %v", err) @@ -227,6 +230,9 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre if _, err = io.Copy(dstfp, lf); err != nil { return fmt.Errorf("failed to Copy: %v", err) } + if err = client.PosixRename(destTmp, dest); err != nil { + logger.Error("failed to PosixRename %s: %v", destTmp, err) + } if isCheckFileMD5() { dh := c.RemoteSha256Sum(host, dest) if dh == "" { @@ -238,14 +244,6 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre return fmt.Errorf("sha256 sum not match %s(%s) != %s(%s), maybe network corruption?", src, sh, dest, dh) } } - if exists { - if err = client.Remove(dest); err != nil { - return fmt.Errorf("failed to Remove %s: %v", dest, err) - } - } - if err = client.Rename(destTmp, dest); err != nil { - return fmt.Errorf("failed to Rename %s to %s: %v", destTmp, dest, err) - } _ = epu.Add(1) } return nil From 880bc326a61c5da57082ca175a73a47aa0207845 Mon Sep 17 00:00:00 2001 From: ghostloda <78798447@qq.com> Date: Sun, 8 Oct 2023 13:07:36 +0800 Subject: [PATCH 3/4] fix: scp files --- pkg/ssh/scp.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/ssh/scp.go b/pkg/ssh/scp.go index 27f4a269094..15f3597159f 100644 --- a/pkg/ssh/scp.go +++ b/pkg/ssh/scp.go @@ -219,19 +219,27 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre defer lf.Close() destTmp := dest + ".tmp" - dstfp, err := client.Create(destTmp) - 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) + copyToTmp := 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 } - defer dstfp.Close() - if _, err = io.Copy(dstfp, lf); err != nil { - return fmt.Errorf("failed to Copy: %v", err) + if err = copyToTmp(destTmp); err != nil { + return err } + if err = client.PosixRename(destTmp, dest); err != nil { - logger.Error("failed to PosixRename %s: %v", destTmp, err) + return fmt.Errorf("failed to rename %s to %s: %v", destTmp, dest, err) } if isCheckFileMD5() { dh := c.RemoteSha256Sum(host, dest) From 50c7e2a2b2ea4e34c5ac993fb4dcd44786e930f4 Mon Sep 17 00:00:00 2001 From: ghostloda <78798447@qq.com> Date: Sun, 8 Oct 2023 14:15:44 +0800 Subject: [PATCH 4/4] fix: scp files --- pkg/ssh/scp.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/ssh/scp.go b/pkg/ssh/scp.go index 15f3597159f..bbbf90e5ea2 100644 --- a/pkg/ssh/scp.go +++ b/pkg/ssh/scp.go @@ -219,7 +219,7 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre defer lf.Close() destTmp := dest + ".tmp" - copyToTmp := func(tmpName string) error { + if err = func(tmpName string) error { dstfp, err := client.Create(tmpName) if err != nil { return fmt.Errorf("failed to create: %v", err) @@ -233,8 +233,7 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre return fmt.Errorf("failed to Copy: %v", err) } return nil - } - if err = copyToTmp(destTmp); err != nil { + }(destTmp); err != nil { return err }