summaryrefslogtreecommitdiffstats
path: root/dh_installtmpfiles
blob: de684b9b085cd53330191f880ca2247b5ebbc808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl -w

=head1 NAME

dh_installtmpfiles - install tmpfiles.d configuration files

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
use File::Find;

our $VERSION = DH_BUILTIN_VERSION;

=head1 SYNOPSIS

B<dh_installtmpfiles> [S<I<debhelper options>>][B<--name=>I<name>]

=head1 DESCRIPTION

B<dh_installtmpfiles> is a debhelper program that is responsible for
installing package maintainer supplied tmpfiles.d configuration files
(e.g. for systemd-tmpfiles).

It also finds the tmpfiles.d configuration files installed by a package
and generates F<postinst> code blocks for activating the tmpfiles.d
configuration when the package is installed. These snippets are added
to the maintainer scripts by L<dh_installdeb(1)>.

In compat 14+, tmpfiles.d files are copied into the F<postrm> script, and
they are used with systemd-tmpfiles --remove after the package is removed
and --purge when the package is purged. This allows one to use the
tmpfiles.d mechanism to clean up files that are no longer needed after a
package has been removed/purged.


=head1 OPTIONS

=over 4

=item B<--name=>I<name>

This option controls both a prefix used for looking up maintainer provided
tmpfiles.d configuration files (those mentioned in the L</FILES> section)
and also the base name used for the installed version of the file.

=back

=head1 FILES

=over 4

=item debian/I<package>.tmpfiles

If this exists, it is installed into F<usr/lib/tmpfiles.d/> in the
package build directory. Note that the C<tmpfiles.d> mechanism is
currently only used by systemd.

=item debian/I<package>.tmpfile

Deprecated name for debian/I<package>.tmpfiles.

=back

=head1 NOTES

This command is not idempotent. L<dh_prep(1)> should be called between
invocations of this command (with the same arguments). Otherwise, it
may cause multiple instances of the same text to be added to
maintainer scripts.

=cut

init();

sub uniq {
	my %seen;
	return grep { !$seen{$_}++ } @_;
}

# PROMISE: DH NOOP WITHOUT tmp(usr/lib/tmpfiles.d) tmp(etc/tmpfiles.d) pkgfile(tmpfiles) pkgfile(tmpfile) cli-options()

# Install package maintainer supplied tmpfiles files
foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmpdir = tmpdir($package);
	my $tmpfile = pkgfile({'named' => 1}, $package, 'tmpfiles');
	my $name = $dh{NAME} // $package;
	my $old_tmpfile = pkgfile({'named' => 1}, $package, 'tmpfile');
	my $dir;
	if (not $tmpfile) {
		my $new_name;
		next if not $old_tmpfile;
		$tmpfile = $old_tmpfile;
		$new_name = $old_tmpfile;
		$new_name =~ s{^(.+[./])tmpfile(\..+|)$}{$1tmpfiles$2};
		warning("The name $tmpfile is deprecated; please use $new_name instead");
		warning(qq{Possible fix: mv -f "${tmpfile}" "${new_name}"});
	} elsif ($old_tmpfile) {
		warning("There is both a $tmpfile and a $old_tmpfile that is relevant for this package!?");
		warning(qq{Possible fix: rm -f "${old_tmpfile}"});
		error("Aborting; Please resolve the ambiguity between ${tmpfile} and ${old_tmpfile}.");
	}

	$dir = "$tmpdir/usr/lib/tmpfiles.d";
	install_dir($dir);
	install_file($tmpfile, "${dir}/${name}.conf");
}

# Add postinst code blocks to handle tmpfiles
foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmpdir = tmpdir($package);
	my @tmpfiles;
	my @purge;
	my @remove;

	my @dirs = grep { -d } map { "${tmpdir}/$_" } qw(usr/lib/tmpfiles.d etc/tmpfiles.d);

	find({
		wanted => sub {
			my $lines;
			my $name = $File::Find::name;
			return if not -f $name or not $name =~ m{[.]conf$};
			push(@tmpfiles, basename($name));

			# We copy the needed lines to the postrm, as the tmpfiles.d files
			# will be gone by the time the remove/purge postscript is run.
			if (! compat(13) && (! $dh{NO_PURGE} || ! $dh{NO_REMOVE})) {
				open(my $fd, '<', $name) or error("open($name) failed: $!");
				{
					local $/;
					$lines = <$fd>;
				}
				close($fd);

				chomp $lines;

				if ($lines) {
					push(@purge, $lines);
					push(@remove, $lines);
				}
			}
		},
		no_chdir => 1,
	}, @dirs) if @dirs;

	if (@tmpfiles) {
		autoscript($package, 'postinst', 'postinst-init-tmpfiles', { 'TMPFILES' => join(' ', uniq(sort(@tmpfiles))) });
	}

	if (@remove) {
		autoscript($package, 'postrm', 'postrm-init-tmpfiles-remove', {
			'TMPFILES_REMOVE' => join("\n", @remove),
		});
	}

	if (@purge) {
		autoscript($package, 'postrm', 'postrm-init-tmpfiles-purge', {
			'TMPFILES_PURGE' => join("\n", @purge),
		});
	}
}

=head1 SEE ALSO

L<debhelper(7)>

=cut