Blame subr.rtl/rtl_filepath.subr

Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_basename() {
e9fa07
	rtl_basename2 "${1}" "${1}";
e9fa07
};
e9fa07
e9fa07
rtl_basename2() {
e9fa07
	local	_rb2_rfname="${1#\$}" _rb2_rfname_out="${2#\$}"	\
e9fa07
		_rb2_fname="";
e9fa07
e9fa07
	eval _rb2_fname="\${${_rb2_rfname}}";
e9fa07
	eval ${_rb2_rfname_out}='${_rb2_fname##*/}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_check_digest() {
e9fa07
	local	_rcd_rdigest="${1#\$}" _rcd_fname="${2}" _rcd_digest_check="${3}"	\
e9fa07
		_rcd_digest="";
e9fa07
e9fa07
	if ! [ -e "${_rcd_fname}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		return 1;
e9fa07
	else	set -- $(sha256sum "${_rcd_fname}");
e9fa07
		_rcd_digest="${1}";
e9fa07
		eval ${_rcd_rdigest}='${_rcd_digest}';
e9fa07
		if [ "${_rcd_digest}" = "${_rcd_digest_check}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 0;
Lucio Andrés Illanes Albornoz aeeaa0
		else
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_check_digest_file() {
e9fa07
	local	_rcdf_fname="${1}" _rcdf_digest_check="${2}" _rcdf_digest_fname="${3}"	\
e9fa07
		_rcdf_digest="" _rcdf_digest_file="";
e9fa07
e9fa07
	if !  [ -e "${_rcdf_digest_fname}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		return 1;
e9fa07
	else	_rcdf_digest_file="$(cat "${_rcdf_digest_fname}")";
e9fa07
		if [ "${_rcdf_digest_file}" != "${_rcdf_digest_check}" ]\
e9fa07
		|| ! rtl_check_digest \$_rcdf_digest "${_rcdf_fname}"	\
e9fa07
				"${_rcdf_digest_check}";
e9fa07
		then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
e9fa07
		else
e9fa07
			return 0;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_check_path_vars() {
e9fa07
	local	_rcpv_rstatus="${1#\$}" _rcpv_vnames="${2}"	\
e9fa07
		_rcpv_rc=0 _rcpv_vname="" _rcpv_vname_val="";
e9fa07
e9fa07
	for _rcpv_vname in ${_rcpv_vnames}; do
e9fa07
		rtl_get_var_unsafe \$_rcpv_vname_val "${_rcpv_vname}";
e9fa07
		if [ "${_rcpv_vname_val:+1}" != 1 ]; then
e9fa07
			_rcpv_rc=1;
e9fa07
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' is empty or unset.';
e9fa07
			break;
e9fa07
		elif [ "${_rcpv_vname_val#* *}" != "${_rcpv_vname_val}" ]; then
e9fa07
			_rcpv_rc=2;
e9fa07
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' contains one or more whitespace characters.';
e9fa07
			break;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
e9fa07
	return "${_rcpv_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_dirname() {
e9fa07
	rtl_dirname2 "${1}" "${1}";
e9fa07
};
e9fa07
e9fa07
rtl_dirname2() {
e9fa07
	local	_rdname="${1#\$}" _rdname_out="${2#\$}"	\
e9fa07
		_rd2_dname="";
e9fa07
e9fa07
	eval _rd2_dname="\${${_rdname}}";
e9fa07
	_rd2_dname="${_rd2_dname%/*}";
e9fa07
e9fa07
	case "${_rd2_dname}" in
e9fa07
	"")	_rd2_dname="."; ;;
e9fa07
	*)	while rtl_matchr "${_rd2_dname}" "*/"; do
e9fa07
			_rd2_dname="${_rd2_dname%/}";
Lucio Andrés Illanes Albornoz aeeaa0
		done; ;;
e9fa07
	esac;
e9fa07
e9fa07
	eval ${_rdname_out}='${_rd2_dname}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_exists_any() {
e9fa07
	local _rea_subdir="${1}"; shift;
e9fa07
Lucio Andrés Illanes Albornoz aeeaa0
	while [ "${#}" -gt 0 ]; do
e9fa07
		if [ -e "${_rea_subdir}/${1}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 0;
Lucio Andrés Illanes Albornoz aeeaa0
		else
Lucio Andrés Illanes Albornoz aeeaa0
			shift;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
e9fa07
	done;
e9fa07
	return 1;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_flock_acquire() {
e9fa07
	local _rfa_fd="${1}" _rfa_conflict_exit_code="${2:-253}" _rfa_wait="${3:-3600}";
e9fa07
Lucio Andrés Illanes Albornoz aeeaa0
	while true; do
e9fa07
		if flock				\
e9fa07
			-E "${_rfa_conflict_exit_code}"	\
e9fa07
			-w "${_rfa_wait}"		\
e9fa07
			"${_rfa_fd}";
e9fa07
		then
e9fa07
			return 0;
e9fa07
		elif [ "${?}" -eq "${_rfa_conflict_exit_code}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			continue;
Lucio Andrés Illanes Albornoz aeeaa0
		else
Lucio Andrés Illanes Albornoz aeeaa0
			return "${?}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_is_newer() {
e9fa07
	local	_ris_new_fname="${1}" _ris_old_fname="${2}"	\
e9fa07
		_ris_new_ts="" _ris_old_ts="";
e9fa07
e9fa07
	if ! [ -e "${_ris_old_fname}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		return 0;
e9fa07
	else
e9fa07
		_ris_new_ts="$(stat -c %Y "${_ris_new_fname}" 2>/dev/null)";
e9fa07
		_ris_old_ts="$(stat -c %Y "${_ris_old_fname}" 2>/dev/null)";
e9fa07
e9fa07
		if [ "${_ris_new_ts:-0}" -gt "${_ris_old_ts:-0}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 0;
Lucio Andrés Illanes Albornoz aeeaa0
		else
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
64844b
# vim:filetype=sh textwidth=0