Permalink
Browse files

reworked fetch, forced validation of downloaded ruby (if possible)

  • Loading branch information...
1 parent 90ffb1f commit 27ce130375cea08c40c4a982fdb596c08662101a @mpapis mpapis committed Aug 23, 2012
Showing with 108 additions and 166 deletions.
  1. +81 −127 scripts/fetch
  2. +7 −1 scripts/functions/logging
  3. +20 −38 scripts/md5
View
@@ -1,177 +1,131 @@
#!/usr/bin/env bash
-rvm_base_except="selector"
+url="$1"
+archive="$2"
-source "$rvm_scripts_path/base"
+shift ||
+ rvm_fail "BUG: $0 called without an argument :/"
-result=0
+builtin command -v curl > /dev/null ||
+ rvm_fail "rvm requires curl. curl was not found in your current PATH."
+
+rvm_base_except="selector"
+source "$rvm_scripts_path/base"
-# handled by teardown
+# handled by teardown - scripts/functions/environment:314
__rvm_cleanup_download()
{
[[ -f "$archive" ]] && __rvm_rm_rf "$archive"
}
-record_md5()
-{
- case "$(uname)" in
- Darwin|FreeBSD)
- archive_md5="$(/sbin/md5 -q "${archive}")"
- ;;
- OpenBSD)
- archive_md5="$(/bin/md5 -q "${archive}")"
- ;;
- Linux|*)
- archive_md5="$(md5sum "${archive}")"
- archive_md5="${archive_md5%% *}"
- ;;
- esac
-
- "$rvm_scripts_path/db" "$rvm_user_path/md5" "$archive" "$archive_md5"
-}
-
builtin cd "$rvm_archives_path"
-# args=($*) # Reserved for future use
-
-if
- [[ -z "$1" ]]
-then
- rvm_error "BUG: $0 called without an argument :/"
- exit 1
-fi
-
-url="$1"; download=1 ; package_name="$2"
+[[ -n "$archive" ]] || archive=$(basename "$url")
+fetch_command="curl ${rvm_proxy:+-x}${rvm_proxy:-} -f -L --create-dirs -C - -o ${archive}"
+rvm_debug "Fetching: $archive"
+rvm_debug "Fetch command: $fetch_command"
-if
- ! builtin command -v curl > /dev/null
-then
- rvm_error "rvm requires curl. curl was not found in your active path."
- exit 1
-elif
- [[ ! -z ${rvm_proxy} ]]
-then
- fetch_command="curl -x${rvm_proxy} -f -L --create-dirs -C - " # -s for silent
-else
- fetch_command="curl -f -L --create-dirs -C - " # -s for silent
-fi
+download=1
+try_ftp=0
+result=0
+retry=0
+[[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_path/config/md5" "$url" | head -n 1 )"
+[[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_user_path/md5" "$url" | head -n 1 )"
if
- [[ ! -z "$package_name" ]]
+ [[ "$url" =~ "?" ]] # try url without ?... like ?rvm={version}
then
- fetch_command="${fetch_command} -o ${package_name} "
- archive="$package_name"
-else
- fetch_command="${fetch_command} -O "
- archive=$(basename "$url")
+ [[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_path/config/md5" "${url%?*}" | head -n 1 )"
+ [[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_user_path/md5" "${url%?*}" | head -n 1 )"
fi
-
-[[ ${rvm_debug_flag:-0} -gt 0 ]] && rvm_debug "Fetching $archive"
+[[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_path/config/md5" "$archive" | head -n 1 )"
+[[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_user_path/md5" "$archive" | head -n 1 )"
+[[ -n "$archive_md5" ]] || rvm_warn "There is no md5 for '$archive', it's not possible to validate it."
# Check first if we have the correct archive
-archive_md5="$("$rvm_scripts_path/db" "$rvm_path/config/md5" "$archive" | head -n1)"
-[[ -n "$archive_md5" ]] ||
- archive_md5="$("$rvm_scripts_path/db" "$rvm_user_path/md5" "$archive" | head -n1)"
-
if
- [[ -e "$archive" && ! -z "$archive_md5" ]]
+ [[ -e "$archive" && -n "$archive_md5" ]]
then
- [[ ${rvm_debug_flag:-0} -gt 0 ]] &&
- rvm_debug "Found archive and its md5, testing correctness"
if
- ! "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5"
+ "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5"
then
- [[ ${rvm_debug_flag:-0} -gt 0 ]] &&
- rvm_debug "Archive md5 did not match, downloading"
- download=1
- else
- [[ ${rvm_debug_flag:-0} -gt 0 ]] &&
- rvm_debug "Archive md5 matched, not downloading"
+ rvm_debug "Archive md5 matched, not downloading"
download=0
- result=0
+ else
+ rvm_debug "Archive md5 did not match, downloading"
+ download=1
fi
else
- [[ ${rvm_debug_flag:-0} -gt 0 ]] &&
- rvm_debug "No archive or no MD5, downloading"
+ rvm_debug "No archive or no MD5, downloading"
download=1
fi
-# try to convert the http url to a ftp url
-ftp_url="$(echo "$url" | sed -e 's/http:/ftp:/')"
-
if
- [[ $download -gt 0 ]]
+ (( download > 0 ))
then
rm -f $archive
-
- eval $fetch_command "$url"
- result=$?
-
if
- [[ $result -gt 0 ]]
+ eval $fetch_command "$url"
then
- retry=0
- try_ftp=0
+ true
+ else
+ reult=$?
+ case "$result" in
+ (22|78)
+ rvm_error "The requested url does not exist($result): '$url'"
+ try_ftp=1
+ ;;
+ (18)
+ rvm_error "Partial file($result). Only a part of the file was transferred. Removing partial and re-trying."
+ rm -f "$archive"
+ retry=1
+ ;;
+ (33)
+ rvm_debug "Server does not support 'range' command($result), removing '$archive'"
+ rm -f "$archive"
+ retry=1
+ ;;
+ (*)
+ rvm_error "There was an error($result), please check ${rvm_log_path}/$rvm_ruby_string/*.log. Next we'll try to fetch via http."
+ try_ftp=1
+ ;;
+ esac
if
- [[ $result -eq 78 ]]
- then
- rvm_error "The requested url does not exist: '$url'"
- try_ftp=1
- elif
- [[ $result -eq 22 ]]
- then
- rvm_error "The requested url does not exist: '$url'"
- try_ftp=1
- elif
- [[ $result -eq 18 ]]
- then
- rvm_error "Partial file. Only a part of the file was transferred. Removing partial and re-trying."
- rm -f "$archive"
- retry=1
- elif
- [[ $result -eq 33 ]]
+ [[ $try_ftp -eq 1 ]]
then
- [[ ${rvm_debug_flag:-0} -gt 0 ]] &&
- rvm_debug "Server does not support 'range' command, removing '$archive'"
- rm -f "$archive"
+ rvm_log "Trying ftp:// URL instead."
+ url="${url/http:/ftp:/}"
retry=1
- else
- rvm_error "There was an error, please check ${rvm_log_path}/$rvm_ruby_string/*.log. Next we'll try to fetch via http."
- try_ftp=1
fi
if
[[ $retry -eq 1 ]]
then
- eval $fetch_command "$url"
- result=$?
-
if
- [[ $result -gt 0 ]]
+ eval $fetch_command "$url"
then
- rvm_error "There was an error, please check ${rvm_log_path}/$rvm_ruby_string/*.log"
+ true
else
- record_md5
+ result=$?
+ rvm_fail "There was an error($result), please check ${rvm_log_path}/$rvm_ruby_string/*.log" $result
fi
fi
- if
- [[ $try_ftp -eq 1 ]]
- then
- rvm_log "Trying ftp:// URL instead."
-
- eval $fetch_command "$ftp_url"
- result=$?
+ fi
+fi
- if
- [[ $result -gt 0 ]]
- then
- rvm_error "There was an error, please check ${rvm_log_path}/$rvm_ruby_string/*.log"
- else
- record_md5
- fi
- fi
+# Check if we have downloaded the correct archive
+if
+ [[ -n "$archive_md5" ]]
+then
+ if
+ "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5"
+ then
+ rvm_debug "Downloaded archive md5 matched."
else
- record_md5
+ rm -f $archive
+ rvm_fail "Downloaded archive md5 did not match, removing!"
fi
+else
+ rvm_debug "No md5, recording."
+ archive_md5="$( "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" )"
+ "$rvm_scripts_path/db" "$rvm_user_path/md5" "$archive" "$archive_md5"
fi
-
-exit $result
@@ -67,6 +67,11 @@ rvm_error_help()
shift
"${rvm_scripts_path}/help" "$@"
}
+rvm_fail()
+{
+ rvm_error "$1"
+ exit "${2:-1}"
+}
rvm_warn()
{
if rvm_pretty_print stdout
@@ -76,10 +81,11 @@ rvm_warn()
}
rvm_debug()
{
+ (( ${rvm_debug_flag:-0} > 0 )) || return 0
if rvm_pretty_print stdout
then printf "%b" "${rvm_debug_clr:-}$*${rvm_reset_clr:-}\n"
else printf "%b" "$*\n"
- fi
+ fi >&2
}
rvm_log()
{
View
@@ -1,64 +1,46 @@
#!/usr/bin/env bash
-variable_is_nonempty()
-{
- typeset _variable
- _variable="${1:-}"
-
- if [[ -n "${_variable}" ]]
- then
- eval "[[ -n \"\${${_variable}:-}\" ]]" || return $?
- else
- fail "Cannot check if variable is nonempty; no variable was given."
- fi
-
-}
-
-command_exists()
-{
- typeset _name
- _name="${1:-}"
-
- if variable_is_nonempty _name
- then
- builtin command -v "${_name}" > /dev/null 2>&1 || return 1
- else
- fail "Cannot test if command exists; no command name was given."
- fi
-}
-
-if (( ${rvm_trace_flag:=0} == 2 ))
+if
+ (( ${rvm_trace_flag:-0} == 2 ))
then
set -x
export rvm_trace_flag
fi
_archive="${1}"
-shift || fail "archive name not given in first param"
+md5="${2:-}"
+shift || rvm_fail "archive name not given in first param"
-md5="${1}"
-shift || fail "md5 value not given in second param"
-
-# Swiped from BDSM
-if command_exists md5
+# Swiped from SMF
+if
+ builtin command -v md5 > /dev/null 2>&1
then
archive_md5=$(md5 -q "${_archive}")
-elif command_exists md5sum
+elif
+ builtin command -v md5sum > /dev/null 2>&1
then
archive_md5="$(md5sum "${_archive}")"
archive_md5="${archive_md5%% *}"
else
for _path in /usr/gnu/bin /sbin /bin /usr/bin /usr/sbin
do
- if [[ -x "${_path}/md5" ]]
+ if
+ [[ -x "${_path}/md5" ]]
then
archive_md5=$(${_path}/md5 -q "${_archive}")
- elif [[ -x "${_path}/md5sum" ]]
+ elif
+ [[ -x "${_path}/md5sum" ]]
then
archive_md5="$(${_path}/md5sum "${_archive}")"
archive_md5="${archive_md5%% *}"
fi
done
fi
-[[ "${archive_md5}" == "${md5}" ]] || exit $?
+if
+ [[ -n "${md5}" ]]
+then
+ [[ "${archive_md5}" == "${md5}" ]] || exit $?
+else
+ echo "${archive_md5}"
+fi

0 comments on commit 27ce130

Please sign in to comment.