-/*
- * Handle index for a data stream.
- *
- * Called with the stream lock held.
- *
- * Return 0 on success else a negative value.
- */
-static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
- bool rotate_index, bool *flushed, uint64_t total_size)
-{
- int ret = 0;
- uint64_t data_offset;
- struct relay_index *index;
-
- /* Get data offset because we are about to update the index. */
- data_offset = htobe64(stream->tracefile_size_current);
-
- DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64,
- stream->stream_handle, net_seq_num, stream->tracefile_size_current);
-
- /*
- * Lookup for an existing index for that stream id/sequence
- * number. If it exists, the control thread has already received the
- * data for it, thus we need to write it to disk.
- */
- index = relay_index_get_by_id_or_create(stream, net_seq_num);
- if (!index) {
- ret = -1;
- goto end;
- }
-
- if (rotate_index || !stream->index_file) {
- const char *stream_path;
-
- /*
- * The data connection creates the stream's first index file.
- *
- * This can happen _after_ a ROTATE_STREAM command. In
- * other words, the data of the first packet of this stream
- * can be received after a ROTATE_STREAM command.
- *
- * The ROTATE_STREAM command changes the stream's path_name
- * to point to the "next" chunk. If a rotation is pending for
- * this stream, as indicated by "rotate_at_seq_num != -1ULL",
- * it means that we are still receiving data that belongs in the
- * stream's former path.
- *
- * In this very specific case, we must ensure that the index
- * file is created in the streams's former path,
- * "prev_path_name".
- *
- * All other rotations beyond the first one are not affected
- * by this problem since the actual rotation operation creates
- * the new chunk's index file.
- */
- stream_path = stream->rotate_at_seq_num == -1ULL ?
- stream->path_name:
- stream->prev_path_name;
-
- ret = create_rotate_index_file(stream, stream_path);
- if (ret < 0) {
- ERR("Failed to rotate index");
- /* Put self-ref for this index due to error. */
- relay_index_put(index);
- index = NULL;
- goto end;
- }
- }
-
- if (relay_index_set_file(index, stream->index_file, data_offset)) {
- ret = -1;
- /* Put self-ref for this index due to error. */
- relay_index_put(index);
- index = NULL;
- goto end;
- }
-
- ret = relay_index_try_flush(index);
- if (ret == 0) {
- tracefile_array_commit_seq(stream->tfa);
- stream->index_received_seqcount++;
- *flushed = true;
- } else if (ret > 0) {
- index->total_size = total_size;
- /* No flush. */
- ret = 0;
- } else {
- /*
- * ret < 0
- *
- * relay_index_try_flush is responsible for the self-reference
- * put of the index object on error.
- */
- ERR("relay_index_try_flush error %d", ret);
- ret = -1;
- }
-end:
- return ret;
-}
-