]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
install: optionally return discovered unit file path in unit_file_exists()
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Jan 2024 17:38:50 +0000 (18:38 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 11 Jan 2024 15:05:20 +0000 (16:05 +0100)
src/shared/install.c
src/shared/install.h

index ad30e9b49c1f6d7a068bc47b94aa517356e5f3c9..fabf5db7ed2e139548ff30073c96cf9addd442ac 100644 (file)
@@ -3142,8 +3142,10 @@ int unit_file_get_state(
         return unit_file_lookup_state(scope, &lp, name, ret);
 }
 
-int unit_file_exists(RuntimeScope scope, const LookupPaths *lp, const char *name) {
-        _cleanup_(install_context_done) InstallContext c = { .scope = scope };
+int unit_file_exists_full(RuntimeScope scope, const LookupPaths *lp, const char *name, char **ret_path) {
+        _cleanup_(install_context_done) InstallContext c = {
+                .scope = scope,
+        };
         int r;
 
         assert(lp);
@@ -3152,12 +3154,33 @@ int unit_file_exists(RuntimeScope scope, const LookupPaths *lp, const char *name
         if (!unit_name_is_valid(name, UNIT_NAME_ANY))
                 return -EINVAL;
 
-        r = install_info_discover(&c, lp, name, 0, NULL, NULL, NULL);
-        if (r == -ENOENT)
+        InstallInfo *info = NULL;
+        r = install_info_discover(
+                        &c,
+                        lp,
+                        name,
+                        /* flags= */ 0,
+                        ret_path ? &info : NULL,
+                        /* changes= */ NULL,
+                        /* n_changes= */ NULL);
+        if (r == -ENOENT) {
+                if (ret_path)
+                        *ret_path = NULL;
                 return 0;
+        }
         if (r < 0)
                 return r;
 
+        if (ret_path) {
+                assert(info);
+
+                _cleanup_free_ char *p = strdup(info->path);
+                if (!p)
+                        return -ENOMEM;
+
+                *ret_path = TAKE_PTR(p);
+        }
+
         return 1;
 }
 
index bc0c6db828ddebcfb63e63e595239fb23111def0..3e2ada45f495f5a697ebdd25d53675041c388183 100644 (file)
@@ -193,7 +193,11 @@ int unit_file_lookup_state(
                 UnitFileState *ret);
 
 int unit_file_get_state(RuntimeScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
-int unit_file_exists(RuntimeScope scope, const LookupPaths *paths, const char *name);
+
+int unit_file_exists_full(RuntimeScope scope, const LookupPaths *paths, const char *name, char **ret_path);
+static inline int unit_file_exists(RuntimeScope scope, const LookupPaths *paths, const char *name) {
+        return unit_file_exists_full(scope, paths, name, NULL);
+}
 
 int unit_file_get_list(RuntimeScope scope, const char *root_dir, Hashmap *h, char **states, char **patterns);