dump_systemstate: Extract the function to read ini files 90/253490/5
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 15 Feb 2021 12:09:34 +0000 (13:09 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 9 Mar 2021 21:14:08 +0000 (22:14 +0100)
Change-Id: I53b79be8953bfc1d6c3c420913d8afa47cac5004

src/dump_systemstate/extras.c
src/dump_systemstate/extras.h

index 016acf6..88e42d8 100644 (file)
@@ -45,15 +45,6 @@ static inline void cleanup_dictionary(dictionary **ini)
        iniparser_freedict(*ini);
 }
 
-enum ini_fields {
-       INI_FIELD_TITLE = 0,
-       INI_FIELD_PATH,
-       INI_FIELD_ARGS,
-       INI_FIELD_ENV,
-       INI_FIELD_FLAG,
-       COUNT_INI_FIELDS,
-};
-
 static const char *const INI_KEYS[COUNT_INI_FIELDS] = {
        [INI_FIELD_TITLE] = "title",
        [INI_FIELD_PATH]  = "path",
@@ -64,23 +55,12 @@ static const char *const INI_KEYS[COUNT_INI_FIELDS] = {
 };
 static const size_t MAX_INI_KEY_LEN = 7;
 
-struct extra_dump_item {
-       // not separate named fields, for convenient iteration
-       char *fields[COUNT_INI_FIELDS];
-       int order;
-};
-
 void cleanup_extra_dump_item(struct extra_dump_item *edi)
 {
        for (size_t i = 0; i < ARRAY_SIZE(edi->fields); ++i)
                free(edi->fields[i]);
 }
 
-struct extra_items_vector {
-       size_t size;
-       struct extra_dump_item *data;
-};
-
 bool check_cmdflag(const char *const flag, int argc, char **argv)
 {
        if (!flag)
@@ -241,11 +221,6 @@ static int config_entry_filter(const struct dirent *de)
 
 int handle_items_vector(struct extra_items_vector *eiv, int out_fd, handle_ini_section_t handle_ini_section, int argc, char **argv)
 {
-       inline int cmp(const void *a, const void *b) {
-               return ((struct extra_dump_item *)a)->order - ((struct extra_dump_item *)b)->order;
-       }
-       qsort(eiv->data, eiv->size, sizeof *eiv->data, cmp);
-
        int ret = 0;
        for (size_t i = 0; i < eiv->size; ++i)
                ret |= handle_ini_section(out_fd, eiv->data + i, argc, argv);
@@ -259,11 +234,11 @@ void free_items_vector(struct extra_items_vector *eiv)
        free(eiv->data);
 }
 
-int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section, int argc, char **argv)
+int read_ini_files(int out_fd, char *dir_path, struct extra_items_vector *eiv)
 {
        assert(out_fd >= 0);
        assert(dir_path);
-       assert(handle_ini_section);
+       assert(eiv);
 
        const int dir_fd = open(dir_path, O_DIRECTORY | O_RDONLY);
        if (dir_fd < 0) {
@@ -279,11 +254,6 @@ int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini
                return EXIT_ERR;
        }
 
-       __attribute__((cleanup(free_items_vector))) struct extra_items_vector eiv = {
-               .size = 0,
-               .data = NULL,
-       };
-
        int ret = 0;
        for (int i = 0; i < entry_count; ++i) {
                struct dirent *const de = entries[i];
@@ -296,12 +266,37 @@ int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini
                snprintf(ini_path, sizeof ini_path, "%s/%s", dir_path, de->d_name);
                free(de);
 
-               ret |= handle_extra_ini(out_fd, &eiv, ini_path);
+               ret |= handle_extra_ini(out_fd, eiv, ini_path);
+       }
+
+       if (ret) {
+               inline int cmp(const void *a, const void *b) {
+                       return ((struct extra_dump_item *)a)->order - ((struct extra_dump_item *)b)->order;
+               }
+               qsort(eiv->data, eiv->size, sizeof *eiv->data, cmp);
        }
-       ret |= handle_items_vector(&eiv, out_fd, handle_ini_section, argc, argv);
 
        free(entries);
        close(dir_fd);
+
+       return ret;
+}
+
+int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section, int argc, char **argv)
+{
+       assert(out_fd >= 0);
+       assert(dir_path);
+       assert(handle_ini_section);
+
+       __attribute__((cleanup(free_items_vector))) struct extra_items_vector eiv = {
+               .size = 0,
+               .data = NULL,
+       };
+
+       int ret = 0;
+       ret |= read_ini_files(out_fd, dir_path, &eiv);
+       ret |= handle_items_vector(&eiv, out_fd, handle_ini_section, argc, argv);
+
        return ret;
 }
 
index 392bff0..3544cab 100644 (file)
@@ -18,6 +18,7 @@
 
 #pragma once
 
+#include <sys/types.h>
 #include "defs.h"
 
 #define DUMP_SYSTEMSTATE_CONFIG_DIR_PROGRAMS_PATH \
 #define DUMP_SYSTEMSTATE_CONFIG_DIR_FILES_PATH \
        DUMP_SYSTEMSTATE_CONFIG_DIR_PATH "/files"
 
-struct extra_dump_item;
+enum ini_fields {
+       INI_FIELD_TITLE = 0,
+       INI_FIELD_PATH,
+       INI_FIELD_ARGS,
+       INI_FIELD_ENV,
+       INI_FIELD_FLAG,
+       COUNT_INI_FIELDS,
+};
+
+struct extra_items_vector {
+       size_t size;
+       struct extra_dump_item *data;
+};
+
+struct extra_dump_item {
+       // not separate named fields, for convenient iteration
+       char *fields[COUNT_INI_FIELDS];
+       int order;
+};
+
 
 typedef int (*handle_ini_section_t)(int out_fd, struct extra_dump_item *, int argc, char **argv);
 
 int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section, int argc, char **argv);
 int handle_extra_file(int out_fd, struct extra_dump_item *item, int argc, char **argv);
 int handle_extra_program(int out_fd, struct extra_dump_item *item, int argc, char **argv);
+int read_ini_files(int out_fd, char *dir_path, struct extra_items_vector *eiv);