Debian Codegraph @

src/analyze/analyze-unit-files.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include <fnmatch.h>
6
7 #include "alloc-util.h"
8 #include "analyze.h"
9 #include "analyze-unit-files.h"
10 #include "hashmap.h"
11 #include "log.h"
12 #include "path-lookup.h"
13 #include "strv.h"
14 #include "unit-file.h"
15
16 static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
17 STRV_FOREACH(s, strv)
18 if (strv_fnmatch_or_empty(patterns, *s, flags))
19 return true;
20
21 return false;
22 }
23
24 int verb_unit_files(int argc, char *argv[], uintptr_t _data, void *userdata) {
25 _cleanup_hashmap_free_ Hashmap *unit_ids = NULL, *unit_names = NULL;
26 _cleanup_(lookup_paths_done) LookupPaths lp = {};
27 char **patterns = strv_skip(argv, 1);
28 const char *k, *dst;
29 char **v;
30 int r;
31
32 r = lookup_paths_init_or_warn(&lp, arg_runtime_scope, 0, NULL);
33 if (r < 0)
34 return r;
35
36 r = unit_file_build_name_map(&lp, NULL, &unit_ids, &unit_names, NULL);
37 if (r < 0)
38 return log_error_errno(r, "unit_file_build_name_map() failed: %m");
39
40 HASHMAP_FOREACH_KEY(dst, k, unit_ids) {
41 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
42 !strv_fnmatch_or_empty(patterns, dst, FNM_NOESCAPE))
43 continue;
44
45 printf("ids: %s → %s\n", k, dst);
46 }
47
48 HASHMAP_FOREACH_KEY(v, k, unit_names) {
49 if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
50 !strv_fnmatch_strv_or_empty(patterns, v, FNM_NOESCAPE))
51 continue;
52
53 _cleanup_free_ char *j = strv_join(v, ", ");
54 printf("aliases: %s ← %s\n", k, j);
55 }
56
57 return EXIT_SUCCESS;
58 }