summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_fetch_wget.subr
blob: 1b5946c9873f42c26d580486539eae815f1998cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

# N.B.	URLs ($1) may contain `?' or '&' characters.
rtl_fetch_url_wget() {
	local	_rfuw_urls="${1}" _rfuw_sha256sum_src="${2}" _rfuw_target_dname="${3}"		\
		_rfuw_target_fname="${4}" _rfuw_target_name="${5}" _rfuw_mirrors="${6:-}"	\
		_rfuw_rc=0 _rfuw_sha256sum_target="" _rfuw_target_fname_full="" _rfuw_url=""	\
		_rfuw_url_base="" _rfuw_urls_count=0 _rfuw_urls_full="";

	_rfuw_urls_full="${_rfuw_urls}";
	for _rfuw_url_base in ${_rfuw_mirrors}; do
		rtl_lconcat \$_rfuw_urls_full "${_rfuw_url_base%/}/${_rfuw_target_name}/${_rfuw_target_fname}";
	done;
	rtl_llength \$_rfuw_urls_count \$_rfuw_urls_full;

	for _rfuw_url in ${_rfuw_urls_full}; do
		if [ "${_rfuw_target_fname:+1}" != 1 ]; then
			rtl_basename2 \$_rfuw_url \$_rfuw_target_fname;
		fi;
		_rfuw_target_fname_full="${_rfuw_target_dname}/${_rfuw_target_fname}";

		(set +o errexit -o noglob -o nounset;
		rtl_flock_acquire 4 || exit 1;
		trap "_rfuw_rc=\"\${?}\"; rm -f \"${_rfuw_target_fname_full}.fetching\"; exit \"\${_rfuw_rc}\";" EXIT;

		if [ "${_rfuw_sha256sum_src:+1}" != 1 ]\
		|| ! rtl_check_digest_file "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}" "${_rfuw_target_fname_full}.fetched"; then
			wget ${DEFAULT_WGET_ARGS} -O "${_rfuw_target_fname_full}" "${_rfuw_url}"; _rfuw_rc="${?}";
			if [ "${_rfuw_rc}" -ne 0 ]; then
				exit $((${_rfuw_rc}+2));
			elif [ "${_rfuw_sha256sum_src:+1}" = 1 ]\
			&& ! rtl_check_digest \$_digest "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}"; then
				exit 2;
			else
				printf "%s" "${_digest}" > "${_rfuw_target_fname_full}.fetched"; exit 0;
			fi;
		else
			exit 0;
		fi;) 4<>"${_rfuw_target_fname_full}.fetching"; _rfuw_rc="${?}"; : $((_rfuw_urls_count-=1));

		case "${_rfuw_rc}" in

		0)	break; ;;

		1)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
				rtl_log_msg "warning" "${MSG_rtl_fetch_lockfail_retryurl}" "${_rfuw_url}";
			else
				rtl_log_msg "fatal" "${MSG_rtl_fetch_lockfail}" "${_rfuw_url}";
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
			fi; ;;

		2)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
				rtl_log_msg "warning" "${MSG_rtl_fetch_hashfail_retryurl}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
			else
				if _rfuw_sha256sum_target="$(sha256sum "${_rfuw_target_fname_full}" |\
							awk '{print $1}' 2>/dev/null)"; then
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail1}" "${_rfuw_url}" "${_rfuw_sha256sum_src}" "${_rfuw_sha256sum_target}";
				else
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail2}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
				fi;
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
			fi; ;;

		*)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
				rtl_log_msg "warning" "${MSG_rtl_fetch_fail_retryurl}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
			else
				rtl_log_msg "fatal" "${MSG_rtl_fetch_fail}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
			fi; ;;

		esac;
	done;

	return "${_rfuw_rc}";
};

# vim:filetype=sh textwidth=0