Skip to content
Myr

[Robustness] `nvm_install_binary_extract`: replace a broken version dir atomically

Before adoption: this commit came in with the history the project adopted, so no landing records it.

A version directory left without a working `bin/node` - e.g. a partial or interrupted earlier install - wedged reinstallation: `nvm install` saw the version as not installed and re-extracted, but the per-entry `mv` refused to overwrite the leftover non-empty `bin/`, `lib/`, … subdirectories and left a half-updated tree behind (and, without `-b`, fell back to a from-source compile) Remove any pre-existing version directory and move the freshly extracted tree into place with a single rename, so a version is either fully installed or not present at all. The removal is safe: it runs only after the tarball has downloaded and extracted successfully into the cache. Fall back to the previous per-entry move when a single rename cannot cross filesystems.

authored by Jordan Harband · committed by Jordan Harband · parent b17550a · Browse files · 2 files+89 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+89 added−10 removed

  • tests:+62 added−0 removed
  • other:+27 added−10 removed

2 files+89 added−10 removed

added:test/fast/Unit tests/nvm_install_binary_extract self-heals a broken version dir+62 added
1#!/bin/sh23cleanup() {4 [ -n "${tmp_dir}" ] && [ -d "${tmp_dir}" ] && rm -rf "${tmp_dir}"5 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"6 unset -f die cleanup nvm_supports_xz7 unset NVM_DIR tmp_dir version archi node_dir version_path8}910die() { echo "$@" ; cleanup ; exit 1; }1112: nvm.sh13\. ../../../nvm.sh1415# Use gzip so the test does not depend on xz being installed.16nvm_supports_xz() { return 1; }1718set -ex1920type nvm_install_binary_extract > /dev/null 2>&1 || die 'nvm_install_binary_extract is not available'2122NVM_DIR="$(mktemp -d)"23tmp_dir="$(mktemp -d)"24[ -n "${NVM_DIR}" ] && [ -n "${tmp_dir}" ] || die 'Unable to create temporary folders'2526version='v14.15.4'27archi='linux-x64'28node_dir="${tmp_dir}/node-${version}-${archi}"2930# Build a fake binary tarball: a runnable node, an npm symlink, and a lib tree.31mkdir -p "${node_dir}/bin" "${node_dir}/lib/node_modules/npm/bin" || die 'setup mkdir failed'32printf '#!/bin/sh\necho "%s"\n' "${version}" > "${node_dir}/bin/node"33chmod +x "${node_dir}/bin/node"34echo 'npm-cli' > "${node_dir}/lib/node_modules/npm/bin/npm-cli.js"35( cd "${node_dir}/bin" && ln -s ../lib/node_modules/npm/bin/npm-cli.js npm )36echo 'fresh' > "${node_dir}/lib/FRESHFILE"37( cd "${tmp_dir}" && tar -czf "${node_dir}.tar.gz" "node-${version}-${archi}" ) || die 'unable to create fake tarball'3839# Pre-create a BROKEN, non-empty version directory: npm and a stale lib file40# are present, but bin/node is missing - exactly the shape that wedges a41# per-entry move onto existing non-empty subdirectories.42version_path="${NVM_DIR}/versions/node/${version}"43mkdir -p "${version_path}/bin" "${version_path}/lib" || die 'unable to stage broken dir'44echo 'stale-npm' > "${version_path}/bin/npm"45echo 'stale' > "${version_path}/lib/STALEFILE"46[ -e "${version_path}/bin/node" ] && die 'precondition: broken dir should have no bin/node'4748# Extract over the broken directory.49nvm_install_binary_extract 'linux' "${version}" "$(expr "${version}" : '.\(.*\)')" "${node_dir}.tar.gz" "${tmp_dir}/files" || die 'nvm_install_binary_extract failed over a broken version dir'5051# bin/node is restored and runnable.52[ -x "${version_path}/bin/node" ] || die 'bin/node was not restored'53[ "$("${version_path}/bin/node")" = "${version}" ] || die 'restored bin/node has wrong contents'5455# The stale file is gone: the whole directory was replaced, not merged into.56[ ! -e "${version_path}/lib/STALEFILE" ] || die 'stale lib file survived: install was not atomic'57[ -e "${version_path}/lib/FRESHFILE" ] || die 'fresh lib content missing after install'5859# npm resolves to the freshly installed target.60[ -e "${version_path}/bin/npm" ] || die 'npm missing after install'6162cleanup
modified:nvm.sh+27 added-10 removed
2375 unmodified lines
2376 # For Windows system (Git Bash with MSYS, Cygwin)2377 if [ "${NVM_OS}" = 'win' ]; then2378 VERSION_PATH="${VERSION_PATH}/bin"2379 command unzip -q "${TARBALL}" -d "${TMPDIR}" || return 12380 # For non Windows system (including WSL running on Windows)2381 else2382 nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}"2383 fi23842385 command mkdir -p "${VERSION_PATH}" || return 123862387 if [ "${NVM_OS}" = 'win' ]; then2380 # Replace any pre-existing (possibly broken or partial) install so the2381 # move below cannot collide with leftover files. Safe here: the archive2382 # has already downloaded and unzipped successfully into TMPDIR.2383 command rm -rf "${VERSION_PATH}"2384 command mkdir -p "${VERSION_PATH}" || return 12385 command mv "${TMPDIR}/"*/* "${VERSION_PATH}/" || return 12386 command chmod +x "${VERSION_PATH}"/node.exe || return 12387 command chmod +x "${VERSION_PATH}"/npm || return 12388 command chmod +x "${VERSION_PATH}"/npx 2>/dev/null2392 else2393 command mv "${TMPDIR}/"* "${VERSION_PATH}" || return 12389 command rm -rf "${TMPDIR}"2390 return 02391 fi23922393 # For non-Windows systems (including WSL running on Windows)2394 nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}" || return 123952396 # Install atomically: replace any pre-existing version directory with a2397 # single rename, so a partial or broken tree is never observed as installed.2398 # A leftover directory without a working bin/node otherwise wedges the2399 # install - the per-entry `mv` refuses to overwrite the non-empty bin/, lib/,2400 # ... subdirectories and leaves a half-updated tree behind. Removing it first2401 # is safe: the tarball has already downloaded and extracted into TMPDIR.2402 command rm -rf "${VERSION_PATH}" || return 12403 command mkdir -p "$(dirname "${VERSION_PATH}")" || return 12404 if command mv "${TMPDIR}" "${VERSION_PATH}" 2>/dev/null; then2405 return 02406 fi24072408 # Fall back to a per-entry move when a single rename is not possible (e.g.2409 # TMPDIR and the versions directory are on different filesystems).2410 command rm -rf "${VERSION_PATH}"2411 command mkdir -p "${VERSION_PATH}" || return 12412 command mv "${TMPDIR}/"* "${VERSION_PATH}" || return 12413 command rm -rf "${TMPDIR}"24142415 return 02416}
2561 unmodified lines