+ fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
+ return ret;
+}
+
+static
+int import_stream_packet_index(struct ctf_trace *td,
+ struct ctf_file_stream *file_stream)
+{
+ struct ctf_stream_pos *pos;
+ struct ctf_packet_index *ctf_index = NULL;
+ struct ctf_packet_index_file_hdr index_hdr;
+ struct packet_index index;
+ uint32_t packet_index_len;
+ int ret = 0;
+ int first_packet = 1;
+ size_t len;
+
+ pos = &file_stream->pos;
+
+ len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
+ if (len != 1) {
+ perror("read index file header");
+ goto error;
+ }
+
+ /* Check the index header */
+ if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
+ fprintf(stderr, "[error] wrong index magic\n");
+ ret = -1;
+ goto error;
+ }
+ if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
+ fprintf(stderr, "[error] Incompatible index file %" PRIu64
+ ".%" PRIu64 ", supported %d.%d\n",
+ be64toh(index_hdr.index_major),
+ be64toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
+ CTF_INDEX_MINOR);
+ ret = -1;
+ goto error;
+ }
+ packet_index_len = be32toh(index_hdr.packet_index_len);
+ if (packet_index_len == 0) {
+ fprintf(stderr, "[error] Packet index length cannot be 0.\n");
+ ret = -1;
+ goto error;
+ }
+ /*
+ * Allocate the index length found in header, not internal
+ * representation.
+ */
+ ctf_index = g_malloc0(packet_index_len);
+ while (fread(ctf_index, packet_index_len, 1,
+ pos->index_fp) == 1) {
+ uint64_t stream_id;
+ struct ctf_stream_declaration *stream = NULL;
+
+ memset(&index, 0, sizeof(index));
+ index.offset = be64toh(ctf_index->offset);
+ index.packet_size = be64toh(ctf_index->packet_size);
+ index.content_size = be64toh(ctf_index->content_size);
+ index.ts_cycles.timestamp_begin = be64toh(ctf_index->timestamp_begin);
+ index.ts_cycles.timestamp_end = be64toh(ctf_index->timestamp_end);
+ index.events_discarded = be64toh(ctf_index->events_discarded);
+ index.events_discarded_len = 64;
+ index.data_offset = -1;
+ stream_id = be64toh(ctf_index->stream_id);
+
+ if (!first_packet) {
+ /* add index to packet array */
+ g_array_append_val(file_stream->pos.packet_index, index);
+ continue;
+ }
+
+ file_stream->parent.stream_id = stream_id;
+ if (stream_id < td->streams->len) {
+ stream = g_ptr_array_index(td->streams, stream_id);
+ }
+ if (!stream) {
+ fprintf(stderr, "[error] Stream %" PRIu64
+ " is not declared in metadata.\n",
+ stream_id);
+ ret = -EINVAL;
+ goto error;
+ }
+ file_stream->parent.stream_class = stream;
+ ret = create_stream_definitions(td, &file_stream->parent);
+ if (ret)
+ goto error;
+ first_packet = 0;
+ /* add index to packet array */
+ g_array_append_val(file_stream->pos.packet_index, index);
+ }
+
+ /* Index containing only the header. */
+ if (!file_stream->parent.stream_class) {
+ ret = -1;
+ goto error;
+ }
+
+ ret = 0;
+
+error:
+ g_free(ctf_index);