From: Michael Tremer Date: Sun, 26 Oct 2025 17:42:46 +0000 (+0000) Subject: file: Implement path formatting into the quick read function X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d14c3793de753322576bbfef8155facfbfe5641;p=collecty.git file: Implement path formatting into the quick read function Signed-off-by: Michael Tremer --- diff --git a/src/daemon/file.c b/src/daemon/file.c index b8d2082..3283a39 100644 --- a/src/daemon/file.c +++ b/src/daemon/file.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -316,10 +317,21 @@ int td_file_parse_json(td_file* self, sd_json_variant** ret, sd_json_parse_flags return 0; } -int td_read_uint64(td_ctx* ctx, const char* path, uint64_t* number) { +int td_read_uint64(td_ctx* ctx, uint64_t* number, const char* format, ...) { td_file* file = NULL; + char path[PATH_MAX]; + va_list args; int r; + // Format the path + va_start(args, format); + r = td_string_vformat(path, format, args); + va_end(args); + + // Handle formatting errors + if (r < 0) + goto ERROR; + // Open the file r = td_file_open_path(&file, ctx, path); if (r < 0) diff --git a/src/daemon/file.h b/src/daemon/file.h index 7f04659..5bb4ef9 100644 --- a/src/daemon/file.h +++ b/src/daemon/file.h @@ -77,7 +77,8 @@ int td_file_parse_json(td_file* self, sd_json_variant** json, sd_json_parse_flag // Shorthands -int td_read_uint64(td_ctx* ctx, const char* path, uint64_t* number); +int td_read_uint64(td_ctx* ctx, uint64_t* number, const char* format, ...) + __attribute__((format(printf, 3, 4))); int td_parse(td_ctx* ctx, const char* path, td_file_parser* parser); #endif /* TELEMETRY_FILE_H */ diff --git a/src/daemon/sources/conntrack.c b/src/daemon/sources/conntrack.c index 8a8b470..2cc4bbe 100644 --- a/src/daemon/sources/conntrack.c +++ b/src/daemon/sources/conntrack.c @@ -31,7 +31,7 @@ static int conntrack_heartbeat(td_ctx* ctx, td_source* source) { int r; // Read the total number of connections - r = td_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_count", &count); + r = td_read_uint64(ctx, &count, "/proc/sys/net/netfilter/nf_conntrack_count"); if (r < 0) { ERROR(ctx, "Failed to read %s: %s\n", "/proc/sys/net/netfilter/nf_conntrack_count", strerror(-r)); @@ -39,7 +39,7 @@ static int conntrack_heartbeat(td_ctx* ctx, td_source* source) { } // Read the maximum number of connections - r = td_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_max", &max); + r = td_read_uint64(ctx, &max, "/proc/sys/net/netfilter/nf_conntrack_max"); if (r < 0) { ERROR(ctx, "Failed to read %s: %s\n", "/proc/sys/net/netfilter/nf_conntrack_max", strerror(-r));