summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_filepath.subr
blob: 7d4ec553faa2e74bda2ee8b5704c4547b841c13a (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

rtl_basename() {
	rtl_basename2 "${1}" "${1}";
};

rtl_basename2() {
	local	_rb2_rfname="${1#\$}" _rb2_rfname_out="${2#\$}"	\
		_rb2_fname="";

	eval _rb2_fname="\${${_rb2_rfname}}";
	eval ${_rb2_rfname_out}='${_rb2_fname##*/}';
	return 0;
};

rtl_check_digest() {
	local	_rcd_rdigest="${1#\$}" _rcd_fname="${2}" _rcd_digest_check="${3}"	\
		_rcd_digest="";

	if ! [ -e "${_rcd_fname}" ]; then
		return 1;
	else	set -- $(sha256sum "${_rcd_fname}");
		_rcd_digest="${1}";
		eval ${_rcd_rdigest}='${_rcd_digest}';
		if [ "${_rcd_digest}" = "${_rcd_digest_check}" ]; then
			return 0;
		else
			return 1;
		fi;
	fi;
};

rtl_check_digest_file() {
	local	_rcdf_fname="${1}" _rcdf_digest_check="${2}" _rcdf_digest_fname="${3}"	\
		_rcdf_digest="" _rcdf_digest_file="";

	if !  [ -e "${_rcdf_digest_fname}" ]; then
		return 1;
	else	_rcdf_digest_file="$(cat "${_rcdf_digest_fname}")";
		if [ "${_rcdf_digest_file}" != "${_rcdf_digest_check}" ]\
		|| ! rtl_check_digest \$_rcdf_digest "${_rcdf_fname}"	\
				"${_rcdf_digest_check}";
		then
			return 1;
		else
			return 0;
		fi;
	fi;
};

rtl_check_path_vars() {
	local	_rcpv_rstatus="${1#\$}" _rcpv_vnames="${2}"	\
		_rcpv_rc=0 _rcpv_vname="" _rcpv_vname_val="";

	for _rcpv_vname in ${_rcpv_vnames}; do
		rtl_get_var_unsafe \$_rcpv_vname_val "${_rcpv_vname}";
		if [ "${_rcpv_vname_val:+1}" != 1 ]; then
			_rcpv_rc=1;
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' is empty or unset.';
			break;
		elif [ "${_rcpv_vname_val#* *}" != "${_rcpv_vname_val}" ]; then
			_rcpv_rc=2;
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' contains one or more whitespace characters.';
			break;
		fi;
	done;
	return "${_rcpv_rc}";
};

rtl_dirname() {
	rtl_dirname2 "${1}" "${1}";
};

rtl_dirname2() {
	local	_rdname="${1#\$}" _rdname_out="${2#\$}"	\
		_rd2_dname="";

	eval _rd2_dname="\${${_rdname}}";
	_rd2_dname="${_rd2_dname%/*}";

	case "${_rd2_dname}" in
	"")	_rd2_dname="."; ;;
	*)	while rtl_matchr "${_rd2_dname}" "*/"; do
			_rd2_dname="${_rd2_dname%/}";
		done; ;;
	esac;

	eval ${_rdname_out}='${_rd2_dname}';
	return 0;
};

rtl_exists_any() {
	local _rea_subdir="${1}"; shift;

	while [ "${#}" -gt 0 ]; do
		if [ -e "${_rea_subdir}/${1}" ]; then
			return 0;
		else
			shift;
		fi;
	done;
	return 1;
};

rtl_flock_acquire() {
	local _rfa_fd="${1}" _rfa_conflict_exit_code="${2:-253}" _rfa_wait="${3:-3600}";

	while true; do
		if flock				\
			-E "${_rfa_conflict_exit_code}"	\
			-w "${_rfa_wait}"		\
			"${_rfa_fd}";
		then
			return 0;
		elif [ "${?}" -eq "${_rfa_conflict_exit_code}" ]; then
			continue;
		else
			return "${?}";
		fi;
	done;
};

rtl_is_newer() {
	local	_ris_new_fname="${1}" _ris_old_fname="${2}"	\
		_ris_new_ts="" _ris_old_ts="";

	if ! [ -e "${_ris_old_fname}" ]; then
		return 0;
	else
		_ris_new_ts="$(stat -c %Y "${_ris_new_fname}" 2>/dev/null)";
		_ris_old_ts="$(stat -c %Y "${_ris_old_fname}" 2>/dev/null)";

		if [ "${_ris_new_ts:-0}" -gt "${_ris_old_ts:-0}" ]; then
			return 0;
		else
			return 1;
		fi;
	fi;
};

rtl_patch_files() {
	local	_rpf_patch_cwd="${1}" _rpf_strip_count="${2}" _rpf_fn="${3}" 	\
		_rpf_patch_fname="" _rpf_patch_idx=0;
	shift $((3 + 2));

	_rpf_patch_idx=1;
	while "${_rpf_fn}"				\
		\$_rpf_patch_fname "${_rpf_patch_idx}"	\
		"${@}"					\
	   && [ "${_rpf_patch_fname:+1}" = 1 ];
	do
		: $((_rpf_patch_idx += 1));
		if [ -r "${_rpf_patch_fname}" ]\
		&& ! patch				\
			-b				\
			-d "${_rpf_patch_cwd}"		\
			"-p${_rpf_strip_count}"	\
			< "${_rpf_patch_fname}";
		then
			return 1;
		fi;
	done;
	return 0;
};

# vim:filetype=sh textwidth=0