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