diff options
| author | Federico Angelilli <code@fedang.net> | 2024-09-08 19:53:37 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-09-08 19:53:37 +0200 |
| commit | ee8f2ac000a577f20a1a38c5ddd20831a8e38311 (patch) | |
| tree | 55a6db899e8921734693aa72d733a157064a43a6 /src/blocks | |
| parent | 123d035b8164999b086c08ed472cd83ead72599c (diff) | |
Add fs block
Diffstat (limited to 'src/blocks')
| -rw-r--r-- | src/blocks/fs.c | 100 | ||||
| -rw-r--r-- | src/blocks/ram.c | 3 | ||||
| -rw-r--r-- | src/blocks/scheme.c | 2 |
3 files changed, 103 insertions, 2 deletions
diff --git a/src/blocks/fs.c b/src/blocks/fs.c new file mode 100644 index 0000000..a47eb9c --- /dev/null +++ b/src/blocks/fs.c @@ -0,0 +1,100 @@ +#include <string.h> +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> + +#include <sys/statvfs.h> +#include <sys/stat.h> + +#include "scheme.h" + +#include "../any_log.h" + +static void block_fs_update(block_t *block) +{ + const char *path = ((char **)block->state)[0]; + const char *text = ((char **)block->state)[1]; + + struct statvfs sbuf; + assert(statvfs(path, &sbuf) == 0); + + static const char *fs_formats[] = { + "total", + "free", + "used", + NULL, + }; + + char buffer[32][3] = { 0 }; + snprintf(buffer[0], 32, "%ld", sbuf.f_blocks * sbuf.f_frsize); + snprintf(buffer[1], 32, "%ld", sbuf.f_bavail * sbuf.f_bsize); + snprintf(buffer[2], 32, "%ld", (sbuf.f_blocks - sbuf.f_bavail) * sbuf.f_bsize); + + const char *fs_values[] = { + buffer[0], + buffer[1], + buffer[2], + NULL, + }; + + free(block->text.text); + block->text.text = strformat(text, '%', fs_formats, fs_values); + assert(block->text.text != NULL); +} + +static void block_fs_finalize(block_t *block) +{ + free(block->state); +} + +static bool block_fs_validate(block_t *block, const block_scheme_t *scheme) +{ + if (block->text.text == NULL) { + log_error("Block '%s' requires key '%s'", block->label, "text"); + return false; + } + + if (strstr(block->text.text, "%{") == NULL) { + log_warn("Block '%s' does not use any fs variable", block->label); + + block->update_cb = 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); + return false; + } + + ((char **)block->state)[1] = block->text.text; + block->text.text = NULL; + + return true; +} + +static const config_entry_t block_fs_entries[] = { + { "path", CONFIG_STRING, NULL, 0 }, + { 0 }, +}; + +const block_scheme_t block_fs_scheme = { + .name = "fs", + .block = { + .type = BLOCK_TEXT, + .update_interval = { + .tv_sec = 20, + .tv_nsec = 0, + }, + .update_cb = block_fs_update, + .finalize_cb = block_fs_finalize, + }, + .size = 2 * sizeof(char *), + .entries = block_fs_entries, + .validate = block_fs_validate, +}; + + diff --git a/src/blocks/ram.c b/src/blocks/ram.c index 780ff9e..c6ed727 100644 --- a/src/blocks/ram.c +++ b/src/blocks/ram.c @@ -22,7 +22,6 @@ static void block_ram_update(block_t *block) &total, &unused, &available, &buffers, &cached)); fclose(meminfo); - static const char *ram_formats[] = { "total", "free", @@ -48,7 +47,7 @@ static void block_ram_update(block_t *block) NULL, }; - + free(block->text.text); block->text.text = strformat(block->state, '%', ram_formats, ram_values); assert(block->text.text != NULL); } diff --git a/src/blocks/scheme.c b/src/blocks/scheme.c index c474de3..a17ef3a 100644 --- a/src/blocks/scheme.c +++ b/src/blocks/scheme.c @@ -3,10 +3,12 @@ extern const block_scheme_t block_text_scheme; extern const block_scheme_t block_group_scheme; extern const block_scheme_t block_ram_scheme; +extern const block_scheme_t block_fs_scheme; const block_scheme_t *block_schemes[] = { &block_text_scheme, &block_group_scheme, &block_ram_scheme, + &block_fs_scheme, NULL, }; |
