From 6e4c690a37f2d01a3d6449cfbabcbf45b96d0310 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 12 Dec 2017 14:56:21 +1000 Subject: [PATCH] Fix fetch when retrieving a file with a multiple of the buffer size (#33697) * Fix fetch when retrieving a file with a multiple of the buffer size * fixed sanity issue --- lib/ansible/plugins/connection/winrm.py | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 3e9626a2762..f01925f9591 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -571,23 +571,29 @@ class Connection(ConnectionBase): while True: try: script = ''' - If (Test-Path -PathType Leaf "%(path)s") + $path = "%(path)s" + If (Test-Path -Path $path -PathType Leaf) { - $stream = New-Object IO.FileStream("%(path)s", [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [IO.FileShare]::ReadWrite); - $stream.Seek(%(offset)d, [System.IO.SeekOrigin]::Begin) | Out-Null; - $buffer = New-Object Byte[] %(buffer_size)d; - $bytesRead = $stream.Read($buffer, 0, %(buffer_size)d); - $bytes = $buffer[0..($bytesRead-1)]; - [System.Convert]::ToBase64String($bytes); - $stream.Close() | Out-Null; + $buffer_size = %(buffer_size)d + $offset = %(offset)d + + $stream = New-Object -TypeName IO.FileStream($path, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite) + $stream.Seek($offset, [System.IO.SeekOrigin]::Begin) > $null + $buffer = New-Object -TypeName byte[] $buffer_size + $bytes_read = $stream.Read($buffer, 0, $buffer_size) + if ($bytes_read -gt 0) { + $bytes = $buffer[0..($bytes_read - 1)] + [System.Convert]::ToBase64String($bytes) + } + $stream.Close() > $null } - ElseIf (Test-Path -PathType Container "%(path)s") + ElseIf (Test-Path -Path $path -PathType Container) { Write-Host "[DIR]"; } Else { - Write-Error "%(path)s does not exist"; + Write-Error "$path does not exist"; Exit 1; } ''' % dict(buffer_size=buffer_size, path=self._shell._escape(in_path), offset=offset)