basic/env-file: make load-env-file deduplicate entries with the same key

We generally assume parsing like the shell would do it, so the last value
should win when there are repeats.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-03-29 16:17:56 +02:00
parent 80e72f80bc
commit 25407ad2a7
3 changed files with 26 additions and 13 deletions
+20 -11
View File
@@ -414,30 +414,39 @@ static int load_env_file_push_pairs(
const char *key, char *value,
void *userdata,
int *n_pushed) {
char ***m = userdata;
char ***m = ASSERT_PTR(userdata);
bool added = false;
int r;
r = check_utf8ness_and_warn(filename, line, key, value);
if (r < 0)
return r;
/* Check if the key is present */
for (char **t = *m; t && *t; t += 2)
if (streq(t[0], key)) {
if (value)
r = free_and_replace(t[1], value);
else
r = free_and_strdup(t+1, "");
goto finish;
}
r = strv_extend(m, key);
if (r < 0)
return -ENOMEM;
if (!value) {
r = strv_extend(m, "");
if (r < 0)
return -ENOMEM;
} else {
if (value)
r = strv_push(m, value);
if (r < 0)
return r;
}
else
r = strv_extend(m, "");
added = true;
finish:
if (r < 0)
return r;
if (n_pushed)
if (n_pushed && added)
(*n_pushed)++;
return 0;
}