Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / fs-sink / fs-sink.c
index 077a08b29522bd866d8d085e49c3b658cea0fee5..235ff8de65bd6d7b520bade6fc2df63884002813 100644 (file)
@@ -1,23 +1,7 @@
 /*
- * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
  */
 
 #define BT_COMP_LOG_SELF_COMP (fs_sink->self_comp)
@@ -31,6 +15,7 @@
 #include <glib.h>
 #include "common/assert.h"
 #include "ctfser/ctfser.h"
+#include "plugins/common/param-validation/param-validation.h"
 
 #include "fs-sink.h"
 #include "fs-sink-trace.h"
@@ -43,11 +28,11 @@ static
 const char * const in_port_name = "in";
 
 static
-bt_component_class_init_method_status ensure_output_dir_exists(
+bt_component_class_initialize_method_status ensure_output_dir_exists(
                struct fs_sink_comp *fs_sink)
 {
-       bt_component_class_init_method_status status =
-               BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status status =
+               BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
        int ret;
 
        ret = g_mkdir_with_parents(fs_sink->output_dir_path->str, 0755);
@@ -56,7 +41,7 @@ bt_component_class_init_method_status ensure_output_dir_exists(
                        "Cannot create directories for output directory",
                        ": output-dir-path=\"%s\"",
                        fs_sink->output_dir_path->str);
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
        }
 
@@ -64,51 +49,47 @@ end:
        return status;
 }
 
+static struct bt_param_validation_map_value_entry_descr fs_sink_params_descr[] = {
+       { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_STRING } },
+       { "assume-single-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       { "ignore-discarded-events", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       { "ignore-discarded-packets", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       { "quiet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
+};
+
 static
-bt_component_class_init_method_status
-configure_component(struct fs_sink_comp *fs_sink,
-               const bt_value *params)
+bt_component_class_initialize_method_status
+configure_component(struct fs_sink_comp *fs_sink, const bt_value *params)
 {
-       bt_component_class_init_method_status status =
-               BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status status;
        const bt_value *value;
+       enum bt_param_validation_status validation_status;
+       gchar *validation_error;
 
-       value = bt_value_map_borrow_entry_value_const(params, "path");
-       if (!value) {
-               BT_COMP_LOGE_STR("Missing mandatory `path` parameter.");
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+       validation_status = bt_param_validation_validate(params,
+               fs_sink_params_descr, &validation_error);
+       if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
-       }
-
-       if (!bt_value_is_string(value)) {
-               BT_COMP_LOGE_STR("`path` parameter: expecting a string.");
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+       } else if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto end;
        }
 
+       value = bt_value_map_borrow_entry_value_const(params, "path");
        g_string_assign(fs_sink->output_dir_path,
                bt_value_string_get(value));
+
        value = bt_value_map_borrow_entry_value_const(params,
                "assume-single-trace");
        if (value) {
-               if (!bt_value_is_bool(value)) {
-                       BT_COMP_LOGE_STR("`assume-single-trace` parameter: expecting a boolean.");
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
-                       goto end;
-               }
-
                fs_sink->assume_single_trace = (bool) bt_value_bool_get(value);
        }
 
        value = bt_value_map_borrow_entry_value_const(params,
                "ignore-discarded-events");
        if (value) {
-               if (!bt_value_is_bool(value)) {
-                       BT_COMP_LOGE_STR("`ignore-discarded-events` parameter: expecting a boolean.");
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
-                       goto end;
-               }
-
                fs_sink->ignore_discarded_events =
                        (bool) bt_value_bool_get(value);
        }
@@ -116,12 +97,6 @@ configure_component(struct fs_sink_comp *fs_sink,
        value = bt_value_map_borrow_entry_value_const(params,
                "ignore-discarded-packets");
        if (value) {
-               if (!bt_value_is_bool(value)) {
-                       BT_COMP_LOGE_STR("`ignore-discarded-packets` parameter: expecting a boolean.");
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
-                       goto end;
-               }
-
                fs_sink->ignore_discarded_packets =
                        (bool) bt_value_bool_get(value);
        }
@@ -129,16 +104,13 @@ configure_component(struct fs_sink_comp *fs_sink,
        value = bt_value_map_borrow_entry_value_const(params,
                "quiet");
        if (value) {
-               if (!bt_value_is_bool(value)) {
-                       BT_COMP_LOGE_STR("`quiet` parameter: expecting a boolean.");
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
-                       goto end;
-               }
-
                fs_sink->quiet = (bool) bt_value_bool_get(value);
        }
 
+       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
+
 end:
+       g_free(validation_error);
        return status;
 }
 
@@ -159,7 +131,7 @@ void destroy_fs_sink_comp(struct fs_sink_comp *fs_sink)
                fs_sink->traces = NULL;
        }
 
-       BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
+       BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
                fs_sink->upstream_iter);
        g_free(fs_sink);
 
@@ -168,12 +140,14 @@ end:
 }
 
 BT_HIDDEN
-bt_component_class_init_method_status ctf_fs_sink_init(
-               bt_self_component_sink *self_comp_sink, const bt_value *params,
+bt_component_class_initialize_method_status ctf_fs_sink_init(
+               bt_self_component_sink *self_comp_sink,
+               bt_self_component_sink_configuration *config,
+               const bt_value *params,
                void *init_method_data)
 {
-       bt_component_class_init_method_status status =
-               BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status status =
+               BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
        bt_self_component_add_port_status add_port_status;
        struct fs_sink_comp *fs_sink = NULL;
        bt_self_component *self_comp =
@@ -185,7 +159,7 @@ bt_component_class_init_method_status ctf_fs_sink_init(
        if (!fs_sink) {
                BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
                        "Failed to allocate one CTF FS sink structure.");
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto end;
        }
 
@@ -193,7 +167,7 @@ bt_component_class_init_method_status ctf_fs_sink_init(
        fs_sink->self_comp = self_comp;
        fs_sink->output_dir_path = g_string_new(NULL);
        status = configure_component(fs_sink, params);
-       if (status != BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
+       if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
                /* configure_component() logs errors */
                goto end;
        }
@@ -203,12 +177,12 @@ bt_component_class_init_method_status ctf_fs_sink_init(
                                G_FILE_TEST_EXISTS)) {
                BT_COMP_LOGE("Single trace mode, but output path exists: "
                        "output-path=\"%s\"", fs_sink->output_dir_path->str);
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
        }
 
        status = ensure_output_dir_exists(fs_sink);
-       if (status != BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
+       if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
                /* ensure_output_dir_exists() logs errors */
                goto end;
        }
@@ -217,7 +191,7 @@ bt_component_class_init_method_status ctf_fs_sink_init(
                NULL, (GDestroyNotify) fs_sink_trace_destroy);
        if (!fs_sink->traces) {
                BT_COMP_LOGE_STR("Failed to allocate one GHashTable.");
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto end;
        }
 
@@ -225,10 +199,10 @@ bt_component_class_init_method_status ctf_fs_sink_init(
                self_comp_sink, in_port_name, NULL, NULL);
        switch (add_port_status) {
        case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
        case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto end;
        default:
                break;
@@ -237,7 +211,7 @@ bt_component_class_init_method_status ctf_fs_sink_init(
        bt_self_component_set_data(self_comp, fs_sink);
 
 end:
-       if (status != BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
+       if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
                destroy_fs_sink_comp(fs_sink);
        }
 
@@ -306,7 +280,7 @@ bt_component_class_sink_consume_method_status handle_event_msg(
                goto end;
        }
 
-       BT_ASSERT(ec);
+       BT_ASSERT_DBG(ec);
 
        if (stream->sc->default_clock_class) {
                cs = bt_message_event_borrow_default_clock_snapshot_const(
@@ -348,7 +322,7 @@ bt_component_class_sink_consume_method_status handle_event_msg(
                }
        }
 
-       BT_ASSERT(stream->packet_state.is_open);
+       BT_ASSERT_DBG(stream->packet_state.is_open);
        ret = fs_sink_stream_write_event(stream, cs, ir_event, ec);
        if (G_UNLIKELY(ret)) {
                status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
@@ -476,7 +450,7 @@ bt_component_class_sink_consume_method_status handle_packet_beginning_msg(
                 */
                if (stream->prev_packet_state.end_cs == UINT64_C(-1)) {
                        BT_COMP_LOGE("Incompatible discarded packets message "
-                               "occuring before the stream's first packet: "
+                               "occurring before the stream's first packet: "
                                "stream-id=%" PRIu64 ", stream-name=\"%s\", "
                                "trace-name=\"%s\", path=\"%s/%s\"",
                                bt_stream_get_id(ir_stream),
@@ -655,7 +629,7 @@ bt_component_class_sink_consume_method_status handle_stream_beginning_msg(
        /*
         * Not supported: discarded events or discarded packets support
         * without packets support. Packets are the way to know where
-        * discarded events/packets occured in CTF 1.8.
+        * discarded events/packets occurred in CTF 1.8.
         */
        if (!bt_stream_class_supports_packets(ir_sc)) {
                BT_ASSERT(!bt_stream_class_supports_discarded_packets(ir_sc));
@@ -830,7 +804,7 @@ bt_component_class_sink_consume_method_status handle_discarded_events_msg(
        if (stream->packet_state.is_open &&
                        stream->sc->discarded_events_has_ts) {
                BT_COMP_LOGE("Unsupported discarded events message with "
-                       "default clock snapshots occuring within a packet: "
+                       "default clock snapshots occurring within a packet: "
                        "stream-id=%" PRIu64 ", stream-name=\"%s\", "
                        "trace-name=\"%s\", path=\"%s/%s\"",
                        bt_stream_get_id(ir_stream),
@@ -997,11 +971,11 @@ bt_component_class_sink_consume_method_status ctf_fs_sink_consume(
 
        fs_sink = bt_self_component_get_data(
                        bt_self_component_sink_as_self_component(self_comp));
-       BT_ASSERT(fs_sink);
-       BT_ASSERT(fs_sink->upstream_iter);
+       BT_ASSERT_DBG(fs_sink);
+       BT_ASSERT_DBG(fs_sink->upstream_iter);
 
        /* Consume messages */
-       next_status = bt_self_component_port_input_message_iterator_next(
+       next_status = bt_message_iterator_next(
                fs_sink->upstream_iter, &msgs, &msg_count);
        if (next_status < 0) {
                status = (int) next_status;
@@ -1016,7 +990,7 @@ bt_component_class_sink_consume_method_status ctf_fs_sink_consume(
                for (i = 0; i < msg_count; i++) {
                        const bt_message *msg = msgs[i];
 
-                       BT_ASSERT(msg);
+                       BT_ASSERT_DBG(msg);
 
                        switch (bt_message_get_type(msg)) {
                        case BT_MESSAGE_TYPE_EVENT:
@@ -1051,7 +1025,7 @@ bt_component_class_sink_consume_method_status ctf_fs_sink_consume(
                                        fs_sink, msg);
                                break;
                        default:
-                               abort();
+                               bt_common_abort();
                        }
 
                        BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]);
@@ -1095,24 +1069,27 @@ end:
 }
 
 BT_HIDDEN
-bt_component_class_sink_graph_is_configured_method_status ctf_fs_sink_graph_is_configured(
+bt_component_class_sink_graph_is_configured_method_status
+ctf_fs_sink_graph_is_configured(
                bt_self_component_sink *self_comp)
 {
-       bt_component_class_sink_graph_is_configured_method_status status =
-               BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
+       bt_component_class_sink_graph_is_configured_method_status status;
+       bt_message_iterator_create_from_sink_component_status
+               msg_iter_status;
        struct fs_sink_comp *fs_sink = bt_self_component_get_data(
                        bt_self_component_sink_as_self_component(self_comp));
 
-       fs_sink->upstream_iter =
-               bt_self_component_port_input_message_iterator_create_from_sink_component(
+       msg_iter_status =
+               bt_message_iterator_create_from_sink_component(
                        self_comp,
                        bt_self_component_sink_borrow_input_port_by_name(
-                               self_comp, in_port_name));
-       if (!fs_sink->upstream_iter) {
-               status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
+                               self_comp, in_port_name), &fs_sink->upstream_iter);
+       if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
+               status = (int) msg_iter_status;
                goto end;
        }
 
+       status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
 end:
        return status;
 }
This page took 0.030965 seconds and 4 git commands to generate.