summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/windows/win_copy.ps1
blob: e3f081df4f255bf21f42f09a5d41b405157aa28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!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 -supports_check_mode $true

$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false

$src = Get-AnsibleParam -obj $params -name "src" -type "path" -failifempty $true
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true
$force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $true
$original_basename = Get-AnsibleParam -obj $params -name "original_basename" -type "str"

# 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

$result = @{
    changed = $false
    dest = $dest
    original_basename = $original_basename
    src = $src
}

if (($force -eq $false) -and (Test-Path -Path $dest)) {
    $result.msg = "file already exists"
    Exit-Json $result
}

Function Copy-Folder($src, $dest) {
    if (Test-Path -Path $dest) {
        if (-not (Get-Item -Path $dest -Force).PSIsContainer) {
            Fail-Json $result "If src is a folder, dest must also be a folder. src: $src, dest: $dest"
        }
    } else {
        try {
            New-Item -Path $dest -ItemType Directory -Force -WhatIf:$check_mode
            $result.changed = $true
        } catch {
            Fail-Json $result "Failed to create new folder $dest $($_.Exception.Message)"
        }
    }

    foreach ($item in Get-ChildItem -Path $src) {
        $dest_path = Join-Path -Path $dest -ChildPath $item.PSChildName
        if ($item.PSIsContainer) {
            Copy-Folder -src $item.FullName -dest $dest_path
        } else {
            Copy-File -src $item.FullName -dest $dest_path
        }
    }
}

Function Copy-File($src, $dest) {
    if (Test-Path -Path $dest) {
        if ((Get-Item -Path $dest -Force).PSIsContainer) {
            Fail-Json $result "If src is a file, dest must also be a file. src: $src, dest: $dest"
        }
    }

    $src_checksum = Get-FileChecksum -Path $src
    $dest_checksum = Get-FileChecksum -Path $dest
    if ($src_checksum -ne $dest_checksum) {
        try {
            Copy-Item -Path $src -Destination $dest -Force -WhatIf:$check_mode
            $result.changed = $true
        } catch {
            Fail-Json $result "Failed to copy file $($_.Exception.Message)"
        }
    }

    # Verify the file we copied is the same
    $dest_checksum_verify = Get-FileChecksum -Path $dest
    if (-not ($check_mode) -and ($src_checksum -ne $dest_checksum_verify)) {
        Fail-Json $result "Copied file does not match checksum. src: $src_checksum, dest: $dest_checksum_verify. Failed to copy file from $src to $dest"
    }
}

Function Get-FileSize($path) {
    $file = Get-Item -Path $path -Force
    $size = $null
    if ($file.PSIsContainer) {
        $dir_files_sum = Get-ChildItem $file.FullName -Recurse
        if ($dir_files_sum -eq $null -or ($dir_files_sum.PSObject.Properties.name -contains 'length' -eq $false)) {
            $size = 0
        } else {
            $size = ($dir_files_sum | Measure-Object -property length -sum).Sum
        }
    } else {
        $size = $file.Length
    }

    $size
}

if (-not (Test-Path -Path $src)) {
    Fail-Json $result "Cannot copy src file: $src as it does not exist"
}

# If copying from remote we need to get the original folder path and name and change dest to this path
if ($original_basename) {
    $parent_path = Split-Path -Path $original_basename -Parent
    if ($parent_path.length -gt 0) {
        $dest_folder = Join-Path -Path $dest -ChildPath $parent_path
        try {
            New-Item -Path $dest_folder -Type directory -Force -WhatIf:$check_mode
            $result.changed = $true
        } catch {
            Fail-Json $result "Failed to create directory $($dest_folder): $($_.Exception.Message)"
        }
    }

    if ((Get-Item -Path $dest -Force).PSIsContainer) {
        $dest = Join-Path $dest -ChildPath $original_basename
    }
}

# If the source is a container prepare for some recursive magic
if ((Get-Item -Path $src -Force).PSIsContainer) {
    if (Test-Path -Path $dest) {
        if (-not (Get-Item -Path $dest -Force).PSIsContainer) {
            Fail-Json $result "If src is a folder, dest must also be a folder. src: $src, dest: $dest"
        }
    }

    $folder_name = (Get-Item -Path $src -Force).Name
    $dest_path = Join-Path -Path $dest -ChildPath $folder_name
    Copy-Folder -src $src -dest $dest_path
    if ($result.changed -eq $true) {
        $result.operation = "folder_copy"
    }
} else {
    Copy-File -src $src -dest $dest
    if ($result.changed -eq $true) {
        $result.operation = "file_copy"
    }
    $result.original_basename = (Get-Item -Path $src -Force).Name
    $result.checksum = Get-FileChecksum -Path $src
}

if ($check_mode) {
    # When in check mode the dest won't exit, just get the source size
    $result.size = Get-FileSize -path $src
} else {
    $result.size = Get-FileSize -path $dest
}

Exit-Json $result