Blob Blame History Raw
From 3a68febdb636924b941a84e123b6e51e042e1273 Mon Sep 17 00:00:00 2001
From: Michal Sekletar <msekleta@redhat.com>
Date: Mon, 22 Sep 2014 09:38:38 +0200
Subject: [PATCH] fileio: make parse_env_file() return number of parsed items

This commit introduces possibility to call parse_env_file_internal() and hand
over extra argument where we will accumulate how many items were successfully
parsed and pushed by callback. We make use of this in parse_env_file() and
return number of parsed items on success instead of always returning zero.

As a side-effect this commit should fix bug that locale settings in
/etc/locale.conf are not overriden by options passed via kernel command line.

Conflicts:
        src/shared/fileio.c

(cherry picked from commit a5f6c30da3ac58081108221bf8a0f6f1d84b33a9)

Resolves: #1069420
---
 src/shared/fileio.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/shared/fileio.c b/src/shared/fileio.c
index 733b320..fbc28bd 100644
--- a/src/shared/fileio.c
+++ b/src/shared/fileio.c
@@ -180,8 +180,9 @@ static int parse_env_file_internal(
                 const char *fname,
                 const char *newline,
                 int (*push) (const char *filename, unsigned line,
-                             const char *key, char *value, void *userdata),
-                void *userdata) {
+                             const char *key, char *value, void *userdata, int *n_pushed),
+                void *userdata,
+                int *n_pushed) {
 
         _cleanup_free_ char *contents = NULL, *key = NULL;
         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;
@@ -268,7 +269,7 @@ static int parse_env_file_internal(
                                 if (last_key_whitespace != (size_t) -1)
                                         key[last_key_whitespace] = 0;
 
-                                r = push(fname, line, key, value, userdata);
+                                r = push(fname, line, key, value, userdata, n_pushed);
                                 if (r < 0)
                                         goto fail;
 
@@ -313,7 +314,7 @@ static int parse_env_file_internal(
                                 if (last_key_whitespace != (size_t) -1)
                                         key[last_key_whitespace] = 0;
 
-                                r = push(fname, line, key, value, userdata);
+                                r = push(fname, line, key, value, userdata, n_pushed);
                                 if (r < 0)
                                         goto fail;
 
@@ -448,7 +449,7 @@ static int parse_env_file_internal(
                 if (last_key_whitespace != (size_t) -1)
                         key[last_key_whitespace] = 0;
 
-                r = push(fname, line, key, value, userdata);
+                r = push(fname, line, key, value, userdata, n_pushed);
                 if (r < 0)
                         goto fail;
         }
@@ -461,7 +462,7 @@ fail:
 }
 
 static int parse_env_file_push(const char *filename, unsigned line,
-                               const char *key, char *value, void *userdata) {
+                               const char *key, char *value, void *userdata, int *n_pushed) {
         assert(utf8_is_valid(key));
 
         if (value && !utf8_is_valid(value))
@@ -484,6 +485,10 @@ static int parse_env_file_push(const char *filename, unsigned line,
                                 va_end(aq);
                                 free(*v);
                                 *v = value;
+
+                                if (n_pushed)
+                                        (*n_pushed)++;
+
                                 return 1;
                         }
                 }
@@ -500,20 +505,20 @@ int parse_env_file(
                 const char *newline, ...) {
 
         va_list ap;
-        int r;
+        int r, n_pushed = 0;
 
         if (!newline)
                 newline = NEWLINE;
 
         va_start(ap, newline);
-        r = parse_env_file_internal(fname, newline, parse_env_file_push, &ap);
+        r = parse_env_file_internal(fname, newline, parse_env_file_push, &ap, &n_pushed);
         va_end(ap);
 
-        return r;
+        return r < 0 ? r : n_pushed;
 }
 
 static int load_env_file_push(const char *filename, unsigned line,
-                              const char *key, char *value, void *userdata) {
+                              const char *key, char *value, void *userdata, int *n_pushed) {
         assert(utf8_is_valid(key));
 
         if (value && !utf8_is_valid(value))
@@ -534,6 +539,9 @@ static int load_env_file_push(const char *filename, unsigned line,
                         free(p);
                         return r;
                 }
+
+                if (n_pushed)
+                        (*n_pushed)++;
         }
 
         free(value);
@@ -547,7 +555,7 @@ int load_env_file(const char *fname, const char *newline, char ***rl) {
         if (!newline)
                 newline = NEWLINE;
 
-        r = parse_env_file_internal(fname, newline, load_env_file_push, &m);
+        r = parse_env_file_internal(fname, newline, load_env_file_push, &m, NULL);
         if (r < 0) {
                 strv_free(m);
                 return r;