mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ce2284a793
* 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.
107 lines
3.1 KiB
PowerShell
107 lines
3.1 KiB
PowerShell
#!powershell
|
|
# This file is part of Ansible
|
|
#
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# WANT_JSON
|
|
# POWERSHELL_COMMON
|
|
|
|
$params = Parse-Args $args
|
|
|
|
$src = Get-Attr $params "src" $FALSE -type "path"
|
|
If ($src -eq $FALSE)
|
|
{
|
|
Fail-Json (New-Object psobject) "missing required argument: src"
|
|
}
|
|
|
|
$dest = Get-Attr $params "dest" $FALSE -type "path"
|
|
If ($dest -eq $FALSE)
|
|
{
|
|
Fail-Json (New-Object psobject) "missing required argument: dest"
|
|
}
|
|
|
|
$original_basename = Get-Attr $params "original_basename" $FALSE
|
|
If ($original_basename -eq $FALSE)
|
|
{
|
|
Fail-Json (New-Object psobject) "missing required argument: original_basename "
|
|
}
|
|
|
|
$result = New-Object psobject @{
|
|
changed = $FALSE
|
|
original_basename = $original_basename
|
|
}
|
|
|
|
# original_basename gets set if src and dest are dirs
|
|
# but includes subdir if the source folder contains sub folders
|
|
# e.g. you could get subdir/foo.txt
|
|
|
|
# detect if doing recursive folder copy and create any non-existent destination sub folder
|
|
$parent = Split-Path -Path $original_basename -Parent
|
|
if ($parent.length -gt 0)
|
|
{
|
|
$dest_folder = Join-Path $dest $parent
|
|
New-Item -Force $dest_folder -Type directory
|
|
}
|
|
|
|
# if $dest is a dir, append $original_basename so the file gets copied with its intended name.
|
|
if (Test-Path $dest -PathType Container)
|
|
{
|
|
$dest = Join-Path $dest $original_basename
|
|
}
|
|
|
|
$orig_checksum = Get-FileChecksum ($dest)
|
|
$src_checksum = Get-FileChecksum ($src)
|
|
|
|
If ($src_checksum.Equals($orig_checksum))
|
|
{
|
|
# if both are "3" then both are folders, ok to copy
|
|
If ($src_checksum.Equals("3"))
|
|
{
|
|
# New-Item -Force creates subdirs for recursive copies
|
|
New-Item -Force $dest -Type file
|
|
Copy-Item -Path $src -Destination $dest -Force
|
|
$result.operation = "folder_copy"
|
|
}
|
|
|
|
}
|
|
ElseIf (-Not $src_checksum.Equals($orig_checksum))
|
|
{
|
|
If ($src_checksum.Equals("3"))
|
|
{
|
|
Fail-Json (New-Object psobject) "If src is a folder, dest must also be a folder"
|
|
}
|
|
# The checksums don't match, there's something to do
|
|
Copy-Item -Path $src -Destination $dest -Force
|
|
$result.operation = "file_copy"
|
|
}
|
|
|
|
# verify before we return that the file has changed
|
|
$dest_checksum = Get-FileChecksum ($dest)
|
|
If ($src_checksum.Equals($dest_checksum))
|
|
{
|
|
If (-Not $orig_checksum.Equals($dest_checksum)) {
|
|
$result.changed = $TRUE
|
|
}
|
|
}
|
|
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"
|
|
}
|
|
|
|
$info = Get-Item $dest
|
|
$result.size = $info.Length
|
|
$result.src = $src
|
|
$result.dest = $dest
|
|
|
|
Exit-Json $result
|