Skip to content

rustup: add support for caching of old nightlies, download resumption and verification #19927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 123 additions & 15 deletions src/etc/rustup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ validate_opt() {
}

create_tmp_dir() {
local TMP_DIR=./rustup-tmp-install
local TMP_DIR=`pwd`/rustup-tmp-install

rm -Rf "${TMP_DIR}"
need_ok "failed to remove temporary installation directory"
Expand All @@ -245,6 +245,21 @@ probe_need CFG_CURL curl
probe_need CFG_TAR tar
probe_need CFG_FILE file

probe CFG_SHA256SUM sha256sum
probe CFG_SHASUM shasum

if [ -z "$CFG_SHA256SUM" -a -z "$CFG_SHASUM" ]; then
err "unable to find either sha256sum or shasum"
fi

calculate_hash() {
if [ -n "$CFG_SHA256SUM" ]; then
${CFG_SHA256SUM} $@
else
${CFG_SHASUM} -a 256 $@
fi
}

CFG_SRC_DIR="$(cd $(dirname $0) && pwd)/"
CFG_SELF="$0"
CFG_ARGS="$@"
Expand All @@ -270,6 +285,10 @@ VAL_OPTIONS=""
flag uninstall "only uninstall from the installation prefix"
valopt prefix "" "set installation prefix"
opt cargo 1 "install cargo with rust"
valopt date "" "use the YYYY-MM-DD nightly instead of the current nightly"
valopt rust-date "" "use the YYYY-MM-DD rust nightly instead of the current nightly"
valopt cargo-date "" "use the YYYY-MM-DD cargo nightly instead of the current nightly"
flag save "save the downloaded nightlies to ~/.rustup"

if [ $HELP -eq 1 ]
then
Expand Down Expand Up @@ -417,6 +436,21 @@ CFG_TMP_DIR=$(mktemp -d 2>/dev/null \
|| mktemp -d -t 'rustup-tmp-install' 2>/dev/null \
|| create_tmp_dir)

# If we're saving nightlies and we didn't specify which one, grab todays.
# Otherwise we'll use the latest version.
if [ -n "${CFG_SAVE}" -a -z "${CFG_DATE}" ];
then
CFG_DATE=`date "+%Y-%m-%d"`
fi

if [ -z "${CFG_RUST_DATE}" ]; then
CFG_RUST_DATE="${CFG_DATE}"
fi

if [ -z "${CFG_CARGO_DATE}" ]; then
CFG_CARGO_DATE="${CFG_DATE}"
fi

RUST_URL="https://static.rust-lang.org/dist"
RUST_PACKAGE_NAME=rust-nightly
RUST_PACKAGE_NAME_AND_TRIPLE="${RUST_PACKAGE_NAME}-${HOST_TRIPLE}"
Expand All @@ -431,28 +465,86 @@ CARGO_TARBALL_NAME="${CARGO_PACKAGE_NAME_AND_TRIPLE}.tar.gz"
CARGO_LOCAL_INSTALL_DIR="${CFG_TMP_DIR}/${CARGO_PACKAGE_NAME_AND_TRIPLE}"
CARGO_LOCAL_INSTALL_SCRIPT="${CARGO_LOCAL_INSTALL_DIR}/install.sh"

# Fetch the package.
# add a date suffix if we want a particular nighly.
if [ -n "${CFG_RUST_DATE}" ];
then
RUST_URL="${RUST_URL}/${CFG_RUST_DATE}"
fi

if [ -n "${CFG_CARGO_DATE}" ];
then
CARGO_URL="${CARGO_URL}/${CFG_CARGO_DATE}"
fi

verify_hash() {
remote_sha256="$1"
local_file="$2"

msg "Downloading ${remote_sha256}"
remote_sha256=`"${CFG_CURL}" -f "${remote_sha256}"`
if [ "$?" -ne 0 ]; then
rm -Rf "${CFG_TMP_DIR}"
err "Failed to download ${remote_url}"
fi

msg "Verifying hash"
local_sha256=$(calculate_hash "${local_file}")
if [ "$?" -ne 0 ]; then
rm -Rf "${CFG_TMP_DIR}"
err "Failed to compute hash for ${local_tarball}"
fi

# We only need the sha, not the filenames
remote_sha256=`echo ${remote_sha256} | cut -f 1 -d ' '`
local_sha256=`echo ${local_sha256} | cut -f 1 -d ' '`

if [ "${remote_sha256}" != "${local_sha256}" ]; then
rm -Rf "${CFG_TMP_DIR}"
err "invalid sha256.\n ${remote_sha256}\t${remote_tarball}\n ${local_sha256}\t${local_tarball}"
fi
}

# Fetch the package. Optionally caches the tarballs.
download_package() {
remote_tarball="$1"
local_tarball="$2"
remote_sha256="${remote_tarball}.sha256"

msg "Downloading ${remote_tarball} to ${local_tarball}"
# Check if we've already downloaded this file.
if [ -e "${local_tarball}.tmp" ]; then
msg "Resuming ${remote_tarball} to ${local_tarball}"

"${CFG_CURL}" -f -o "${local_tarball}" "${remote_tarball}"
if [ $? -ne 0 ]
then
rm -Rf "${CFG_TMP_DIR}"
err "failed to download installer"
"${CFG_CURL}" -f -C - -o "${local_tarball}.tmp" "${remote_tarball}"
if [ $? -ne 0 ]
then
rm -Rf "${CFG_TMP_DIR}"
err "failed to download installer"
fi

mv "${local_tarball}.tmp" "${local_tarball}"
elif [ ! -e "${local_tarball}" ]; then
msg "Downloading ${remote_tarball} to ${local_tarball}"

"${CFG_CURL}" -f -o "${local_tarball}.tmp" "${remote_tarball}"
if [ $? -ne 0 ]
then
rm -Rf "${CFG_TMP_DIR}"
err "failed to download installer"
fi

mv "${local_tarball}.tmp" "${local_tarball}"
fi

verify_hash "${remote_sha256}" "${local_tarball}"
}

# Wrap all the commands needed to install a package.
install_package() {
tarball_name="$1"
local_tarball="$1"
install_script="$2"

msg "Extracting ${tarball_name}"
(cd "${CFG_TMP_DIR}" && "${CFG_TAR}" -xzf "${tarball_name}")
msg "Extracting ${local_tarball}"
(cd "${CFG_TMP_DIR}" && "${CFG_TAR}" -xvf "${local_tarball}")
if [ $? -ne 0 ]; then
rm -Rf "${CFG_TMP_DIR}"
err "failed to unpack installer"
Expand All @@ -479,8 +571,24 @@ install_packages() {
mkdir -p "${CFG_TMP_DIR}"
need_ok "failed to create create temporary installation directory"

RUST_LOCAL_TARBALL="${CFG_TMP_DIR}/${RUST_TARBALL_NAME}"
CARGO_LOCAL_TARBALL="${CFG_TMP_DIR}/${CARGO_TARBALL_NAME}"
# If we're saving our nightlies, put them in $HOME/.rustup.
if [ -n "${CFG_SAVE}" ]
then
RUST_DOWNLOAD_DIR="${HOME}/.rustup/${CFG_RUST_DATE}"
CARGO_DOWNLOAD_DIR="${HOME}/.rustup/${CFG_CARGO_DATE}"
else
RUST_DOWNLOAD_DIR="${CFG_TMP_DIR}"
CARGO_DOWNLOAD_DIR="${CFG_TMP_DIR}"
fi

mkdir -p "${RUST_DOWNLOAD_DIR}"
need_ok "failed to create create download directory"

mkdir -p "${CARGO_DOWNLOAD_DIR}"
need_ok "failed to create create download directory"

RUST_LOCAL_TARBALL="${RUST_DOWNLOAD_DIR}/${RUST_TARBALL_NAME}"
CARGO_LOCAL_TARBALL="${CARGO_DOWNLOAD_DIR}/${CARGO_TARBALL_NAME}"

download_package \
"${RUST_URL}/${RUST_TARBALL_NAME}" \
Expand All @@ -493,12 +601,12 @@ install_packages() {
fi

install_package \
"${RUST_TARBALL_NAME}" \
"${RUST_LOCAL_TARBALL}" \
"${RUST_LOCAL_INSTALL_SCRIPT}"

if [ -z "${CFG_DISABLE_CARGO}" ]; then
install_package \
"${CARGO_TARBALL_NAME}" \
"${CARGO_LOCAL_TARBALL}" \
"${CARGO_LOCAL_INSTALL_SCRIPT}"
fi

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ impl LitIntType {
#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)]
pub enum Lit_ {
LitStr(InternedString, StrStyle),
LitBinary(Rc<Vec<u8> >),
LitBinary(Rc<Vec<u8>>),
LitByte(u8),
LitChar(char),
LitInt(u64, LitIntType),
Expand Down