config: Separate config loading from processing 72/220172/5
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 13 Dec 2019 12:11:08 +0000 (13:11 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 18 Dec 2019 14:56:58 +0000 (15:56 +0100)
Change-Id: Ief38894e03904f9947ee72773f07044280f62b41

src/shared/config.c

index 3154752..1e728c4 100644 (file)
@@ -49,9 +49,55 @@ enum ReportType report_type_from_str(const char *report_type_str)
        return REP_TYPE_INVALID;
 }
 
-bool config_init(config_t *c, const char *const path)
+static bool config_load_from_dict(config_t *c, dictionary *ini)
 {
        assert(c);
+
+       char *str = iniparser_getstring(ini, CRASH_SECTION ":CrashRootPath", NULL);
+       if (str) {
+               char *crash_root_path = strdup(str);
+               if (!crash_root_path) {
+                       _E("config: Unable to set CrashRootPath - aborting");
+                       return false;
+               }
+
+               free(c->crash_root_path);
+               c->crash_root_path = crash_root_path;
+       }
+
+       /* strdup() can technically fail, but we don't mind. It is better to
+        * create a report without the extra script than to abort completely. */
+       str = iniparser_getstring(ini, CRASH_SECTION ":ExtraScript", NULL);
+       if (str) {
+               char *extra_script = strdup(str);
+               if (extra_script) {
+                       free(c->extra_script);
+                       c->extra_script = extra_script;
+               }
+       }
+
+       str = iniparser_getstring(ini, CRASH_SECTION ":ReportType", NULL);
+       if (str) {
+               int type = report_type_from_str(str);
+               if (report_type_to_str(type))
+                       c->report_type = type;
+       }
+
+#define UPDATE(where, type, key) where = iniparser_get##type(ini, CRASH_SECTION ":" key, where)
+
+       UPDATE(c->system_max_use, int, "SystemMaxUse");
+       UPDATE(c->max_retention_sec, int, "MaxRetentionSec");
+       UPDATE(c->max_crash_dump, int, "MaxCrashDump");
+       UPDATE(c->dump_core, boolean, "DumpCore");
+       UPDATE(c->allow_zip, boolean, "AllowZip");
+
+#undef UPDATE
+
+       return true;
+}
+
+static bool config_load_from_path(config_t *c, const char *const path)
+{
        assert(path);
 
        dictionary *ini = iniparser_load(path);
@@ -60,37 +106,64 @@ bool config_init(config_t *c, const char *const path)
                return false;
        }
 
-       bool ret = false;
+       bool ret = config_load_from_dict(c, ini);
+       iniparser_freedict(ini);
 
-#define GET(type, key, defval) iniparser_get##type(ini, CRASH_SECTION ":" key, defval)
+       return ret;
+}
 
-       c->crash_root_path = strdup(GET(string, "CrashRootPath", CRASH_ROOT_PATH));
-       if (!c->crash_root_path)
-               goto out;
+static bool config_apply_defaults(config_t *c)
+{
+       assert(c);
 
-       /* strdup() can technically fail, but we don't mind. It is better to
-        * create a report without the extra script than to abort completely. */
-       char *extrascript = GET(string, "ExtraScript", NULL);
-       c->extra_script = extrascript ? strdup(extrascript) : NULL;
+       memset(c, 0, sizeof(*c));
 
-       char *reptype = GET(string, "ReportType", (char *)report_type_strmap[REP_TYPE_FULL]);
-       c->report_type = report_type_from_str(reptype);
-       if (!report_type_to_str(c->report_type))
-               goto out;
+       c->crash_root_path = strdup(CRASH_ROOT_PATH);
+       c->report_type = REP_TYPE_FULL;
+       c->system_max_use = SYSTEM_MAX_USE;
+       c->system_keep_free = SYSTEM_KEEP_FREE;
+       c->max_retention_sec = MAX_RETENTION_SEC;
+       c->max_crash_dump = MAX_CRASH_DUMP;
+       c->dump_core = DUMP_CORE;
+       c->allow_zip = ALLOW_ZIP;
 
-       c->system_max_use = GET(int, "SystemMaxUse", SYSTEM_MAX_USE);
-       c->system_keep_free = GET(int, "SystemKeepFree", SYSTEM_KEEP_FREE);
-       c->max_retention_sec = GET(int, "MaxRetentionSec", MAX_RETENTION_SEC);
-       c->max_crash_dump = GET(int, "MaxCrashDump", MAX_CRASH_DUMP);
-       c->dump_core = GET(boolean, "DumpCore", DUMP_CORE);
-       c->allow_zip = GET(boolean, "AllowZip", ALLOW_ZIP);
+       return c->crash_root_path != NULL;
+}
 
-#undef GET
+static void config_dump(config_t *c)
+{
+       assert(c);
 
-       ret = true;
-out:
-       iniparser_freedict(ini);
-       return ret;
+       _D("config: crash_root_path = %s\n"
+          "config: extra_script = %s\n"
+          "config: report_type = %s\n"
+          "config: system_max_use = %d\n"
+          "config: system_keep_free = %d\n"
+          "config: max_retention_sec = %d\n"
+          "config: max_crash_dump = %d\n"
+          "config: dump_core = %d\n"
+          "config: allow_zip = %d\n",
+          c->crash_root_path,
+          c->extra_script,
+          report_type_to_str(c->report_type),
+          c->system_max_use,
+          c->system_keep_free,
+          c->max_retention_sec,
+          c->max_crash_dump,
+          c->dump_core,
+          c->allow_zip);
+}
+
+bool config_init(config_t *c, const char *const path)
+{
+       if (!config_apply_defaults(c) || !config_load_from_path(c, path)) {
+               _E("config: Unable to initialize configuration");
+               return false;
+       }
+
+       config_dump(c);
+
+       return true;
 }
 
 void config_free(config_t *c)