summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_filepath.subr
blob: 671f853418d9d98adbd5d1f4fec51e33aa32fc55 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# 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>/dev/null)";
		_old_ts="$(stat -c %Y "${_old_fname}" 2>/dev/null)";
		if [ "${_new_ts:-0}" -gt "${_old_ts:-0}" ]; then
			return 0;
		else
			return 1;
		fi;
	fi;
};

# vim:filetype=sh