From 071af50671cd7c64379a734c4cf0897f3672f52e Mon Sep 17 00:00:00 2001 From: Pierre CHARLES Date: Mon, 12 Mar 2018 21:23:21 +0100 Subject: [PATCH] Added retry when unzip fail because of AV (#1822) --- lib/core.ps1 | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 1640d1a433..cfb66df4aa 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -166,10 +166,49 @@ function env($name,$global,$val='__get') { else { [environment]::setEnvironmentVariable($name,$val,$target) } } -function unzip($path,$to) { - if(!(test-path $path)) { abort "can't find $path to unzip"} +function isFileLocked([string]$path) { + $file = New-Object System.IO.FileInfo $path + + if ((Test-Path -Path $path) -eq $false) { + return $false + } + + try { + $stream = $file.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None) + if ($stream) { + $stream.Close() + } + return $false + } + catch { + # file is locked by a process. + return $true + } +} + +function unzip($path, $to) { + if (!(test-path $path)) { abort "can't find $path to unzip"} try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop } catch { unzip_old $path $to; return } # for .net earlier than 4.5 + $retries = 0 + while ($retries -le 10) { + if ($retries -eq 10) { + if (7zip_installed) { + extract_7zip $path $to $false + return + } else { + abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again." + } + } + if (isFileLocked $path) { + write-host "Waiting for $path to be unlocked by another process... ($retries/10)" + $retries++ + Start-Sleep -s 2 + } else { + break + } + } + try { [io.compression.zipfile]::extracttodirectory($path,$to) } catch [system.io.pathtoolongexception] {