+static
+int init_index_entry(struct ctf_fs_ds_index_entry *entry,
+ struct ctf_fs_ds_file *ds_file,
+ struct bt_msg_iter_packet_properties *props,
+ off_t packet_size, off_t packet_offset)
+{
+ int ret;
+ struct ctf_stream_class *sc;
+
+ sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
+ props->stream_class_id);
+ BT_ASSERT(sc);
+ BT_ASSERT(packet_offset >= 0);
+ entry->offset = packet_offset;
+ BT_ASSERT(packet_size >= 0);
+ entry->packet_size = packet_size;
+
+ /* Convert the packet's bound to nanoseconds since Epoch. */
+ ret = convert_cycles_to_ns(sc->default_clock_class,
+ props->snapshots.beginning_clock,
+ &entry->timestamp_begin_ns);
+ if (ret) {
+ BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
+ goto end;
+ }
+
+ ret = convert_cycles_to_ns(sc->default_clock_class,
+ props->snapshots.end_clock,
+ &entry->timestamp_end_ns);
+ if (ret) {
+ BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
+ goto end;
+ }
+
+end:
+ return ret;
+}
+
+static
+struct ctf_fs_ds_index *build_index_from_stream_file(
+ struct ctf_fs_ds_file *ds_file)
+{
+ int ret;
+ struct ctf_fs_ds_index *index = NULL;
+ enum bt_msg_iter_status iter_status;
+
+ BT_LOGD("Indexing stream file %s", ds_file->file->path->str);
+
+ index = ctf_fs_ds_index_create(0);
+ if (!index) {
+ goto error;
+ }
+
+ do {
+ off_t current_packet_offset;
+ off_t next_packet_offset;
+ off_t current_packet_size_bytes;
+ struct ctf_fs_ds_index_entry *entry;
+ struct bt_msg_iter_packet_properties props;
+
+ iter_status = bt_msg_iter_get_packet_properties(
+ ds_file->msg_iter, &props);
+ if (iter_status != BT_MSG_ITER_STATUS_OK) {
+ if (iter_status == BT_MSG_ITER_STATUS_EOF) {
+ break;
+ }
+ goto error;
+ }
+
+ current_packet_offset =
+ bt_msg_iter_get_current_packet_offset(
+ ds_file->msg_iter);
+ if (current_packet_offset < 0) {
+ BT_LOGE_STR("Cannot get the current packet's offset.");
+ goto error;
+ }
+
+ current_packet_size_bytes =
+ ((props.exp_packet_total_size + 7) & ~7) / CHAR_BIT;
+
+ if (current_packet_offset + current_packet_size_bytes >
+ ds_file->file->size) {
+ BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
+ "packet-offset=%jd, packet-size-bytes=%jd, "
+ "file-size=%jd",
+ ds_file->file->path->str,
+ current_packet_offset,
+ current_packet_size_bytes,
+ ds_file->file->size);
+ goto error;
+ }
+
+ next_packet_offset = current_packet_offset +
+ current_packet_size_bytes;
+ BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
+ "next-packet-offset=%jd", current_packet_offset,
+ next_packet_offset);
+
+ entry = ctf_fs_ds_index_add_new_entry(index);
+ if (!entry) {
+ BT_LOGE_STR("Failed to allocate a new index entry.");
+ goto error;
+ }
+
+ ret = init_index_entry(entry, ds_file, &props,
+ current_packet_size_bytes, current_packet_offset);
+ if (ret) {
+ goto error;
+ }
+
+ iter_status = bt_msg_iter_seek(ds_file->msg_iter,
+ next_packet_offset);
+ } while (iter_status == BT_MSG_ITER_STATUS_OK);
+
+ if (iter_status != BT_MSG_ITER_STATUS_EOF) {
+ goto error;
+ }
+
+end:
+ return index;
+
+error:
+ ctf_fs_ds_index_destroy(index);
+ index = NULL;
+ goto end;
+}
+