From 005e29a22382c5fbc0edbaa8278614c52da88f74 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 17 Apr 2016 22:22:37 +0100 Subject: [PATCH 1/5] Check dir permissions before attempting to nvm uninstall --- nvm.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/nvm.sh b/nvm.sh index 0e1c9dc..b712838 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1828,6 +1828,17 @@ nvm() { NVM_PREFIX="$(nvm_node_prefix)" NVM_SUCCESS_MSG="Uninstalled node $VERSION" fi + + # Check version dir permissions + local FILES_WITHOUT_WRITE_PERMISIONS="$(find "$VERSION_PATH" -exec [ ! -w "{}" ] \; -exec echo "{}" \;)" + if [ "$FILES_WITHOUT_WRITE_PERMISIONS" ]; then + >&2 echo 'Cannot uninstall, incorrect permissions on installation folder.' + >&2 echo 'This is usually caused by running `npm install -g` as root. Run the following command as root to fix the permissions and then try again.' + >&2 echo + >&2 echo " chown -R $(whoami) \"$VERSION_PATH\"" + return 1 + fi + # Delete all files related to target version. command rm -rf "$NVM_DIR/src/$NVM_PREFIX-$VERSION" \ "$NVM_DIR/src/$NVM_PREFIX-$VERSION.tar.*" \ From 6fb528f4e49a673103ad669f0e49bd5acb919f16 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 17 Apr 2016 22:23:56 +0100 Subject: [PATCH 2/5] Add slow test to check nvm uninstall --- ...nvm uninstall 0.12.6\" uninstalls v0.12.6" | 28 +++++++++++++++++++ test/slow/nvm uninstall/setup_dir | 4 +++ test/slow/nvm uninstall/teardown_dir | 5 ++++ 3 files changed, 37 insertions(+) create mode 100755 "test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" create mode 100755 test/slow/nvm uninstall/setup_dir create mode 100755 test/slow/nvm uninstall/teardown_dir diff --git "a/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" "b/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" new file mode 100755 index 0000000..9af0171 --- /dev/null +++ "b/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" @@ -0,0 +1,28 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +# Source nvm +. ../../../nvm.sh + +# Version to install/uninstall +NVM_TEST_VERSION=0.12.6 + +# Make sure it's not already here +[ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION + +# Install it +nvm install $NVM_TEST_VERSION + +# Make sure it installed +nvm ls | grep "$NVM_TEST_VERSION" || die "Failed to install node" + +# Switch to another version so we can uninstall +nvm use 0.12.7 + +# Uninstall it +nvm uninstall $NVM_TEST_VERSION + +# Make sure it uninstalled +nvm ls | grep "$NVM_TEST_VERSION" +[ "$?" != "0" ] || die "Failed to uninstall node" diff --git a/test/slow/nvm uninstall/setup_dir b/test/slow/nvm uninstall/setup_dir new file mode 100755 index 0000000..f385d98 --- /dev/null +++ b/test/slow/nvm uninstall/setup_dir @@ -0,0 +1,4 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm install 0.12.7 diff --git a/test/slow/nvm uninstall/teardown_dir b/test/slow/nvm uninstall/teardown_dir new file mode 100755 index 0000000..f505f21 --- /dev/null +++ b/test/slow/nvm uninstall/teardown_dir @@ -0,0 +1,5 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm uninstall 0.12.7 +nvm deactivate From dff4f80f325146783162f57f6f26629bf240ef8d Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 17 Apr 2016 22:26:09 +0100 Subject: [PATCH 3/5] Add fast test to check nvm uninstall with file permission error --- ..." with incorrect file permissions fails nicely" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 "test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" diff --git "a/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" "b/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" new file mode 100755 index 0000000..1fad640 --- /dev/null +++ "b/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" @@ -0,0 +1,14 @@ +#!/bin/sh + +cd ../.. +mkdir v0.0.1 +mkdir src/node-v0.0.1 + +sudo touch v0.0.1/sudo + +. ./nvm.sh + +RETURN_MESSAGE="$(nvm uninstall v0.0.1 2>&1)" +CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder" + +test "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" || exit 1 From 5970a06b197085a7104121f0d78beedd33313d0e Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Mon, 18 Apr 2016 22:08:40 +0100 Subject: [PATCH 4/5] Add slow test to check nvm uninstall with file permission error --- ...h incorrect file permissions fails nicely" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 "test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" diff --git "a/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" "b/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" new file mode 100755 index 0000000..eff2bc4 --- /dev/null +++ "b/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" @@ -0,0 +1,30 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +# Source nvm +. ../../../nvm.sh + +# Version to install/uninstall +NVM_TEST_VERSION=5.10.1 + +# Make sure it's not already here +[ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION + +# Install it +nvm install $NVM_TEST_VERSION + +# Make sure it installed +nvm ls | grep "$NVM_TEST_VERSION" || die "Failed to install node" + +# Install global module as root +npm_path=$(which npm) +sudo "$npm_path" install jspm -g + +# Switch to another version so we can uninstall +nvm use 0.12.7 + +# Attempt to uninstall it +RETURN_MESSAGE="$(nvm uninstall $NVM_TEST_VERSION 2>&1)" +CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder" +test "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" || die "Failed to show error message" From 3a2d35f1670f170167c773cc1f6a23196d58be5e Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Tue, 19 Apr 2016 01:40:16 +0100 Subject: [PATCH 5/5] Check permissions in shell rather than using find -exec --- nvm.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index b712838..926ca35 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1830,8 +1830,18 @@ nvm() { fi # Check version dir permissions - local FILES_WITHOUT_WRITE_PERMISIONS="$(find "$VERSION_PATH" -exec [ ! -w "{}" ] \; -exec echo "{}" \;)" - if [ "$FILES_WITHOUT_WRITE_PERMISIONS" ]; then + local PERMISSIONS_OK=true + check_file_permissions() { + for FILE in $1/* $1/.[!.]* $1/..?* ; do + if [ -d "$FILE" ]; then + check_file_permissions "$FILE" + elif [ -e "$FILE" ]; then + [ ! -w "$FILE" ] && PERMISSIONS_OK=false + fi + done + } + check_file_permissions "$VERSION_PATH" + if [ "$PERMISSIONS_OK" = false ]; then >&2 echo 'Cannot uninstall, incorrect permissions on installation folder.' >&2 echo 'This is usually caused by running `npm install -g` as root. Run the following command as root to fix the permissions and then try again.' >&2 echo