Skip to content

Commit

Permalink
reworked fetch, forced validation of downloaded ruby (if possible)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapis committed Aug 23, 2012
1 parent 90ffb1f commit 27ce130
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 166 deletions.
208 changes: 81 additions & 127 deletions scripts/fetch
Original file line number Original file line Diff line number Diff line change
@@ -1,177 +1,131 @@
#!/usr/bin/env bash #!/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() __rvm_cleanup_download()
{ {
[[ -f "$archive" ]] && __rvm_rm_rf "$archive" [[ -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" builtin cd "$rvm_archives_path"


# args=($*) # Reserved for future use [[ -n "$archive" ]] || archive=$(basename "$url")

fetch_command="curl ${rvm_proxy:+-x}${rvm_proxy:-} -f -L --create-dirs -C - -o ${archive}"
if rvm_debug "Fetching: $archive"
[[ -z "$1" ]] rvm_debug "Fetch command: $fetch_command"
then
rvm_error "BUG: $0 called without an argument :/"
exit 1
fi

url="$1"; download=1 ; package_name="$2"


if download=1
! builtin command -v curl > /dev/null try_ftp=0
then result=0
rvm_error "rvm requires curl. curl was not found in your active path." retry=0
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


[[ -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 if
[[ ! -z "$package_name" ]] [[ "$url" =~ "?" ]] # try url without ?... like ?rvm={version}
then then
fetch_command="${fetch_command} -o ${package_name} " [[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_path/config/md5" "${url%?*}" | head -n 1 )"
archive="$package_name" [[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_user_path/md5" "${url%?*}" | head -n 1 )"
else
fetch_command="${fetch_command} -O "
archive=$(basename "$url")
fi fi

[[ -n "$archive_md5" ]] || archive_md5="$( "$rvm_scripts_path/db" "$rvm_path/config/md5" "$archive" | head -n 1 )"
[[ ${rvm_debug_flag:-0} -gt 0 ]] && rvm_debug "Fetching $archive" [[ -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 # 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 if
[[ -e "$archive" && ! -z "$archive_md5" ]] [[ -e "$archive" && -n "$archive_md5" ]]
then then
[[ ${rvm_debug_flag:-0} -gt 0 ]] &&
rvm_debug "Found archive and its md5, testing correctness"
if if
! "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5" "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5"
then then
[[ ${rvm_debug_flag:-0} -gt 0 ]] && rvm_debug "Archive md5 matched, not downloading"
rvm_debug "Archive md5 did not match, downloading"
download=1
else
[[ ${rvm_debug_flag:-0} -gt 0 ]] &&
rvm_debug "Archive md5 matched, not downloading"
download=0 download=0
result=0 else
rvm_debug "Archive md5 did not match, downloading"
download=1
fi fi
else 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 download=1
fi fi


# try to convert the http url to a ftp url
ftp_url="$(echo "$url" | sed -e 's/http:/ftp:/')"

if if
[[ $download -gt 0 ]] (( download > 0 ))
then then
rm -f $archive rm -f $archive

eval $fetch_command "$url"
result=$?

if if
[[ $result -gt 0 ]] eval $fetch_command "$url"
then then
retry=0 true
try_ftp=0 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 if
[[ $result -eq 78 ]] [[ $try_ftp -eq 1 ]]
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 ]]
then then
[[ ${rvm_debug_flag:-0} -gt 0 ]] && rvm_log "Trying ftp:// URL instead."
rvm_debug "Server does not support 'range' command, removing '$archive'" url="${url/http:/ftp:/}"
rm -f "$archive"
retry=1 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 fi
if if
[[ $retry -eq 1 ]] [[ $retry -eq 1 ]]
then then
eval $fetch_command "$url"
result=$?

if if
[[ $result -gt 0 ]] eval $fetch_command "$url"
then then
rvm_error "There was an error, please check ${rvm_log_path}/$rvm_ruby_string/*.log" true
else else
record_md5 result=$?
rvm_fail "There was an error($result), please check ${rvm_log_path}/$rvm_ruby_string/*.log" $result
fi fi
fi fi
if fi
[[ $try_ftp -eq 1 ]] fi
then
rvm_log "Trying ftp:// URL instead."

eval $fetch_command "$ftp_url"
result=$?


if # Check if we have downloaded the correct archive
[[ $result -gt 0 ]] if
then [[ -n "$archive_md5" ]]
rvm_error "There was an error, please check ${rvm_log_path}/$rvm_ruby_string/*.log" then
else if
record_md5 "$rvm_scripts_path"/md5 "$rvm_archives_path/${archive}" "$archive_md5"
fi then
fi rvm_debug "Downloaded archive md5 matched."
else else
record_md5 rm -f $archive
rvm_fail "Downloaded archive md5 did not match, removing!"
fi 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 fi

exit $result
8 changes: 7 additions & 1 deletion scripts/functions/logging
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ rvm_error_help()
shift shift
"${rvm_scripts_path}/help" "$@" "${rvm_scripts_path}/help" "$@"
} }
rvm_fail()
{
rvm_error "$1"
exit "${2:-1}"
}
rvm_warn() rvm_warn()
{ {
if rvm_pretty_print stdout if rvm_pretty_print stdout
Expand All @@ -76,10 +81,11 @@ rvm_warn()
} }
rvm_debug() rvm_debug()
{ {
(( ${rvm_debug_flag:-0} > 0 )) || return 0
if rvm_pretty_print stdout if rvm_pretty_print stdout
then printf "%b" "${rvm_debug_clr:-}$*${rvm_reset_clr:-}\n" then printf "%b" "${rvm_debug_clr:-}$*${rvm_reset_clr:-}\n"
else printf "%b" "$*\n" else printf "%b" "$*\n"
fi fi >&2
} }
rvm_log() rvm_log()
{ {
Expand Down
58 changes: 20 additions & 38 deletions scripts/md5
Original file line number Original file line Diff line number Diff line change
@@ -1,64 +1,46 @@
#!/usr/bin/env bash #!/usr/bin/env bash


variable_is_nonempty() if
{ (( ${rvm_trace_flag:-0} == 2 ))
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 ))
then then
set -x set -x
export rvm_trace_flag export rvm_trace_flag
fi fi


_archive="${1}" _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}" # Swiped from SMF
shift || fail "md5 value not given in second param" if

builtin command -v md5 > /dev/null 2>&1
# Swiped from BDSM
if command_exists md5
then then
archive_md5=$(md5 -q "${_archive}") archive_md5=$(md5 -q "${_archive}")
elif command_exists md5sum elif
builtin command -v md5sum > /dev/null 2>&1
then then
archive_md5="$(md5sum "${_archive}")" archive_md5="$(md5sum "${_archive}")"
archive_md5="${archive_md5%% *}" archive_md5="${archive_md5%% *}"
else else
for _path in /usr/gnu/bin /sbin /bin /usr/bin /usr/sbin for _path in /usr/gnu/bin /sbin /bin /usr/bin /usr/sbin
do do
if [[ -x "${_path}/md5" ]] if
[[ -x "${_path}/md5" ]]
then then
archive_md5=$(${_path}/md5 -q "${_archive}") archive_md5=$(${_path}/md5 -q "${_archive}")
elif [[ -x "${_path}/md5sum" ]] elif
[[ -x "${_path}/md5sum" ]]
then then
archive_md5="$(${_path}/md5sum "${_archive}")" archive_md5="$(${_path}/md5sum "${_archive}")"
archive_md5="${archive_md5%% *}" archive_md5="${archive_md5%% *}"
fi fi
done done
fi 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.