1
0
Fork 0
llvm-premerge-checks/containers/agent-windows-buildkite/unlock_path.ps1
Mikhail Goncharov ea943424f3 Enable windows builds back
Now we run a pre-commit hook that tries to kill any process that locked
a file in target directory.

Updated timeout interval on windows from 120 to 90 minutes.

Fixes #243
2020-10-07 16:59:37 +02:00

34 lines
No EOL
1 KiB
PowerShell

# Based ob https://dandraka.com/2019/08/13/find-and-kill-processes-that-lock-a-file-or-directory/
$path = $args[0]
$handleOutput = & handle -a $path
if ($handleOutput -match "no matching handles found") {
Write-Host "Nothing to kill, exiting"
exit
}
$pidList = New-Object System.Collections.ArrayList
$lines = $handleOutput -split "`n"
foreach($line in $lines) {
# sample line:
# chrome.exe pid: 11392 type: File 5BC: C:\Windows\Fonts\timesbd.ttf
# regex to get pid and process name: (.*)\b(?:.*)(?:pid: )(\d*)
$matches = $null
$line -match "(.*)\b(?:.*)(?:pid: )(\d*)" | Out-Null
if (-not $matches) { continue }
if ($matches.Count -eq 0) { continue }
$pidName = $matches[1]
$pidStr = $matches[2]
if ($pidList -notcontains $pidStr) {
Write-Host "Will kill process $pidStr $pidName"
$pidList.Add($pidStr) | Out-Null
}
}
foreach($pidStr in $pidList) {
$pidInt = [int]::Parse($pidStr)
Stop-Process -Id $pidInt -Force
Write-Host "Killed process $pidInt"
}
Write-Host "Finished"