win_copy: Fix for idempotency (#20281)

* include source file in error message

* win_copy: Fix for idempotency

This patch fixes an idempotency issue with win_copy. Without this patch
files would always be considered changed (unless the copy operation failed).

It also fixes the resulting output cfr. what was deocumented.
pull/4420/head
Dag Wieers 2017-01-27 17:17:35 +01:00 committed by John R Barker
parent c950767898
commit ce2284a793
2 changed files with 16 additions and 16 deletions

View File

@ -60,13 +60,13 @@ if (Test-Path $dest -PathType Container)
$dest = Join-Path $dest $original_basename $dest = Join-Path $dest $original_basename
} }
$dest_checksum = Get-FileChecksum ($dest) $orig_checksum = Get-FileChecksum ($dest)
$src_checksum = Get-FileChecksum ($src) $src_checksum = Get-FileChecksum ($src)
If ($src_checksum.Equals($dest_checksum)) If ($src_checksum.Equals($orig_checksum))
{ {
# if both are "3" then both are folders, ok to copy # if both are "3" then both are folders, ok to copy
If ($src_checksum.Equals("3")) If ($src_checksum.Equals("3"))
{ {
# New-Item -Force creates subdirs for recursive copies # New-Item -Force creates subdirs for recursive copies
New-Item -Force $dest -Type file New-Item -Force $dest -Type file
@ -75,9 +75,9 @@ If ($src_checksum.Equals($dest_checksum))
} }
} }
ElseIf (! $src_checksum.Equals($dest_checksum)) ElseIf (-Not $src_checksum.Equals($orig_checksum))
{ {
If ($src_checksum.Equals("3")) If ($src_checksum.Equals("3"))
{ {
Fail-Json (New-Object psobject) "If src is a folder, dest must also be a folder" Fail-Json (New-Object psobject) "If src is a folder, dest must also be a folder"
} }
@ -88,17 +88,20 @@ ElseIf (! $src_checksum.Equals($dest_checksum))
# verify before we return that the file has changed # verify before we return that the file has changed
$dest_checksum = Get-FileChecksum ($dest) $dest_checksum = Get-FileChecksum ($dest)
If ( $src_checksum.Equals($dest_checksum)) If ($src_checksum.Equals($dest_checksum))
{ {
$result.changed = $TRUE If (-Not $orig_checksum.Equals($dest_checksum)) {
$result.changed = $TRUE
}
} }
Else Else
{ {
Fail-Json (New-Object psobject) "src checksum $src_checksum did not match dest_checksum $dest_checksum Failed to place file $original_basename in $dest" Fail-Json (New-Object psobject) "src checksum $src_checksum did not match dest_checksum $dest_checksum Failed to place file $original_basename in $dest"
} }
# generate return values
$info = Get-Item $dest $info = Get-Item $dest
$result.size = $info.Length $result.size = $info.Length
$result.src = $src
$result.dest = $dest
Exit-Json $result Exit-Json $result

View File

@ -38,15 +38,12 @@ options:
with "/", only inside contents of that directory are copied to destination. with "/", only inside contents of that directory are copied to destination.
Otherwise, if it does not end with "/", the directory itself with all contents Otherwise, if it does not end with "/", the directory itself with all contents
is copied. This behavior is similar to Rsync. is copied. This behavior is similar to Rsync.
required: false required: true
default: null
aliases: []
dest: dest:
description: description:
- Remote absolute path where the file should be copied to. If src is a directory, - Remote absolute path where the file should be copied to. If src is a directory,
this must be a directory too. Use \\ for path separators. this must be a directory too. Use \\ for path separators.
required: true required: true
default: null
author: "Jon Hawkesworth (@jhawkesworth)" author: "Jon Hawkesworth (@jhawkesworth)"
''' '''
@ -54,19 +51,19 @@ EXAMPLES = r'''
- name: Copy a single file - name: Copy a single file
win_copy: win_copy:
src: /srv/myfiles/foo.conf src: /srv/myfiles/foo.conf
dest: c:\TEMP\foo.conf dest: c:\Temp\foo.conf
- name: Copy files/temp_files to c:\temp - name: Copy files/temp_files to c:\temp
win_copy: win_copy:
src: files/temp_files/ src: files/temp_files/
dest: c:\temp dest: c:\Temp
''' '''
RETURN = r''' RETURN = r'''
dest: dest:
description: destination file/path description: destination file/path
returned: changed returned: changed
type: string type: string
sample: c:\temp sample: c:\Temp
src: src:
description: source file used for the copy on the target machine description: source file used for the copy on the target machine
returned: changed returned: changed