From e85ad7414c063e42296f7ef4e202829567484ac0 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Tue, 12 Nov 2024 01:44:14 +0100 Subject: Fix config and blocks --- src/blocks/fs.c | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'src/blocks/fs.c') diff --git a/src/blocks/fs.c b/src/blocks/fs.c index 65e8fe2..9bb4452 100644 --- a/src/blocks/fs.c +++ b/src/blocks/fs.c @@ -10,13 +10,18 @@ #include "../any_log.h" +typedef struct { + block_text_t block; + char *format; + char *path; +} block_fs_t; + static void block_fs_update(block_t *block) { - const char *path = ((char **)block->state)[0]; - const char *text = ((char **)block->state)[1]; + block_fs_t *fs = (block_fs_t *)block; struct statvfs sbuf; - assert(statvfs(path, &sbuf) == 0); + assert(statvfs(fs->path, &sbuf) == 0); static const char *fs_formats[] = { "total", @@ -38,47 +43,48 @@ static void block_fs_update(block_t *block) buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], NULL, }; - free(block->text.text); - block->text.text = strformat(text, '%', fs_formats, fs_values); - assert(block->text.text != NULL); + free(fs->block.text); + fs->block.text = strformat(fs->format, '%', fs_formats, fs_values); + assert(fs->block.text != NULL); } -static void block_fs_finalize(block_t *block) +static void block_fs_clean(block_t *block) { - free(block->state); + block_fs_t *fs = (block_fs_t *)block; + free(fs->format); + free(fs->path); } static bool block_fs_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_fs_t *fs = (block_fs_t *)block; + + if (fs->block.text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } - if (strstr(block->text.text, "%{") == NULL) { + if (strstr(fs->block.text, "%{") == NULL) { log_warn("Block '%s' does not use any fs variable", block->label); - block->update_cb = NULL; + block->update_fn = NULL; log_debug("Disabled updates for block '%s'", block->label); return true; } - const char *path = ((char **)block->state)[0]; struct stat sbuf; - - if (path == NULL || stat(path, &sbuf) < 0) { - log_error("Block '%s' was given an invalid path '%s'", block->label, path); + if (fs->path == NULL || stat(fs->path, &sbuf) < 0) { + log_error("Block '%s' was given an invalid path '%s'", block->label, fs->path); return false; } - ((char **)block->state)[1] = block->text.text; - block->text.text = NULL; - + fs->format = fs->block.text; + fs->block.text = NULL; return true; } static const config_entry_t block_fs_entries[] = { - { "path", CONFIG_STRING, NULL, 0 }, + { "path", CONFIG_STRING, NULL, offsetof(block_fs_t, path) }, { 0 }, }; @@ -90,10 +96,10 @@ const block_scheme_t block_fs_scheme = { .tv_sec = 20, .tv_nsec = 0, }, - .update_cb = block_fs_update, - .finalize_cb = block_fs_finalize, + .update_fn = block_fs_update, + .clean_fn = block_fs_clean, }, - .size = 2 * sizeof(char *), + .size = sizeof(block_fs_t), .entries = block_fs_entries, .validate = block_fs_validate, }; -- cgit v1.2.3