summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_fetch_wget.subr
diff options
context:
space:
mode:
authorLucía Andrea Illanes Albornoz <lucia@luciaillanes.de>2023-02-17 19:29:28 +0100
committerLucía Andrea Illanes Albornoz <lucia@luciaillanes.de>2023-02-17 19:29:28 +0100
commite9fa0774ed2e7e030a68f5b0ae51fe6dd69fe492 (patch)
tree37e46c2578bd8f4f435073db01abc514976da8a8 /subr.rtl/rtl_fetch_wget.subr
parent56495632fc8bf612766a9c431e37ff27a903e8c6 (diff)
downloadmidipix_build-e9fa0774ed2e7e030a68f5b0ae51fe6dd69fe492.tar.bz2
midipix_build-e9fa0774ed2e7e030a68f5b0ae51fe6dd69fe492.tar.xz
Make everything a bit faster.
0) Issues several prayers and sacrifices to Enki under threat of a terrible deluge sent down by Ellil 1) Convert fork-write/read exprs to be non-forking 2) Pass mostly everything by reference 3) Don't bother cleaning the variable namespace because Bourne shell is an abomination 4) Removes broken ./pkgtool.sh -s, --restart-at, --update-diff & ./build.sh --dump-{in,on-abort} 5) Cleanup
Diffstat (limited to 'subr.rtl/rtl_fetch_wget.subr')
-rw-r--r--subr.rtl/rtl_fetch_wget.subr79
1 files changed, 79 insertions, 0 deletions
diff --git a/subr.rtl/rtl_fetch_wget.subr b/subr.rtl/rtl_fetch_wget.subr
new file mode 100644
index 00000000..e5a60677
--- /dev/null
+++ b/subr.rtl/rtl_fetch_wget.subr
@@ -0,0 +1,79 @@
+#
+# 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