forked from openSUSE/rpmlint-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTmpFilesCheck.py
115 lines (94 loc) · 3.9 KB
/
TmpFilesCheck.py
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
# File : TmpFilesCheck.py
# Package : rpmlint
# Author : Ludwig Nussel
# Created on : Wed Sep 03 10:36 2014
# Purpose : Check systemd created tmpfiles are included in filelist
import re
import os.path
from Filter import addDetails, printWarning
import AbstractCheck
import stat
import rpm
class TmpFilesCheck(AbstractCheck.AbstractCheck):
'''Check systemd created tmpfiles are included in filelist'''
def __init__(self):
AbstractCheck.AbstractCheck.__init__(self, "TmpFilesCheck")
self._spec_file = None
def check(self, pkg):
if pkg.isSource():
return
# file names handled by systemd-tmpfiles
tmp_files = set()
postin = pkg[rpm.RPMTAG_POSTIN]
prein = pkg[rpm.RPMTAG_PREIN]
# see tmpfiles.d(5)
interesting_types = ('f', 'F', 'w', 'd', 'D', 'p', 'L', 'c', 'b')
for fn, pkgfile in pkg.files().items():
if not fn.startswith('/usr/lib/tmpfiles.d/'):
continue
if not stat.S_ISREG(pkgfile.mode):
printWarning(pkg, "tmpfile-not-regular-file", fn)
continue
basename = os.path.basename(fn)
pattern = re.compile(
r'systemd-tmpfiles --create .*%s' % re.escape(basename))
if (not postin or not pattern.search(postin)) and \
(not prein or not pattern.search(prein)):
printWarning(pkg,
'postin-without-tmpfile-creation', fn)
with open(pkgfile.path) as inputf:
for line in inputf:
# skip comments
line = line.split('#')[0].split('\n')[0]
line = line.lstrip()
if not len(line):
continue
line = re.split(r'\s+', line)
# format is
# Type Path Mode UID GID Age Argument
# we only need type and path
if len(line) < 3:
continue
t = line[0]
p = line[1]
if t.endswith('!'):
t = t[:-1]
if t not in interesting_types:
continue
tmp_files.add(p)
if p not in pkg.files():
printWarning(pkg, "tmpfile-not-in-filelist", p)
continue
if not pkg.files()[p].is_ghost:
printWarning(pkg, "tmpfile-not-ghost", p)
# now check remaining ghost files that are not already
# handled by systemd-tmpfiles
ghost_files = set(pkg.ghostFiles()) - tmp_files
if ghost_files:
for f in ghost_files:
if f in pkg.missingOkFiles():
continue
if not postin and not prein:
printWarning(pkg, 'ghost-files-without-postin')
if (not postin or f not in postin) and \
(not prein or f not in prein):
printWarning(pkg,
'postin-without-ghost-file-creation', f)
check = TmpFilesCheck()
addDetails(
'postin-without-ghost-file-creation',
'''A file tagged as ghost is not created during %prein nor during %postin.''',
'postin-without-tmpfile-creation',
'''Please use the %tmpfiles_create macro in %post for each of your
tmpfiles.d files''',
'tmpfile-not-regular-file',
'''files in tmpfiles.d need to be regular files''',
'tmpfile-not-in-filelist',
'''please add the specified file to your %files section as %ghost so
users can easily query who created the file, it gets uninstalled on
package removal and finally other rpmlint checks see it''',
'tmpfile-not-ghost',
'''the specified file is not marked as %ghost although created at
runtime via tmpfiles mechanism.'''
)
# vim: sw=4 et