1#!/bin/bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3#
4# Tests for configuration directory and file precedences
5#
6set -eux
7
8rm -f  /{usr/lib,etc}/tmpfiles.d/{L,w}-*.conf
9rm -fr /tmp/precedence/{L,w}
10
11mkdir -p /{usr/lib,etc}/tmpfiles.d
12mkdir -p /tmp/precedence/{L,w}
13
14#
15# 'L'
16#
17ln -s /dev/null /tmp/precedence/L
18
19# Overwrite the existing symlink
20cat >/usr/lib/tmpfiles.d/L-z.conf<<EOF
21L+ /tmp/precedence/L - - - - /usr/lib/tmpfiles.d/L-z.conf
22EOF
23
24systemd-tmpfiles --create
25test "$(readlink /tmp/precedence/L)" = "/usr/lib/tmpfiles.d/L-z.conf"
26
27# Files in /etc should override those in /usr
28cat >/etc/tmpfiles.d/L-z.conf<<EOF
29L+ /tmp/precedence/L - - - - /etc/tmpfiles.d/L-z.conf
30EOF
31
32systemd-tmpfiles --create
33test "$(readlink /tmp/precedence/L)" = "/etc/tmpfiles.d/L-z.conf"
34
35# /usr/…/L-a.conf has higher prio than /etc/…/L-z.conf
36cat >/usr/lib/tmpfiles.d/L-a.conf<<EOF
37L+ /tmp/precedence/L - - - - /usr/lib/tmpfiles.d/L-a.conf
38EOF
39
40systemd-tmpfiles --create
41test "$(readlink /tmp/precedence/L)" = "/usr/lib/tmpfiles.d/L-a.conf"
42
43# Files in /etc should override those in /usr
44cat >/etc/tmpfiles.d/L-a.conf<<EOF
45L+ /tmp/precedence/L - - - - /etc/tmpfiles.d/L-a.conf
46EOF
47
48systemd-tmpfiles --create
49test "$(readlink /tmp/precedence/L)" = "/etc/tmpfiles.d/L-a.conf"
50
51#
52# 'w'
53#
54touch /tmp/precedence/w/f
55
56# Multiple configuration files specifying 'w+' for the same path is allowed.
57for i in a c; do
58    cat >/usr/lib/tmpfiles.d/w-$i.conf<<EOF
59w+ /tmp/precedence/w/f - - - - /usr/lib/tmpfiles.d/w-$i.conf\n
60EOF
61    cat >/etc/tmpfiles.d/w-$i.conf<<EOF
62w+ /tmp/precedence/w/f - - - - /etc/tmpfiles.d/w-$i.conf\n
63EOF
64done
65
66cat >/usr/lib/tmpfiles.d/w-b.conf<<EOF
67w+ /tmp/precedence/w/f - - - - /usr/lib/tmpfiles.d/w-b.conf\n
68EOF
69
70systemd-tmpfiles --create
71cmp /tmp/precedence/w/f <<EOF
72/etc/tmpfiles.d/w-a.conf
73/usr/lib/tmpfiles.d/w-b.conf
74/etc/tmpfiles.d/w-c.conf
75EOF
76