src.ctf.fs: sort inputs paths
[babeltrace.git] / src / plugins / ctf / fs-src / fs.c
index 451e411664f126e2e311be8517e5a969d532dc65..e8121140e8251967b967e33b5d57e0328274071b 100644 (file)
@@ -265,12 +265,6 @@ bt_component_class_message_iterator_initialize_method_status ctf_fs_iterator_ini
                goto error;
        }
 
-       /* FIXME: This is temporary, those functions will be removed. */
-       ctf_msg_iter_set_emit_stream_end_message(
-               msg_iter_data->msg_iter, true);
-       ctf_msg_iter_set_emit_stream_beginning_message(
-               msg_iter_data->msg_iter, true);
-
        /*
         * This iterator can seek forward if its stream class has a default
         * clock class.
@@ -560,12 +554,7 @@ void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
                g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
        }
 
-       if (ds_file_group->index) {
-               if (ds_file_group->index->entries) {
-                       g_ptr_array_free(ds_file_group->index->entries, TRUE);
-               }
-               g_free(ds_file_group->index);
-       }
+       ctf_fs_ds_index_destroy(ds_file_group->index);
 
        bt_stream_put_ref(ds_file_group->stream);
        g_free(ds_file_group);
@@ -775,6 +764,8 @@ int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
                goto error;
        }
 
+       ctf_msg_iter_set_dry_run(msg_iter, true);
+
        ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
        if (ret) {
                BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
@@ -819,7 +810,7 @@ int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
 
        if (begin_ns == -1) {
                /*
-                * No beggining timestamp to sort the stream files
+                * No beginning timestamp to sort the stream files
                 * within a stream file group, so consider that this
                 * file must be the only one within its group.
                 */
@@ -2054,6 +2045,15 @@ gint compare_ds_file_groups_by_first_path(gconstpointer a, gconstpointer b)
                first_ds_file_info_b->path->str);
 }
 
+static
+gint compare_strings(gconstpointer p_a, gconstpointer p_b)
+{
+       const char *a = *((const char **) p_a);
+       const char *b = *((const char **) p_b);
+
+       return strcmp(a, b);
+}
+
 int ctf_fs_component_create_ctf_fs_trace(
                struct ctf_fs_component *ctf_fs,
                const bt_value *paths_value,
@@ -2064,6 +2064,7 @@ int ctf_fs_component_create_ctf_fs_trace(
        int ret = 0;
        uint64_t i;
        bt_logging_level log_level = ctf_fs->log_level;
+       GPtrArray *paths = NULL;
        GPtrArray *traces;
        const char *trace_name;
 
@@ -2077,15 +2078,43 @@ int ctf_fs_component_create_ctf_fs_trace(
                goto error;
        }
 
+       paths = g_ptr_array_new_with_free_func(g_free);
+       if (!paths) {
+               BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
+                       "Failed to allocate a GPtrArray.");
+               goto error;
+       }
+
        trace_name = trace_name_value ? bt_value_string_get(trace_name_value) : NULL;
 
-       /* Start by creating a separate ctf_fs_trace object for each path. */
+       /*
+        * Create a sorted array of the paths, to make the execution of this
+        * component deterministic.
+        */
        for (i = 0; i < bt_value_array_get_length(paths_value); i++) {
-               const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i);
+               const bt_value *path_value =
+                       bt_value_array_borrow_element_by_index_const(paths_value, i);
                const char *input = bt_value_string_get(path_value);
+               gchar *input_copy;
+
+               input_copy = g_strdup(input);
+               if (!input_copy) {
+                       BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
+                               "Failed to copy a string.");
+                       goto error;
+               }
+
+               g_ptr_array_add(paths, input_copy);
+       }
+
+       g_ptr_array_sort(paths, compare_strings);
+
+       /* Create a separate ctf_fs_trace object for each path. */
+       for (i = 0; i < paths->len; i++) {
+               const char *path = g_ptr_array_index(paths, i);
 
                ret = ctf_fs_component_create_ctf_fs_trace_one_path(ctf_fs,
-                       input, trace_name, traces, self_comp, self_comp_class);
+                       path, trace_name, traces, self_comp, self_comp_class);
                if (ret) {
                        goto end;
                }
@@ -2169,7 +2198,14 @@ error:
        ret = -1;
 
 end:
-       g_ptr_array_free(traces, TRUE);
+       if (traces) {
+               g_ptr_array_free(traces, TRUE);
+       }
+
+       if (paths) {
+               g_ptr_array_free(paths, TRUE);
+       }
+
        return ret;
 }
 
This page took 0.029707 seconds and 4 git commands to generate.