src.ctf.fs: data-stream-file.cpp: declare variables on first use
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.cpp
index aa36c7c6b95070599d8268b5a761d8365bc56d72..330e47f55b9932f5dd6793c15757d6d66cf3b235 100644 (file)
@@ -310,10 +310,9 @@ ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data,
 static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data)
 {
     struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
-    struct ctf_fs_ds_index_entry *index_entry;
 
     /* If we have gone through all index entries, we are done. */
-    if (data->next_index_entry_index >= data->ds_file_group->index->entries.size()) {
+    if (data->next_index_entry_index >= data->ds_file_group->index.entries.size()) {
         return CTF_MSG_ITER_MEDIUM_STATUS_EOF;
     }
 
@@ -321,9 +320,8 @@ static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data
      * Otherwise, look up the next index entry / packet and prepare it
      *  for reading.
      */
-    index_entry = data->ds_file_group->index->entries[data->next_index_entry_index].get();
-
-    ctf_msg_iter_medium_status status = ctf_fs_ds_group_medops_set_file(data, index_entry);
+    ctf_msg_iter_medium_status status = ctf_fs_ds_group_medops_set_file(
+        data, &data->ds_file_group->index.entries[data->next_index_entry_index]);
     if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
         return status;
     }
@@ -344,8 +342,7 @@ enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
 {
     BT_ASSERT(self_msg_iter);
     BT_ASSERT(ds_file_group);
-    BT_ASSERT(ds_file_group->index);
-    BT_ASSERT(!ds_file_group->index->entries.empty());
+    BT_ASSERT(!ds_file_group->index.entries.empty());
 
     out.reset(new ctf_fs_ds_group_medops_data {parentLogger});
 
@@ -379,16 +376,6 @@ struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
     .borrow_stream = medop_group_borrow_stream,
 };
 
-static ctf_fs_ds_index_entry::UP ctf_fs_ds_index_entry_create(const bt2c::DataLen offset,
-                                                              const bt2c::DataLen packetSize)
-{
-    ctf_fs_ds_index_entry::UP entry = bt2s::make_unique<ctf_fs_ds_index_entry>(offset, packetSize);
-
-    entry->packet_seq_num = UINT64_MAX;
-
-    return entry;
-}
-
 static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns)
 {
     return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency,
@@ -396,66 +383,50 @@ static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cy
                                                   clock_class->offset_cycles, ns);
 }
 
-static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_file,
-                                                     struct ctf_fs_ds_file_info *file_info,
-                                                     struct ctf_msg_iter *msg_iter)
+static bt2s::optional<ctf_fs_ds_index>
+build_index_from_idx_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info,
+                          struct ctf_msg_iter *msg_iter)
 {
-    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;
-    ctf_fs_ds_index::UP index;
-    ctf_fs_ds_index_entry::UP index_entry;
-    ctf_fs_ds_index_entry *prev_index_entry = NULL;
-    auto totalPacketsSize = bt2c::DataLen::fromBytes(0);
-    size_t file_index_entry_size;
-    size_t file_entry_count;
-    size_t i;
-    struct ctf_stream_class *sc;
-    struct ctf_msg_iter_packet_properties props;
-    uint32_t version_major, version_minor;
-
     BT_CPPLOGI_SPEC(ds_file->logger, "Building index from .idx file of stream file {}",
                     ds_file->file->path);
+    ctf_msg_iter_packet_properties props;
     int ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
     if (ret) {
         BT_CPPLOGI_STR_SPEC(ds_file->logger,
                             "Cannot read first packet's header and context fields.");
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
+    ctf_stream_class *sc =
+        ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
     BT_ASSERT(sc);
     if (!sc->default_clock_class) {
         BT_CPPLOGI_STR_SPEC(ds_file->logger, "Cannot find stream class's default clock class.");
-        return nullptr;
+        return bt2s::nullopt;
     }
 
     /* Look for index file in relative path index/name.idx. */
-    basename.reset(g_path_get_basename(ds_file->file->path.c_str()));
+    bt2c::GCharUP basename {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);
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    directory.reset(g_path_get_dirname(ds_file->file->path.c_str()));
+    bt2c::GCharUP directory {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);
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    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));
+    std::string index_basename = fmt::format("{}.idx", basename.get());
+    bt2c::GCharUP index_file_path {
+        g_build_filename(directory.get(), "index", index_basename.c_str(), NULL)};
+    bt2c::GMappedFileUP mapped_file {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.get());
-        return nullptr;
+        return bt2s::nullopt;
     }
 
     /*
@@ -463,63 +434,65 @@ 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.get());
-    if (filesize < sizeof(*header)) {
+    gsize filesize = g_mapped_file_get_length(mapped_file.get());
+    if (filesize < sizeof(ctf_packet_index_file_hdr)) {
         BT_CPPLOGW_SPEC(ds_file->logger,
                         "Invalid LTTng trace index file: "
                         "file size ({} bytes) < header size ({} bytes)",
-                        filesize, sizeof(*header));
-        return nullptr;
+                        filesize, sizeof(ctf_packet_index_file_hdr));
+        return bt2s::nullopt;
     }
 
-    mmap_begin = g_mapped_file_get_contents(mapped_file.get());
-    header = (struct ctf_packet_index_file_hdr *) mmap_begin;
+    const char *mmap_begin = g_mapped_file_get_contents(mapped_file.get());
+    const ctf_packet_index_file_hdr *header = (const ctf_packet_index_file_hdr *) mmap_begin;
 
-    file_pos = g_mapped_file_get_contents(mapped_file.get()) + sizeof(*header);
+    const char *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");
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    version_major = be32toh(header->index_major);
-    version_minor = be32toh(header->index_minor);
+    uint32_t version_major = be32toh(header->index_major);
+    uint32_t version_minor = be32toh(header->index_minor);
     if (version_major != 1) {
         BT_CPPLOGW_SPEC(ds_file->logger, "Unknown LTTng trace index version: major={}, minor={}",
                         version_major, version_minor);
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    file_index_entry_size = be32toh(header->packet_index_len);
+    size_t file_index_entry_size = be32toh(header->packet_index_len);
     if (file_index_entry_size < CTF_INDEX_1_0_SIZE) {
         BT_CPPLOGW_SPEC(
             ds_file->logger,
             "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
             "packet_index_len={}, CTF_INDEX_1_0_SIZE={}",
             file_index_entry_size, CTF_INDEX_1_0_SIZE);
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
+    size_t file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
     if ((filesize - sizeof(*header)) % file_index_entry_size) {
         BT_CPPLOGW_SPEC(ds_file->logger,
                         "Invalid LTTng trace index: the index's size after the header "
                         "({} bytes) is not a multiple of the index entry size "
                         "({} bytes)",
                         (filesize - sizeof(*header)), sizeof(*header));
-        return nullptr;
+        return bt2s::nullopt;
     }
 
-    index = bt2s::make_unique<ctf_fs_ds_index>();
+    ctf_fs_ds_index index;
+    ctf_fs_ds_index_entry *prev_index_entry = nullptr;
+    auto totalPacketsSize = bt2c::DataLen::fromBytes(0);
 
-    for (i = 0; i < file_entry_count; i++) {
+    for (size_t i = 0; i < file_entry_count; i++) {
         struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos;
         const auto packetSize = bt2c::DataLen::fromBits(be64toh(file_index->packet_size));
 
         if (packetSize.hasExtraBits()) {
             BT_CPPLOGW_SPEC(ds_file->logger,
                             "Invalid packet size encountered in LTTng trace index file");
-            return nullptr;
+            return bt2s::nullopt;
         }
 
         const auto offset = bt2c::DataLen::fromBytes(be64toh(file_index->offset));
@@ -530,58 +503,53 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
                 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
                 "previous offset={} bytes, current offset={} bytes",
                 prev_index_entry->offset.bytes(), offset.bytes());
-            return nullptr;
+            return bt2s::nullopt;
         }
 
-        index_entry = ctf_fs_ds_index_entry_create(offset, packetSize);
-        if (!index_entry) {
-            BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
-                                         "Failed to create a ctf_fs_ds_index_entry.");
-            return nullptr;
-        }
+        ctf_fs_ds_index_entry index_entry {offset, packetSize};
 
         /* Set path to stream file. */
-        index_entry->path = file_info->path.c_str();
+        index_entry.path = file_info->path.c_str();
 
-        index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
-        index_entry->timestamp_end = be64toh(file_index->timestamp_end);
-        if (index_entry->timestamp_end < index_entry->timestamp_begin) {
+        index_entry.timestamp_begin = be64toh(file_index->timestamp_begin);
+        index_entry.timestamp_end = be64toh(file_index->timestamp_end);
+        if (index_entry.timestamp_end < index_entry.timestamp_begin) {
             BT_CPPLOGW_SPEC(
                 ds_file->logger,
                 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
                 "timestamp_begin={}, timestamp_end={}",
-                index_entry->timestamp_begin, index_entry->timestamp_end);
-            return nullptr;
+                index_entry.timestamp_begin, index_entry.timestamp_end);
+            return bt2s::nullopt;
         }
 
         /* Convert the packet's bound to nanoseconds since Epoch. */
-        ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_begin,
-                                   &index_entry->timestamp_begin_ns);
+        ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_begin,
+                                   &index_entry.timestamp_begin_ns);
         if (ret) {
             BT_CPPLOGI_STR_SPEC(
                 ds_file->logger,
                 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
-            return nullptr;
+            return bt2s::nullopt;
         }
-        ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_end,
-                                   &index_entry->timestamp_end_ns);
+        ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_end,
+                                   &index_entry.timestamp_end_ns);
         if (ret) {
             BT_CPPLOGI_STR_SPEC(
                 ds_file->logger,
                 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
-            return nullptr;
+            return bt2s::nullopt;
         }
 
         if (version_minor >= 1) {
-            index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
+            index_entry.packet_seq_num = be64toh(file_index->packet_seq_num);
         }
 
         totalPacketsSize += packetSize;
         file_pos += file_index_entry_size;
 
-        prev_index_entry = index_entry.get();
+        index.entries.emplace_back(index_entry);
 
-        index->entries.emplace_back(std::move(index_entry));
+        prev_index_entry = &index.entries.back();
     }
 
     /* Validate that the index addresses the complete stream. */
@@ -590,66 +558,62 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
                         "Invalid LTTng trace index file; indexed size != stream file size: "
                         "file-size={} bytes, total-packets-size={} bytes",
                         ds_file->file->size, totalPacketsSize.bytes());
-        return nullptr;
+        return bt2s::nullopt;
     }
 
     return index;
 }
 
-static int init_index_entry(struct ctf_fs_ds_index_entry *entry, struct ctf_fs_ds_file *ds_file,
+static int init_index_entry(ctf_fs_ds_index_entry& entry, struct ctf_fs_ds_file *ds_file,
                             struct ctf_msg_iter_packet_properties *props)
 {
-    struct ctf_stream_class *sc;
-
-    sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id);
+    ctf_stream_class *sc =
+        ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id);
     BT_ASSERT(sc);
 
     if (props->snapshots.beginning_clock != UINT64_C(-1)) {
-        entry->timestamp_begin = props->snapshots.beginning_clock;
+        entry.timestamp_begin = props->snapshots.beginning_clock;
 
         /* Convert the packet's bound to nanoseconds since Epoch. */
         int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock,
-                                       &entry->timestamp_begin_ns);
+                                       &entry.timestamp_begin_ns);
         if (ret) {
             BT_CPPLOGI_STR_SPEC(ds_file->logger,
                                 "Failed to convert raw timestamp to nanoseconds since Epoch.");
             return ret;
         }
     } else {
-        entry->timestamp_begin = UINT64_C(-1);
-        entry->timestamp_begin_ns = UINT64_C(-1);
+        entry.timestamp_begin = UINT64_C(-1);
+        entry.timestamp_begin_ns = UINT64_C(-1);
     }
 
     if (props->snapshots.end_clock != UINT64_C(-1)) {
-        entry->timestamp_end = props->snapshots.end_clock;
+        entry.timestamp_end = props->snapshots.end_clock;
 
         /* Convert the packet's bound to nanoseconds since Epoch. */
         int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock,
-                                       &entry->timestamp_end_ns);
+                                       &entry.timestamp_end_ns);
         if (ret) {
             BT_CPPLOGI_STR_SPEC(ds_file->logger,
                                 "Failed to convert raw timestamp to nanoseconds since Epoch.");
             return ret;
         }
     } else {
-        entry->timestamp_end = UINT64_C(-1);
-        entry->timestamp_end_ns = UINT64_C(-1);
+        entry.timestamp_end = UINT64_C(-1);
+        entry.timestamp_end_ns = UINT64_C(-1);
     }
 
     return 0;
 }
 
-static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *ds_file,
-                                                        struct ctf_fs_ds_file_info *file_info,
-                                                        struct ctf_msg_iter *msg_iter)
+static bt2s::optional<ctf_fs_ds_index>
+build_index_from_stream_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info,
+                             struct ctf_msg_iter *msg_iter)
 {
-    int ret;
-    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);
 
-    ctf_fs_ds_index::UP index = bt2s::make_unique<ctf_fs_ds_index>();
+    ctf_fs_ds_index index;
+    auto currentPacketOffset = bt2c::DataLen::fromBytes(0);
 
     while (true) {
         struct ctf_msg_iter_packet_properties props;
@@ -657,20 +621,20 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d
         if (currentPacketOffset.bytes() > ds_file->file->size) {
             BT_CPPLOGE_STR_SPEC(ds_file->logger,
                                 "Unexpected current packet's offset (larger than file).");
-            return nullptr;
+            return bt2s::nullopt;
         } else if (currentPacketOffset.bytes() == ds_file->file->size) {
             /* No more data */
             break;
         }
 
-        iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes());
+        ctf_msg_iter_status iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes());
         if (iter_status != CTF_MSG_ITER_STATUS_OK) {
-            return nullptr;
+            return bt2s::nullopt;
         }
 
         iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props);
         if (iter_status != CTF_MSG_ITER_STATUS_OK) {
-            return nullptr;
+            return bt2s::nullopt;
         }
 
         /*
@@ -689,25 +653,20 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d
                             "file-size-bytes={}",
                             ds_file->file->path, currentPacketOffset.bytes(),
                             currentPacketSize.bytes(), ds_file->file->size);
-            return nullptr;
+            return bt2s::nullopt;
         }
 
-        auto index_entry = ctf_fs_ds_index_entry_create(currentPacketOffset, currentPacketSize);
-        if (!index_entry) {
-            BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
-                                         "Failed to create a ctf_fs_ds_index_entry.");
-            return nullptr;
-        }
+        ctf_fs_ds_index_entry index_entry {currentPacketOffset, currentPacketSize};
 
         /* Set path to stream file. */
-        index_entry->path = file_info->path.c_str();
+        index_entry.path = file_info->path.c_str();
 
-        ret = init_index_entry(index_entry.get(), ds_file, &props);
+        int ret = init_index_entry(index_entry, ds_file, &props);
         if (ret) {
-            return nullptr;
+            return bt2s::nullopt;
         }
 
-        index->entries.emplace_back(std::move(index_entry));
+        index.entries.emplace_back(index_entry);
 
         currentPacketOffset += currentPacketSize;
         BT_CPPLOGD_SPEC(ds_file->logger,
@@ -724,28 +683,27 @@ ctf_fs_ds_file::UP ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace,
                                          bt2::Stream::Shared stream, const char *path,
                                          const bt2c::Logger& parentLogger)
 {
-    int ret;
     auto ds_file = bt2s::make_unique<ctf_fs_ds_file>(parentLogger);
-    size_t offset_align;
 
     ds_file->file = bt2s::make_unique<ctf_fs_file>(parentLogger);
     ds_file->stream = std::move(stream);
     ds_file->metadata = ctf_fs_trace->metadata.get();
     ds_file->file->path = path;
-    ret = ctf_fs_file_open(ds_file->file.get(), "rb");
+    int ret = ctf_fs_file_open(ds_file->file.get(), "rb");
     if (ret) {
         return nullptr;
     }
 
-    offset_align = bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
+    const size_t offset_align =
+        bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
     ds_file->mmap_max_len = offset_align * 2048;
 
     return ds_file;
 }
 
-ctf_fs_ds_index::UP ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
-                                               struct ctf_fs_ds_file_info *file_info,
-                                               struct ctf_msg_iter *msg_iter)
+bt2s::optional<ctf_fs_ds_index> ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
+                                                           struct ctf_fs_ds_file_info *file_info,
+                                                           struct ctf_msg_iter *msg_iter)
 {
     auto index = build_index_from_idx_file(ds_file, file_info, msg_iter);
     if (index) {
@@ -774,7 +732,7 @@ ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t beg
 ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace,
                                                      struct ctf_stream_class *sc,
                                                      uint64_t stream_instance_id,
-                                                     ctf_fs_ds_index::UP index)
+                                                     ctf_fs_ds_index index)
 {
     ctf_fs_ds_file_group::UP ds_file_group {new ctf_fs_ds_file_group};
 
This page took 0.034335 seconds and 4 git commands to generate.