Files
finit/test/tmpfiles-files.sh
T
Joachim Wiberg 575267f09b tmpfiles: add --replace, files on the command line in place of one
A package script may run before the package's tmpfiles.d file is on
disk, and gives the entries on the command line or on standard input.
Then only those entries were read, so one that the admin has changed
in /etc/tmpfiles.d was run as the package has it.

With --replace=PATH all the tmpfiles.d files are read, and the ones on
the command line are read where the file PATH would be, as in
systemd-tmpfiles:

    tmpfiles --create --replace=/usr/lib/tmpfiles.d/app.conf -

They are not read at all when a later directory has a file of that
name, as with a file that is installed.

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

70 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
# Verify which tmpfiles.d files are read, in what order, and that the
# first entry for a path is the one that counts
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/files
CONF=/tmp/files.conf
LIB=/usr/lib/tmpfiles.d
ETC=/etc/tmpfiles.d
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF $LIB/[0-9]0-test.conf $ETC/[0-9]0-test.conf"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $LIB $ETC"
run "cat > $LIB/50-test.conf" <<CONF
f $DIR/same - - - - lib
f $DIR/masked - - - - lib
CONF
run "cat > $ETC/50-test.conf" <<CONF
f $DIR/same - - - - etc
CONF
run "cat > $LIB/10-test.conf" <<CONF
f $DIR/first - - - - one
CONF
run "cat > $ETC/20-test.conf" <<CONF
f $DIR/first - - - - two
f $DIR/second - - - - two
w+ $DIR/first - - - - +three
w+ $DIR/first - - - - +four
CONF
run "/libexec/finit/tmpfiles --create --prefix=$DIR"
say 'A file in /etc is read in place of one of the same name in /usr/lib'
assert "Entry from /etc" "$(texec cat $DIR/same)" = "etc"
assert "None from /usr/lib" "$(texec sh -c "ls $DIR | grep -c masked")" = "0"
sep
say 'Files are read by name, and the first entry for a path counts'
assert "First by file name" "$(texec cat $DIR/first)" = "one+three+four"
assert "Told about in a dry run" \
"$(texec sh -c "/libexec/finit/tmpfiles -n --create --prefix=$DIR 2>&1 | grep -c 'already has an entry'")" = "1"
sep
say 'With --replace a file on the command line is read as if it was the one named'
run "cat > $CONF" <<CONF
f $DIR/replace - - - - new
f $DIR/first - - - - new
f $DIR/second - - - - new
CONF
run "rm -rf $DIR; /libexec/finit/tmpfiles --create --prefix=$DIR --replace=$LIB/15-test.conf $CONF"
assert "Read" "$(texec cat $DIR/replace)" = "new"
assert "After the files before its name" "$(texec cat $DIR/first)" = "one+three+four"
assert "Before the ones after it" "$(texec cat $DIR/second)" = "new"
assert "With the other files" "$(texec cat $DIR/same)" = "etc"
run "rm -rf $DIR; /libexec/finit/tmpfiles --create --prefix=$DIR --replace=$LIB/50-test.conf $CONF"
assert "Not when /etc has that name" "$(texec sh -c "ls $DIR | grep -c replace")" = "0"
sep
say 'A file named - is read from standard input'
run "echo 'f $DIR/stdin' | /libexec/finit/tmpfiles --create -"
assert "Entry on stdin" "$(texec sh -c "ls $DIR | grep -c stdin")" = "1"