summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_state.subr
blob: 3deec5999cfee0527307fc52673a21162148748b (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
#
# 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_state_clear() - clear state for item
# @_workdir:	pathname to directory containing state files
# @_item_name:	name of item to clear state for
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_state_clear() {
	local	_rsc_workdir="${1}" _rsc_item_name="${2}"	\
		_rsc_item_fname="";

	for _rsc_item_fname in $(
		find "${_rsc_workdir}"			\
			-maxdepth 1			\
			-mindepth 1			\
			-name .${_rsc_item_name}.\*	\
			-type f);
	do
		rtl_fileop rm "${_rsc_item_fname}";
	done;
	return 0;
};

#
# rtl_state_set() - set state for item
# @_workdir:	pathname to directory containing state files
# @_item_name:	name of item to set state for
# @_state:	state to set
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_state_set() {
	local	_rss_workdir="${1}" _rss_item_name="${2}" _rss_state="${3}"	\
		_rss_done_name_pfx="${1}/.${2}";
	shift 3;

	rtl_fileop touch "${_rss_done_name_pfx}.${_rss_state}";
	while [ ${#} -ge 1 ]; do
		if [ "${#1}" -gt 0 ]; then
			rtl_fileop rm "${_rss_done_name_pfx}.${1}";
		fi; shift;
	done;
	return 0;
};

#
# rtl_state_test() - test state(s) for item
# @_workdir:	pathname to directory containing state files
# @_item_name:	name of item to set state for
# @_states:	state(s) to test for
# @[_lforce]:	one of "ALL" (force all states to test as set,) "LAST" or "" (test all states,)
#		or exclusive list of states to force to test as set
#
# Returns:	zero (0) if any of state(s) set, non-zero (>0) if none of state(s) set
#
rtl_state_test() {
	local	_rst_workdir="${1}" _rst_item_name="${2}"	\
		_rst_states="${3}" _rst_lforce="${4:-}"	\
		_rst_state="" _rst_done_fname="" IFS="," _rst_rc=0;

	for _rst_state in ${_rst_states}; do
		_rst_done_fname="${_rst_workdir}/.${_rst_item_name}.${_rst_state}";

		case "${_rst_lforce}" in
		ALL)
			_rst_rc=1; ;;
		""|LAST)
			rtl_fileop test "${_rst_done_fname}"; _rst_rc="${?}"; ;;
		*)
			rtl_lmatch \$_rst_lforce "${_rst_state}" ",";
			_rst_rc=$((${?} ? 0 : 1));
			;;
		esac;

		if [ "${_rst_rc}" -eq 0 ]; then
			break;
		fi;
	done;
	return "${_rst_rc}";
};

# vim:filetype=sh textwidth=0