dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 90.8% 129 / 0 / 142
Functions: 94.1% 16 / 0 / 17
Branches: 87.8% 79 / 20 / 110

src/filetype.c
Line Branch Exec Source
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include "filetype.h"
4 #include "filetype/types.h"
5 #include "command/serialize.h"
6 #include "regexp.h"
7 #include "util/ascii.h"
8 #include "util/bsearch.h"
9 #include "util/debug.h"
10 #include "util/hashset.h"
11 #include "util/path.h"
12 #include "util/str-util.h"
13 #include "util/xmalloc.h"
14
15 typedef struct {
16 unsigned int str_len;
17 char str[];
18 } FlexArrayStr;
19
20 // Filetypes dynamically added via the `ft` command.
21 // Not grouped by name to make it possible to order them freely.
22 typedef struct {
23 union {
24 FlexArrayStr *str;
25 const InternedRegexp *regexp;
26 } u;
27 uint8_t type; // FileDetectionType
28 char name[];
29 } UserFileTypeEntry;
30
31 136 static bool ft_uses_regex(FileDetectionType type)
32 {
33 136 return type == FT_CONTENT || type == FT_FILENAME;
34 }
35
36 24 bool add_filetype (
37 PointerArray *filetypes,
38 const char *name,
39 const char *str,
40 FileDetectionType type,
41 ErrorBuffer *ebuf
42 ) {
43 24 BUG_ON(!is_valid_filetype_name(name));
44 24 const InternedRegexp *ir = NULL;
45
2/2
✓ Branch 4 → 5 taken 20 times.
✓ Branch 4 → 7 taken 4 times.
24 if (ft_uses_regex(type)) {
46 20 ir = regexp_intern(ebuf, str);
47
1/2
✓ Branch 6 → 7 taken 20 times.
✗ Branch 6 → 16 not taken.
20 if (unlikely(!ir)) {
48 return false;
49 }
50 }
51
52 24 size_t name_len = strlen(name);
53 24 UserFileTypeEntry *ft = xmalloc(xadd3(sizeof(*ft), name_len, 1));
54 24 ft->type = type;
55
56
2/2
✓ Branch 9 → 10 taken 20 times.
✓ Branch 9 → 11 taken 4 times.
24 if (ir) {
57 20 ft->u.regexp = ir;
58 } else {
59 4 size_t str_len = strlen(str);
60 4 FlexArrayStr *s = xmalloc(xadd3(sizeof(*s), str_len, 1));
61 4 s->str_len = str_len;
62 4 ft->u.str = s;
63 4 memcpy(s->str, str, str_len + 1);
64 }
65
66 24 memcpy(ft->name, name, name_len + 1);
67 24 ptr_array_append(filetypes, ft);
68 24 return true;
69 }
70
71 299 static StringView path_extension(StringView filename)
72 {
73 299 ssize_t dot = strview_memrchr_idx(filename, '.');
74
2/2
✓ Branch 2 → 3 taken 117 times.
✓ Branch 2 → 4 taken 182 times.
299 return strview_suffix(filename, (dot > 0) ? dot + 1 : filename.length);
75 }
76
77 284 static StringView get_filename_extension(StringView filename)
78 {
79 284 StringView ext = path_extension(filename);
80
2/2
✓ Branch 4 → 5 taken 15 times.
✓ Branch 4 → 6 taken 269 times.
284 if (is_ignored_extension(ext)) {
81 15 filename.length -= ext.length + 1;
82 15 ext = path_extension(filename);
83 }
84 284 strview_remove_matching_suffix(&ext, "~");
85 284 return ext;
86 }
87
88 // Parse hashbang and return interpreter name, without version number.
89 // For example, if line is "#!/usr/bin/env python2", "python" is returned.
90 284 static StringView get_interpreter(StringView line)
91 {
92 284 StringView sv = strview(NULL);
93
2/2
✓ Branch 3 → 4 taken 210 times.
✓ Branch 3 → 5 taken 74 times.
284 if (!strview_remove_matching_prefix(&line, "#!")) {
94 210 return sv;
95 }
96
97 74 strview_trim_left(&line);
98
3/4
✓ Branch 6 → 7 taken 74 times.
✗ Branch 6 → 8 not taken.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 73 times.
74 if (line.length < 2 || line.data[0] != '/') {
99 1 return sv;
100 }
101
102 73 size_t pos = 0;
103 73 sv = get_delim(line, &pos, ' ');
104
4/4
✓ Branch 10 → 11 taken 8 times.
✓ Branch 10 → 17 taken 65 times.
✓ Branch 12 → 14 taken 6 times.
✓ Branch 12 → 17 taken 2 times.
73 if (pos < line.length && strview_equal_cstring(sv, "/usr/bin/env")) {
105
4/4
✓ Branch 14 → 15 taken 6 times.
✓ Branch 14 → 16 taken 1 time.
✓ Branch 15 → 13 taken 1 time.
✓ Branch 15 → 16 taken 5 times.
7 while (pos + 1 < line.length && line.data[pos] == ' ') {
106 1 pos++;
107 }
108 6 sv = get_delim(line, &pos, ' ');
109 }
110
111 73 ssize_t last_slash_idx = strview_memrchr_idx(sv, '/');
112
2/2
✓ Branch 17 → 18 taken 68 times.
✓ Branch 17 → 20 taken 5 times.
73 if (last_slash_idx >= 0) {
113 68 strview_remove_prefix(&sv, last_slash_idx + 1);
114 }
115
116
4/4
✓ Branch 21 → 22 taken 84 times.
✓ Branch 21 → 23 taken 5 times.
✓ Branch 22 → 19 taken 16 times.
✓ Branch 22 → 23 taken 68 times.
89 while (sv.length && ascii_is_digit_or_dot(sv.data[sv.length - 1])) {
117 16 sv.length--;
118 }
119
120 73 return sv;
121 }
122
123 25 static bool ft_str_match(const UserFileTypeEntry *ft, StringView sv)
124 {
125 25 const FlexArrayStr *s = ft->u.str;
126
4/4
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 6 taken 9 times.
✓ Branch 4 → 5 taken 13 times.
✓ Branch 4 → 6 taken 3 times.
25 return sv.length && strview_equal(sv, string_view(s->str, s->str_len));
127 }
128
129 60 static bool ft_regex_match(const UserFileTypeEntry *ft, const StringView sv)
130 {
131 60 const regex_t *re = &ft->u.regexp->re;
132
4/4
✓ Branch 2 → 3 taken 54 times.
✓ Branch 2 → 6 taken 6 times.
✓ Branch 4 → 5 taken 52 times.
✓ Branch 4 → 6 taken 2 times.
60 return sv.length > 0 && regexp_exec(re, sv, 0, NULL, 0);
133 }
134
135 85 static bool ft_match(const UserFileTypeEntry *ft, const StringView sv)
136 {
137 85 FileDetectionType t = ft->type;
138
2/2
✓ Branch 2 → 3 taken 60 times.
✓ Branch 2 → 4 taken 25 times.
85 return ft_uses_regex(t) ? ft_regex_match(ft, sv) : ft_str_match(ft, sv);
139 }
140
141 typedef FileTypeEnum (*FileTypeLookupFunc)(const StringView sv);
142
143 284 const char *find_ft(const PointerArray *filetypes, const char *filename, StringView line)
144 {
145 284 const StringView path = strview(filename);
146 284 const StringView base = path_slice_basename(path);
147 284 const StringView ext = get_filename_extension(base);
148 284 const StringView interpreter = get_interpreter(line);
149 284 BUG_ON(path.length == 0 && (base.length != 0 || ext.length != 0));
150 284 BUG_ON(line.length == 0 && interpreter.length != 0);
151
152 // The order of elements in this array determines the order of
153 // precedence for the lookup() functions (but note that changing
154 // the initializer below makes no difference to the array order)
155 284 static const FileTypeLookupFunc funcs[] = {
156 [FT_INTERPRETER] = filetype_from_interpreter,
157 [FT_BASENAME] = filetype_from_basename,
158 [FT_CONTENT] = filetype_from_signature,
159 [FT_EXTENSION] = filetype_from_extension,
160 [FT_FILENAME] = filetype_from_path,
161 };
162
163 284 const StringView params[] = {
164 [FT_INTERPRETER] = interpreter,
165 [FT_BASENAME] = base,
166 [FT_CONTENT] = line,
167 [FT_EXTENSION] = ext,
168 [FT_FILENAME] = path,
169 };
170
171 // Search user `ft` entries
172
2/2
✓ Branch 17 → 13 taken 85 times.
✓ Branch 17 → 24 taken 279 times.
364 for (size_t i = 0, n = filetypes->count; i < n; i++) {
173 85 const UserFileTypeEntry *ft = filetypes->ptrs[i];
174
2/2
✓ Branch 14 → 15 taken 5 times.
✓ Branch 14 → 16 taken 80 times.
85 if (ft_match(ft, params[ft->type])) {
175 5 return ft->name;
176 }
177 }
178
179 // Search built-in lookup tables
180 static_assert(ARRAYLEN(funcs) == ARRAYLEN(params));
181
2/2
✓ Branch 24 → 18 taken 929 times.
✓ Branch 24 → 25 taken 94 times.
1023 for (FileDetectionType i = 0; i < ARRAYLEN(funcs); i++) {
182 929 BUG_ON(!funcs[i]);
183 929 FileTypeEnum ft = funcs[i](params[i]);
184
2/2
✓ Branch 21 → 22 taken 185 times.
✓ Branch 21 → 23 taken 744 times.
929 if (ft != NONE) {
185 185 return builtin_filetype_names[ft];
186 }
187 }
188
189 // Search lower precedence file signatures
190 94 FileTypeEnum ft = filetype_from_signature_late(line);
191
2/2
✓ Branch 26 → 27 taken 2 times.
✓ Branch 26 → 28 taken 92 times.
94 if (ft != NONE) {
192 2 return builtin_filetype_names[ft];
193 }
194
195
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 36 taken 92 times.
92 if (strview_equal_cstring(ext, "conf")) {
196 ✗ BUG_ON(!filename);
197 ✗ const StringView dir = path_slice_dirname(filename);
198 ✗ if (strview_has_suffix(dir, "/tmpfiles.d")) {
199 ✗ return builtin_filetype_names[CONFIG];
200 }
201 }
202
203 return NULL;
204 }
205
206 31 bool is_ft(const PointerArray *filetypes, const char *name)
207 {
208
2/2
✓ Branch 3 → 4 taken 14 times.
✓ Branch 3 → 8 taken 17 times.
31 if (BSEARCH(name, builtin_filetype_names, vstrcmp)) {
209 return true;
210 }
211
212
2/2
✓ Branch 7 → 5 taken 26 times.
✓ Branch 7 → 8 taken 8 times.
34 for (size_t i = 0, n = filetypes->count; i < n; i++) {
213 26 const UserFileTypeEntry *ft = filetypes->ptrs[i];
214
2/2
✓ Branch 5 → 6 taken 20 times.
✓ Branch 5 → 8 taken 6 times.
26 if (streq(ft->name, name)) {
215 return true;
216 }
217 }
218
219 return false;
220 }
221
222 2 void collect_ft(const PointerArray *filetypes, PointerArray *a, StringView prefix)
223 {
224 // Insert all filetype names beginning with `prefix` into a HashSet
225 // (to avoid duplicates)
226 2 size_t nr_builtin_ft = ARRAYLEN(builtin_filetype_names);
227
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
2 HashSet set = hashset_new(20 + (prefix.length ? 0 : nr_builtin_ft), false);
228
229
2/2
✓ Branch 10 → 6 taken 254 times.
✓ Branch 10 → 11 taken 2 times.
258 for (size_t i = 0; i < nr_builtin_ft; i++) {
230 254 StringView name = strview(builtin_filetype_names[i]);
231
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 252 times.
254 if (strview_has_sv_prefix(name, prefix)) {
232 2 hashset_insert(&set, name);
233 }
234 }
235
236
1/2
✗ Branch 16 → 12 not taken.
✓ Branch 16 → 17 taken 2 times.
2 for (size_t i = 0, n = filetypes->count; i < n; i++) {
237 ✗ const UserFileTypeEntry *ft = filetypes->ptrs[i];
238 ✗ StringView name = strview(ft->name);
239 ✗ if (strview_has_sv_prefix(name, prefix)) {
240 ✗ hashset_insert(&set, name);
241 }
242 }
243
244 // Append the collected strings to the PointerArray
245
2/2
✓ Branch 21 → 18 taken 2 times.
✓ Branch 21 → 22 taken 2 times.
4 for (HashSetIter iter = hashset_iter(&set); hashset_next(&iter); ) {
246 2 ptr_array_append(a, xmemdup(iter.entry->str, iter.entry->str_len + 1));
247 }
248
249 2 hashset_free(&set);
250 2 }
251
252 3 static const char *ft_get_str(const UserFileTypeEntry *ft)
253 {
254
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 1 time.
3 return ft_uses_regex(ft->type) ? ft->u.regexp->str : ft->u.str->str;
255 }
256
257 1 String dump_filetypes(const PointerArray *filetypes)
258 {
259 1 static const char flags[][4] = {
260 [FT_EXTENSION] = "",
261 [FT_FILENAME] = "-f ",
262 [FT_CONTENT] = "-c ",
263 [FT_INTERPRETER] = "-i ",
264 [FT_BASENAME] = "-b ",
265 };
266
267 1 String s = string_new(4096);
268
2/2
✓ Branch 15 → 4 taken 3 times.
✓ Branch 15 → 16 taken 1 time.
4 for (size_t i = 0, n = filetypes->count; i < n; i++) {
269 3 const UserFileTypeEntry *ft = filetypes->ptrs[i];
270 3 BUG_ON(ft->type >= ARRAYLEN(flags));
271 3 BUG_ON(ft->name[0] == '-');
272 3 string_append_literal(&s, "ft ");
273 3 string_append_cstring(&s, flags[ft->type]);
274 3 string_append_escaped_arg(&s, ft->name, true);
275 3 string_append_byte(&s, ' ');
276 3 string_append_escaped_arg(&s, ft_get_str(ft), true);
277 3 string_append_byte(&s, '\n');
278 }
279 1 return s;
280 }
281
282 24 static void free_filetype_entry(UserFileTypeEntry *ft)
283 {
284
2/2
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 4 taken 20 times.
24 if (!ft_uses_regex(ft->type)) {
285 4 free(ft->u.str);
286 }
287 24 free(ft);
288 24 }
289
290 12 void free_filetypes(PointerArray *filetypes)
291 {
292 12 ptr_array_free_cb(filetypes, FREE_FUNC(free_filetype_entry));
293 12 }
294
295 3525 bool is_valid_filetype_name_sv(StringView name)
296 {
297 3525 const char *data = name.data;
298 3525 const size_t len = name.length;
299
4/4
✓ Branch 2 → 3 taken 3516 times.
✓ Branch 2 → 8 taken 9 times.
✓ Branch 3 → 7 taken 3513 times.
✓ Branch 3 → 8 taken 3 times.
3525 if (unlikely(len == 0 || len > FILETYPE_NAME_MAX || data[0] == '-')) {
300 return false;
301 }
302
303 const AsciiCharType mask = ASCII_SPACE | ASCII_CNTRL;
304
2/2
✓ Branch 7 → 4 taken 17994 times.
✓ Branch 7 → 8 taken 3502 times.
21496 for (size_t i = 0; i < len; i++) {
305 17994 unsigned char ch = data[i];
306
4/4
✓ Branch 4 → 5 taken 17985 times.
✓ Branch 4 → 8 taken 9 times.
✓ Branch 5 → 6 taken 17983 times.
✓ Branch 5 → 8 taken 2 times.
17994 if (unlikely(ascii_test(ch, mask) || ch == '/')) {
307 return false;
308 }
309 }
310
311 return true;
312 }
313
314 ✗ const char *filetype_str_from_extension(const char *path)
315 {
316 ✗ StringView base = path_slice_basename(strview(path));
317 ✗ StringView ext = get_filename_extension(base);
318 ✗ FileTypeEnum ft = filetype_from_extension(ext);
319 ✗ return (ft == NONE) ? NULL : builtin_filetype_names[ft];
320 }
321