tmpfiles.eclass: add eclass for tmpfiles processing
[gentoo.git] / eclass / tmpfiles.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: tmpfiles.eclass
6 # @MAINTAINER:
7 # Gentoo systemd project <systemd@gentoo.org>
8 # William Hubbs <williamh@gentoo.org>
9 # @AUTHOR:
10 # Mike Gilbert <floppym@gentoo.org>
11 # William Hubbs <williamh@gentoo.org>
12 # @BLURB: Functions related to tmpfiles.d files
13 # @DESCRIPTION:
14 # This eclass provides functionality related to installing and
15 # creating volatile and temporary files based on configuration files$and
16 # locations defined at this URL:
17 #
18 # https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html
19 #
20 # The dotmpfiles and newtmpfiles functions are used to install
21 # configuration files into /usr/lib/tmpfiles.d, then in pkg_postinst, the
22 # tmpfiles_process function can be called to process the newly
23 # installed tmpfiles.d entries.
24 #
25 # @EXAMPLE:
26 # Typical usage of this eclass:
27 #
28 # @CODE
29 #       EAPI=6
30 #       inherit tmpfiles
31 #
32 #       ...
33 #
34 #       src_install() {
35 #               ...
36 #               dotmpfiles "${FILESDIR}"/file1.conf "${FILESDIR}"/file2.conf
37 #               newtmpfiles "${FILESDIR}"/file3.conf-${PV} file3.conf
38 #               ...
39 #       }
40 #
41 #       pkg_postinst() {
42 #               ...
43 #               tmpfiles_process file1.conf file2.conf file3.conf
44 #               ...
45 #       }
46 #
47 # @CODE
48
49 if [[ -z ${TMPFILES_ECLASS} ]]; then
50 TMPFILES_ECLASS=1
51
52 case "${EAPI}" in
53 6) ;;
54 *) die "API is undefined for EAPI ${EAPI}" ;;
55 esac
56
57 RDEPEND="kernel_linux? ( virtual/tmpfiles )"
58
59 # @FUNCTION: dotmpfiles
60 # @USAGE: dotmpfiles <tmpfiles.d_file> ...
61 # @DESCRIPTION:
62 # Install one or more tmpfiles.d files into /usr/lib/tmpfiles.d.
63 dotmpfiles() {
64         debug-print-function "${FUNCNAME}" "$@"
65
66         use kernel_linux || return 0
67         local f
68         for f; do
69                 if [[ ${f} != *.conf ]]
70                         die "tmpfiles.d files must end with .conf"
71                 fi
72         done
73
74         (
75                 insinto /usr/lib/tmpfiles.d
76                 doins "$@"
77         )
78 }
79
80 # @FUNCTION: newtmpfiles
81 # @USAGE: newtmpfiles <old-name> <new-name>.conf
82 # @DESCRIPTION:
83 # Install a tmpfiles.d file in /usr/lib/tmpfiles.d under a new name.
84 newtmpfiles() {
85         debug-print-function "${FUNCNAME}" "$@"
86
87         use kernel_linux || return 0
88         if [[ $2 != *.conf ]]; then
89                 die "tmpfiles.d files must end with .conf"
90         fi
91
92         (
93                 insinto /usr/lib/tmpfiles.d
94                 newins "$@"
95         )
96 }
97
98 # @FUNCTION: tmpfiles_process
99 # @USAGE: tmpfiles_process <filename> <filename> ...
100 # @DESCRIPTION:
101 # Call a tmpfiles.d implementation to create new volatile and temporary
102 # files and directories.
103 tmpfiles_process() {
104         debug-print-function "${FUNCNAME}" "$@"
105
106         use kernel_linux || return 0
107         [[ ${EBUILD_PHASE} == postinst ]] || die "${FUNCNAME}: Only valid in pkg_postinst"
108         [[ ${#} -gt 0 ]] || die "${FUNCNAME}: Must specify at least one filename"
109
110         # Only process tmpfiles for the currently running system
111         [[ ${ROOT} == / ]] || return 0
112
113         if type systemd-tmpfiles &> /dev/null; then
114                 systemd-tmpfiles --create "$@"
115         elif type opentmpfiles &> /dev/null; then
116                 opentmpfiles --create "$@"
117         fi
118         if [[ $? -ne 0 ]]; then
119                 ewarn "The tmpfiles processor exited with a non-zero exit code"
120         fi
121 }
122
123 fi