copy_trace: copy the trace name
[babeltrace.git] / plugins / libctfcopytrace / ctfcopytrace.c
index 0689ee380c8651261c65fdffd17da10d64ff5594..54a7fd14d175221989e01cf9ecabd0915d4a3f28 100644 (file)
@@ -66,18 +66,14 @@ struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
        }
 
        description = bt_ctf_clock_class_get_description(clock_class);
-       if (!description) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
-       }
-
-       int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
-                       description);
-       if (int_ret != 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
+       if (description) {
+               int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
+                               description);
+               if (int_ret != 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto end_destroy;
+               }
        }
 
        u64_ret = bt_ctf_clock_class_get_frequency(clock_class);
@@ -214,8 +210,9 @@ struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                struct bt_ctf_event_class *event_class)
 {
        struct bt_ctf_event_class *writer_event_class = NULL;
+       struct bt_ctf_field_type *context, *payload_type;
        const char *name;
-       int count, i;
+       int count, i, ret;
 
        name = bt_ctf_event_class_get_name(event_class);
        if (!name) {
@@ -262,25 +259,26 @@ struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                }
        }
 
-       count = bt_ctf_event_class_get_payload_type_field_count(event_class);
-       for (i = 0; i < count; i++) {
-               const char *field_name;
-               struct bt_ctf_field_type *field_type;
-               int ret;
-
-               ret = bt_ctf_event_class_get_payload_type_field_by_index(
-                               event_class, &field_name, &field_type, i);
+       payload_type = bt_ctf_event_class_get_payload_type(event_class);
+       if (payload_type) {
+               ret = bt_ctf_event_class_set_payload_type(writer_event_class,
+                               payload_type);
                if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
                        goto error;
                }
+               BT_PUT(payload_type);
+       }
 
-               ret = bt_ctf_event_class_add_field(writer_event_class, field_type,
-                               field_name);
-               BT_PUT(field_type);
+       context = bt_ctf_event_class_get_context_type(event_class);
+       if (context) {
+               ret = bt_ctf_event_class_set_context_type(
+                               writer_event_class, context);
+               BT_PUT(context);
                if (ret < 0) {
-                       fprintf(err, "[error] Cannot add field %s\n", field_name);
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
                        goto error;
                }
        }
@@ -310,7 +308,6 @@ enum bt_component_status ctf_copy_event_classes(FILE *err,
        }
 
        for (i = 0; i < count; i++) {
-               struct bt_ctf_field_type *context;
                int int_ret;
 
                event_class = bt_ctf_stream_class_get_event_class_by_index(
@@ -329,21 +326,6 @@ enum bt_component_status ctf_copy_event_classes(FILE *err,
                        goto error;
                }
 
-               context = bt_ctf_event_class_get_context_type(event_class);
-               if (!context) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       ret = BT_COMPONENT_STATUS_ERROR;
-                       goto error;
-               }
-               ret = bt_ctf_event_class_set_context_type(writer_event_class, context);
-               BT_PUT(context);
-               if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                                       __LINE__);
-                       goto error;
-               }
-
                int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class,
                                writer_event_class);
                if (int_ret < 0) {
@@ -530,15 +512,14 @@ end:
 BT_HIDDEN
 struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
                struct bt_ctf_packet *packet,
-               struct bt_ctf_stream *writer_stream,
-               int skip_content_size)
+               struct bt_ctf_stream *writer_stream)
 {
        enum bt_component_status ret;
        struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
        struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL;
        struct bt_ctf_stream_class *writer_stream_class = NULL;
        struct bt_ctf_field *field = NULL;
-       struct bt_ctf_field_type *field_type;
+       struct bt_ctf_field_type *field_type = NULL;
        int nr_fields, i;
 
        packet_context = bt_ctf_packet_get_context(packet);
@@ -593,13 +574,6 @@ struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
                                        __FILE__, __LINE__);
                        goto error;
                }
-               if (skip_content_size &&
-                               (!strncmp(field_name, "content_size", strlen("content_size")) ||
-                               !strncmp(field_name, "packet_size", strlen("packet_size")))) {
-                       BT_PUT(field_type);
-                       BT_PUT(field);
-                       continue;
-               }
 
                if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
                        fprintf(err, "[error] Unexpected packet context field type\n");
@@ -833,6 +807,8 @@ enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        int field_count, i, int_ret;
        struct bt_ctf_field_type *header_type = NULL;
+       enum bt_ctf_byte_order order;
+       const char *trace_name;
 
        field_count = bt_ctf_trace_get_environment_field_count(trace);
        for (i = 0; i < field_count; i++) {
@@ -870,19 +846,47 @@ enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
                }
        }
 
-       header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
-       if (!header_type) {
+       order = bt_ctf_trace_get_native_byte_order(trace);
+       if (order == BT_CTF_BYTE_ORDER_UNKNOWN) {
                fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
 
-       int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
-       BT_PUT(header_type);
-       if (int_ret < 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto end;
+       /*
+        * Only explicitly set the writer trace's native byte order if
+        * the original trace has a specific one. Otherwise leave what
+        * the CTF writer object chooses, which is the machine's native
+        * byte order.
+        */
+       if (order != BT_CTF_BYTE_ORDER_NONE) {
+               ret = bt_ctf_trace_set_native_byte_order(writer_trace, order);
+               if (ret) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+       }
+
+       header_type = bt_ctf_trace_get_packet_header_type(writer_trace);
+       if (header_type) {
+               int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type);
+               BT_PUT(header_type);
+               if (int_ret < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+       }
+
+       trace_name = bt_ctf_trace_get_name(trace);
+       if (trace_name) {
+               int_ret = bt_ctf_trace_set_name(writer_trace, trace_name);
+               if (int_ret < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
        }
 
 end:
This page took 0.02557 seconds and 4 git commands to generate.