@ -2,6 +2,15 @@
# Use of this source code is governed by a BSD-style license that can be
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# found in the LICENSE file.
# Note: to run this on e.g. OSX for adhoc testing or debugging in case Windows
# is not around:
#
# pwsh cipd.ps1 \
# -CipdBinary _cipd.exe \
# -BackendURL https://chrome-infra-packages.appspot.com \
# -VersionFile ./cipd_client_version
# file _cipd.exe
Param (
Param (
# Path to download the CIPD binary to.
# Path to download the CIPD binary to.
[ parameter ( Mandatory = $true ) ] [ string ] $CipdBinary ,
[ parameter ( Mandatory = $true ) ] [ string ] $CipdBinary ,
@ -82,18 +91,30 @@ $Version = (Get-Content $VersionFile).Trim()
$URL = " $BackendURL /client?platform= $Platform &version= $Version "
$URL = " $BackendURL /client?platform= $Platform &version= $Version "
# Use a lock file to prevent simultaneous processes from stepping on each other.
# Grab a lock to prevent simultaneous processes from stepping on each other.
# This depends on "exclusive write" file sharing mode used by OpenWrite.
$CipdLockPath = Join-Path $DepotToolsPath -ChildPath " .cipd_client.lock "
$CipdLockPath = Join-Path $DepotToolsPath -ChildPath " .cipd_client.lock "
$TmpPath = $CipdBinary + " .tmp "
$CipdLockFile = $null
while ( $true ) {
while ( $CipdLockFile -eq $null ) {
$CipdLockFile = $null
try {
try {
$CipdLockFile = [ System.IO.File ] :: OpenWrite ( $CipdLockPath )
$CipdLockFile = [ System.IO.File ] :: OpenWrite ( $CipdLockPath )
} catch [ System.IO.IOException ] {
echo " CIPD bootstrap lock is held, trying again after delay... "
Start-Sleep -s 1
}
}
# Fetch the binary now that the lock is ours.
$TmpPath = $CipdBinary + " .tmp "
try {
echo " Downloading CIPD client for $Platform from $URL ... "
echo " Downloading CIPD client for $Platform from $URL ... "
$wc = ( New-Object System . Net . WebClient )
$wc = ( New-Object System . Net . WebClient )
$wc . Headers . Add ( " User-Agent " , $UserAgent )
$wc . Headers . Add ( " User-Agent " , $UserAgent )
try {
$wc . DownloadFile ( $URL , $TmpPath )
$wc . DownloadFile ( $URL , $TmpPath )
} catch {
throw " Failed to download the file, check your network connection "
}
$ActualSHA256 = Get-Actual -SHA256 $TmpPath
$ActualSHA256 = Get-Actual -SHA256 $TmpPath
if ( $ActualSHA256 -ne $ExpectedSHA256 ) {
if ( $ActualSHA256 -ne $ExpectedSHA256 ) {
@ -101,18 +122,8 @@ while ($true) {
}
}
Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
break
} finally {
} catch [ System.IO.IOException ] {
echo " CIPD bootstrap lock is held, trying again after delay... "
Start-Sleep -s 1
} catch {
throw # for some reason this is needed to exit while(...) loop on errors
} finally {
Delete-If -Possible $TmpPath
if ( $CipdLockFile ) {
$CipdLockFile . Close ( )
$CipdLockFile . Close ( )
Delete-If -Possible $CipdLockPath
Delete-If -Possible $CipdLockPath
}
Delete-If -Possible $TmpPath
}
}
}