#
# set +o errexit -o noglob -o nounset is assumed.
#
rtl_basename() {
local _fname="${1##*/}"; printf "%s" "${_fname}";
};
rtl_check_digest() {
local _fname="${1}" _digest_check="${2}" _digest=""; RTL_CHECK_DIGEST_DIGEST="";
if ! [ -e "${_fname}" ]; then
return 1;
else set -- $(sha256sum "${_fname}");
RTL_CHECK_DIGEST_DIGEST="${1}";
if [ "${RTL_CHECK_DIGEST_DIGEST}" = "${_digest_check}" ]; then
return 0;
else
return 1;
fi;
fi;
};
rtl_check_digest_file() {
local _fname="${1}" _digest_check="${2}" _digest_fname="${3}" _digest="" RTL_CHECK_DIGEST_DIGEST="";
if ! [ -e "${_digest_fname}" ]; then
return 1;
else _digest="$(cat "${_digest_fname}")";
if [ "${_digest}" != "${_digest_check}" ]\
|| ! rtl_check_digest "${_fname}" "${_digest_check}"; then
return 1;
fi;
fi;
};
rtl_check_path_vars() {
local _vnames="${1}" _rc=0 _vname="" _vname_val=""; _status="";
for _vname in ${_vnames}; do
_vname_val="$(rtl_get_var_unsafe "${_vname}")";
if [ -z "${_vname_val}" ]; then
_rc=1; _status="Error: variable \`${_vname}' is empty or unset."; break;
elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
_rc=2; _status="Error: variable \`${_vname}' contains one or more whitespace characters."; break;
fi;
done;
return "${_rc}";
};
rtl_dirname() {
local _dname="${1%/*}";
case "${_dname}" in
"") _dname="."; ;;
*) while rtl_matchr "${_dname}" "*/"; do
_dname="${_dname%/}";
done; ;;
esac; printf "%s" "${_dname:-/}";
};
rtl_exists_any() {
local _subdir="${1}"; shift;
while [ "${#}" -gt 0 ]; do
if [ -e "${_subdir}/${1}" ]; then
return 0;
else
shift;
fi;
done; return 1;
};
rtl_flock_acquire() {
local _fd="${1}" _conflict_exit_code="${2:-253}" _wait="${3:-3600}"
while true; do
if flock -E "${_conflict_exit_code}" -w "${_wait}" "${_fd}"; then
break;
elif [ "${?}" -eq "${_conflict_exit_code}" ]; then
continue;
else
return "${?}";
fi;
done;
};
rtl_is_newer() {
local _new_fname="${1}" _old_fname="${2}" _new_ts="" _old_ts="";
if ! [ -e "${_old_fname}" ]; then
return 0;
else _new_ts="$(stat -c %Y "${_new_fname}" 2>&1)";
_old_ts="$(stat -c %Y "${_old_fname}" 2>&1)";
if [ "${_new_ts:-0}" -gt "${_old_ts:-0}" ]; then
return 0;
else
return 1;
fi;
fi;
};
# vim:filetype=sh