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