Skip to content

Commit 4053e82

Browse files
committed
auto merge of #19170 : erickt/rust/rustup, r=erickt
This closes #19168. Please be careful reviewing this since this gets used all over the place. I've tested all the options and everything appears to be working though.
2 parents 6d965cc + f867379 commit 4053e82

File tree

1 file changed

+86
-65
lines changed

1 file changed

+86
-65
lines changed

src/etc/rustup.sh

100644100755
+86-65
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ flag() {
188188
fi
189189
}
190190

191-
validate_opt () {
191+
validate_opt() {
192192
for arg in $CFG_ARGS
193193
do
194194
isArgValid=0
@@ -230,6 +230,7 @@ validate_opt () {
230230
}
231231

232232
probe_need CFG_CURL curl
233+
probe_need CFG_TAR tar
233234

234235
CFG_SRC_DIR="$(cd $(dirname $0) && pwd)/"
235236
CFG_SELF="$0"
@@ -388,89 +389,109 @@ esac
388389

389390
msg "host triple: ${HOST_TRIPLE}"
390391

391-
PACKAGE_NAME=rust-nightly
392-
PACKAGE_NAME_AND_TRIPLE="${PACKAGE_NAME}-${HOST_TRIPLE}"
393-
TARBALL_NAME="${PACKAGE_NAME_AND_TRIPLE}.tar.gz"
394-
REMOTE_TARBALL="https://static.rust-lang.org/dist/${TARBALL_NAME}"
395-
TMP_DIR="./rustup-tmp-install"
396-
LOCAL_TARBALL="${TMP_DIR}/${TARBALL_NAME}"
397-
LOCAL_INSTALL_DIR="${TMP_DIR}/${PACKAGE_NAME_AND_TRIPLE}"
398-
LOCAL_INSTALL_SCRIPT="${LOCAL_INSTALL_DIR}/install.sh"
392+
CFG_INSTALL_FLAGS=""
393+
if [ -n "${CFG_UNINSTALL}" ]
394+
then
395+
CFG_INSTALL_FLAGS="${CFG_INSTALL_FLAGS} --uninstall"
396+
fi
397+
398+
if [ -n "${CFG_PREFIX}" ]
399+
then
400+
CFG_INSTALL_FLAGS="${CFG_INSTALL_FLAGS} --prefix=${CFG_PREFIX}"
401+
fi
402+
403+
CFG_TMP_DIR="./rustup-tmp-install"
399404

405+
RUST_URL="https://static.rust-lang.org/dist"
406+
RUST_PACKAGE_NAME=rust-nightly
407+
RUST_PACKAGE_NAME_AND_TRIPLE="${RUST_PACKAGE_NAME}-${HOST_TRIPLE}"
408+
RUST_TARBALL_NAME="${RUST_PACKAGE_NAME_AND_TRIPLE}.tar.gz"
409+
RUST_LOCAL_INSTALL_DIR="${CFG_TMP_DIR}/${RUST_PACKAGE_NAME_AND_TRIPLE}"
410+
RUST_LOCAL_INSTALL_SCRIPT="${RUST_LOCAL_INSTALL_DIR}/install.sh"
411+
412+
CARGO_URL="https://static.rust-lang.org/cargo-dist"
400413
CARGO_PACKAGE_NAME=cargo-nightly
401414
CARGO_PACKAGE_NAME_AND_TRIPLE="${CARGO_PACKAGE_NAME}-${HOST_TRIPLE}"
402415
CARGO_TARBALL_NAME="${CARGO_PACKAGE_NAME_AND_TRIPLE}.tar.gz"
403-
CARGO_REMOTE_TARBALL="https://static.rust-lang.org/cargo-dist/${CARGO_TARBALL_NAME}"
404-
CARGO_LOCAL_TARBALL="${TMP_DIR}/${CARGO_TARBALL_NAME}"
405-
CARGO_LOCAL_INSTALL_DIR="${TMP_DIR}/${CARGO_PACKAGE_NAME_AND_TRIPLE}"
416+
CARGO_LOCAL_INSTALL_DIR="${CFG_TMP_DIR}/${CARGO_PACKAGE_NAME_AND_TRIPLE}"
406417
CARGO_LOCAL_INSTALL_SCRIPT="${CARGO_LOCAL_INSTALL_DIR}/install.sh"
407418

408-
rm -Rf "${TMP_DIR}"
409-
need_ok "failed to remove temporary installation directory"
419+
# Fetch the package.
420+
download_package() {
421+
remote_tarball="$1"
422+
local_tarball="$2"
410423

411-
mkdir -p "${TMP_DIR}"
412-
need_ok "failed to create create temporary installation directory"
424+
msg "Downloading ${remote_tarball} to ${local_tarball}"
413425

414-
msg "downloading rust installer"
415-
"${CFG_CURL}" "${REMOTE_TARBALL}" > "${LOCAL_TARBALL}"
416-
if [ $? -ne 0 ]
417-
then
418-
rm -Rf "${TMP_DIR}"
419-
err "failed to download installer"
420-
fi
426+
mkdir -p "${CFG_TMP_DIR}"
427+
need_ok "failed to create create download directory"
421428

422-
if [ -z "${CFG_DISABLE_CARGO}" ]; then
423-
msg "downloading cargo installer"
424-
"${CFG_CURL}" "${CARGO_REMOTE_TARBALL}" > "${CARGO_LOCAL_TARBALL}"
429+
"${CFG_CURL}" -f -o "${local_tarball}" "${remote_tarball}"
425430
if [ $? -ne 0 ]
426431
then
427-
rm -Rf "${TMP_DIR}"
428-
err "failed to download cargo installer"
432+
rm -Rf "${CFG_TMP_DIR}"
433+
err "failed to download installer"
429434
fi
430-
fi
435+
}
431436

437+
# Wrap all the commands needed to install a package.
438+
install_package() {
439+
tarball_name="$1"
440+
install_script="$2"
432441

433-
(cd "${TMP_DIR}" && tar xzf "${TARBALL_NAME}")
434-
if [ $? -ne 0 ]
435-
then
436-
rm -Rf "${TMP_DIR}"
442+
msg "Extracting ${tarball_name}"
443+
(cd "${CFG_TMP_DIR}" && "${CFG_TAR}" -xvf "${tarball_name}")
444+
if [ $? -ne 0 ]; then
445+
rm -Rf "${CFG_TMP_DIR}"
437446
err "failed to unpack installer"
438-
fi
439-
440-
MAYBE_UNINSTALL=
441-
if [ -n "${CFG_UNINSTALL}" ]
442-
then
443-
MAYBE_UNINSTALL="--uninstall"
444-
fi
445-
446-
MAYBE_PREFIX=
447-
if [ -n "${CFG_PREFIX}" ]
448-
then
449-
MAYBE_PREFIX="--prefix=${CFG_PREFIX}"
450-
fi
451-
452-
sh "${LOCAL_INSTALL_SCRIPT}" "${MAYBE_UNINSTALL}" "${MAYBE_PREFIX}"
453-
if [ $? -ne 0 ]
454-
then
455-
rm -Rf "${TMP_DIR}"
456-
err "failed to install Rust"
457-
fi
447+
fi
458448

459-
if [ -z "${CFG_DISABLE_CARGO}" ]; then
460-
(cd "${TMP_DIR}" && tar xzf "${CARGO_TARBALL_NAME}")
449+
sh "${install_script}" "${CFG_INSTALL_FLAGS}"
461450
if [ $? -ne 0 ]
462451
then
463-
rm -Rf "${TMP_DIR}"
464-
err "failed to unpack cargo installer"
452+
rm -Rf "${CFG_TMP_DIR}"
453+
err "failed to install Rust"
465454
fi
455+
}
466456

467-
sh "${CARGO_LOCAL_INSTALL_SCRIPT}" "${MAYBE_UNINSTALL}" "${MAYBE_PREFIX}"
468-
if [ $? -ne 0 ]
469-
then
470-
rm -Rf "${TMP_DIR}"
471-
err "failed to install Cargo"
457+
# It's possible that curl could be interrupted partway though downloading
458+
# `rustup.sh`, truncating the file. This could be especially bad if we were in
459+
# the middle of a line that would run "rm -rf ". To protect against this, we
460+
# wrap up the `rustup.sh` destructive functionality in this helper function,
461+
# which we call as the last thing we do. This means we will not do anything
462+
# unless we have the entire file downloaded.
463+
install_packages() {
464+
rm -Rf "${CFG_TMP_DIR}"
465+
need_ok "failed to remove temporary installation directory"
466+
467+
mkdir -p "${CFG_TMP_DIR}"
468+
need_ok "failed to create create temporary installation directory"
469+
470+
RUST_LOCAL_TARBALL="${CFG_TMP_DIR}/${RUST_TARBALL_NAME}"
471+
CARGO_LOCAL_TARBALL="${CFG_TMP_DIR}/${CARGO_TARBALL_NAME}"
472+
473+
download_package \
474+
"${RUST_URL}/${RUST_TARBALL_NAME}" \
475+
"${RUST_LOCAL_TARBALL}"
476+
477+
if [ -z "${CFG_DISABLE_CARGO}" ]; then
478+
download_package \
479+
"${CARGO_URL}/${CARGO_TARBALL_NAME}" \
480+
"${CARGO_LOCAL_TARBALL}"
472481
fi
473-
fi
474482

475-
rm -Rf "${TMP_DIR}"
476-
need_ok "couldn't rm temporary installation directory"
483+
install_package \
484+
"${RUST_TARBALL_NAME}" \
485+
"${RUST_LOCAL_INSTALL_SCRIPT}"
486+
487+
if [ -z "${CFG_DISABLE_CARGO}" ]; then
488+
install_package \
489+
"${CARGO_TARBALL_NAME}" \
490+
"${CARGO_LOCAL_INSTALL_SCRIPT}"
491+
fi
492+
493+
rm -Rf "${CFG_TMP_DIR}"
494+
need_ok "couldn't rm temporary installation directory"
495+
}
496+
497+
install_packages

0 commit comments

Comments
 (0)