kumquat-buildroot/package/fakedate/fakedate
Yann E. MORIN 0fe536b797 package/fakedate: avoid recursion if not first in the PATH
With BR2_REPRODUCIBLE, our 'fakedate' wrapper script will end up in
host/bin/date. Currently, it iterates over all the 'date' found in PATH,
until it finds one that is not the script itself, in an attempt to avoid
infinite recursion by calling itself again and again.

This heuristic works OK in Buildroot, because host/bin/ is first in the
PATH, and so that means the first entry in the PATH is skipped.

However, this is going to fail as soon as our wrapper is not the first
in the PATH. Indeed, in that situation, the current heuristic will stop
on the first 'date' in the PATH, as it is not the script itself, and
since our script was executed, that probably means the first 'date' was
itself a wrapper that ended up calling us. So, calling it again will
eventually trickle to calling us again, and thus creating the loop our
heuristic was made to avoid.

This situation currently does not occur in Buildroot, because host/bin/
is first, *and* we have no package that provide their own 'date' wrapper
during their build steps.

But when we generate an SDK with BR2_REPRODUCIBLE, then our wrapper
script will be in sdk/bin/, and there is no longer any guarantee this
comes first in the PATH, thus opening the possibility that another
buildsystem based on our SDK, but which has its own 'date' wrapper, will
trigger this infinite recursion.

We fix that by iterating, in reverse order, over all the 'date' we can
find in PATH, and when we find ourselves, then we know the one we found
in the iteration just before is the one that we should call.

'which -a' is old enough that we can expect it to be always available;
it has been present at least since Debian Squeeze, released 2011.

Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2021-08-28 15:56:52 +02:00

50 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# vim: set sw=4 expandtab:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Copyright (C) 2016 Jérôme Pouiller <jezz@sysmic.org>
#
DATE_BIN=false
# Do not call any 'date' before us in the PATH, or that would create
# an infinite recursion.
for date in $(which -a date |tac); do
if [ "${date}" -ef "$0" ]; then
break
fi
DATE_BIN="${date}"
done
if [ -n "$SOURCE_DATE_EPOCH" ]; then
FORCE_EPOCH=1
for i in "$@"; do
# Use of --date, --file and --reference (and their short option counter
# parts) is incompatible with SOURCE_DATE_EPOCH.
# -u and -R are the only short options without argument. So they could
# appear between '-' and option we want to match.
if echo "$i" | grep -qE '^-([uR]*d|-date|[uR]*f|-file|[uR]*r|--reference)'; then
FORCE_EPOCH=0
break;
fi
done
if [ $FORCE_EPOCH -eq 1 ]; then
echo "date: Warning: using \$SOURCE_DATE_EPOCH instead of true time" >&2
ARGS="-d @$SOURCE_DATE_EPOCH"
fi
fi
exec $DATE_BIN $ARGS "$@"