summaryrefslogtreecommitdiff
path: root/windows/win_file.ps1
blob: 958f9f04fcc8eb8f2392c85ab73b89921ce6a213 (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
#!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

$ErrorActionPreference = "Stop"

$params = Parse-Args $args

# path
$path = Get-Attr $params "path" $FALSE
If ($path -eq $FALSE)
{
    $path = Get-Attr $params "dest" $FALSE
    If ($path -eq $FALSE)
    {
        $path = Get-Attr $params "name" $FALSE
        If ($path -eq $FALSE)
        {
            Fail-Json (New-Object psobject) "missing required argument: path"
        }
    }
}

# JH Following advice from Chris Church, only allow the following states
# in the windows version for now:
# state - file, directory, touch, absent
# (originally was: state - file, link, directory, hard, touch, absent)

$state = Get-Attr $params "state" "unspecified"
# if state is not supplied, test the $path to see if it looks like 
# a file or a folder and set state to file or folder

# result
$result = New-Object psobject @{
    changed = $FALSE
}

If ( $state -eq "touch" )
{
    If(Test-Path $path)
    {
        (Get-ChildItem $path).LastWriteTime = Get-Date
    }
    Else
    {
        echo $null > $path
    }
    $result.changed = $TRUE
}

If (Test-Path $path)
{
    $fileinfo = Get-Item $path
    If ( $state -eq "absent" )
    {   
        Remove-Item -Recurse -Force $fileinfo
        $result.changed = $TRUE
    }
    Else
    {
        If ( $state -eq "directory" -and -not $fileinfo.PsIsContainer )
        {
            Fail-Json (New-Object psobject) "path is not a directory"
        }

        If ( $state -eq "file" -and $fileinfo.PsIsContainer )
        {
            Fail-Json (New-Object psobject) "path is not a file"
        }
    }
}
Else
# doesn't yet exist
{
    If ( $state -eq "unspecified" )
    {
        $basename = Split-Path -Path $path -Leaf
        If ($basename.length -gt 0) 
        {
           $state = "file"
        }
        Else
        {
           $state = "directory"
        }
    }

    If ( $state -eq "directory" )
    {
        New-Item -ItemType directory -Path $path
        $result.changed = $TRUE
    }

    If ( $state -eq "file" )
    {
        Fail-Json (New-Object psobject) "path will not be created"
    }
}

Exit-Json $result