core/manager: move environment serialization out to basic/env-util.c
This protocol is generally useful, we might just as well reuse it for the env. generators. The implementation is changed a bit: instead of making a new strv and freeing the old one, just mutate the original. This is much faster with larger arrays, while in fact atomicity is preserved, since we only either insert the new entry or not, without being in inconsistent state. v2: - fix confusion with return value
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "env-util.h"
|
||||
#include "escape.h"
|
||||
#include "extract-word.h"
|
||||
#include "macro.h"
|
||||
#include "parse-util.h"
|
||||
@@ -644,3 +645,36 @@ int getenv_bool(const char *p) {
|
||||
|
||||
return parse_boolean(e);
|
||||
}
|
||||
|
||||
int serialize_environment(FILE *f, char **environment) {
|
||||
char **e;
|
||||
|
||||
STRV_FOREACH(e, environment) {
|
||||
_cleanup_free_ char *ce;
|
||||
|
||||
ce = cescape(*e);
|
||||
if (!ce)
|
||||
return -ENOMEM;
|
||||
|
||||
fprintf(f, "env=%s\n", *e);
|
||||
}
|
||||
|
||||
/* caller should call ferror() */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int deserialize_environment(char ***environment, const char *line) {
|
||||
char *uce = NULL;
|
||||
int r;
|
||||
|
||||
assert(line);
|
||||
assert(environment);
|
||||
|
||||
assert(startswith(line, "env="));
|
||||
r = cunescape(line + 4, UNESCAPE_RELAX, &uce);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return strv_env_replace(environment, uce);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user