|
|
84b277 |
From 3a68febdb636924b941a84e123b6e51e042e1273 Mon Sep 17 00:00:00 2001
|
|
|
84b277 |
From: Michal Sekletar <msekleta@redhat.com>
|
|
|
84b277 |
Date: Mon, 22 Sep 2014 09:38:38 +0200
|
|
|
84b277 |
Subject: [PATCH] fileio: make parse_env_file() return number of parsed items
|
|
|
84b277 |
|
|
|
84b277 |
This commit introduces possibility to call parse_env_file_internal() and hand
|
|
|
84b277 |
over extra argument where we will accumulate how many items were successfully
|
|
|
84b277 |
parsed and pushed by callback. We make use of this in parse_env_file() and
|
|
|
84b277 |
return number of parsed items on success instead of always returning zero.
|
|
|
84b277 |
|
|
|
84b277 |
As a side-effect this commit should fix bug that locale settings in
|
|
|
84b277 |
/etc/locale.conf are not overriden by options passed via kernel command line.
|
|
|
84b277 |
|
|
|
84b277 |
Conflicts:
|
|
|
84b277 |
src/shared/fileio.c
|
|
|
84b277 |
|
|
|
84b277 |
(cherry picked from commit a5f6c30da3ac58081108221bf8a0f6f1d84b33a9)
|
|
|
84b277 |
|
|
|
84b277 |
Resolves: #1069420
|
|
|
84b277 |
---
|
|
|
84b277 |
src/shared/fileio.c | 30 +++++++++++++++++++-----------
|
|
|
84b277 |
1 file changed, 19 insertions(+), 11 deletions(-)
|
|
|
84b277 |
|
|
|
84b277 |
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
|
|
|
84b277 |
index 733b320..fbc28bd 100644
|
|
|
84b277 |
--- a/src/shared/fileio.c
|
|
|
84b277 |
+++ b/src/shared/fileio.c
|
|
|
84b277 |
@@ -180,8 +180,9 @@ static int parse_env_file_internal(
|
|
|
84b277 |
const char *fname,
|
|
|
84b277 |
const char *newline,
|
|
|
84b277 |
int (*push) (const char *filename, unsigned line,
|
|
|
84b277 |
- const char *key, char *value, void *userdata),
|
|
|
84b277 |
- void *userdata) {
|
|
|
84b277 |
+ const char *key, char *value, void *userdata, int *n_pushed),
|
|
|
84b277 |
+ void *userdata,
|
|
|
84b277 |
+ int *n_pushed) {
|
|
|
84b277 |
|
|
|
84b277 |
_cleanup_free_ char *contents = NULL, *key = NULL;
|
|
|
84b277 |
size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1;
|
|
|
84b277 |
@@ -268,7 +269,7 @@ static int parse_env_file_internal(
|
|
|
84b277 |
if (last_key_whitespace != (size_t) -1)
|
|
|
84b277 |
key[last_key_whitespace] = 0;
|
|
|
84b277 |
|
|
|
84b277 |
- r = push(fname, line, key, value, userdata);
|
|
|
84b277 |
+ r = push(fname, line, key, value, userdata, n_pushed);
|
|
|
84b277 |
if (r < 0)
|
|
|
84b277 |
goto fail;
|
|
|
84b277 |
|
|
|
84b277 |
@@ -313,7 +314,7 @@ static int parse_env_file_internal(
|
|
|
84b277 |
if (last_key_whitespace != (size_t) -1)
|
|
|
84b277 |
key[last_key_whitespace] = 0;
|
|
|
84b277 |
|
|
|
84b277 |
- r = push(fname, line, key, value, userdata);
|
|
|
84b277 |
+ r = push(fname, line, key, value, userdata, n_pushed);
|
|
|
84b277 |
if (r < 0)
|
|
|
84b277 |
goto fail;
|
|
|
84b277 |
|
|
|
84b277 |
@@ -448,7 +449,7 @@ static int parse_env_file_internal(
|
|
|
84b277 |
if (last_key_whitespace != (size_t) -1)
|
|
|
84b277 |
key[last_key_whitespace] = 0;
|
|
|
84b277 |
|
|
|
84b277 |
- r = push(fname, line, key, value, userdata);
|
|
|
84b277 |
+ r = push(fname, line, key, value, userdata, n_pushed);
|
|
|
84b277 |
if (r < 0)
|
|
|
84b277 |
goto fail;
|
|
|
84b277 |
}
|
|
|
84b277 |
@@ -461,7 +462,7 @@ fail:
|
|
|
84b277 |
}
|
|
|
84b277 |
|
|
|
84b277 |
static int parse_env_file_push(const char *filename, unsigned line,
|
|
|
84b277 |
- const char *key, char *value, void *userdata) {
|
|
|
84b277 |
+ const char *key, char *value, void *userdata, int *n_pushed) {
|
|
|
84b277 |
assert(utf8_is_valid(key));
|
|
|
84b277 |
|
|
|
84b277 |
if (value && !utf8_is_valid(value))
|
|
|
84b277 |
@@ -484,6 +485,10 @@ static int parse_env_file_push(const char *filename, unsigned line,
|
|
|
84b277 |
va_end(aq);
|
|
|
84b277 |
free(*v);
|
|
|
84b277 |
*v = value;
|
|
|
84b277 |
+
|
|
|
84b277 |
+ if (n_pushed)
|
|
|
84b277 |
+ (*n_pushed)++;
|
|
|
84b277 |
+
|
|
|
84b277 |
return 1;
|
|
|
84b277 |
}
|
|
|
84b277 |
}
|
|
|
84b277 |
@@ -500,20 +505,20 @@ int parse_env_file(
|
|
|
84b277 |
const char *newline, ...) {
|
|
|
84b277 |
|
|
|
84b277 |
va_list ap;
|
|
|
84b277 |
- int r;
|
|
|
84b277 |
+ int r, n_pushed = 0;
|
|
|
84b277 |
|
|
|
84b277 |
if (!newline)
|
|
|
84b277 |
newline = NEWLINE;
|
|
|
84b277 |
|
|
|
84b277 |
va_start(ap, newline);
|
|
|
84b277 |
- r = parse_env_file_internal(fname, newline, parse_env_file_push, &ap);
|
|
|
84b277 |
+ r = parse_env_file_internal(fname, newline, parse_env_file_push, &ap, &n_pushed);
|
|
|
84b277 |
va_end(ap);
|
|
|
84b277 |
|
|
|
84b277 |
- return r;
|
|
|
84b277 |
+ return r < 0 ? r : n_pushed;
|
|
|
84b277 |
}
|
|
|
84b277 |
|
|
|
84b277 |
static int load_env_file_push(const char *filename, unsigned line,
|
|
|
84b277 |
- const char *key, char *value, void *userdata) {
|
|
|
84b277 |
+ const char *key, char *value, void *userdata, int *n_pushed) {
|
|
|
84b277 |
assert(utf8_is_valid(key));
|
|
|
84b277 |
|
|
|
84b277 |
if (value && !utf8_is_valid(value))
|
|
|
84b277 |
@@ -534,6 +539,9 @@ static int load_env_file_push(const char *filename, unsigned line,
|
|
|
84b277 |
free(p);
|
|
|
84b277 |
return r;
|
|
|
84b277 |
}
|
|
|
84b277 |
+
|
|
|
84b277 |
+ if (n_pushed)
|
|
|
84b277 |
+ (*n_pushed)++;
|
|
|
84b277 |
}
|
|
|
84b277 |
|
|
|
84b277 |
free(value);
|
|
|
84b277 |
@@ -547,7 +555,7 @@ int load_env_file(const char *fname, const char *newline, char ***rl) {
|
|
|
84b277 |
if (!newline)
|
|
|
84b277 |
newline = NEWLINE;
|
|
|
84b277 |
|
|
|
84b277 |
- r = parse_env_file_internal(fname, newline, load_env_file_push, &m);
|
|
|
84b277 |
+ r = parse_env_file_internal(fname, newline, load_env_file_push, &m, NULL);
|
|
|
84b277 |
if (r < 0) {
|
|
|
84b277 |
strv_free(m);
|
|
|
84b277 |
return r;
|