From: Michael Tremer Date: Wed, 22 Oct 2025 18:55:37 +0000 (+0000) Subject: source: Use rrd_info_r() to check if a RRD file exists X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4866f6cee0a546084a6e8d7c1797d1e321eae26;p=telemetry.git source: Use rrd_info_r() to check if a RRD file exists Signed-off-by: Michael Tremer --- diff --git a/src/daemon/source.c b/src/daemon/source.c index 6f529c9..b19ad67 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -24,7 +24,6 @@ #include #include #include -#include #include @@ -874,34 +873,22 @@ static int td_source_needs_migration(td_source* self, const char* path, rrd_info return 0; } -static int td_source_migrate(td_source* self, const char* path) { - rrd_info_t* info = NULL; +static int td_source_migrate(td_source* self, const char* path, rrd_info_t* info) { int r; - // Fetch information from the RRD file - info = rrd_info_r(path); - // Does this RRD need migration? r = td_source_needs_migration(self, path, info); if (r < 0) { ERROR(self->ctx, "Failed to check whether %s needs migration: %s\n", path, strerror(-r)); - goto ERROR; - - // Migrate - } else if (r) { - DEBUG(self->ctx, "%s needs migration!\n", path); - - // Create a new database but use the old one as source - r = td_source_create_database(self, path, path); - if (r < 0) - goto ERROR; + return r; } -ERROR: - if (info) - rrd_info_free(info); + // No need to migrate + if (r == 0) + return 0; - return r; + // Migrate the database + return td_source_create_database(self, path, path); } /* @@ -909,7 +896,7 @@ ERROR: */ static int td_source_commit_samples(td_source* self, const char* object, unsigned int num_samples, const char** samples) { - struct stat st = {}; + rrd_info_t* info = NULL; char path[PATH_MAX]; int r; @@ -918,25 +905,27 @@ static int td_source_commit_samples(td_source* self, if (r < 0) return r; - // Try to stat() the file - r = stat(path, &st); - if (r < 0) { + // Fetch information from the RRD file + info = rrd_info_r(path); + if (!info) { switch (errno) { case ENOENT: r = td_source_create_database(self, path, NULL); if (r < 0) - return r; + goto ERROR; break; default: - return -errno; + ERROR(self->ctx, "Failed to read RRD header from %s: %m\n", path); + r = -errno; + goto ERROR; } } // Migrate the RRD file - r = td_source_migrate(self, path); + r = td_source_migrate(self, path, info); if (r < 0) - return r; + goto ERROR; // Write the samples r = rrd_update_r(path, NULL, num_samples, samples); @@ -946,7 +935,11 @@ static int td_source_commit_samples(td_source* self, return -EFAULT; } - return 0; +ERROR: + if (info) + rrd_info_free(info); + + return r; } int td_source_commit_metrics(td_source* self, const char* object,