Skip to content

Commit

Permalink
Fix fetch when retrieving a file with a multiple of the buffer size (a…
Browse files Browse the repository at this point in the history
…nsible#33697)

* Fix fetch when retrieving a file with a multiple of the buffer size

* fixed sanity issue

(cherry picked from commit 6e4c690)
  • Loading branch information
jborean93 committed Dec 12, 2017
1 parent 109471c commit 39f5905
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/ansible/plugins/connection/winrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,23 +505,29 @@ def fetch_file(self, in_path, out_path):
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)
Expand Down

0 comments on commit 39f5905

Please sign in to comment.