Skip to content

Commit

Permalink
(chocolateyGH-285) Fix download loop if connection lost
Browse files Browse the repository at this point in the history
Previously, if the network connection is lost in the middle of
downloading a file, it will cause an infinite loop of error messages
similar to

~~~
Exception calling "Read" with "3" argument(s): "Unable to read data
from the transport connection:
~~~

Another unwelcome side effect is that the (corrupted) download file will
proceed to fill up all available disk space if left unchecked.

With this fix, throw immediately when this happens as there is no
recovery even if the network connection comes back up.
  • Loading branch information
ferventcoder committed Jul 25, 2015
1 parent 69f3466 commit eb11ead
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,34 @@ param(
[byte[]]$buffer = new-object byte[] 1048576
[long]$total = [long]$count = [long]$iterLoop =0

do
{
$count = $reader.Read($buffer, 0, $buffer.Length);
if($fileName) {
$originalEAP = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
try {
do
{
$count = $reader.Read($buffer, 0, $buffer.Length);
if($fileName) {
$writer.Write($buffer, 0, $count);
}
if($Passthru){
}

if($Passthru){
$output += $encoding.GetString($buffer,0,$count)
} elseif(!$quiet) {
} elseif(!$quiet) {
$total += $count
if($goal -gt 0 -and ++$iterLoop%10 -eq 0) {
Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
}

if ($total -eq $goal) {
Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed
}
}
} while ($count -gt 0)
}
} while ($count -gt 0)
} catch {
throw $_.Exception
} finally {
$ErrorActionPreference = $originalEAP
}

$reader.Close()
if($fileName) {
Expand Down

0 comments on commit eb11ead

Please sign in to comment.