Files
finit/test/tmpfiles-arg.sh
T
Joachim Wiberg dd186287d7 tmpfiles: read the files by name, the first entry for a path counts
The files were read one directory at a time, Finit's own first and
/etc/tmpfiles.d last, and every entry for a path was run.  With two
entries for a path the last one read won, so an entry in /etc won over
one in /usr/lib whatever the files were called.

tmpfiles.d(5) reads the files in the order of their names, whatever
directory they are in, and goes by the first entry that makes a path.
A package relies on that when it names its file to sort before or
after another.  Do the same.  As before, a file in a later directory
is read in place of one of the same name in an earlier one.

A later entry is skipped when it makes a path an earlier one makes, or
when both take a glob and remove, write to, or keep the path.  Several
w+ entries may add to one file.  The skip is only told about with
--dry-run or --debug, since Finit's files and those of other packages
overlap on most systems.

Finit's own files had two entries for some paths, and relied on the
last one.  Drop the ones that lost: /run/lock, /run/lock/subsys, and
/var/lock in legacy.conf, and /var/run and a second /var/spool in
var.conf.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-21 19:23:49 +01:00

132 lines
4.5 KiB
Bash
Executable File

#!/bin/sh
# Verify how tmpfiles reads the argument, the last field of an entry
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/arg
CONF=/tmp/arg.conf
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "cat > $CONF" <<CONF
d $DIR - - - - -
L $DIR/aligned - - - - /dev/null
f $DIR/dash - - - - -
f $DIR/text - - - - two words
f^ $DIR/cred - - - - some.credential
CONF
run "/libexec/finit/tmpfiles --create $CONF"
say 'Space between the age and the argument is not part of the argument'
assert "Link target" "$(texec readlink $DIR/aligned)" = "/dev/null"
assert "File content" "$(texec cat $DIR/text)" = "two words"
say 'A lone - is no argument'
assert "Empty file" "$(texec stat -c %s $DIR/dash)" = "0"
say 'An entry that takes its argument from a credential is skipped'
assert "No file" "$(texec sh -c "ls $DIR | grep -c cred")" = "0"
sep
say 'L? only makes the link when what it points to is there'
run "cat > $CONF" <<CONF
L? $DIR/there - - - - /etc/passwd
L? $DIR/missing - - - - /no/such/file
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "Link to a path that is there" "$(texec readlink $DIR/there)" = "/etc/passwd"
assert "No link to a missing path" "$(texec sh -c "ls $DIR | grep -c missing")" = "0"
sep
say 'With --prefix only the entries below it are run'
run "cat > $CONF" <<CONF
f $DIR/in/file
f $DIR/out/file
f $DIR/input
CONF
run "/libexec/finit/tmpfiles --create --prefix=$DIR/in $CONF"
assert "Only below the prefix" "$(texec sh -c "ls -d $DIR/in* $DIR/out* 2>/dev/null" | tr '\n' ' ')" = "$DIR/in "
sep
say 'w writes all of its argument to every path a glob matches'
run "touch $DIR/w1 $DIR/w2"
run "cat > $CONF" <<'CONF'
w /tmp/arg/w? - - - - one\ntwo
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "First match" "$(texec cat $DIR/w1 | tr '\n' ' ')" = "one two"
assert "Second match" "$(texec cat $DIR/w2 | tr '\n' ' ')" = "one two"
sep
say 'w writes over the start of what is there, w+ adds to the end'
run "printf abcdef > $DIR/w1"
run "cat > $CONF" <<'CONF'
w /tmp/arg/w1 - - - - XY
w+ /tmp/arg/w1 - - - - Z
w+ /tmp/arg/w1 - - - - Z
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "Written in place, added to by each w+" "$(texec cat $DIR/w1)" = "XYcdefZZ"
sep
say 'Escapes, and a # that is not at the start of the line'
run "cat > $CONF" <<'CONF'
# A comment
# and one more
f /tmp/arg/hex - - - - \x39\x41\x4a\x6b
f /tmp/arg/hash - - - - a#b c
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "All hex digits" "$(texec cat $DIR/hex)" = "9AJk"
assert "No comment in line" "$(texec cat $DIR/hash)" = "a#b c"
sep
say 'A path can be quoted, and have escapes'
run "cat > $CONF" <<'CONF'
f "/tmp/arg/with space" - - - - double
f '/tmp/arg/single quote' - - - - single
f /tmp/arg/mid"dle sp"ace - - - - middle
f /tmp/arg/esc\x41\sped - - - - "kept as is"
f "/tmp/arg/open - - - - quote
f tmp/arg/relative - - - - path
CONF
run "cd /; /libexec/finit/tmpfiles --create $CONF"
assert "Double quotes" "$(texec cat "$DIR/with space")" = "double"
assert "Single quotes" "$(texec cat "$DIR/single quote")" = "single"
assert "Quotes in path" "$(texec cat "$DIR/middle space")" = "middle"
assert "Escapes in path" "$(texec cat "$DIR/escA ped")" = '"kept as is"'
assert "Skipped with a quote left open" "$(texec sh -c "ls $DIR | grep -c open")" = "0"
assert "Skipped when not absolute" "$(texec sh -c "ls $DIR | grep -c relative")" = "0"
sep
say 'With ~ the argument is Base64'
run "printf abcdef > $DIR/w1"
run "cat > $CONF" <<'CONF'
f~ /tmp/arg/b64 - - - - aGVsbG8gJXQK
f~ /tmp/arg/b64pad - - - - aGk=
f~ /tmp/arg/b64bad - - - - a!!!
w~ /tmp/arg/w1 - - - - WFk=
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "Decoded, no specifiers" "$(texec cat $DIR/b64)" = "hello %t"
assert "Padded" "$(texec cat $DIR/b64pad)" = "hi"
assert "Written with w" "$(texec cat $DIR/w1)" = "XYcdef"
assert "Skipped when not Base64" "$(texec sh -c "ls $DIR | grep -c b64bad")" = "0"
sep
say '--cat-config shows the files that would be read'
assert "Files with their name" "$(texec /libexec/finit/tmpfiles --cat-config $CONF | head -2 | tr '\n' ' ')" = "# $CONF f~ /tmp/arg/b64 - - - - aGVsbG8gJXQK "
sep
say '--graceful is taken, and changes nothing'
run "rm $DIR/b64; /libexec/finit/tmpfiles --create --graceful $CONF"
assert "Runs with --graceful" "$(texec cat $DIR/b64)" = "hello %t"