1#!/bin/bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3#
4# Basic tests for types creating directories
5set -eux
6set -o pipefail
7
8rm -fr /tmp/{C,d,D,e}
9mkdir  /tmp/{C,d,D,e}
10
11#
12# 'd'
13#
14mkdir /tmp/d/2
15chmod 777 /tmp/d/2
16
17systemd-tmpfiles --create - <<EOF
18d     /tmp/d/1    0755 daemon daemon - -
19d     /tmp/d/2    0755 daemon daemon - -
20EOF
21
22test -d /tmp/d/1
23test "$(stat -c %U:%G:%a /tmp/d/1)" = "daemon:daemon:755"
24
25test -d /tmp/d/2
26test "$(stat -c %U:%G:%a /tmp/d/2)" = "daemon:daemon:755"
27
28#
29# 'D'
30#
31mkdir /tmp/D/2
32chmod 777 /tmp/D/2
33touch /tmp/D/2/foo
34
35systemd-tmpfiles --create - <<EOF
36D     /tmp/D/1    0755 daemon daemon - -
37D     /tmp/D/2    0755 daemon daemon - -
38EOF
39
40test -d /tmp/D/1
41test "$(stat -c %U:%G:%a /tmp/D/1)" = "daemon:daemon:755"
42
43test -d /tmp/D/2
44test "$(stat -c %U:%G:%a /tmp/D/2)" = "daemon:daemon:755"
45
46systemd-tmpfiles --remove - <<EOF
47D     /tmp/D/2    0755 daemon daemon - -
48EOF
49
50# the content of '2' should be removed
51test "$(echo /tmp/D/2/*)" = "/tmp/D/2/*"
52
53#
54# 'e'
55#
56mkdir -p /tmp/e/2/{d1,d2}
57chmod 777 /tmp/e/2
58chmod 777 /tmp/e/2/d*
59
60systemd-tmpfiles --create - <<EOF
61e     /tmp/e/1     0755 daemon daemon - -
62e     /tmp/e/2/*   0755 daemon daemon - -
63EOF
64
65test ! -d /tmp/e/1
66
67test -d /tmp/e/2
68test "$(stat -c %U:%G:%a /tmp/e/2)" = "root:root:777"
69
70test -d /tmp/e/2/d1
71test "$(stat -c %U:%G:%a /tmp/e/2/d1)" = "daemon:daemon:755"
72test -d /tmp/e/2/d2
73test "$(stat -c %U:%G:%a /tmp/e/2/d2)" = "daemon:daemon:755"
74
75# 'e' operates on directories only
76mkdir -p /tmp/e/3/{d1,d2}
77chmod 777 /tmp/e/3
78chmod 777 /tmp/e/3/d*
79touch /tmp/e/3/f1
80chmod 644 /tmp/e/3/f1
81
82systemd-tmpfiles --create - <<EOF
83e     /tmp/e/3/*   0755 daemon daemon - -
84EOF
85
86# the directories should have been processed although systemd-tmpfiles failed
87# previously due to the presence of a file.
88test -d /tmp/e/3/d1
89test "$(stat -c %U:%G:%a /tmp/e/3/d1)" = "daemon:daemon:755"
90test -d /tmp/e/3/d2
91test "$(stat -c %U:%G:%a /tmp/e/3/d2)" = "daemon:daemon:755"
92
93test -f /tmp/e/3/f1
94test "$(stat -c %U:%G:%a /tmp/e/3/f1)" = "root:root:644"
95
96#
97# 'C'
98#
99
100mkdir /tmp/C/{1,2,3}-origin
101touch /tmp/C/{1,2,3}-origin/f1
102chmod 755 /tmp/C/{1,2,3}-origin/f1
103
104mkdir /tmp/C/{2,3}
105touch /tmp/C/3/f1
106
107systemd-tmpfiles --create - <<EOF
108C     /tmp/C/1    0755 daemon daemon - /tmp/C/1-origin
109C     /tmp/C/2    0755 daemon daemon - /tmp/C/2-origin
110EOF
111
112test -d /tmp/C/1
113test "$(stat -c %U:%G:%a /tmp/C/1/f1)" = "daemon:daemon:755"
114test -d /tmp/C/2
115test "$(stat -c %U:%G:%a /tmp/C/2/f1)" = "daemon:daemon:755"
116
117systemd-tmpfiles --create - <<EOF
118C     /tmp/C/3    0755 daemon daemon - /tmp/C/3-origin
119EOF
120
121test "$(stat -c %U:%G:%a /tmp/C/3/f1)" = "root:root:644"
122