src/filetype/directories.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stddef.h> | ||
| 2 | #include "filetype/types.h" | ||
| 3 | #include "util/debug.h" | ||
| 4 | |||
| 5 | static const struct DirPrefixMap { | ||
| 6 | StringView prefix; | ||
| 7 | StringView suffix; | ||
| 8 | FileTypeEnum filetype; | ||
| 9 | } affixes[] = { | ||
| 10 | {SV("/etc/default/"), SV(""), SH}, | ||
| 11 | {SV("/etc/firejail/"), SV(""), CONFIG}, | ||
| 12 | {SV("/etc/nginx/"), SV(""), NGINX}, | ||
| 13 | {SV("/etc/pam.d/"), SV(""), CONFIG}, | ||
| 14 | {SV("/etc/sudoers.d/"), SV(""), CONFIG}, | ||
| 15 | {SV("/etc/systemd/"), SV(".conf"), INI}, | ||
| 16 | {SV("/etc/"), SV(".conf"), CONFIG}, | ||
| 17 | {SV("/usr/share/"), SV(".conf"), CONFIG}, | ||
| 18 | {SV("/usr/local/share/"), SV(".conf"), CONFIG}, | ||
| 19 | }; | ||
| 20 | |||
| 21 | 24 | UNITTEST { | |
| 22 |
2/2✓ Branch 18 → 3 taken 216 times.
✓ Branch 18 → 19 taken 24 times.
|
240 | for (size_t i = 0; i < ARRAYLEN(affixes); i++) { |
| 23 | 216 | const struct DirPrefixMap *p = &affixes[i]; | |
| 24 | 216 | BUG_ON(p->prefix.length < STRLEN("/etc/")); | |
| 25 | 216 | BUG_ON(!strview_has_prefix(p->prefix, "/")); | |
| 26 | 216 | BUG_ON(!strview_has_suffix(p->prefix, "/")); | |
| 27 | 216 | BUG_ON(p->suffix.length && !strview_has_prefix(p->suffix, ".")); | |
| 28 | 216 | BUG_ON(p->filetype >= NR_BUILTIN_FILETYPES); | |
| 29 | } | ||
| 30 | 24 | } | |
| 31 | |||
| 32 | 99 | FileTypeEnum filetype_from_path(StringView path) | |
| 33 | { | ||
| 34 |
2/2✓ Branch 2 → 7 taken 43 times.
✓ Branch 2 → 8 taken 56 times.
|
99 | if (path.length < STRLEN("/etc/_")) { |
| 35 | return NONE; | ||
| 36 | } | ||
| 37 | |||
| 38 |
2/2✓ Branch 7 → 3 taken 363 times.
✓ Branch 7 → 8 taken 38 times.
|
401 | for (size_t i = 0; i < ARRAYLEN(affixes); i++) { |
| 39 | 363 | const struct DirPrefixMap *p = &affixes[i]; | |
| 40 |
2/2✓ Branch 4 → 5 taken 5 times.
✓ Branch 4 → 6 taken 358 times.
|
363 | if (strview_has_sv_prefix_and_suffix(path, p->prefix, p->suffix)) { |
| 41 | 5 | return p->filetype; | |
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | return NONE; | ||
| 46 | } | ||
| 47 |