]> git.ipfire.org Git - telemetry.git/commitdiff
source: Use rrd_info_r() to check if a RRD file exists
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Oct 2025 18:55:37 +0000 (18:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Oct 2025 18:55:37 +0000 (18:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/source.c

index 6f529c9b7b0a33bc185eb0f4e53881a17d33c89f..b19ad67d4fa73914a38f30c2dfad3adc375a51c7 100644 (file)
@@ -24,7 +24,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/stat.h>
 
 #include <rrd.h>
 
@@ -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,