ctf.fs source: add `get-metadata-info` query info action
[babeltrace.git] / plugins / ctf / fs / fs.c
index 75be8f13a552b2b0ee164821e61e50612508baa8..b406ee96deee4e494626c9ad4542a4c9383491b5 100644 (file)
@@ -33,6 +33,7 @@
 #include <babeltrace/component/notification/event.h>
 #include <babeltrace/component/notification/packet.h>
 #include <babeltrace/component/notification/heap.h>
+#include <plugins-common.h>
 #include <glib.h>
 #include <assert.h>
 #include <unistd.h>
 #define PRINT_ERR_STREAM       ctf_fs->error_fp
 #define PRINT_PREFIX           "ctf-fs"
 #include "print.h"
+#define METADATA_TEXT_SIG      "/* CTF 1.8"
 
 BT_HIDDEN
 bool ctf_fs_debug;
 
-static
 enum bt_notification_iterator_status ctf_fs_iterator_next(
                struct bt_notification_iterator *iterator);
 
-static
 struct bt_notification *ctf_fs_iterator_get(
                struct bt_notification_iterator *iterator)
 {
@@ -245,7 +245,6 @@ end:
        return ret;
 }
 
-static
 enum bt_notification_iterator_status ctf_fs_iterator_next(
                struct bt_notification_iterator *iterator)
 {
@@ -352,6 +351,9 @@ end:
 static
 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it)
 {
+       if (!ctf_it) {
+               return;
+       }
        bt_put(ctf_it->current_notification);
        bt_put(ctf_it->pending_notifications);
        if (ctf_it->pending_streams) {
@@ -363,7 +365,6 @@ void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it)
        g_free(ctf_it);
 }
 
-static
 void ctf_fs_iterator_destroy(struct bt_notification_iterator *it)
 {
        void *data = bt_notification_iterator_get_private_data(it);
@@ -583,24 +584,25 @@ end:
        return ret;
 }
 
-enum bt_component_status ctf_fs_iterator_init(struct bt_component *source,
-               struct bt_notification_iterator *it)
+enum bt_notification_iterator_status ctf_fs_iterator_init(struct bt_component *source,
+               struct bt_notification_iterator *it,
+               UNUSED_VAR void *init_method_data)
 {
        struct ctf_fs_iterator *ctf_it;
        struct ctf_fs_component *ctf_fs;
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
 
        assert(source && it);
 
        ctf_fs = bt_component_get_private_data(source);
        if (!ctf_fs) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+               ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
                goto end;
        }
 
        ctf_it = g_new0(struct ctf_fs_iterator, 1);
        if (!ctf_it) {
-               ret = BT_COMPONENT_STATUS_NOMEM;
+               ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
                goto end;
        }
 
@@ -625,22 +627,6 @@ enum bt_component_status ctf_fs_iterator_init(struct bt_component *source,
                goto error;
        }
 
-       ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get);
-       if (ret) {
-               goto error;
-       }
-
-       ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next);
-       if (ret) {
-               goto error;
-       }
-
-       ret = bt_notification_iterator_set_destroy_cb(it,
-                       ctf_fs_iterator_destroy);
-       if (ret) {
-               goto error;
-       }
-
        ret = bt_notification_iterator_set_private_data(it, ctf_it);
        if (ret) {
                goto error;
@@ -657,6 +643,9 @@ error:
 static
 void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs)
 {
+       if (!ctf_fs) {
+               return;
+       }
        if (ctf_fs->trace_path) {
                g_string_free(ctf_fs->trace_path, TRUE);
        }
@@ -723,11 +712,10 @@ end:
 
 BT_HIDDEN
 enum bt_component_status ctf_fs_init(struct bt_component *source,
-               struct bt_value *params, void *init_method_data)
+               struct bt_value *params, UNUSED_VAR void *init_method_data)
 {
        struct ctf_fs_component *ctf_fs;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
-       (void) init_method_data;
 
        assert(source);
        ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
@@ -748,3 +736,129 @@ error:
         ctf_fs_destroy_data(ctf_fs);
        return ret;
 }
+
+BT_HIDDEN
+struct bt_value *ctf_fs_query_info(struct bt_component_class *comp_class,
+               const char *action, struct bt_value *params)
+{
+       struct bt_value *results = NULL;
+       struct bt_value *path_value = NULL;
+       char *metadata_text = NULL;
+       FILE *metadata_fp = NULL;
+       GString *g_metadata_text = NULL;
+
+       if (strcmp(action, "get-metadata-info") == 0) {
+               int ret;
+               int bo;
+               const char *path;
+               bool is_packetized;
+
+               results = bt_value_map_create();
+               if (!results) {
+                       goto error;
+               }
+
+               if (!bt_value_is_map(params)) {
+                       fprintf(stderr,
+                               "Query info parameters is not a map value object\n");
+                       goto error;
+               }
+
+               path_value = bt_value_map_get(params, "path");
+               ret = bt_value_string_get(path_value, &path);
+               if (ret) {
+                       fprintf(stderr,
+                               "Cannot get `path` string parameter\n");
+                       goto error;
+               }
+
+               assert(path);
+               metadata_fp = ctf_fs_metadata_open_file(path);
+               if (!metadata_fp) {
+                       fprintf(stderr,
+                               "Cannot open trace at path `%s`\n", path);
+                       goto error;
+               }
+
+               is_packetized = ctf_metadata_is_packetized(metadata_fp, &bo);
+
+               if (is_packetized) {
+                       ret = ctf_metadata_packetized_file_to_buf(NULL,
+                               metadata_fp, (uint8_t **) &metadata_text, bo);
+                       if (ret) {
+                               fprintf(stderr,
+                                       "Cannot decode packetized metadata file\n");
+                               goto error;
+                       }
+               } else {
+                       long filesize;
+
+                       fseek(metadata_fp, 0, SEEK_END);
+                       filesize = ftell(metadata_fp);
+                       rewind(metadata_fp);
+                       metadata_text = malloc(filesize + 1);
+                       if (!metadata_text) {
+                               fprintf(stderr,
+                                       "Cannot allocate buffer for metadata text\n");
+                               goto error;
+                       }
+
+                       if (fread(metadata_text, filesize, 1, metadata_fp) !=
+                                       1) {
+                               fprintf(stderr,
+                                       "Cannot read metadata file\n");
+                               goto error;
+                       }
+
+                       metadata_text[filesize] = '\0';
+               }
+
+               g_metadata_text = g_string_new(NULL);
+               if (!g_metadata_text) {
+                       goto error;
+               }
+
+               if (strncmp(metadata_text, METADATA_TEXT_SIG,
+                               sizeof(METADATA_TEXT_SIG) - 1) != 0) {
+                       g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
+                       g_string_append(g_metadata_text, " */\n\n");
+               }
+
+               g_string_append(g_metadata_text, metadata_text);
+
+               ret = bt_value_map_insert_string(results, "text",
+                       g_metadata_text->str);
+               if (ret) {
+                       fprintf(stderr, "Cannot insert metadata text into results\n");
+                       goto error;
+               }
+
+               ret = bt_value_map_insert_bool(results, "is-packetized",
+                       is_packetized);
+               if (ret) {
+                       fprintf(stderr, "Cannot insert is packetized into results\n");
+                       goto error;
+               }
+       } else {
+               fprintf(stderr, "Unknown query info action `%s`\n", action);
+               goto error;
+       }
+
+       goto end;
+
+error:
+       BT_PUT(results);
+
+end:
+       bt_put(path_value);
+       free(metadata_text);
+
+       if (g_metadata_text) {
+               g_string_free(g_metadata_text, TRUE);
+       }
+
+       if (metadata_fp) {
+               fclose(metadata_fp);
+       }
+       return results;
+}
This page took 0.043416 seconds and 4 git commands to generate.