ctf: use unique_ptr to manage ctf_metadata_decoder lifetime
[babeltrace.git] / src / plugins / ctf / fs-src / query.cpp
index c4bd65410f6b5792648ebb4ab56240b55ec44947..6625ec7d403e7fdbd1bd6508b253d1ff47d76372 100644 (file)
@@ -12,6 +12,8 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/libc-up.hpp"
+
 #include "../common/src/metadata/tsdl/decoder.hpp"
 #include "fs.hpp"
 #include "query.hpp"
@@ -32,12 +34,12 @@ bt_component_class_query_method_status metadata_info_query(const bt_value *param
     bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
     bt_value *result = NULL;
     const bt_value *path_value = NULL;
-    FILE *metadata_fp = NULL;
+    bt2c::FileUP metadata_fp;
     int ret;
     int bo;
     const char *path;
     bool is_packetized;
-    struct ctf_metadata_decoder *decoder = NULL;
+    ctf_metadata_decoder_up decoder;
     ctf_metadata_decoder_config decoder_cfg {logger};
     enum ctf_metadata_decoder_status decoder_status;
     GString *g_metadata_text = NULL;
@@ -73,13 +75,13 @@ bt_component_class_query_method_status metadata_info_query(const bt_value *param
     path = bt_value_string_get(path_value);
 
     BT_ASSERT(path);
-    metadata_fp = ctf_fs_metadata_open_file(path, logger);
+    metadata_fp.reset(ctf_fs_metadata_open_file(path, logger));
     if (!metadata_fp) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
         goto error;
     }
 
-    ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, &bo, logger);
+    ret = ctf_metadata_decoder_is_packetized(metadata_fp.get(), &is_packetized, &bo, logger);
     if (ret) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(
             logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
@@ -94,15 +96,15 @@ bt_component_class_query_method_status metadata_info_query(const bt_value *param
         goto error;
     }
 
-    rewind(metadata_fp);
-    decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp);
+    rewind(metadata_fp.get());
+    decoder_status = ctf_metadata_decoder_append_content(decoder.get(), metadata_fp.get());
     if (decoder_status) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(
             logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
         goto error;
     }
 
-    plaintext = ctf_metadata_decoder_get_text(decoder);
+    plaintext = ctf_metadata_decoder_get_text(decoder.get());
     g_metadata_text = g_string_new(NULL);
 
     if (!g_metadata_text) {
@@ -115,7 +117,6 @@ bt_component_class_query_method_status metadata_info_query(const bt_value *param
     }
 
     g_string_append(g_metadata_text, plaintext);
-
     ret = bt_value_map_insert_string_entry(result, "text", g_metadata_text->str);
     if (ret) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
@@ -143,15 +144,6 @@ end:
     if (g_metadata_text) {
         g_string_free(g_metadata_text, TRUE);
     }
-    ctf_metadata_decoder_destroy(decoder);
-
-    if (metadata_fp) {
-        ret = fclose(metadata_fp);
-        if (ret) {
-            BT_CPPLOGE_ERRNO_SPEC(logger, "Cannot close metadata file stream", ": path=\"{}\"",
-                                  path);
-        }
-    }
 
     *user_result = result;
     return status;
@@ -196,7 +188,7 @@ static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *gr
     int ret = 0;
     bt_value_map_insert_entry_status insert_status;
     struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
-    gchar *port_name = NULL;
+    bt2c::GCharUP port_name;
 
     /*
      * Since each `struct ctf_fs_ds_file_group` has a sorted array of
@@ -238,14 +230,13 @@ static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *gr
         goto end;
     }
 
-    insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name);
+    insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name.get());
     if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
         ret = -1;
         goto end;
     }
 
 end:
-    g_free(port_name);
     return ret;
 }
 
@@ -299,7 +290,7 @@ end:
 bt_component_class_query_method_status
 trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
 {
-    struct ctf_fs_component *ctf_fs = NULL;
+    ctf_fs_component::UP ctf_fs;
     bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
     bt_value *result = NULL;
     const bt_value *inputs_value = NULL;
@@ -321,12 +312,12 @@ trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_v
         goto error;
     }
 
-    if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs)) {
+    if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
         status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
         goto error;
     }
 
-    if (ctf_fs_component_create_ctf_fs_trace(ctf_fs, inputs_value, trace_name_value, NULL)) {
+    if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
         goto error;
     }
 
@@ -357,11 +348,6 @@ error:
     }
 
 end:
-    if (ctf_fs) {
-        ctf_fs_destroy(ctf_fs);
-        ctf_fs = NULL;
-    }
-
     *user_result = result;
     return status;
 }
@@ -374,9 +360,9 @@ support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_
     bt_component_class_query_method_status status;
     bt_value_map_insert_entry_status insert_entry_status;
     double weight = 0;
-    gchar *metadata_path = NULL;
+    bt2c::GCharUP metadata_path;
     bt_value *result = NULL;
-    struct ctf_metadata_decoder *metadata_decoder = NULL;
+    ctf_metadata_decoder_up metadata_decoder;
     FILE *metadata_file = NULL;
     char uuid_str[BT_UUID_STR_LEN + 1];
     bool has_uuid = false;
@@ -397,13 +383,13 @@ support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_
     BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
     input = bt_value_string_get(input_value);
 
-    metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL);
+    metadata_path.reset(g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL));
     if (!metadata_path) {
         status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
         goto end;
     }
 
-    metadata_file = g_fopen(metadata_path, "rb");
+    metadata_file = g_fopen(metadata_path.get(), "rb");
     if (metadata_file) {
         enum ctf_metadata_decoder_status decoder_status;
         bt_uuid_t uuid;
@@ -416,7 +402,7 @@ support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_
             goto end;
         }
 
-        decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file);
+        decoder_status = ctf_metadata_decoder_append_content(metadata_decoder.get(), metadata_file);
         if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
             BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
                             decoder_status);
@@ -431,7 +417,7 @@ support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_
         weight = 0.75;
 
         /* If the trace has a UUID, return the stringified UUID as the group. */
-        if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) {
+        if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder.get(), uuid) == 0) {
             bt_uuid_to_str(uuid, uuid_str);
             has_uuid = true;
         }
@@ -466,9 +452,7 @@ create_result:
     status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
 
 end:
-    g_free(metadata_path);
     bt_value_put_ref(result);
-    ctf_metadata_decoder_destroy(metadata_decoder);
 
     return status;
 }
This page took 0.027878 seconds and 4 git commands to generate.