windows: fix for checking locked system files (#30665)

* fix for checking locked system files

* moved functions to share module util and created tests

* fixed windows-paths test based on win_stat changes
This commit is contained in:
Jordan Borean 2017-11-16 10:04:03 +10:00 committed by ansibot
commit e16e6313c7
12 changed files with 166 additions and 29 deletions

View file

@ -6,6 +6,7 @@
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.CommandUtil
#Requires -Module Ansible.ModuleUtils.FileUtil
# TODO: add check mode support
@ -27,11 +28,11 @@ $result = @{
cmd = $raw_command_line
}
If($creates -and $(Test-Path -Path $creates)) {
if ($creates -and $(Test-FilePath -path $creates)) {
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
If($removes -and -not $(Test-Path -Path $removes)) {
if ($removes -and -not $(Test-FilePath -path $removes)) {
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}

View file

@ -6,6 +6,7 @@
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.CommandUtil
#Requires -Module Ansible.ModuleUtils.FileUtil
# TODO: add check mode support
@ -55,11 +56,11 @@ $result = @{
cmd = $raw_command_line
}
If($creates -and $(Test-Path $creates)) {
if ($creates -and $(Test-FilePath -path $creates)) {
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
If($removes -and -not $(Test-Path $removes)) {
if ($removes -and -not $(Test-FilePath -path $removes)) {
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}

View file

@ -1,21 +1,11 @@
#!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
# Copyright (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.FileUtil
# C# code to determine link target, copied from http://chrisbensen.blogspot.com.au/2010/06/getfinalpathnamebyhandle.html
$symlink_util = @"
@ -85,10 +75,11 @@ $result = @{
# Backward compatibility
if ($get_md5 -eq $true -and (Get-Member -inputobject $params -name "get_md5") ) {
Add-DeprecationWarning $result "The parameter 'get_md5' is being replaced with 'checksum_algorithm: md5'"
Add-DeprecationWarning -obj $result -message "The parameter 'get_md5' is being replaced with 'checksum_algorithm: md5'" -version 2.7
}
If (Test-Path -Path $path)
$info = Get-FileItem -path $path
If ($info -ne $null)
{
$result.stat.exists = $true
@ -98,9 +89,6 @@ If (Test-Path -Path $path)
$result.stat.isreg = $false
$result.stat.isshared = $false
# Need to use -Force so it picks up hidden files
$info = Get-Item -Force $path
$epoch_date = Get-Date -Date "01/01/1970"
$result.stat.creationtime = (Date_To_Timestamp $epoch_date $info.CreationTime)
$result.stat.lastaccesstime = (Date_To_Timestamp $epoch_date $info.LastAccessTime)

View file

@ -44,6 +44,7 @@ options:
use specified algorithm.
- This option is deprecated in Ansible 2.3 and is replaced with
C(checksum_algorithm=md5).
- This option will be removed in Ansible 2.7
required: no
default: True
get_checksum:
@ -199,7 +200,7 @@ stat:
type: string
sample: C:\temp
md5:
description: The MD5 checksum of a file (Between Ansible 1.9 and 2.2 this was returned as a SHA1 hash)
description: The MD5 checksum of a file (Between Ansible 1.9 and 2.2 this was returned as a SHA1 hash), will be removed in 2.7
returned: success, path exist, path is a file, get_md5 == True, md5 is supported
type: string
sample: 09cb79e8fc7453c84a07f644e441fd81623b7f98

View file

@ -5,6 +5,7 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.FileUtil
$ErrorActionPreference = "Stop"
@ -118,7 +119,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
$complete = $false
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-Path -Path $path) {
if (Test-FilePath -path $path) {
if ($search_regex -eq $null) {
$complete = $true
break
@ -149,7 +150,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
$complete = $false
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-Path -Path $path) {
if (Test-FilePath -path $path) {
if ($search_regex -ne $null) {
$file_contents = Get-Content -Path $path -Raw
if ($file_contents -notmatch $search_regex) {