Introduce strv_consume which takes ownership

This mirrors set_consume and makes the common use a bit nicer.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2014-03-04 09:20:51 -05:00
parent e062dec5ae
commit 6e18964d3a
15 changed files with 42 additions and 62 deletions

View file

@ -562,11 +562,9 @@ static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userd
if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
return -ENOMEM;
r = strv_push(&owners, n);
if (r < 0) {
free(n);
return -ENOMEM;
}
r = strv_consume(&owners, n);
if (r < 0)
return r;
}
r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);

View file

@ -385,11 +385,9 @@ static int property_get_syscall_filter(
if (!name)
continue;
r = strv_push(&l, name);
if (r < 0) {
free(name);
return -ENOMEM;
}
r = strv_consume(&l, name);
if (r < 0)
return r;
}
#endif

View file

@ -133,8 +133,7 @@ int locale_setup(char ***environment) {
goto finish;
}
if (strv_push(&add, s) < 0) {
free(s);
if (strv_consume(&add, s) < 0) {
r = -ENOMEM;
goto finish;
}

View file

@ -3181,11 +3181,9 @@ int unit_require_mounts_for(Unit *u, const char *path) {
return 0;
}
r = strv_push(&u->requires_mounts_for, p);
if (r < 0) {
free(p);
r = strv_consume(&u->requires_mounts_for, p);
if (r < 0)
return r;
}
PATH_FOREACH_PREFIX_MORE(prefix, p) {
Set *x;

View file

@ -237,11 +237,9 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
return -ENOMEM;
r = strv_push(x, n);
if (r < 0) {
free(n);
return -ENOMEM;
}
r = strv_consume(x, n);
if (r < 0)
return r;
previous_id = name->owner_id;
}

View file

@ -399,11 +399,9 @@ int seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
if (!p)
return -ENOMEM;
r = strv_push(&l, p);
if (r < 0) {
free(p);
r = strv_consume(&l, p);
if (r < 0)
return r;
}
}
*nodes = l;

View file

@ -558,11 +558,9 @@ int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
if (!p)
return -ENOMEM;
r = strv_push(&l, p);
if (r < 0) {
free(p);
r = strv_consume(&l, p);
if (r < 0)
return r;
}
}
*nodes = l;

View file

@ -315,11 +315,9 @@ int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
if (!p)
return -ENOMEM;
r = strv_push(&l, p);
if (r < 0) {
free(p);
r = strv_consume(&l, p);
if (r < 0)
return r;
}
}
*nodes = l;

View file

@ -225,11 +225,9 @@ int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
if (!p)
return -ENOMEM;
r = strv_push(&l, p);
if (r < 0) {
free(p);
r = strv_consume(&l, p);
if (r < 0)
return r;
}
}
*nodes = l;

View file

@ -136,9 +136,8 @@ int search_acl_groups(char*** dst, const char* path, bool* belong) {
return log_oom();
}
r = strv_push(dst, name);
r = strv_consume(dst, name);
if (r < 0) {
free(name);
acl_free(acl);
return log_oom();
}

View file

@ -681,7 +681,7 @@ int config_parse_strv(const char *unit,
}
FOREACH_WORD_QUOTED(w, l, rvalue, state) {
_cleanup_free_ char *n;
char *n;
n = cunescape_length(w, l);
if (!n)
@ -693,7 +693,7 @@ int config_parse_strv(const char *unit,
continue;
}
r = strv_extend(sv, n);
r = strv_consume(sv, n);
if (r < 0)
return log_oom();
}

View file

@ -614,11 +614,9 @@ static int load_env_file_push(const char *filename, unsigned line,
if (!p)
return -ENOMEM;
r = strv_push(m, p);
if (r < 0) {
free(p);
r = strv_consume(m, p);
if (r < 0)
return r;
}
free(value);
return 0;

View file

@ -378,9 +378,18 @@ int strv_push(char ***l, char *value) {
return 0;
}
int strv_consume(char ***l, char *value) {
int r;
r = strv_push(l, value);
if (r < 0)
free(value);
return r;
}
int strv_extend(char ***l, const char *value) {
char *v;
int r;
if (!value)
return 0;
@ -389,11 +398,7 @@ int strv_extend(char ***l, const char *value) {
if (!v)
return -ENOMEM;
r = strv_push(l, v);
if (r < 0)
free(v);
return r;
return strv_consume(l, v);
}
char **strv_uniq(char **l) {

View file

@ -40,6 +40,7 @@ int strv_extend_strv(char ***a, char **b);
int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
int strv_extend(char ***l, const char *value);
int strv_push(char ***l, char *value);
int strv_consume(char ***l, char *value);
char **strv_remove(char **l, const char *s);
char **strv_uniq(char **l);

View file

@ -2167,13 +2167,11 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r
return log_oom();
if (string_is_glob(t))
r = strv_push(&globs, t);
r = strv_consume(&globs, t);
else
r = strv_push(&mangled, t);
if (r < 0) {
free(t);
r = strv_consume(&mangled, t);
if (r < 0)
return log_oom();
}
}
/* Query the manager only if any of the names are a glob, since
@ -5346,10 +5344,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
if (!prop)
return log_oom();
if (strv_push(&arg_properties, prop) < 0) {
free(prop);
if (strv_consume(&arg_properties, prop) < 0)
return log_oom();
}
}
}
@ -5518,10 +5514,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
if (!s)
return log_oom();
if (strv_push(&arg_states, s) < 0) {
free(s);
if (strv_consume(&arg_states, s) < 0)
return log_oom();
}
}
break;
}