src.ctf.fs: use GMappedFileUP in build_index_from_idx_file
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.cpp
index caad4b58123d1d341e5ca82c685d317a6fcfd740..f8ea1d973e0a202aa24d67ee5d5c80d68dc6f64a 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "compat/endian.h" /* IWYU pragma: keep  */
 #include "compat/mman.h"   /* IWYU: pragma keep  */
+#include "cpp-common/bt2c/glib-up.hpp"
 #include "cpp-common/bt2s/make-unique.hpp"
 #include "cpp-common/vendor/fmt/format.h"
 
@@ -445,11 +446,11 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
                                                      struct ctf_msg_iter *msg_iter)
 {
     int ret;
-    gchar *directory = NULL;
-    gchar *basename = NULL;
-    GString *index_basename = NULL;
-    gchar *index_file_path = NULL;
-    GMappedFile *mapped_file = NULL;
+    bt2c::GCharUP directory;
+    bt2c::GCharUP basename;
+    std::string index_basename;
+    bt2c::GCharUP index_file_path;
+    bt2c::GMappedFileUP mapped_file;
     gsize filesize;
     const char *mmap_begin = NULL, *file_pos = NULL;
     const struct ctf_packet_index_file_hdr *header = NULL;
@@ -481,31 +482,25 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
     }
 
     /* Look for index file in relative path index/name.idx. */
-    basename = g_path_get_basename(ds_file->file->path.c_str());
+    basename.reset(g_path_get_basename(ds_file->file->path.c_str()));
     if (!basename) {
         BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get the basename of datastream file {}",
                         ds_file->file->path);
         goto error;
     }
 
-    directory = g_path_get_dirname(ds_file->file->path.c_str());
+    directory.reset(g_path_get_dirname(ds_file->file->path.c_str()));
     if (!directory) {
         BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get dirname of datastream file {}",
                         ds_file->file->path);
         goto error;
     }
 
-    index_basename = g_string_new(basename);
-    if (!index_basename) {
-        BT_CPPLOGE_STR_SPEC(ds_file->logger, "Cannot allocate index file basename string");
-        goto error;
-    }
-
-    g_string_append(index_basename, ".idx");
-    index_file_path = g_build_filename(directory, "index", index_basename->str, NULL);
-    mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
+    index_basename = fmt::format("{}.idx", basename.get());
+    index_file_path.reset(g_build_filename(directory.get(), "index", index_basename.c_str(), NULL));
+    mapped_file.reset(g_mapped_file_new(index_file_path.get(), FALSE, NULL));
     if (!mapped_file) {
-        BT_CPPLOGD_SPEC(ds_file->logger, "Cannot create new mapped file {}", index_file_path);
+        BT_CPPLOGD_SPEC(ds_file->logger, "Cannot create new mapped file {}", index_file_path.get());
         goto error;
     }
 
@@ -514,7 +509,7 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
      * Traces with such large indexes have never been seen in the wild,
      * but this would need to be adjusted to support them.
      */
-    filesize = g_mapped_file_get_length(mapped_file);
+    filesize = g_mapped_file_get_length(mapped_file.get());
     if (filesize < sizeof(*header)) {
         BT_CPPLOGW_SPEC(ds_file->logger,
                         "Invalid LTTng trace index file: "
@@ -523,10 +518,10 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
         goto error;
     }
 
-    mmap_begin = g_mapped_file_get_contents(mapped_file);
+    mmap_begin = g_mapped_file_get_contents(mapped_file.get());
     header = (struct ctf_packet_index_file_hdr *) mmap_begin;
 
-    file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
+    file_pos = g_mapped_file_get_contents(mapped_file.get()) + sizeof(*header);
     if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
         BT_CPPLOGW_STR_SPEC(ds_file->logger,
                             "Invalid LTTng trace index: \"magic\" field validation failed");
@@ -561,10 +556,7 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
         goto error;
     }
 
-    index = ctf_fs_ds_index_create();
-    if (!index) {
-        goto error;
-    }
+    index = bt2s::make_unique<ctf_fs_ds_index>();
 
     for (i = 0; i < file_entry_count; i++) {
         struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos;
@@ -647,15 +639,6 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
         goto error;
     }
 end:
-    g_free(directory);
-    g_free(basename);
-    g_free(index_file_path);
-    if (index_basename) {
-        g_string_free(index_basename, TRUE);
-    }
-    if (mapped_file) {
-        g_mapped_file_unref(mapped_file);
-    }
     return index;
 error:
     index.reset();
@@ -712,16 +695,12 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d
                                                         struct ctf_msg_iter *msg_iter)
 {
     int ret;
-    ctf_fs_ds_index::UP index;
     enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
     auto currentPacketOffset = bt2c::DataLen::fromBytes(0);
 
     BT_CPPLOGI_SPEC(ds_file->logger, "Indexing stream file {}", ds_file->file->path);
 
-    index = ctf_fs_ds_index_create();
-    if (!index) {
-        goto error;
-    }
+    ctf_fs_ds_index::UP index = bt2s::make_unique<ctf_fs_ds_index>();
 
     while (true) {
         struct ctf_msg_iter_packet_properties props;
@@ -805,13 +784,9 @@ ctf_fs_ds_file::UP ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace,
     auto ds_file = bt2s::make_unique<ctf_fs_ds_file>(parentLogger);
     size_t offset_align;
 
-    ds_file->file = ctf_fs_file_create(parentLogger);
-    if (!ds_file->file) {
-        goto error;
-    }
-
+    ds_file->file = bt2s::make_unique<ctf_fs_file>(parentLogger);
     ds_file->stream = std::move(stream);
-    ds_file->metadata = ctf_fs_trace->metadata;
+    ds_file->metadata = ctf_fs_trace->metadata.get();
     ds_file->file->path = path;
     ret = ctf_fs_file_open(ds_file->file.get(), "rb");
     if (ret) {
@@ -847,11 +822,6 @@ end:
     return index;
 }
 
-ctf_fs_ds_index::UP ctf_fs_ds_index_create()
-{
-    return bt2s::make_unique<ctf_fs_ds_index>();
-}
-
 ctf_fs_ds_file::~ctf_fs_ds_file()
 {
     (void) ds_file_munmap(this);
This page took 0.030709 seconds and 4 git commands to generate.