summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-10 06:12:15 +0100
committerSam James <sam@gentoo.org>2024-08-11 11:11:03 +0100
commit6cf0940b8d336eb35a970af2ffc819f55e3ab429 (patch)
tree6b034ac5dd1dc1d9e3eca852c31645fd52cba394 /functions
parenttest-functions: comment as to the implications of test_local() failing (diff)
downloadgentoo-functions-6cf0940b8d336eb35a970af2ffc819f55e3ab429.tar.gz
gentoo-functions-6cf0940b8d336eb35a970af2ffc819f55e3ab429.tar.bz2
gentoo-functions-6cf0940b8d336eb35a970af2ffc819f55e3ab429.zip
Use the -nt and -ot test primaries again rather than depend on GNU find
As regards the test(1) utility, the POSIX.1-2024 specification defines the -nt and -ot primaries as standard features. Given that the specification in question was only recently published, this would not normally be an adequate reason for using them in gentoo-functions, in and as of itself. However, I was already aware that the these primaries are commonly implemented and have been so for years. So, I decided to evaluate a number of shells and see how things stand now. Here is a list of the ones that I tested: - ash (busybox 1.36.1) - dash 0.5.12 - bash 5.2.26 - ksh 93u+ - loksh 7.5 - mksh 59c - oksh 7.5 - sh (FreeBSD 14.1) - sh (NetBSD 10.0) - sh (OpenBSD 7.5) - yash 2.56.1 Of these, bash, ksh93, loksh, mksh, oksh, OpenBSD sh and yash appear to conform with the POSIX-1.2024 specification. The remaining four fail to conform in one particular respect, which is as follows. $ touch existent $ set -- existent nonexistent $ [ "$1" -nt "$2" ]; echo "$?" # should be 0 1 $ [ "$2" -ot "$1" ]; echo "$?" # should be 0 1 To address this, I discerned a reasonably straightforward workaround that involves testing both whether the file under consideration exists and whether the variable keeping track of the newest/oldest file has yet been assigned to. As far as I am concerned, the coverage is more than adequate for both primaries to be used by gentoo-functions. As such, this commit adjusts the following three functions so as to do exactly that. - is_older_than() - newest() - oldest() It also removes the following functions, since they are no longer used. - _find0() - _select_by_mtime() With this, GNU findutils is no longer a required runtime dependency. Of course, should a newly introduced feature of gentoo-functions benefit from the presence of findutils in the future, there is no reason that it cannot be brought back in that capacity. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'functions')
-rw-r--r--functions/rc.sh25
1 files changed, 20 insertions, 5 deletions
diff --git a/functions/rc.sh b/functions/rc.sh
index 0c14035..4eff3c8 100644
--- a/functions/rc.sh
+++ b/functions/rc.sh
@@ -1,6 +1,6 @@
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
-# shellcheck shell=sh disable=3043
+# shellcheck shell=sh disable=3013,3043
# This file contains alternative implementations for some of the functions and
# utilities provided by OpenRC. Please refer to ../functions.sh for coding
@@ -205,9 +205,13 @@ get_bootparam()
# Takes the first parameter as a reference file/directory then determines
# whether any of the following parameters refer to newer files/directories.
#
+# The test utility is required to support the -nt primary, per POSIX-1.2024.
+# However, measures are in place to to achieve compatibility with shells that
+# implement the primary without yet fully adhering to the specification.
+#
is_older_than()
{
- local ref
+ local path ref
if [ "$#" -eq 0 ]; then
warn "is_older_than: too few arguments (got $#, expected at least 1)"
@@ -218,9 +222,20 @@ is_older_than()
ref=
fi
shift
- { test "$#" -gt 0 && printf '%s\0' "$@"; } \
- | _find0 -L ${ref:+-newermm} ${ref:+"${ref}"} -printf '\n' -quit \
- | read -r _
+ for path; do
+ # The first branch addresses a conformance issue whereby
+ # [ existent -nt nonexistent ] is incorrectly false. As of
+ # August 2024, busybox ash, dash, FreeBSD sh and NetBSD sh are
+ # known to be non-conforming in this respect.
+ if [ ! "${ref}" ] && [ -e "${path}" ]; then
+ return
+ elif [ "${path}" -nt "${ref}" ]; then
+ return
+ elif [ -d "${path}" ] && is_older_than "${ref}" "${path}"/*; then
+ return
+ fi
+ done
+ false
}
#