+static
+int init_index_entry(struct ctf_fs_ds_index_entry *entry,
+ struct bt_field *packet_context, off_t packet_size,
+ off_t packet_offset)
+{
+ int ret;
+ struct bt_field *timestamp_begin = NULL;
+ struct bt_field *timestamp_end = NULL;
+ struct bt_clock_class *timestamp_begin_cc = NULL;
+ struct bt_clock_class *timestamp_end_cc = NULL;
+
+ ret = borrow_packet_bounds_from_packet_context(packet_context,
+ ×tamp_begin_cc, ×tamp_begin,
+ ×tamp_end_cc, ×tamp_end);
+ if (ret || !timestamp_begin_cc || !timestamp_begin ||
+ !timestamp_end_cc || ! timestamp_end) {
+ BT_LOGD_STR("Failed to determine time bound fields of packet.");
+ goto end;
+ }
+
+ BT_ASSERT(packet_offset >= 0);
+ entry->offset = packet_offset;
+
+ BT_ASSERT(packet_size >= 0);
+ entry->packet_size = packet_size;
+
+ ret = bt_field_integer_unsigned_get_value(timestamp_begin,
+ &entry->timestamp_begin);
+ if (ret) {
+ goto end;
+ }
+ ret = bt_field_integer_unsigned_get_value(timestamp_end,
+ &entry->timestamp_end);
+ if (ret) {
+ goto end;
+ }
+
+ /* Convert the packet's bound to nanoseconds since Epoch. */
+ ret = convert_cycles_to_ns(timestamp_begin_cc,
+ entry->timestamp_begin,
+ &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(timestamp_end_cc,
+ entry->timestamp_end,
+ &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_notif_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, current_packet_size_bytes;
+ struct ctf_fs_ds_index_entry *entry;
+ struct bt_field *packet_context = NULL;
+
+ iter_status = bt_notif_iter_borrow_packet_header_context_fields(
+ ds_file->notif_iter, NULL, &packet_context);
+ if (iter_status != BT_NOTIF_ITER_STATUS_OK) {
+ if (iter_status == BT_NOTIF_ITER_STATUS_EOF) {
+ break;
+ }
+ goto error;
+ }
+
+ 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 = bt_notif_iter_get_current_packet_size(
+ ds_file->notif_iter);
+ if (current_packet_size < 0) {
+ BT_LOGE("Cannot get packet size: packet-offset=%jd",
+ current_packet_offset);
+ goto error;
+ }
+ current_packet_size_bytes =
+ ((current_packet_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, packet_context,
+ 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;
+ }
+
+end:
+ return index;
+
+error:
+ ctf_fs_ds_index_destroy(index);
+ index = NULL;
+ goto end;
+}
+