summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_filepath.subr
blob: a2f0f5c4a1cee4b6e37cb1fb2a176e05c6087dae (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# 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() - obtain base name from filename
# @_rfname:	inout reference to filename
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_basename() {
	rtl_basename2 "${1}" "${1}";
};

#
# rtl_basename2() - obtain base name from filename
# @_rfname:	in reference to filename
# @_rfname_out:	out reference to new filename
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
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() - check digest of single file
# @_rdigest:		out reference to digest of file
# @_fname:		name of file to check
# @_digest_check:	digest to check against
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
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() - check digest of single file w/ digest file
# @_fname:		name of file to check
# @_digest_check:	digest to check against
# @_digest_fname:	name of file containing digest
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
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() - check pathname variables for validity
# @_rstatus:	out reference to status string
# @_vnames:	list of variable names
#
# Returns:	zero (0) on success, non-zero (>0) on empty or unset pathname variable or pathname variable containing whitespace characters
#
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() - obtain directory name from filename
# @_rfname:	inout reference to {file,directory} name
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_dirname() {
	rtl_dirname2 "${1}" "${1}";
};

#
# rtl_dirname2() - obtain directory name from filename
# @_rfname:	in reference to filename
# @_rfname_out:	out reference to directory name
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_dirname2() {
	local	_rfname="${1#\$}" _rdname_out="${2#\$}"	\
		_rd2_dname="";

	eval _rd2_dname="\${${_rfname}}";
	_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() - check for existence of pathnames beneath directory
# @_subdir:	single directory name
# @...:		list of pathnames to check
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
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() - acquire file lock
# @_fd:				single file descriptor
# @_conflict_exit_code:		exit code on conflict
# @_wait:			wait period in seconds
#
# Returns:			zero (0) on success, non-zero (>0) on failure
#
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() - check if single file is newer than other single file
# @_new_fname:	single name of newer file
# @_old_fname:	single name of older file
#
# Returns:	zero (0) if @_new_fname is newer, non-zero (>0) if @_old_fname is newer
#
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() - patch files
# @_patch_cwd:		patch(1) -d directory
# @_strip_count:	patch(1) strip count
# @_fn:			name of function that produces patch filenames and takes the arguments @_rpatch_fname @_patch_idx @...
# @...			optional arguments to pass to @_fn
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
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;
};

#
# rtl_set_perms_treeV() - set mode bits of directories and files
# @_mode_dir:		mode bits for directories
# @_mode_file_exec:	mode bits for executable files
# @_mode_file_nonexec:	mode bits for non-executable files
# @...:			list of base directory pathnames as positional parameters; empty strings are ignored
#
# Returns:		zero (0) on success, non-zero (>0) on failure
#
rtl_set_perms_treeV() {
	local	_rspt_mode_dir="${1}" _rspt_mode_file_exec="${2}" _rspt_mode_file_nonexec="${3}"	\
		_rspt_destdir="" _rspt_fname=""								\
		IFS IFS0="${IFS}";
	shift 3;

	for _rspt_destdir in "${@}"; do
		if [ -e "${_rspt_destdir}" ]; then
			rtl_set_IFS_nl;

			for _rspt_fname in $(find "${_rspt_destdir}" -type d); do
				if ! rtl_fileop chmod "${_rspt_mode_dir}"	\
						"${_rspt_fname}";
				then
					return 1;
				fi;
			done;

			for _rspt_fname in $(find		\
					"${_rspt_destdir}"	\
					\( -not -perm /0111 \)	\
					-type f);
			do
				if ! rtl_fileop chmod "${_rspt_mode_file_nonexec}"	\
						"${_rspt_fname}";
				then
					return 1;
				fi;
			done;

			for _rspt_fname in $(find		\
					"${_rspt_destdir}"	\
					-perm /0111		\
					-type f);
			do
				if ! rtl_fileop chmod "${_rspt_mode_file_exec}"	\
						"${_rspt_fname}";
				then
					return 1;
				fi;
			done;

			IFS="${IFS0}";
		fi;
	done;
	return 0;
};

# vim:filetype=sh textwidth=0