+ 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_notif_iter_packet_properties props;
+
+ iter_status = bt_notif_iter_borrow_packet_header_context_fields(
+ ds_file->notif_iter, NULL, NULL);
+ if (iter_status != BT_NOTIF_ITER_STATUS_OK) {
+ if (iter_status == BT_NOTIF_ITER_STATUS_EOF) {
+ break;
+ }
+ goto error;
+ }
+
+ ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter,
+ &props);
+ BT_ASSERT(ret == 0);
+
+ current_packet_offset =
+ bt_notif_iter_get_current_packet_offset(
+ ds_file->notif_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_notif_iter_seek(ds_file->notif_iter,
+ next_packet_offset);
+ } while (iter_status == BT_NOTIF_ITER_STATUS_OK);
+
+ if (iter_status != BT_NOTIF_ITER_STATUS_EOF) {
+ goto error;