Skip to content

Added retry when unzip fail because of AV#1822

Merged
r15ch13 merged 5 commits intoScoopInstaller:masterfrom
Hollow1838:master
Mar 12, 2018
Merged

Added retry when unzip fail because of AV#1822
r15ch13 merged 5 commits intoScoopInstaller:masterfrom
Hollow1838:master

Conversation

@Hollow1838
Copy link
Copy Markdown
Contributor

Trend AV analyses and lock zip files just after download. I have added 2 retries in case of this event.

At work I can't change antivirus settings so I have no other choice than to modify the unzip function.

Trend AV analyses and lock zip files just after download. I have added 2 retries in case of this event.
@r15ch13
Copy link
Copy Markdown
Member

r15ch13 commented Nov 6, 2017

What happens when you install 7zip? Does it still not work?

@r15ch13
Copy link
Copy Markdown
Member

r15ch13 commented Nov 6, 2017

Can you try the following?

Add this function to core.ps1

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
    }
}

And change unzip() to:

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) {
            abort "Unzip failed: Too many retries!"
        }
        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] {
        # try to fall back to 7zip if path is too long
        if(7zip_installed) {
            extract_7zip $path $to $false
            return
        } else {
            abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
        }
    } catch {
        abort "Unzip failed: $_"
    }
}

@Hollow1838
Copy link
Copy Markdown
Contributor Author

Aww... still getting the error for very big files with both my code and your's.
I am trying to install idea-ultimate-eap from extras bucket. Seems like the file is too big and don't get released any time soon... AV must be scanning every dll in the _tmp folder of the app.

@Hollow1838
Copy link
Copy Markdown
Contributor Author

Currently trying with 30 retries

@Hollow1838
Copy link
Copy Markdown
Contributor Author

Hollow1838 commented Nov 6, 2017

Well 30 was not enough, he blocked on a different file, retrying with 60 retries.
-60 failed
-90 failed
-150 failed
-220 failed (felt it was close to the end!)
-250 failed again...
-300 fail
-400 fail
-600 fail
And yeah, 7zip is installed

@Hollow1838
Copy link
Copy Markdown
Contributor Author

Hollow1838 commented Nov 6, 2017

Well seems like scoop reaches a sort of timout and won't proceed further. I'm getting the same result after the same time even while setting numbers like 1000 - 2000.

@Hollow1838
Copy link
Copy Markdown
Contributor Author

OK! It worked, instead of default unzip, I used the "extract_7zip $path $to $false" by default. Seems like 7zip can deal with the waiting.

7zip can deal with an antivirus locking the file but extracttodirectory can't.
@Hollow1838
Copy link
Copy Markdown
Contributor Author

Thanks ^^

@rasa
Copy link
Copy Markdown
Member

rasa commented Mar 12, 2018

@r15ch13: this is ready to merge, yes?

@r15ch13 r15ch13 merged commit 071af50 into ScoopInstaller:master Mar 12, 2018
@rrelmy rrelmy removed their request for review July 4, 2018 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants