basic: Make ret_names param to unit_file_find_fragment() optional

This commit is contained in:
Daan De Meyer 2021-10-27 16:02:56 +01:00
parent 97c373c7de
commit 11e9347bc6
2 changed files with 21 additions and 5 deletions

View file

@ -598,9 +598,11 @@ int unit_file_find_fragment(
if (name_type < 0)
return name_type;
r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name);
if (r < 0)
return r;
if (ret_names) {
r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name);
if (r < 0)
return r;
}
/* First try to load fragment under the original name */
r = unit_ids_map_get(unit_ids_map, unit_name, &fragment);
@ -619,7 +621,7 @@ int unit_file_find_fragment(
return log_debug_errno(r, "Cannot load template %s: %m", template);
}
if (fragment) {
if (fragment && ret_names) {
const char *fragment_basename = basename(fragment);
if (!streq(fragment_basename, unit_name)) {
@ -631,7 +633,8 @@ int unit_file_find_fragment(
}
*ret_fragment_path = fragment;
*ret_names = TAKE_PTR(names);
if (ret_names)
*ret_names = TAKE_PTR(names);
return 0;
}

View file

@ -71,6 +71,19 @@ static void test_unit_file_build_name_map(char **ids) {
SET_FOREACH(name, names)
log_info(" %s", name);
}
/* Make sure everything still works if we don't collect names. */
STRV_FOREACH(id, ids) {
const char *fragment;
log_info("*** %s ***", *id);
r = unit_file_find_fragment(unit_ids,
unit_names,
*id,
&fragment,
NULL);
assert_se(r == 0);
log_info("fragment: %s", fragment);
}
}
static void test_runlevel_to_target(void) {