From 10771a845e5cd7ee07a8fe367188f19bb722864b Mon Sep 17 00:00:00 2001 From: Thomas Gwynne-Timothy Date: Tue, 27 Oct 2020 18:55:27 -0600 Subject: [PATCH] Serialize deployBinary on target directory (#11363) --- scripts/buildsystems/msbuild/applocal.ps1 | 55 +++++++++++++++++------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/scripts/buildsystems/msbuild/applocal.ps1 b/scripts/buildsystems/msbuild/applocal.ps1 index 06f0733a7f..f0cb6ee1ff 100644 --- a/scripts/buildsystems/msbuild/applocal.ps1 +++ b/scripts/buildsystems/msbuild/applocal.ps1 @@ -12,24 +12,53 @@ if ($copiedFilesLog) Set-Content -Path $copiedFilesLog -Value "" -Encoding UTF8 } +function computeHash([System.Security.Cryptography.HashAlgorithm]$alg, [string]$str) { + $bytes = [System.Text.Encoding]::UTF8.GetBytes($str) + $hash = $alg.ComputeHash($bytes) + return [Convert]::ToBase64String($hash) +} + +function getMutex([string]$targetDir) { + $sha512Hash = [System.Security.Cryptography.SHA512]::Create() + if ($sha512Hash) { + $hash = computeHash $sha512Hash $targetDir + $mtxName = "VcpkgAppLocalDeployBinary-" + $hash + return New-Object System.Threading.Mutex($false, $mtxName) + } + + return New-Object System.Threading.Mutex($false, "VcpkgAppLocalDeployBinary") +} + # Note: this function signature is depended upon by the qtdeploy.ps1 script introduced in 5.7.1-7 function deployBinary([string]$targetBinaryDir, [string]$SourceDir, [string]$targetBinaryName) { - if (Test-Path "$targetBinaryDir\$targetBinaryName") { - $sourceModTime = (Get-Item $SourceDir\$targetBinaryName).LastWriteTime - $destModTime = (Get-Item $targetBinaryDir\$targetBinaryName).LastWriteTime - if ($destModTime -lt $sourceModTime) { - Write-Verbose " ${targetBinaryName}: Updating $SourceDir\$targetBinaryName" + try { + $mtx = getMutex($targetBinaryDir) + if ($mtx) { + $mtx.WaitOne() | Out-Null + } + + if (Test-Path "$targetBinaryDir\$targetBinaryName") { + $sourceModTime = (Get-Item $SourceDir\$targetBinaryName).LastWriteTime + $destModTime = (Get-Item $targetBinaryDir\$targetBinaryName).LastWriteTime + if ($destModTime -lt $sourceModTime) { + Write-Verbose " ${targetBinaryName}: Updating $SourceDir\$targetBinaryName" + Copy-Item "$SourceDir\$targetBinaryName" $targetBinaryDir + } else { + Write-Verbose " ${targetBinaryName}: already present" + } + } + else { + Write-Verbose " ${targetBinaryName}: Copying $SourceDir\$targetBinaryName" Copy-Item "$SourceDir\$targetBinaryName" $targetBinaryDir - } else { - Write-Verbose " ${targetBinaryName}: already present" + } + if ($copiedFilesLog) { Add-Content $copiedFilesLog "$targetBinaryDir\$targetBinaryName" -Encoding UTF8 } + if ($tlogFile) { Add-Content $tlogFile "$targetBinaryDir\$targetBinaryName" -Encoding Unicode } + } finally { + if ($mtx) { + $mtx.ReleaseMutex() | Out-Null + $mtx.Dispose() | Out-Null } } - else { - Write-Verbose " ${targetBinaryName}: Copying $SourceDir\$targetBinaryName" - Copy-Item "$SourceDir\$targetBinaryName" $targetBinaryDir - } - if ($copiedFilesLog) { Add-Content $copiedFilesLog "$targetBinaryDir\$targetBinaryName" -Encoding UTF8 } - if ($tlogFile) { Add-Content $tlogFile "$targetBinaryDir\$targetBinaryName" -Encoding Unicode } }