src.ctf.fs: manage lifetime of open file with FileUP
[babeltrace.git] / src / plugins / ctf / fs-src / query.cpp
index 1e87697c9b1585de296d31fac27736d10b871ff0..95453ada5595eb4db7a10228bfcf95bc6135b10f 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,7 +34,7 @@ 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;
@@ -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,8 +96,8 @@ 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, metadata_fp.get());
     if (decoder_status) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(
             logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
@@ -145,14 +147,6 @@ end:
     }
     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 +190,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 +232,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;
 }
 
@@ -369,7 +362,7 @@ 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;
     FILE *metadata_file = NULL;
@@ -392,13 +385,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;
@@ -461,7 +454,6 @@ 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);
 
This page took 0.043654 seconds and 4 git commands to generate.