tmpfiles: fix C entries, never copy over what exists

A C entry for a directory does the opposite of tmpfiles.d(5):

    C /etc/pam.d - - - -

When /etc/pam.d does not exist the entry fails with "Not a directory",
the files of the source are copied to a file with the name of the
path, one over the other.  When it is an empty directory nothing is
copied.  When it has files in it, they are overwritten with the ones
from /usr/share/factory, at every boot.  For a file, an existing one
is overwritten as well.

is_dir_empty() returns true for a directory that is not empty, and
rsync() from libite copies over what is there and needs a slash at the
end of a destination directory that is missing.

Copy with a tree walk of our own instead.  It copies to a path that
is missing or an empty directory, overwrites nothing, and does not
follow links.  Mode, owner, and modification time are those of the
source.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:04 +01:00
1 parent 9160ca3ddf
commit 7b3b217e60
4 files changed
+258 -29

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
#!/bin/sh
# Verify the C entry of tmpfiles, copy a file or a tree
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/copy
SRC=/tmp/copy-src
CONF=/tmp/copy.conf
content()
{
texec sh -c "cat $1 2>/dev/null || echo none"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $SRC $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $SRC/sub $DIR/empty $DIR/full/sub $DIR/merge/sub"
run "echo new > $SRC/file; echo new > $SRC/sub/file; echo new > $SRC/sub/other; ln -s file $SRC/link; chmod 640 $SRC/file"
run "echo old > $DIR/full/file; echo old > $DIR/merge/sub/file; echo old > $DIR/one-old"
run "cat > $CONF" <<CONF
C $DIR/tree - - - - $SRC
C $DIR/empty - - - - $SRC
C $DIR/full - - - - $SRC
C $DIR/one 0600 - - - $SRC/file
C $DIR/one-old - - - - $SRC/file
C $DIR/missing - - - - /no/such/path
CONF
run "/libexec/finit/tmpfiles --create $CONF"
say 'C copies a tree, links are not followed'
assert "File" "$(content $DIR/tree/file)" = "new"
assert "File in subdir" "$(content $DIR/tree/sub/file)" = "new"
assert "Mode is kept" "$(texec stat -c %a $DIR/tree/file)" = "640"
assert "Link is a link" "$(texec readlink $DIR/tree/link)" = "file"
sep
say 'C fills a directory that is empty, and leaves one that is not'
assert "Empty directory" "$(content $DIR/empty/sub/file)" = "new"
assert "Existing file" "$(content $DIR/full/file)" = "old"
assert "Nothing added" "$(content $DIR/full/sub/file)" = "none"
sep
say 'C copies a file, unless it is there'
assert "New file" "$(content $DIR/one)" = "new"
assert "Mode of entry" "$(texec stat -c %a $DIR/one)" = "600"
assert "Existing file" "$(content $DIR/one-old)" = "old"
assert "No source" "$(texec sh -c "ls $DIR | grep -c missing")" = "0"