Add query executor
[babeltrace.git] / plugins / ctf / fs-src / fs.c
index a0008ef21f820fe2a0247066ad247b159ef09398..e36a70c4a7fe8fcfa9a543b6568d269d97d35962 100644 (file)
@@ -48,6 +48,7 @@
 #include "file.h"
 #include "../common/metadata/decoder.h"
 #include "../common/notif-iter/notif-iter.h"
+#include "../common/utils/utils.h"
 #include "query.h"
 
 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
@@ -193,7 +194,7 @@ enum bt_notification_iterator_status ctf_fs_iterator_init(
        }
 
        ret = bt_private_notification_iterator_set_user_data(it, notif_iter_data);
-       if (ret) {
+       if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
                goto error;
        }
 
@@ -203,10 +204,6 @@ enum bt_notification_iterator_status ctf_fs_iterator_init(
 error:
        (void) bt_private_notification_iterator_set_user_data(it, NULL);
 
-       if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
-               ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
-       }
-
 end:
        ctf_fs_notif_iter_data_destroy(notif_iter_data);
        return ret;
@@ -407,51 +404,6 @@ end:
        return stream_instance_id;
 }
 
-struct bt_ctf_stream_class *stream_class_from_packet_header(
-               struct ctf_fs_trace *ctf_fs_trace,
-               struct bt_ctf_field *packet_header_field)
-{
-       struct bt_ctf_field *stream_id_field = NULL;
-       struct bt_ctf_stream_class *stream_class = NULL;
-       uint64_t stream_id = -1ULL;
-       int ret;
-
-       if (!packet_header_field) {
-               goto single_stream_class;
-       }
-
-       stream_id_field = bt_ctf_field_structure_get_field_by_name(
-               packet_header_field, "stream_id");
-       if (!stream_id_field) {
-               goto single_stream_class;
-       }
-
-       ret = bt_ctf_field_unsigned_integer_get_value(stream_id_field,
-               &stream_id);
-       if (ret) {
-               stream_id = -1ULL;
-       }
-
-       if (stream_id == -1ULL) {
-single_stream_class:
-               /* Single stream class */
-               if (bt_ctf_trace_get_stream_class_count(
-                               ctf_fs_trace->metadata->trace) == 0) {
-                       goto end;
-               }
-
-               stream_class = bt_ctf_trace_get_stream_class_by_index(
-                       ctf_fs_trace->metadata->trace, 0);
-       } else {
-               stream_class = bt_ctf_trace_get_stream_class_by_id(
-                       ctf_fs_trace->metadata->trace, stream_id);
-       }
-
-end:
-       bt_put(stream_id_field);
-       return stream_class;
-}
-
 uint64_t get_packet_context_timestamp_begin_ns(
                struct ctf_fs_trace *ctf_fs_trace,
                struct bt_ctf_field *packet_context_field)
@@ -602,6 +554,26 @@ end:
        return ds_file_group;
 }
 
+/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
+static
+void array_insert(GPtrArray *array, gpointer element, size_t pos)
+{
+       size_t original_array_len = array->len;
+
+       /* Allocate an unused element at the end of the array. */
+       g_ptr_array_add(array, NULL);
+
+       /* If we are not inserting at the end, move the elements by one. */
+       if (pos < original_array_len) {
+               memmove(&(array->pdata[pos + 1]),
+                       &(array->pdata[pos]),
+                       (original_array_len - pos) * sizeof(gpointer));
+       }
+
+       /* Insert the value and bump the array len */
+       array->pdata[pos] = element;
+}
+
 static
 int ctf_fs_ds_file_group_add_ds_file_info(
                struct ctf_fs_ds_file_group *ds_file_group,
@@ -629,12 +601,7 @@ int ctf_fs_ds_file_group_add_ds_file_info(
                }
        }
 
-       if (i == ds_file_group->ds_file_infos->len) {
-               /* Append instead */
-               i = -1;
-       }
-
-       g_ptr_array_insert(ds_file_group->ds_file_infos, i, ds_file_info);
+       array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
        ds_file_info = NULL;
        goto end;
 
@@ -687,8 +654,8 @@ int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
                packet_header_field);
        begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace,
                packet_context_field);
-       stream_class = stream_class_from_packet_header(ctf_fs_trace,
-               packet_header_field);
+       stream_class = ctf_utils_stream_class_from_packet_header(
+               ctf_fs_trace->metadata->trace, packet_header_field);
        if (!stream_class) {
                goto error;
        }
@@ -815,13 +782,13 @@ int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
 
                if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
                        /* Ignore the metadata stream. */
-                       BT_LOGD("Ignoring metadata file `%s/%s`",
+                       BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
                                ctf_fs_trace->path->str, basename);
                        continue;
                }
 
                if (basename[0] == '.') {
-                       BT_LOGD("Ignoring hidden file `%s/%s`",
+                       BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
                                ctf_fs_trace->path->str, basename);
                        continue;
                }
@@ -829,13 +796,13 @@ int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
                /* Create the file. */
                file = ctf_fs_file_create();
                if (!file) {
-                       BT_LOGE("Cannot create stream file object for file `%s/%s`",
+                       BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
                                ctf_fs_trace->path->str, basename);
                        goto error;
                }
 
                /* Create full path string. */
-               g_string_append_printf(file->path, "%s/%s",
+               g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
                                ctf_fs_trace->path->str, basename);
                if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
                        BT_LOGD("Ignoring non-regular file `%s`",
@@ -899,7 +866,7 @@ int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
 
                g_string_free(name, TRUE);
 
-               if (!ds_file_group) {
+               if (!ds_file_group->stream) {
                        BT_LOGE("Cannot create stream for DS file group: "
                                "addr=%p, stream-name=\"%s\"",
                                ds_file_group, name->str);
@@ -1037,7 +1004,7 @@ int path_is_ctf_trace(const char *path)
                goto end;
        }
 
-       g_string_printf(metadata_path, "%s/%s", path, CTF_FS_METADATA_FILENAME);
+       g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
 
        if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
                ret = 1;
@@ -1062,6 +1029,7 @@ int add_trace_path(GList **trace_paths, const char *path)
                goto end;
        }
 
+       // FIXME: Remove or ifdef for __MINGW32__
        if (strcmp(norm_path->str, "/") == 0) {
                BT_LOGE("Opening a trace in `/` is not supported.");
                ret = -1;
@@ -1131,7 +1099,7 @@ int ctf_fs_find_traces(GList **trace_paths, const char *start_path)
                        goto end;
                }
 
-               g_string_printf(sub_path, "%s/%s", start_path, basename);
+               g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename);
                ret = ctf_fs_find_traces(trace_paths, sub_path->str);
                g_string_free(sub_path, TRUE);
                if (ret) {
@@ -1304,7 +1272,8 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
        struct ctf_fs_component *ctf_fs;
        struct bt_value *value = NULL;
        const char *path_param;
-       int ret;
+       enum bt_component_status ret;
+       enum bt_value_status value_ret;
 
        ctf_fs = g_new0(struct ctf_fs_component, 1);
        if (!ctf_fs) {
@@ -1312,7 +1281,7 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
        }
 
        ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
-       assert(ret == 0);
+       assert(ret == BT_COMPONENT_STATUS_OK);
 
        /*
         * We don't need to get a new reference here because as long as
@@ -1325,8 +1294,8 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
                goto error;
        }
 
-       ret = bt_value_string_get(value, &path_param);
-       assert(ret == 0);
+       value_ret = bt_value_string_get(value, &path_param);
+       assert(value_ret == BT_VALUE_STATUS_OK);
        BT_PUT(value);
        value = bt_value_map_get(params, "clock-class-offset-s");
        if (value) {
@@ -1334,9 +1303,9 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
                        BT_LOGE("clock-class-offset-s should be an integer");
                        goto error;
                }
-               ret = bt_value_integer_get(value,
+               value_ret = bt_value_integer_get(value,
                        &ctf_fs->metadata_config.clock_class_offset_s);
-               assert(ret == 0);
+               assert(value_ret == BT_VALUE_STATUS_OK);
                BT_PUT(value);
        }
 
@@ -1346,9 +1315,9 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
                        BT_LOGE("clock-class-offset-ns should be an integer");
                        goto error;
                }
-               ret = bt_value_integer_get(value,
+               value_ret = bt_value_integer_get(value,
                        &ctf_fs->metadata_config.clock_class_offset_ns);
-               assert(ret == 0);
+               assert(value_ret == BT_VALUE_STATUS_OK);
                BT_PUT(value);
        }
 
@@ -1363,8 +1332,7 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
                goto error;
        }
 
-       ret = create_ctf_fs_traces(ctf_fs, path_param);
-       if (ret) {
+       if (create_ctf_fs_traces(ctf_fs, path_param)) {
                goto error;
        }
 
@@ -1374,7 +1342,7 @@ error:
        ctf_fs_destroy(ctf_fs);
        ctf_fs = NULL;
        ret = bt_private_component_set_user_data(priv_comp, NULL);
-       assert(ret == 0);
+       assert(ret == BT_COMPONENT_STATUS_OK);
 
 end:
        bt_put(value);
@@ -1397,19 +1365,25 @@ enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
 }
 
 BT_HIDDEN
-struct bt_value *ctf_fs_query(struct bt_component_class *comp_class,
+struct bt_component_class_query_return ctf_fs_query(
+               struct bt_component_class *comp_class,
+               struct bt_query_executor *query_exec,
                const char *object, struct bt_value *params)
 {
-       struct bt_value *result = NULL;
+       struct bt_component_class_query_return ret = {
+               .result = NULL,
+               .status = BT_QUERY_STATUS_OK,
+       };
 
        if (!strcmp(object, "metadata-info")) {
-               result = metadata_info_query(comp_class, params);
+               ret = metadata_info_query(comp_class, params);
        } else if (!strcmp(object, "trace-info")) {
-               result = trace_info_query(comp_class, params);
+               ret = trace_info_query(comp_class, params);
        } else {
                BT_LOGE("Unknown query object `%s`", object);
+               ret.status = BT_QUERY_STATUS_INVALID_OBJECT;
                goto end;
        }
 end:
-       return result;
+       return ret;
 }
This page took 0.027841 seconds and 4 git commands to generate.