Blame subr.rtl/rtl_fetch_wget.subr

e9fa07
#
e9fa07
# set +o errexit -o noglob -o nounset is assumed.
e9fa07
#
e9fa07
e9fa07
# N.B.	URLs ($1) may contain `?' or '&' characters.
e9fa07
rtl_fetch_url_wget() {
e9fa07
	local	_rfuw_urls="${1}" _rfuw_sha256sum_src="${2}" _rfuw_target_dname="${3}"		\
e9fa07
		_rfuw_target_fname="${4}" _rfuw_target_name="${5}" _rfuw_mirrors="${6:-}"	\
e9fa07
		_rfuw_rc=0 _rfuw_sha256sum_target="" _rfuw_target_fname_full="" _rfuw_url=""	\
e9fa07
		_rfuw_url_base="" _rfuw_urls_count=0 _rfuw_urls_full="";
e9fa07
e9fa07
	_rfuw_urls_full="${_rfuw_urls}";
e9fa07
	for _rfuw_url_base in ${_rfuw_mirrors}; do
e9fa07
		rtl_lconcat \$_rfuw_urls_full "${_rfuw_url_base%/}/${_rfuw_target_name}/${_rfuw_target_fname}";
e9fa07
	done;
e9fa07
	rtl_llength \$_rfuw_urls_count \$_rfuw_urls_full;
e9fa07
e9fa07
	for _rfuw_url in ${_rfuw_urls_full}; do
e9fa07
		if [ "${_rfuw_target_fname:+1}" != 1 ]; then
e9fa07
			rtl_basename2 \$_rfuw_url \$_rfuw_target_fname;
e9fa07
		fi;
e9fa07
		_rfuw_target_fname_full="${_rfuw_target_dname}/${_rfuw_target_fname}";
e9fa07
e9fa07
		(set +o errexit -o noglob -o nounset;
e9fa07
		rtl_flock_acquire 4 || exit 1;
e9fa07
		trap "_rfuw_rc=\"\${?}\"; rm -f \"${_rfuw_target_fname_full}.fetching\"; exit \"\${_rfuw_rc}\";" EXIT;
e9fa07
e9fa07
		if [ "${_rfuw_sha256sum_src:+1}" != 1 ]\
e9fa07
		|| ! rtl_check_digest_file "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}" "${_rfuw_target_fname_full}.fetched"; then
e9fa07
			wget ${DEFAULT_WGET_ARGS} -O "${_rfuw_target_fname_full}" "${_rfuw_url}"; _rfuw_rc="${?}";
e9fa07
			if [ "${_rfuw_rc}" -ne 0 ]; then
e9fa07
				exit $((${_rfuw_rc}+2));
e9fa07
			elif [ "${_rfuw_sha256sum_src:+1}" = 1 ]\
e9fa07
			&& ! rtl_check_digest \$_digest "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}"; then
e9fa07
				exit 2;
e9fa07
			else
e9fa07
				printf "%s" "${_digest}" > "${_rfuw_target_fname_full}.fetched"; exit 0;
e9fa07
			fi;
e9fa07
		else
e9fa07
			exit 0;
e9fa07
		fi;) 4<>"${_rfuw_target_fname_full}.fetching"; _rfuw_rc="${?}"; : $((_rfuw_urls_count-=1));
e9fa07
e9fa07
		case "${_rfuw_rc}" in
e9fa07
e9fa07
		0)	break; ;;
e9fa07
e9fa07
		1)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_lockfail_retryurl}" "${_rfuw_url}";
e9fa07
			else
e9fa07
				rtl_log_msg "fatal" "${MSG_rtl_fetch_lockfail}" "${_rfuw_url}";
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		2)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_hashfail_retryurl}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
e9fa07
			else
e9fa07
				if _rfuw_sha256sum_target="$(sha256sum "${_rfuw_target_fname_full}" |\
e9fa07
							awk '{print $1}' 2>/dev/null)"; then
e9fa07
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail1}" "${_rfuw_url}" "${_rfuw_sha256sum_src}" "${_rfuw_sha256sum_target}";
e9fa07
				else
e9fa07
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail2}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
e9fa07
				fi;
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		*)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_fail_retryurl}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
e9fa07
			else
e9fa07
				rtl_log_msg "fatal" "${MSG_rtl_fetch_fail}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		esac;
e9fa07
	done;
e9fa07
e9fa07
	return "${_rfuw_rc}";
e9fa07
};
e9fa07
64844b
# vim:filetype=sh textwidth=0