forked from piastry/cifs-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkopts
executable file
·253 lines (212 loc) · 7.16 KB
/
checkopts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
#
# Script to check for inconsistencies between documented mount options
# and implemented kernel options.
# Copyright (C) 2018 Aurelien Aptel ([email protected])
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import re
import subprocess
import argparse
from pprint import pprint as P
def extract_canonical_opts(s):
"""
Return list of option names present in s.
e.g "opt1=a|opt2=d" => ["opt1", "opt2"])
"""
opts = s.split("|")
res = []
for o in opts:
x = o.split("=")
res.append(x[0])
return res
def extract_kernel_opts(fn):
STATE_BASE = 0
STATE_DEF = 1
STATE_USE = 2
STATE_EXIT = 3
state = STATE_BASE
fmt2enum = {}
enum2code = {}
code = ''
current_opt = ''
rx = RX()
def code_add(s):
if current_opt != '':
if current_opt not in enum2code:
enum2code[current_opt] = ''
enum2code[current_opt] += s
with open(fn) as f:
for s in f.readlines():
if state == STATE_EXIT:
break
elif state == STATE_BASE:
if rx.search(r'cifs_mount_option_tokens.*\{', s):
state = STATE_DEF
elif rx.search(r'^cifs_parse_mount_options', s):
state = STATE_USE
elif state == STATE_DEF:
if rx.search(r'(Opt_[a-zA-Z0-9_]+)\s*,\s*"([^"]+)"', s):
fmt = rx.group(2)
opts = extract_canonical_opts(fmt)
assert(len(opts) == 1)
name = opts[0]
fmt2enum[name] = {'enum':rx.group(1), 'fmt':fmt}
elif rx.search(r'^};', s):
state = STATE_BASE
elif state == STATE_USE:
if rx.search(r'^\s*case (Opt_[a-zA-Z0-9_]+)', s):
current_opt = rx.group(1)
elif current_opt != '' and rx.search(r'^\s*default:', s):
state = STATE_EXIT
else:
code_add(s)
return fmt2enum, enum2code
def chomp(s):
if s[-1] == '\n':
return s[:-1]
return s
def extract_man_opts(fn):
STATE_EXIT = 0
STATE_BASE = 1
STATE_OPT = 2
state = STATE_BASE
rx = RX()
opts = {}
ln = 0
with open(fn) as f:
for s in f.readlines():
ln += 1
if state == STATE_EXIT:
break
elif state == STATE_BASE:
if rx.search(r'^OPTION', s):
state = STATE_OPT
elif state == STATE_OPT:
if rx.search('^[a-z]', s) and len(s) < 50:
s = chomp(s)
names = extract_canonical_opts(s)
for name in names:
if name not in opts:
opts[name] = []
opts[name].append({'ln':ln, 'fmt':s})
elif rx.search(r'^[A-Z]+', s):
state = STATE_EXIT
return opts
def format_code(s):
# remove common indent in the block
min_indent = None
for ln in s.split("\n"):
indent = 0
for c in ln:
if c == '\t': indent += 1
else: break
if min_indent is None:
min_indent = indent
elif indent > 0:
min_indent = min(indent, min_indent)
out = ''
lines = s.split("\n")
if lines[-1].strip() == '':
lines.pop()
for ln in lines:
out += "| %s\n" % ln[min_indent:]
return out
def sortedset(s):
return sorted(list(s), key=lambda x: re.sub('^no', '', x))
def opt_neg(opt):
if opt.startswith("no"):
return opt[2:]
else:
return "no"+opt
def main():
ap = argparse.ArgumentParser(description="Cross-check mount options from cifs.ko/man page")
ap.add_argument("cfile", help="path to connect.c")
ap.add_argument("rstfile", help="path to mount.cifs.rst")
args = ap.parse_args()
fmt2enum, enum2code = extract_kernel_opts(args.cfile)
manopts = extract_man_opts(args.rstfile)
kernel_opts_set = set(fmt2enum.keys())
man_opts_set = set(manopts.keys())
def opt_alias_is_doc(o):
enum = fmt2enum[o]['enum']
aliases = []
for k,v in fmt2enum.items():
if k != o and v['enum'] == enum:
if opt_is_doc(k):
return k
return None
def opt_exists(o):
return o in fmt2enum
def opt_is_doc(o):
return o in manopts
print('DUPLICATED DOC OPTIONS')
print('======================')
for opt in sortedset(man_opts_set):
if len(manopts[opt]) > 1:
lines = ", ".join([str(x['ln']) for x in manopts[opt]])
print("OPTION %-20.20s (lines %s)"%(opt, lines))
print()
print('UNDOCUMENTED OPTIONS')
print('====================')
undoc_opts = kernel_opts_set - man_opts_set
# group opts and their negations together
for opt in sortedset(undoc_opts):
fmt = fmt2enum[opt]['fmt']
enum = fmt2enum[opt]['enum']
code = format_code(enum2code[enum])
neg = opt_neg(opt)
if enum == 'Opt_ignore':
print("# skipping %s (Opt_ignore)\n"%opt)
continue
if opt_exists(neg) and opt_is_doc(neg):
print("# skipping %s (%s is documented)\n"%(opt, neg))
continue
alias = opt_alias_is_doc(opt)
if alias:
print("# skipping %s (alias %s is documented)\n"%(opt, alias))
continue
print('OPTION %s ("%s" -> %s):\n%s'%(opt, fmt, enum, code))
print('')
print('DOCUMENTED BUT NON-EXISTING OPTIONS')
print('===================================')
unex_opts = man_opts_set - kernel_opts_set
# group opts and their negations together
for opt in sortedset(unex_opts):
man = manopts[opt][0]
print('OPTION %s ("%s") line %d' % (opt, man['fmt'], man['ln']))
print('')
print('NEGATIVE OPTIONS WITHOUT POSITIVE')
print('=================================')
for opt in sortedset(kernel_opts_set):
if not opt.startswith('no'):
continue
neg = opt[2:]
if not opt_exists(neg):
print("OPTION %s exists but not %s"%(opt,neg))
# little helper to test AND store result at the same time so you can
# do if/elsif easily instead of nesting them when you need to do
# captures
class RX:
def __init__(self):
pass
def search(self, rx, s, flags=0):
self.r = re.search(rx, s, flags)
return self.r
def group(self, n):
return self.r.group(n)
if __name__ == '__main__':
main()