1 #!powershell
2 
3 # Copyright: (c) 2017, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 
6 #Requires -Module Ansible.ModuleUtils.Legacy
7 
8 $ErrorActionPreference = "Stop"
9 
10 $params = Parse-Args $args -supports_check_mode $true
11 
12 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
13 $_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP
14 
15 $path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","name"
16 $state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","directory","file","touch"
17 
18 # used in template/copy when dest is the path to a dir and source is a file
19 $original_basename = Get-AnsibleParam -obj $params -name "_original_basename" -type "str"
20 if ((Test-Path -LiteralPath $path -PathType Container) -and ($null -ne $original_basename)) {
21     $path = Join-Path -Path $path -ChildPath $original_basename
22 }
23 
24 $result = @{
25     changed = $false
26 }
27 
28 # Used to delete symlinks as powershell cannot delete broken symlinks
29 $symlink_util = @"
30 using System;
31 using System.ComponentModel;
32 using System.Runtime.InteropServices;
33 
34 namespace Ansible.Command {
35     public class SymLinkHelper {
36         [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
37         public static extern bool DeleteFileW(string lpFileName);
38 
39         [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
40         public static extern bool RemoveDirectoryW(string lpPathName);
41 
42         public static void DeleteDirectory(string path) {
43             if (!RemoveDirectoryW(path))
44                 throw new Exception(String.Format("RemoveDirectoryW({0}) failed: {1}", path, new Win32Exception(Marshal.GetLastWin32Error()).Message));
45         }
46 
47         public static void DeleteFile(string path) {
48             if (!DeleteFileW(path))
49                 throw new Exception(String.Format("DeleteFileW({0}) failed: {1}", path, new Win32Exception(Marshal.GetLastWin32Error()).Message));
50         }
51     }
52 }
53 "@
54 $original_tmp = $env:TMP
55 $env:TMP = $_remote_tmp
56 Add-Type -TypeDefinition $symlink_util
57 $env:TMP = $original_tmp
58 
59 # Used to delete directories and files with logic on handling symbolic links
Remove-File($file, $checkmode)60 function Remove-File($file, $checkmode) {
61     try {
62         if ($file.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
63             # Bug with powershell, if you try and delete a symbolic link that is pointing
64             # to an invalid path it will fail, using Win32 API to do this instead
65             if ($file.PSIsContainer) {
66                 if (-not $checkmode) {
67                     [Ansible.Command.SymLinkHelper]::DeleteDirectory($file.FullName)
68                 }
69             } else {
70                 if (-not $checkmode) {
71                     [Ansible.Command.SymlinkHelper]::DeleteFile($file.FullName)
72                 }
73             }
74         } elseif ($file.PSIsContainer) {
75             Remove-Directory -directory $file -checkmode $checkmode
76         } else {
77             Remove-Item -LiteralPath $file.FullName -Force -WhatIf:$checkmode
78         }
79     } catch [Exception] {
80         Fail-Json $result "Failed to delete $($file.FullName): $($_.Exception.Message)"
81     }
82 }
83 
Remove-Directory($directory, $checkmode)84 function Remove-Directory($directory, $checkmode) {
85     foreach ($file in Get-ChildItem -LiteralPath $directory.FullName) {
86         Remove-File -file $file -checkmode $checkmode
87     }
88     Remove-Item -LiteralPath $directory.FullName -Force -Recurse -WhatIf:$checkmode
89 }
90 
91 
92 if ($state -eq "touch") {
93     if (Test-Path -LiteralPath $path) {
94         if (-not $check_mode) {
95             (Get-ChildItem -LiteralPath $path).LastWriteTime = Get-Date
96         }
97         $result.changed = $true
98     } else {
99         Write-Output $null | Out-File -LiteralPath $path -Encoding ASCII -WhatIf:$check_mode
100         $result.changed = $true
101     }
102 }
103 
104 if (Test-Path -LiteralPath $path) {
105     $fileinfo = Get-Item -LiteralPath $path -Force
106     if ($state -eq "absent") {
107         Remove-File -file $fileinfo -checkmode $check_mode
108         $result.changed = $true
109     } else {
110         if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
111             Fail-Json $result "path $path is not a directory"
112         }
113 
114         if ($state -eq "file" -and $fileinfo.PsIsContainer) {
115             Fail-Json $result "path $path is not a file"
116         }
117     }
118 
119 } else {
120 
121     # If state is not supplied, test the $path to see if it looks like
122     # a file or a folder and set state to file or folder
123     if ($null -eq $state) {
124         $basename = Split-Path -Path $path -Leaf
125         if ($basename.length -gt 0) {
126            $state = "file"
127         } else {
128            $state = "directory"
129         }
130     }
131 
132     if ($state -eq "directory") {
133         try {
134             New-Item -Path $path -ItemType Directory -WhatIf:$check_mode | Out-Null
135         } catch {
136             if ($_.CategoryInfo.Category -eq "ResourceExists") {
137                 $fileinfo = Get-Item -LiteralPath $_.CategoryInfo.TargetName
138                 if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
139                     Fail-Json $result "path $path is not a directory"
140                 }
141             } else {
142                 Fail-Json $result $_.Exception.Message
143             }
144         }
145         $result.changed = $true
146     } elseif ($state -eq "file") {
147         Fail-Json $result "path $path will not be created"
148     }
149 
150 }
151 
152 Exit-Json $result
153