]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out "unit-files" verb
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 12:50:35 +0000 (13:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 16:22:23 +0000 (17:22 +0100)
src/analyze/analyze-unit-files.c [new file with mode: 0644]
src/analyze/analyze-unit-files.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/meson.build

diff --git a/src/analyze/analyze-unit-files.c b/src/analyze/analyze-unit-files.c
new file mode 100644 (file)
index 0000000..bfb3edf
--- /dev/null
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "analyze.h"
+#include "analyze-unit-files.h"
+#include "path-lookup.h"
+#include "strv.h"
+
+static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
+        char **s;
+
+        STRV_FOREACH(s, strv)
+                if (strv_fnmatch_or_empty(patterns, *s, flags))
+                        return true;
+
+        return false;
+}
+
+int do_unit_files(int argc, char *argv[], void *userdata) {
+        _cleanup_hashmap_free_ Hashmap *unit_ids = NULL, *unit_names = NULL;
+        _cleanup_(lookup_paths_free) LookupPaths lp = {};
+        char **patterns = strv_skip(argv, 1);
+        const char *k, *dst;
+        char **v;
+        int r;
+
+        r = lookup_paths_init(&lp, arg_scope, 0, NULL);
+        if (r < 0)
+                return log_error_errno(r, "lookup_paths_init() failed: %m");
+
+        r = unit_file_build_name_map(&lp, NULL, &unit_ids, &unit_names, NULL);
+        if (r < 0)
+                return log_error_errno(r, "unit_file_build_name_map() failed: %m");
+
+        HASHMAP_FOREACH_KEY(dst, k, unit_ids) {
+                if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
+                    !strv_fnmatch_or_empty(patterns, dst, FNM_NOESCAPE))
+                        continue;
+
+                printf("ids: %s → %s\n", k, dst);
+        }
+
+        HASHMAP_FOREACH_KEY(v, k, unit_names) {
+                if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
+                    !strv_fnmatch_strv_or_empty(patterns, v, FNM_NOESCAPE))
+                        continue;
+
+                _cleanup_free_ char *j = strv_join(v, ", ");
+                printf("aliases: %s ← %s\n", k, j);
+        }
+
+        return 0;
+}
diff --git a/src/analyze/analyze-unit-files.h b/src/analyze/analyze-unit-files.h
new file mode 100644 (file)
index 0000000..33bccc1
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int do_unit_files(int argc, char *argv[], void *userdata);
index 3f48b06037b20e8439b784041b2ead3e52391d31..5d6ae15038e0ecc5c4dda9916c984770e0c888ec 100644 (file)
@@ -31,6 +31,7 @@
 #include "analyze-time-data.h"
 #include "analyze-timespan.h"
 #include "analyze-timestamp.h"
+#include "analyze-unit-files.h"
 #include "analyze-unit-paths.h"
 #include "analyze-verify.h"
 #include "bus-error.h"
@@ -437,52 +438,6 @@ static int analyze_time(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
-static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
-        char **s;
-        STRV_FOREACH(s, strv)
-                if (strv_fnmatch_or_empty(patterns, *s, flags))
-                        return true;
-
-        return false;
-}
-
-static int do_unit_files(int argc, char *argv[], void *userdata) {
-        _cleanup_(lookup_paths_free) LookupPaths lp = {};
-        _cleanup_hashmap_free_ Hashmap *unit_ids = NULL;
-        _cleanup_hashmap_free_ Hashmap *unit_names = NULL;
-        char **patterns = strv_skip(argv, 1);
-        const char *k, *dst;
-        char **v;
-        int r;
-
-        r = lookup_paths_init(&lp, arg_scope, 0, NULL);
-        if (r < 0)
-                return log_error_errno(r, "lookup_paths_init() failed: %m");
-
-        r = unit_file_build_name_map(&lp, NULL, &unit_ids, &unit_names, NULL);
-        if (r < 0)
-                return log_error_errno(r, "unit_file_build_name_map() failed: %m");
-
-        HASHMAP_FOREACH_KEY(dst, k, unit_ids) {
-                if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
-                    !strv_fnmatch_or_empty(patterns, dst, FNM_NOESCAPE))
-                        continue;
-
-                printf("ids: %s → %s\n", k, dst);
-        }
-
-        HASHMAP_FOREACH_KEY(v, k, unit_names) {
-                if (!strv_fnmatch_or_empty(patterns, k, FNM_NOESCAPE) &&
-                    !strv_fnmatch_strv_or_empty(patterns, v, FNM_NOESCAPE))
-                        continue;
-
-                _cleanup_free_ char *j = strv_join(v, ", ");
-                printf("aliases: %s ← %s\n", k, j);
-        }
-
-        return 0;
-}
-
 void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan) {
         if (calendar && calendar_spec_from_string(p, NULL) >= 0)
                 log_notice("Hint: this expression is a valid calendar specification. "
index a2bbf5cbfdbbcfb80be0416427d7f1eed05fe496..383437eb277f1fc8fb26e67cb3653a4a6270108e 100644 (file)
@@ -37,6 +37,8 @@ systemd_analyze_sources = files('''
         analyze-timespan.h
         analyze-timestamp.c
         analyze-timestamp.h
+        analyze-unit-files.c
+        analyze-unit-files.h
         analyze-unit-paths.c
         analyze-unit-paths.h
         analyze-verify.c