config: sanity checks.

Avoid missing issues like this again.
This commit is contained in:
Alastair Poole 2020-09-05 22:21:57 +01:00
parent 94f4e9c620
commit 74efbb1178
2 changed files with 40 additions and 9 deletions

View File

@ -29,6 +29,33 @@ config_shutdown(void)
efreet_shutdown();
}
static void
_config_fail(const char *msg)
{
fprintf(stderr, "ERR: config %s.\n", msg);
exit(1);
}
static void
_config_check(Evisum_Config *cfg)
{
if (cfg->version > CONFIG_VERSION)
_config_fail("version");
if (cfg->poll_delay <= 0)
_config_fail("poll");
}
static Evisum_Config *
_config_init()
{
Evisum_Config *cfg = calloc(1, sizeof(Evisum_Config));
cfg->version = CONFIG_VERSION;
cfg->poll_delay = 3;
return cfg;
}
Evisum_Config *
config_load(void)
{
@ -39,23 +66,27 @@ config_load(void)
const char *path = _config_file_path();
if (!ecore_file_exists(path))
{
cfg = calloc(1, sizeof(Evisum_Config));
cfg->version = CONFIG_VERSION;
cfg->poll_delay = 3;
cfg = _config_init();
f = eet_open(path, EET_FILE_MODE_WRITE);
if (!f) _config_fail("create");
eet_write(f, "Config", cfg, sizeof(Evisum_Config), 0);
eet_close(f);
}
else
{
f = eet_open(path, EET_FILE_MODE_READ);
if (!f) exit(127);
if (!f) _config_fail("read");
cfg = eet_read(f, "Config", &size);
// Correct a bogus poll delay.
if (cfg->poll_delay <= 0)
_config_check(cfg);
if (cfg->version < CONFIG_VERSION)
{
fprintf(stderr, "WARNING: correcting a bad configuration (sorry!).\n");
cfg->poll_delay = 3;
free(cfg);
fprintf(stderr, "INFO: Reinitialising configuration\n");
cfg = _config_init();
}
eet_close(f);
}

View File

@ -3,7 +3,7 @@
#include "ui/ui.h"
#define CONFIG_VERSION 0x0004
#define CONFIG_VERSION 0x0005
typedef struct _Evisum_Config
{