Handle concurrent CIPD client bootstrap
Download the client into a temporary file, then move it into the
final location atomically.
Change-Id: If9c8485c4a5895949d4ef3f0752abd959f272e86
diff --git a/cipd b/cipd
index beb4c47..ee0af57 100755
--- a/cipd
+++ b/cipd
@@ -46,13 +46,15 @@
if [[ ! -e "${CLIENT}" ]]; then
echo "Bootstrapping cipd client for ${HOST_PLATFORM}-${HOST_ARCH}..."
+ CLIENT_TMP="$(mktemp -p "${SCRIPT_DIR}" 2>/dev/null || mktemp "${SCRIPT_DIR}/.cipd_client.XXXXXXX")"
if hash curl 2> /dev/null ; then
- curl -f --progress-bar "${URL}" -A "${USER_AGENT}" -L -o "${CLIENT}"
- chmod +x "${CLIENT}"
+ curl -f --progress-bar "${URL}" -A "${USER_AGENT}" -L -o "${CLIENT_TMP}"
+ chmod +x "${CLIENT_TMP}"
else
echo "The \`curl\` command is missing, download ${URL} as ${CLIENT}, and then re-run this command" 1>&2
exit 1
fi
+ mv "${CLIENT_TMP}" "${CLIENT}"
fi
export CIPD_HTTP_USER_AGENT_PREFIX="${USER_AGENT}"