Make API CTF-agnostic
[babeltrace.git] / plugins / ctf / fs-src / metadata.c
index 13cc816e124596c3b9560c4bc659decfdec1325d..e949374d6320dbfa2005961ae111eaa32df05c2f 100644 (file)
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdbool.h>
-#include <assert.h>
+#include <babeltrace/assert-internal.h>
 #include <glib.h>
 #include <babeltrace/compat/uuid-internal.h>
 #include <babeltrace/compat/memstream-internal.h>
-
-#define PRINT_ERR_STREAM       ctf_fs->error_fp
-#define PRINT_PREFIX           "ctf-fs-metadata"
-#include "print.h"
+#include <babeltrace/babeltrace.h>
 
 #include "fs.h"
 #include "file.h"
 #include "metadata.h"
 #include "../common/metadata/decoder.h"
 
-#define NSEC_PER_SEC 1000000000LL
+#define BT_LOG_TAG "PLUGIN-CTF-FS-METADATA-SRC"
+#include "logging.h"
 
 BT_HIDDEN
 FILE *ctf_fs_metadata_open_file(const char *trace_path)
 {
-       GString *metadata_path = g_string_new(trace_path);
+       GString *metadata_path;
        FILE *fp = NULL;
 
+       metadata_path = g_string_new(trace_path);
        if (!metadata_path) {
-               goto error;
+               goto end;
        }
 
-       g_string_append(metadata_path, "/" CTF_FS_METADATA_FILENAME);
+       g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
        fp = fopen(metadata_path->str, "rb");
-       if (!fp) {
-               goto error;
-       }
-
-       goto end;
-
-error:
-       if (fp) {
-               fclose(fp);
-               fp = NULL;
-       }
-
-end:
        g_string_free(metadata_path, TRUE);
+end:
        return fp;
 }
 
-static struct ctf_fs_file *get_file(struct ctf_fs_component *ctf_fs,
-               const char *trace_path)
+static struct ctf_fs_file *get_file(const char *trace_path)
 {
-       struct ctf_fs_file *file = ctf_fs_file_create(ctf_fs);
+       struct ctf_fs_file *file = ctf_fs_file_create();
 
        if (!file) {
                goto error;
        }
 
        g_string_append(file->path, trace_path);
-       g_string_append(file->path, "/" CTF_FS_METADATA_FILENAME);
+       g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
 
-       if (ctf_fs_file_open(ctf_fs, file, "rb")) {
+       if (ctf_fs_file_open(file, "rb")) {
                goto error;
        }
 
@@ -100,41 +86,49 @@ end:
        return file;
 }
 
-int ctf_fs_metadata_set_trace(struct ctf_fs_component *ctf_fs)
+int ctf_fs_metadata_set_trace(struct ctf_fs_trace *ctf_fs_trace,
+               struct ctf_fs_metadata_config *config)
 {
        int ret = 0;
        struct ctf_fs_file *file = NULL;
-       struct ctf_metadata_decoder *metadata_decoder = NULL;
+       struct ctf_metadata_decoder_config decoder_config = {
+               .clock_class_offset_s = config ? config->clock_class_offset_s : 0,
+               .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
+       };
 
-       file = get_file(ctf_fs, ctf_fs->trace_path->str);
+       file = get_file(ctf_fs_trace->path->str);
        if (!file) {
-               PERR("Cannot create metadata file object\n");
+               BT_LOGE("Cannot create metadata file object");
                ret = -1;
                goto end;
        }
 
-       metadata_decoder = ctf_metadata_decoder_create(ctf_fs->error_fp,
-               ctf_fs->options.clock_offset * NSEC_PER_SEC +
-               ctf_fs->options.clock_offset_ns);
-       if (!metadata_decoder) {
-               PERR("Cannot create metadata decoder object\n");
+       ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(
+               config ? &decoder_config : NULL,
+               ctf_fs_trace->name->str);
+       if (!ctf_fs_trace->metadata->decoder) {
+               BT_LOGE("Cannot create metadata decoder object");
                ret = -1;
                goto end;
        }
 
-       ret = ctf_metadata_decoder_decode(metadata_decoder, file->fp);
+       ret = ctf_metadata_decoder_decode(ctf_fs_trace->metadata->decoder,
+               file->fp);
        if (ret) {
-               PERR("Cannot decode metadata file\n");
+               BT_LOGE("Cannot decode metadata file");
                goto end;
        }
 
-       ctf_fs->metadata->trace = ctf_metadata_decoder_get_trace(
-               metadata_decoder);
-       assert(ctf_fs->metadata->trace);
+       ctf_fs_trace->metadata->trace = ctf_metadata_decoder_get_ir_trace(
+               ctf_fs_trace->metadata->decoder);
+       BT_ASSERT(ctf_fs_trace->metadata->trace);
+       ctf_fs_trace->metadata->tc =
+               ctf_metadata_decoder_borrow_ctf_trace_class(
+                       ctf_fs_trace->metadata->decoder);
+       BT_ASSERT(ctf_fs_trace->metadata->tc);
 
 end:
        ctf_fs_file_destroy(file);
-       ctf_metadata_decoder_destroy(metadata_decoder);
        return ret;
 }
 
@@ -153,4 +147,8 @@ void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
        if (metadata->trace) {
                BT_PUT(metadata->trace);
        }
+
+       if (metadata->decoder) {
+               ctf_metadata_decoder_destroy(metadata->decoder);
+       }
 }
This page took 0.027457 seconds and 4 git commands to generate.