shared/install: use FOREACH_ARRAY at one more place

This commit is contained in:
Mike Yuan 2024-05-01 17:21:33 +08:00 committed by Luca Boccassi
parent 3e02c8d87d
commit 521a7c9bb9

View file

@ -3398,6 +3398,8 @@ static int pattern_match_multiple_instances(
_cleanup_free_ char *templated_name = NULL; _cleanup_free_ char *templated_name = NULL;
int r; int r;
assert(unit_name);
/* If no ret is needed or the rule itself does not have instances /* If no ret is needed or the rule itself does not have instances
* initialized, we return not matching */ * initialized, we return not matching */
if (!ret || !rule.instances) if (!ret || !rule.instances)
@ -3444,20 +3446,25 @@ static int pattern_match_multiple_instances(
static int query_presets(const char *name, const UnitFilePresets *presets, char ***instance_name_list) { static int query_presets(const char *name, const UnitFilePresets *presets, char ***instance_name_list) {
PresetAction action = PRESET_UNKNOWN; PresetAction action = PRESET_UNKNOWN;
assert(name);
assert(presets);
if (!unit_name_is_valid(name, UNIT_NAME_ANY)) if (!unit_name_is_valid(name, UNIT_NAME_ANY))
return -EINVAL; return -EINVAL;
for (size_t i = 0; i < presets->n_rules; i++) FOREACH_ARRAY(i, presets->rules, presets->n_rules)
if (pattern_match_multiple_instances(presets->rules[i], name, instance_name_list) > 0 || if (pattern_match_multiple_instances(*i, name, instance_name_list) > 0 ||
fnmatch(presets->rules[i].pattern, name, FNM_NOESCAPE) == 0) { fnmatch(i->pattern, name, FNM_NOESCAPE) == 0) {
action = presets->rules[i].action; action = i->action;
break; break;
} }
switch (action) { switch (action) {
case PRESET_UNKNOWN: case PRESET_UNKNOWN:
log_debug("Preset files don't specify rule for %s. Enabling.", name); log_debug("Preset files don't specify rule for %s. Enabling.", name);
return PRESET_ENABLE; return PRESET_ENABLE;
case PRESET_ENABLE: case PRESET_ENABLE:
if (instance_name_list && *instance_name_list) if (instance_name_list && *instance_name_list)
STRV_FOREACH(s, *instance_name_list) STRV_FOREACH(s, *instance_name_list)
@ -3465,12 +3472,15 @@ static int query_presets(const char *name, const UnitFilePresets *presets, char
else else
log_debug("Preset files say enable %s.", name); log_debug("Preset files say enable %s.", name);
return PRESET_ENABLE; return PRESET_ENABLE;
case PRESET_DISABLE: case PRESET_DISABLE:
log_debug("Preset files say disable %s.", name); log_debug("Preset files say disable %s.", name);
return PRESET_DISABLE; return PRESET_DISABLE;
case PRESET_IGNORE: case PRESET_IGNORE:
log_debug("Preset files say ignore %s.", name); log_debug("Preset files say ignore %s.", name);
return PRESET_IGNORE; return PRESET_IGNORE;
default: default:
assert_not_reached(); assert_not_reached();
} }