Blame subr.rtl/rtl_fetch.subr

Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fetch_url_wget() {
Lucio Andrés Illanes Albornoz 368962
	local _urls="${1}" _sha256sum_src="${2}" _target_dname="${3}" _target_fname="${4}" _target_name="${5}" _mirrors="${6:-}"\
Lucio Andrés Illanes Albornoz 4229b4
		_rc=0 _sha256sum_target="" _target_fname_full="" _url="" _url_base="" _urls_count=0 _urls_full="";
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz b63b9b
	_urls_full="${_urls}";
Lucio Andrés Illanes Albornoz b63b9b
	for _url_base in ${_mirrors}; do
Lucio Andrés Illanes Albornoz b63b9b
		_urls_full="$(rtl_lconcat "${_urls_full}" "${_url_base%/}/${_target_name}/${_target_fname}")";
Lucio Andrés Illanes Albornoz b63b9b
	done;
Lucio Andrés Illanes Albornoz b63b9b
	_urls_count="$(rtl_llength "${_urls_full}")";
Lucio Andrés Illanes Albornoz b63b9b
	for _url in ${_urls_full}; do
Lucio Andrés Illanes Albornoz b63b9b
		if [ -z "${_target_fname}" ]; then
Lucio Andrés Illanes Albornoz b63b9b
			_target_fname="$(rtl_basename "${_url}")";
Lucio Andrés Illanes Albornoz b63b9b
		fi;
Lucio Andrés Illanes Albornoz b63b9b
		_target_fname_full="${_target_dname}/${_target_fname}";
Lucio Andrés Illanes Albornoz b63b9b
		(set +o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz b63b9b
		rtl_flock_acquire 4 || exit 1;
Lucio Andrés Illanes Albornoz b63b9b
		trap "_rc=\"\${?}\"; rm -f \"${_target_fname_full}.fetching\"; exit \"\${_rc}\";" EXIT;
Lucio Andrés Illanes Albornoz b63b9b
		if [ -z "${_sha256sum_src}" ]\
Lucio Andrés Illanes Albornoz b63b9b
		|| ! rtl_check_digest_file "${_target_fname_full}" "${_sha256sum_src}" "${_target_fname_full}.fetched"; then
Lucio Andrés Illanes Albornoz b63b9b
			wget ${DEFAULT_WGET_ARGS} -O "${_target_fname_full}" "${_url}"; _rc="${?}";
Lucio Andrés Illanes Albornoz b63b9b
			if [ "${_rc}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				exit $((${_rc}+2));
Lucio Andrés Illanes Albornoz b63b9b
			elif [ -n "${_sha256sum_src}" ]\
Lucio Andrés Illanes Albornoz b63b9b
			&& ! rtl_check_digest "${_target_fname_full}" "${_sha256sum_src}"; then
Lucio Andrés Illanes Albornoz b63b9b
				exit 2;
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz b63b9b
				printf "%s" "${RTL_CHECK_DIGEST_DIGEST}" > "${_target_fname_full}.fetched"; exit 0;
Lucio Andrés Illanes Albornoz 689d3c
			fi;
Lucio Andrés Illanes Albornoz b63b9b
		else
Lucio Andrés Illanes Albornoz b63b9b
			exit 0;
Lucio Andrés Illanes Albornoz b63b9b
		fi;) 4<>"${_target_fname_full}.fetching"; _rc="${?}"; : $((_urls_count-=1));
Lucio Andrés Illanes Albornoz b63b9b
		case "${_rc}" in
Lucio Andrés Illanes Albornoz b63b9b
		0)	break; ;;
Lucio Andrés Illanes Albornoz b63b9b
		1)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: failed to acquire fetching lock for URL \`%s', retrying with alternative URL..." "${_url}";
Lucio Andrés Illanes Albornoz 408967
			else
Lucio Andrés Illanes Albornoz 4229b4
				rtl_log_msg fatal "Error: failed to acquire fetching lock for URL \`%s'." "${_url}";
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		2)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: hash mismatch for URL \`%s', retrying with alternative URL... (from build variables: %s.)"\
Lucio Andrés Illanes Albornoz b63b9b
						"${_url}" "${_sha256sum_src}";
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz 6c8e73
				if _sha256sum_target="$(sha256sum "${_target_fname_full}" |\
Lucio Andrés Illanes Albornoz 6c8e73
							awk '{print $1}' 2>/dev/null)"; then
Lucio Andrés Illanes Albornoz 6c8e73
					rtl_log_msg fatal "Error: hash mismatch for URL \`%s' (should be: %s vs.: %s.)"\
Lucio Andrés Illanes Albornoz 6c8e73
							"${_url}" "${_sha256sum_src}" "${_sha256sum_target}";
Lucio Andrés Illanes Albornoz 6c8e73
				else
Lucio Andrés Illanes Albornoz 6c8e73
					rtl_log_msg fatal "Error: hash mismatch for URL \`%s' (should be: %s.)"\
Lucio Andrés Illanes Albornoz 6c8e73
							"${_url}" "${_sha256sum_src}";
Lucio Andrés Illanes Albornoz 6c8e73
				fi;
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		*)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: failed to fetch URL \`%s', retrying with alternative URL... (wget(1) exit status: %s)"\
Lucio Andrés Illanes Albornoz b63b9b
						"${_url}" "$((${_rc}-2))";
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg fatal "Error: failed to fetch URL \`%s' (wget(1) exit status: %s)"\
Lucio Andrés Illanes Albornoz 4229b4
						"${_url}" "$((${_rc}-2))";
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		esac;
Lucio Andrés Illanes Albornoz b63b9b
	done;
Lucio Andrés Illanes Albornoz b63b9b
	return "${_rc}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
# vim:filetype=sh