cleanup error paths in trimmer, writer and ctfcopytrace
[babeltrace.git] / plugins / writer / write.c
index 5f65ddcb40c333c8a1adff18b40ea7c93097d00f..cd692db2aa0edccc10446041ebf1e0e6bc2bdf30 100644 (file)
@@ -47,7 +47,7 @@ struct bt_ctf_stream_class *insert_new_stream_class(
                struct bt_ctf_stream_class *stream_class)
 {
        struct bt_ctf_stream_class *writer_stream_class = NULL;
-       struct bt_ctf_trace *trace, *writer_trace;
+       struct bt_ctf_trace *trace = NULL, *writer_trace = NULL;
        enum bt_component_status ret;
 
        trace = bt_ctf_stream_class_get_trace(stream_class);
@@ -55,27 +55,24 @@ struct bt_ctf_stream_class *insert_new_stream_class(
                fprintf(writer_component->err,
                                "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end;
+               goto error;
        }
 
        writer_trace = bt_ctf_writer_get_trace(ctf_writer);
        if (!writer_trace) {
-               bt_put(trace);
                fprintf(writer_component->err,
                                "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto end_put_trace;
+               goto error;
        }
 
        ret = ctf_copy_clock_classes(writer_component->err, writer_trace,
                        writer_stream_class, trace);
        if (ret != BT_COMPONENT_STATUS_OK) {
-               bt_put(trace);
                fprintf(writer_component->err,
                                "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end_put_writer_trace;
+               goto error;
        }
 
        writer_stream_class = ctf_copy_stream_class(writer_component->err,
@@ -84,17 +81,19 @@ struct bt_ctf_stream_class *insert_new_stream_class(
                fprintf(writer_component->err, "[error] Failed to copy stream class\n");
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put_writer_trace;
+               goto error;
        }
 
        g_hash_table_insert(writer_component->stream_class_map,
                        (gpointer) stream_class, writer_stream_class);
 
-end_put_writer_trace:
+       goto end;
+
+error:
+       BT_PUT(writer_stream_class);
+end:
        bt_put(writer_trace);
-end_put_trace:
        bt_put(trace);
-end:
        return writer_stream_class;
 }
 
@@ -105,46 +104,40 @@ struct bt_ctf_stream *insert_new_stream(
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_stream *stream)
 {
-       struct bt_ctf_stream *writer_stream;
-       struct bt_ctf_stream_class *writer_stream_class;
+       struct bt_ctf_stream *writer_stream = NULL;
+       struct bt_ctf_stream_class *writer_stream_class = NULL;
 
        writer_stream_class = g_hash_table_lookup(
                        writer_component->stream_class_map,
                        (gpointer) stream_class);
-       if (writer_stream_class) {
-               if (!bt_get(writer_stream_class)) {
-                       writer_stream = NULL;
-                       fprintf(writer_component->err, "[error] %s in %s:%d\n",
-                                       __func__, __FILE__, __LINE__);
-                       goto end;
-               }
-       } else {
+       if (!writer_stream_class) {
                writer_stream_class = insert_new_stream_class(
                                writer_component, ctf_writer, stream_class);
                if (!writer_stream_class) {
-                       writer_stream = NULL;
                        fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                        __func__, __FILE__, __LINE__);
-                       goto end_put;
+                       goto error;
                }
        }
+       bt_get(writer_stream_class);
 
        writer_stream = bt_ctf_writer_create_stream(ctf_writer,
                        writer_stream_class);
        if (!writer_stream) {
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put;
+               goto error;
        }
 
        g_hash_table_insert(writer_component->stream_map, (gpointer) stream,
                        writer_stream);
 
-       bt_ctf_writer_flush_metadata(ctf_writer);
+       goto end;
 
-end_put:
-       bt_put(writer_stream_class);
+error:
+       BT_PUT(writer_stream);
 end:
+       bt_put(writer_stream_class);
        return writer_stream;
 }
 
@@ -170,11 +163,12 @@ struct bt_ctf_writer *insert_new_writer(
                struct writer_component *writer_component,
                struct bt_ctf_trace *trace)
 {
-       struct bt_ctf_writer *ctf_writer;
-       struct bt_ctf_trace *writer_trace;
+       struct bt_ctf_writer *ctf_writer = NULL;
+       struct bt_ctf_trace *writer_trace = NULL;
        char trace_name[PATH_MAX];
        enum bt_component_status ret;
 
+       /* FIXME: replace with trace name when it will work. */
        snprintf(trace_name, PATH_MAX, "%s/%s_%03d",
                        writer_component->base_path->str,
                        writer_component->trace_name_base->str,
@@ -185,20 +179,18 @@ struct bt_ctf_writer *insert_new_writer(
        if (!ctf_writer) {
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        writer_trace = bt_ctf_writer_get_trace(ctf_writer);
        if (!writer_trace) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err,
                                "[error] %s in %s:%d\n", __func__, __FILE__,
                                __LINE__);
-               goto end;
+               goto error;
        }
 
        ret = ctf_copy_trace(writer_component->err, trace, writer_trace);
-       bt_put(writer_trace);
        if (ret != BT_COMPONENT_STATUS_OK) {
                fprintf(writer_component->err, "[error] Failed to copy trace\n");
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
@@ -206,10 +198,16 @@ struct bt_ctf_writer *insert_new_writer(
                BT_PUT(ctf_writer);
                goto end;
        }
+       BT_PUT(writer_trace);
 
        g_hash_table_insert(writer_component->trace_map, (gpointer) trace,
                        ctf_writer);
 
+       goto end;
+
+error:
+       bt_put(writer_trace);
+       BT_PUT(ctf_writer);
 end:
        return ctf_writer;
 }
@@ -218,31 +216,28 @@ static
 struct bt_ctf_writer *get_writer(struct writer_component *writer_component,
                struct bt_ctf_stream_class *stream_class)
 {
-       struct bt_ctf_trace *trace;
-       struct bt_ctf_writer *ctf_writer;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_writer *ctf_writer = NULL;
 
        trace = bt_ctf_stream_class_get_trace(stream_class);
        if (!trace) {
                ctf_writer = NULL;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        ctf_writer = g_hash_table_lookup(writer_component->trace_map,
                        (gpointer) trace);
-       if (ctf_writer) {
-               if (!bt_get(ctf_writer)) {
-                       ctf_writer = NULL;
-                       fprintf(writer_component->err, "[error] %s in %s:%d\n",
-                                       __func__, __FILE__, __LINE__);
-                       goto end;
-               }
-       } else {
+       if (!ctf_writer) {
                ctf_writer = insert_new_writer(writer_component, trace);
        }
-       bt_put(trace);
+       bt_get(ctf_writer);
+       BT_PUT(trace);
+       goto end;
 
+error:
+       BT_PUT(ctf_writer);
 end:
        return ctf_writer;
 }
@@ -252,45 +247,38 @@ struct bt_ctf_stream *get_writer_stream(
                struct writer_component *writer_component,
                struct bt_ctf_packet *packet, struct bt_ctf_stream *stream)
 {
-       struct bt_ctf_stream_class *stream_class;
-       struct bt_ctf_writer *ctf_writer;
-       struct bt_ctf_stream *writer_stream;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_writer *ctf_writer = NULL;
+       struct bt_ctf_stream *writer_stream = NULL;
 
        stream_class = bt_ctf_stream_get_class(stream);
        if (!stream_class) {
-               writer_stream = NULL;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        ctf_writer = get_writer(writer_component, stream_class);
        if (!ctf_writer) {
-               writer_stream = NULL;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put_stream_class;
+               goto error;
        }
 
        writer_stream = lookup_stream(writer_component, stream);
-
-       if (writer_stream) {
-               if (!bt_get(writer_stream)) {
-                       writer_stream = NULL;
-                       fprintf(writer_component->err, "[error] %s in %s:%d\n",
-                                       __func__, __FILE__, __LINE__);
-                       goto end_put_stream_class;
-               }
-       } else {
+       if (!writer_stream) {
                writer_stream = insert_new_stream(writer_component, ctf_writer,
                                stream_class, stream);
-               bt_get(writer_stream);
        }
+       bt_get(writer_stream);
+
+       goto end;
 
+error:
+       BT_PUT(writer_stream);
+end:
        bt_put(ctf_writer);
-end_put_stream_class:
        bt_put(stream_class);
-end:
        return writer_stream;
 }
 
@@ -299,52 +287,52 @@ enum bt_component_status writer_new_packet(
                struct writer_component *writer_component,
                struct bt_ctf_packet *packet)
 {
-       struct bt_ctf_stream *stream, *writer_stream;
-       struct bt_ctf_field *writer_packet_context;
+       struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
+       struct bt_ctf_field *writer_packet_context = NULL;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        int int_ret;
 
        stream = bt_ctf_packet_get_stream(packet);
        if (!stream) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        writer_stream = get_writer_stream(writer_component, packet, stream);
        if (!writer_stream) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put;
+               goto error;
        }
+       BT_PUT(stream);
 
        writer_packet_context = ctf_copy_packet_context(writer_component->err,
                        packet, writer_stream);
        if (!writer_packet_context) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put;
+               goto error;
        }
 
        int_ret = bt_ctf_stream_set_packet_context(writer_stream,
                        writer_packet_context);
        if (int_ret < 0) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_writer_packet_context;
+               goto error;
        }
+       BT_PUT(writer_stream);
+       BT_PUT(writer_packet_context);
 
-       bt_put(writer_stream);
+       goto end;
 
-end_put_writer_packet_context:
+error:
+       ret = BT_COMPONENT_STATUS_ERROR;
+end:
+       bt_put(writer_stream);
        bt_put(writer_packet_context);
-end_put:
        bt_put(stream);
-end:
        return ret;
 }
 
@@ -353,48 +341,42 @@ enum bt_component_status writer_close_packet(
                struct writer_component *writer_component,
                struct bt_ctf_packet *packet)
 {
-       struct bt_ctf_stream *stream, *writer_stream;
+       struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
        enum bt_component_status ret;
 
        stream = bt_ctf_packet_get_stream(packet);
        if (!stream) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        writer_stream = lookup_stream(writer_component, stream);
        if (!writer_stream) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
-               goto end_put;
+               goto error;
        }
+       BT_PUT(stream);
 
-       if (!bt_get(writer_stream)) {
-               fprintf(writer_component->err,
-                               "[error] Failed to get reference on writer stream\n");
-               fprintf(writer_component->err, "[error] %s in %s:%d\n",
-                               __func__, __FILE__, __LINE__);
-               ret = BT_COMPONENT_STATUS_ERROR;
-               goto end_put;
-       }
+       bt_get(writer_stream);
 
        ret = bt_ctf_stream_flush(writer_stream);
        if (ret < 0) {
                fprintf(writer_component->err,
                                "[error] Failed to flush packet\n");
-               ret = BT_COMPONENT_STATUS_ERROR;
+               goto error;
        }
+       BT_PUT(writer_stream);
 
        ret = BT_COMPONENT_STATUS_OK;
+       goto end;
 
+error:
+       ret = BT_COMPONENT_STATUS_ERROR;
+end:
        bt_put(writer_stream);
-
-end_put:
        bt_put(stream);
-end:
        return ret;
 }
 
@@ -404,61 +386,55 @@ enum bt_component_status writer_output_event(
                struct bt_ctf_event *event)
 {
        enum bt_component_status ret;
-       struct bt_ctf_event_class *event_class, *writer_event_class;
-       struct bt_ctf_stream *stream, *writer_stream;
-       struct bt_ctf_stream_class *stream_class, *writer_stream_class;
-       struct bt_ctf_event *writer_event;
+       struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL;
+       struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL, *writer_stream_class = NULL;
+       struct bt_ctf_event *writer_event = NULL;
        const char *event_name;
        int int_ret;
 
        event_class = bt_ctf_event_get_class(event);
        if (!event_class) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end;
+               goto error;
        }
 
        event_name = bt_ctf_event_class_get_name(event_class);
        if (!event_name) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_event_class;
+               goto error;
        }
 
        stream = bt_ctf_event_get_stream(event);
        if (!stream) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_event_class;
+               goto error;
        }
 
        writer_stream = lookup_stream(writer_component, stream);
        if (!writer_stream || !bt_get(writer_stream)) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_stream;
+               goto error;
        }
 
        stream_class = bt_ctf_event_class_get_stream_class(event_class);
        if (!stream_class) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_writer_stream;
+               goto error;
        }
 
        writer_stream_class = g_hash_table_lookup(
                        writer_component->stream_class_map,
                        (gpointer) stream_class);
        if (!writer_stream_class || !bt_get(writer_stream_class)) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
-               goto end_put_stream_class;
+               goto error;
        }
 
        writer_event_class = get_event_class(writer_component,
@@ -467,58 +443,50 @@ enum bt_component_status writer_output_event(
                writer_event_class = ctf_copy_event_class(writer_component->err,
                                event_class);
                if (!writer_event_class) {
-                       ret = BT_COMPONENT_STATUS_ERROR;
                        fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                        __func__, __FILE__, __LINE__);
-                       goto end_put_writer_stream_class;
+                       goto error;
                }
                int_ret = bt_ctf_stream_class_add_event_class(
                                writer_stream_class, writer_event_class);
                if (int_ret) {
-                       ret = BT_COMPONENT_STATUS_ERROR;
                        fprintf(writer_component->err, "[error] %s in %s:%d\n",
                                        __func__, __FILE__, __LINE__);
-                       goto end_put_writer_stream_class;
+                       goto error;
                }
        }
 
        writer_event = ctf_copy_event(writer_component->err, event,
                        writer_event_class, true);
        if (!writer_event) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
                fprintf(writer_component->err, "[error] Failed to copy event %s\n",
                                bt_ctf_event_class_get_name(writer_event_class));
-               goto end_put_writer_event_class;
+               goto error;
        }
 
        int_ret = bt_ctf_stream_append_event(writer_stream, writer_event);
        if (int_ret < 0) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__,
                                __FILE__, __LINE__);
                fprintf(writer_component->err, "[error] Failed to append event %s\n",
                                bt_ctf_event_class_get_name(writer_event_class));
-               goto end_put_writer_event;
+               goto error;
        }
 
        ret = BT_COMPONENT_STATUS_OK;
+       goto end;
 
-end_put_writer_event:
+error:
+       ret = BT_COMPONENT_STATUS_ERROR;
+end:
        bt_put(writer_event);
-end_put_writer_event_class:
        bt_put(writer_event_class);
-end_put_writer_stream_class:
        bt_put(writer_stream_class);
-end_put_stream_class:
        bt_put(stream_class);
-end_put_writer_stream:
        bt_put(writer_stream);
-end_put_stream:
        bt_put(stream);
-end_put_event_class:
        bt_put(event_class);
-end:
        return ret;
 }
This page took 0.031732 seconds and 4 git commands to generate.