Skip to content

Commit

Permalink
include src/dst mode check to copy non-forced verification of the sam…
Browse files Browse the repository at this point in the history
…e file
  • Loading branch information
umputun committed Jun 5, 2023
1 parent bcef7a7 commit c6848b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/executor/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ func (l *Local) Upload(_ context.Context, src, dst string, opts *UpDownOpts) (er

// if destination file exists, and source and destination have the same size and modification time, skip copying
forced := opts != nil && opts.Force
if err == nil && !forced && srcInfo.Size() == dstInfo.Size() && srcInfo.ModTime().Equal(dstInfo.ModTime()) {
isSame := func() bool {
return srcInfo.Size() == dstInfo.Size() && srcInfo.ModTime().Equal(dstInfo.ModTime()) &&
srcInfo.Mode() == dstInfo.Mode()
}
if err == nil && !forced && isSame() {
log.Printf("[DEBUG] skip copying %s to %s, same size and modification time", match, destination)
continue
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/executor/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func TestUploadAndDownload(t *testing.T) {
// set the modification time to be the same as the source file
err = os.Chtimes(dstFile, srcInfo.ModTime(), srcInfo.ModTime())
require.NoError(t, err)

// set the chmod
err = os.Chmod(dstFile, srcInfo.Mode())
require.NoError(t, err)
}

err = fn.fn(context.Background(), srcFile.Name(), dstFile, &UpDownOpts{Mkdir: tc.mkdir, Force: tc.force})
Expand Down
6 changes: 4 additions & 2 deletions pkg/executor/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,10 @@ func (ex *Remote) sftpUpload(ctx context.Context, req sftpReq) error {

remoteFi, err := sftpClient.Stat(req.remoteFile)
if err == nil {
// if remote file exists, and has the same size and mod time, skip upload. Force flag overrides this.
if !req.force && remoteFi.Size() == inpFi.Size() && isWithinOneSecond(remoteFi.ModTime(), inpFi.ModTime()) {
// if remote file exists, and has the same size, mod time and mode, skip upload. Force flag overrides this.
isSame := !req.force && remoteFi.Size() == inpFi.Size() &&
isWithinOneSecond(remoteFi.ModTime(), inpFi.ModTime()) && remoteFi.Mode() == inpFi.Mode()
if isSame {
log.Printf("[INFO] remote file %s identical to local file %s, skipping upload", req.remoteFile, req.localFile)
return nil
}
Expand Down

0 comments on commit c6848b5

Please sign in to comment.