lib: strictly type function return status enumerations
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.c
1 /*
2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_COMP_LOG_SELF_COMP (ds_file->self_comp)
26 #define BT_LOG_OUTPUT_LEVEL (ds_file->log_level)
27 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
28 #include "plugins/comp-logging.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <glib.h>
36 #include <inttypes.h>
37 #include "compat/mman.h"
38 #include "compat/endian.h"
39 #include <babeltrace2/babeltrace.h>
40 #include "common/common.h"
41 #include "file.h"
42 #include "metadata.h"
43 #include "../common/msg-iter/msg-iter.h"
44 #include "common/assert.h"
45 #include "data-stream-file.h"
46 #include <string.h>
47
48 static inline
49 size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
50 {
51 return ds_file->mmap_len - ds_file->request_offset;
52 }
53
54 static
55 int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
56 {
57 int ret = 0;
58
59 if (!ds_file || !ds_file->mmap_addr) {
60 goto end;
61 }
62
63 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
64 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
65 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
66 ds_file->mmap_addr, ds_file->mmap_len,
67 ds_file->file ? ds_file->file->path->str : "NULL",
68 ds_file->file ? ds_file->file->fp : NULL);
69 ret = -1;
70 goto end;
71 }
72
73 ds_file->mmap_addr = NULL;
74
75 end:
76 return ret;
77 }
78
79 static
80 enum bt_msg_iter_medium_status ds_file_mmap_next(
81 struct ctf_fs_ds_file *ds_file)
82 {
83 enum bt_msg_iter_medium_status ret =
84 BT_MSG_ITER_MEDIUM_STATUS_OK;
85
86 /* Unmap old region */
87 if (ds_file->mmap_addr) {
88 if (ds_file_munmap(ds_file)) {
89 goto error;
90 }
91
92 /*
93 * mmap_len is guaranteed to be page-aligned except on the
94 * last mapping where it may not be possible (since the file's
95 * size itself may not be a page multiple).
96 */
97 ds_file->mmap_offset += ds_file->mmap_len;
98 ds_file->request_offset = 0;
99 }
100
101 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
102 ds_file->mmap_max_len);
103 if (ds_file->mmap_len == 0) {
104 ret = BT_MSG_ITER_MEDIUM_STATUS_EOF;
105 goto end;
106 }
107 /* Map new region */
108 BT_ASSERT(ds_file->mmap_len);
109 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
110 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
111 ds_file->mmap_offset);
112 if (ds_file->mmap_addr == MAP_FAILED) {
113 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
114 ds_file->mmap_len, ds_file->file->path->str,
115 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
116 strerror(errno));
117 goto error;
118 }
119
120 goto end;
121 error:
122 ds_file_munmap(ds_file);
123 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
124 end:
125 return ret;
126 }
127
128 static
129 enum bt_msg_iter_medium_status medop_request_bytes(
130 size_t request_sz, uint8_t **buffer_addr,
131 size_t *buffer_sz, void *data)
132 {
133 enum bt_msg_iter_medium_status status =
134 BT_MSG_ITER_MEDIUM_STATUS_OK;
135 struct ctf_fs_ds_file *ds_file = data;
136
137 if (request_sz == 0) {
138 goto end;
139 }
140
141 /* Check if we have at least one memory-mapped byte left */
142 if (remaining_mmap_bytes(ds_file) == 0) {
143 /* Are we at the end of the file? */
144 if (ds_file->mmap_offset >= ds_file->file->size) {
145 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
146 ds_file->file->path->str, ds_file->file->fp);
147 status = BT_MSG_ITER_MEDIUM_STATUS_EOF;
148 goto end;
149 }
150
151 status = ds_file_mmap_next(ds_file);
152 switch (status) {
153 case BT_MSG_ITER_MEDIUM_STATUS_OK:
154 break;
155 case BT_MSG_ITER_MEDIUM_STATUS_EOF:
156 goto end;
157 default:
158 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
159 ds_file->file->path->str,
160 ds_file->file->fp);
161 goto error;
162 }
163 }
164
165 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
166 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
167 ds_file->request_offset += *buffer_sz;
168 goto end;
169
170 error:
171 status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
172
173 end:
174 return status;
175 }
176
177 static
178 bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
179 void *data)
180 {
181 struct ctf_fs_ds_file *ds_file = data;
182 bt_stream_class *ds_file_stream_class;
183 bt_stream *stream = NULL;
184
185 ds_file_stream_class = bt_stream_borrow_class(
186 ds_file->stream);
187
188 if (stream_class != ds_file_stream_class) {
189 /*
190 * Not supported: two packets described by two different
191 * stream classes within the same data stream file.
192 */
193 goto end;
194 }
195
196 stream = ds_file->stream;
197
198 end:
199 return stream;
200 }
201
202 static
203 enum bt_msg_iter_medium_status medop_seek(enum bt_msg_iter_seek_whence whence,
204 off_t offset, void *data)
205 {
206 enum bt_msg_iter_medium_status ret =
207 BT_MSG_ITER_MEDIUM_STATUS_OK;
208 struct ctf_fs_ds_file *ds_file = data;
209 off_t file_size = ds_file->file->size;
210
211 if (whence != BT_MSG_ITER_SEEK_WHENCE_SET ||
212 offset < 0 || offset > file_size) {
213 BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
214 "file-size=%jd", (int) whence, offset,
215 file_size);
216 ret = BT_MSG_ITER_MEDIUM_STATUS_INVAL;
217 goto end;
218 }
219
220 /*
221 * Determine whether or not the destination is contained within the
222 * current mapping.
223 */
224 if (ds_file->mmap_addr && (offset < ds_file->mmap_offset ||
225 offset >= ds_file->mmap_offset + ds_file->mmap_len)) {
226 int unmap_ret;
227 off_t offset_in_mapping = offset %
228 bt_common_get_page_size(ds_file->log_level);
229
230 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
231 "file mapping: offset=%jd, mmap-offset=%jd, "
232 "mmap-len=%zu", offset, ds_file->mmap_offset,
233 ds_file->mmap_len);
234 unmap_ret = ds_file_munmap(ds_file);
235 if (unmap_ret) {
236 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
237 goto end;
238 }
239
240 ds_file->mmap_offset = offset - offset_in_mapping;
241 ds_file->request_offset = offset_in_mapping;
242 ret = ds_file_mmap_next(ds_file);
243 if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) {
244 goto end;
245 }
246 } else {
247 ds_file->request_offset = offset - ds_file->mmap_offset;
248 }
249
250 ds_file->end_reached = (offset == file_size);
251 end:
252 return ret;
253 }
254
255 BT_HIDDEN
256 struct bt_msg_iter_medium_ops ctf_fs_ds_file_medops = {
257 .request_bytes = medop_request_bytes,
258 .borrow_stream = medop_borrow_stream,
259 .seek = medop_seek,
260 };
261
262 static
263 int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
264 uint64_t cycles, int64_t *ns)
265 {
266 return bt_util_clock_cycles_to_ns_from_origin(cycles,
267 clock_class->frequency, clock_class->offset_seconds,
268 clock_class->offset_cycles, ns);
269 }
270
271 static
272 struct ctf_fs_ds_index *build_index_from_idx_file(
273 struct ctf_fs_ds_file *ds_file)
274 {
275 int ret;
276 gchar *directory = NULL;
277 gchar *basename = NULL;
278 GString *index_basename = NULL;
279 gchar *index_file_path = NULL;
280 GMappedFile *mapped_file = NULL;
281 gsize filesize;
282 const char *mmap_begin = NULL, *file_pos = NULL;
283 const struct ctf_packet_index_file_hdr *header = NULL;
284 struct ctf_fs_ds_index *index = NULL;
285 struct ctf_fs_ds_index_entry *index_entry = NULL;
286 uint64_t total_packets_size = 0;
287 size_t file_index_entry_size;
288 size_t file_entry_count;
289 size_t i;
290 struct ctf_stream_class *sc;
291 struct bt_msg_iter_packet_properties props;
292
293 BT_COMP_LOGI("Building index from .idx file of stream file %s",
294 ds_file->file->path->str);
295 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
296 if (ret) {
297 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
298 goto error;
299 }
300
301 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
302 props.stream_class_id);
303 BT_ASSERT(sc);
304 if (!sc->default_clock_class) {
305 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
306 goto error;
307 }
308
309 /* Look for index file in relative path index/name.idx. */
310 basename = g_path_get_basename(ds_file->file->path->str);
311 if (!basename) {
312 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
313 ds_file->file->path->str);
314 goto error;
315 }
316
317 directory = g_path_get_dirname(ds_file->file->path->str);
318 if (!directory) {
319 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
320 ds_file->file->path->str);
321 goto error;
322 }
323
324 index_basename = g_string_new(basename);
325 if (!index_basename) {
326 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
327 goto error;
328 }
329
330 g_string_append(index_basename, ".idx");
331 index_file_path = g_build_filename(directory, "index",
332 index_basename->str, NULL);
333 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
334 if (!mapped_file) {
335 BT_COMP_LOGD("Cannot create new mapped file %s",
336 index_file_path);
337 goto error;
338 }
339
340 /*
341 * The g_mapped_file API limits us to 4GB files on 32-bit.
342 * Traces with such large indexes have never been seen in the wild,
343 * but this would need to be adjusted to support them.
344 */
345 filesize = g_mapped_file_get_length(mapped_file);
346 if (filesize < sizeof(*header)) {
347 BT_COMP_LOGW("Invalid LTTng trace index file: "
348 "file size (%zu bytes) < header size (%zu bytes)",
349 filesize, sizeof(*header));
350 goto error;
351 }
352
353 mmap_begin = g_mapped_file_get_contents(mapped_file);
354 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
355
356 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
357 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
358 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
359 goto error;
360 }
361
362 file_index_entry_size = be32toh(header->packet_index_len);
363 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
364 if ((filesize - sizeof(*header)) % file_index_entry_size) {
365 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
366 "(%zu bytes) is not a multiple of the index entry size "
367 "(%zu bytes)", (filesize - sizeof(*header)),
368 sizeof(*header));
369 goto error;
370 }
371
372 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
373 if (!index) {
374 goto error;
375 }
376
377 for (i = 0; i < file_entry_count; i++) {
378 struct ctf_packet_index *file_index =
379 (struct ctf_packet_index *) file_pos;
380 uint64_t packet_size = be64toh(file_index->packet_size);
381
382 if (packet_size % CHAR_BIT) {
383 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
384 goto error;
385 }
386
387 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
388 if (!index_entry) {
389 goto error;
390 }
391
392 /* Convert size in bits to bytes. */
393 packet_size /= CHAR_BIT;
394 index_entry->packet_size = packet_size;
395
396 index_entry->offset = be64toh(file_index->offset);
397 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
398 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
399 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
400 (index_entry - 1)->offset, index_entry->offset);
401 goto error;
402 }
403
404 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
405 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
406 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
407 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
408 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
409 index_entry->timestamp_begin,
410 index_entry->timestamp_end);
411 goto error;
412 }
413
414 /* Convert the packet's bound to nanoseconds since Epoch. */
415 ret = convert_cycles_to_ns(sc->default_clock_class,
416 index_entry->timestamp_begin,
417 &index_entry->timestamp_begin_ns);
418 if (ret) {
419 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
420 goto error;
421 }
422 ret = convert_cycles_to_ns(sc->default_clock_class,
423 index_entry->timestamp_end,
424 &index_entry->timestamp_end_ns);
425 if (ret) {
426 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
427 goto error;
428 }
429
430 total_packets_size += packet_size;
431 file_pos += file_index_entry_size;
432
433 g_ptr_array_add(index->entries, index_entry);
434 }
435
436 /* Validate that the index addresses the complete stream. */
437 if (ds_file->file->size != total_packets_size) {
438 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
439 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
440 ds_file->file->size, total_packets_size);
441 goto error;
442 }
443 end:
444 g_free(directory);
445 g_free(basename);
446 g_free(index_file_path);
447 if (index_basename) {
448 g_string_free(index_basename, TRUE);
449 }
450 if (mapped_file) {
451 g_mapped_file_unref(mapped_file);
452 }
453 return index;
454 error:
455 ctf_fs_ds_index_destroy(index);
456 g_free(index_entry);
457 index = NULL;
458 goto end;
459 }
460
461 static
462 int init_index_entry(struct ctf_fs_ds_index_entry *entry,
463 struct ctf_fs_ds_file *ds_file,
464 struct bt_msg_iter_packet_properties *props,
465 off_t packet_size, off_t packet_offset)
466 {
467 int ret = 0;
468 struct ctf_stream_class *sc;
469
470 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
471 props->stream_class_id);
472 BT_ASSERT(sc);
473 BT_ASSERT(packet_offset >= 0);
474 entry->offset = packet_offset;
475 BT_ASSERT(packet_size >= 0);
476 entry->packet_size = packet_size;
477
478 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
479 /* Convert the packet's bound to nanoseconds since Epoch. */
480 ret = convert_cycles_to_ns(sc->default_clock_class,
481 props->snapshots.beginning_clock,
482 &entry->timestamp_begin_ns);
483 if (ret) {
484 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
485 goto end;
486 }
487 } else {
488 entry->timestamp_begin_ns = UINT64_C(-1);
489 }
490
491 if (props->snapshots.end_clock != UINT64_C(-1)) {
492 ret = convert_cycles_to_ns(sc->default_clock_class,
493 props->snapshots.end_clock,
494 &entry->timestamp_end_ns);
495 if (ret) {
496 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
497 goto end;
498 }
499 } else {
500 entry->timestamp_end_ns = UINT64_C(-1);
501 }
502
503 end:
504 return ret;
505 }
506
507 static
508 struct ctf_fs_ds_index *build_index_from_stream_file(
509 struct ctf_fs_ds_file *ds_file)
510 {
511 int ret;
512 struct ctf_fs_ds_index *index = NULL;
513 enum bt_msg_iter_status iter_status = BT_MSG_ITER_STATUS_OK;
514 off_t current_packet_offset_bytes = 0;
515
516 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
517
518 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
519 if (!index) {
520 goto error;
521 }
522
523 do {
524 off_t current_packet_size_bytes;
525 struct ctf_fs_ds_index_entry *index_entry;
526 struct bt_msg_iter_packet_properties props;
527
528 if (current_packet_offset_bytes < 0) {
529 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
530 goto error;
531 } else if (current_packet_offset_bytes > ds_file->file->size) {
532 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
533 goto error;
534 } else if (current_packet_offset_bytes == ds_file->file->size) {
535 /* No more data */
536 break;
537 }
538
539 iter_status = bt_msg_iter_seek(ds_file->msg_iter,
540 current_packet_offset_bytes);
541 if (iter_status != BT_MSG_ITER_STATUS_OK) {
542 goto error;
543 }
544
545 iter_status = bt_msg_iter_get_packet_properties(
546 ds_file->msg_iter, &props);
547 if (iter_status != BT_MSG_ITER_STATUS_OK) {
548 goto error;
549 }
550
551 if (props.exp_packet_total_size >= 0) {
552 current_packet_size_bytes =
553 (uint64_t) props.exp_packet_total_size / 8;
554 } else {
555 current_packet_size_bytes = ds_file->file->size;
556 }
557
558 if (current_packet_offset_bytes + current_packet_size_bytes >
559 ds_file->file->size) {
560 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
561 "packet-offset=%jd, packet-size-bytes=%jd, "
562 "file-size=%jd",
563 ds_file->file->path->str,
564 current_packet_offset_bytes,
565 current_packet_size_bytes,
566 ds_file->file->size);
567 goto error;
568 }
569
570 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
571 if (!index_entry) {
572 BT_COMP_LOGE_STR("Failed to allocate a new index entry.");
573 goto error;
574 }
575
576 ret = init_index_entry(index_entry, ds_file, &props,
577 current_packet_size_bytes, current_packet_offset_bytes);
578 if (ret) {
579 g_free(index_entry);
580 goto error;
581 }
582
583 g_ptr_array_add(index->entries, index_entry);
584
585 current_packet_offset_bytes += current_packet_size_bytes;
586 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
587 "next-packet-offset=%jd",
588 current_packet_offset_bytes - current_packet_size_bytes,
589 current_packet_offset_bytes);
590
591 } while (iter_status == BT_MSG_ITER_STATUS_OK);
592
593 if (iter_status != BT_MSG_ITER_STATUS_OK) {
594 goto error;
595 }
596
597 end:
598 return index;
599
600 error:
601 ctf_fs_ds_index_destroy(index);
602 index = NULL;
603 goto end;
604 }
605
606 BT_HIDDEN
607 struct ctf_fs_ds_file *ctf_fs_ds_file_create(
608 struct ctf_fs_trace *ctf_fs_trace,
609 bt_self_message_iterator *pc_msg_iter,
610 struct bt_msg_iter *msg_iter,
611 bt_stream *stream, const char *path,
612 bt_logging_level log_level)
613 {
614 int ret;
615 const size_t page_size = bt_common_get_page_size(log_level);
616 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
617
618 if (!ds_file) {
619 goto error;
620 }
621
622 ds_file->log_level = log_level;
623 ds_file->self_comp = ctf_fs_trace->self_comp;
624 ds_file->pc_msg_iter = pc_msg_iter;
625 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
626 if (!ds_file->file) {
627 goto error;
628 }
629
630 ds_file->stream = stream;
631 bt_stream_get_ref(ds_file->stream);
632 ds_file->metadata = ctf_fs_trace->metadata;
633 g_string_assign(ds_file->file->path, path);
634 ret = ctf_fs_file_open(ds_file->file, "rb");
635 if (ret) {
636 goto error;
637 }
638
639 ds_file->msg_iter = msg_iter;
640 bt_msg_iter_set_medops_data(ds_file->msg_iter, ds_file);
641 if (!ds_file->msg_iter) {
642 goto error;
643 }
644
645 ds_file->mmap_max_len = page_size * 2048;
646
647 goto end;
648
649 error:
650 /* Do not touch "borrowed" file. */
651 ctf_fs_ds_file_destroy(ds_file);
652 ds_file = NULL;
653
654 end:
655 return ds_file;
656 }
657
658 BT_HIDDEN
659 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
660 struct ctf_fs_ds_file *ds_file)
661 {
662 struct ctf_fs_ds_index *index;
663
664 index = build_index_from_idx_file(ds_file);
665 if (index) {
666 goto end;
667 }
668
669 BT_COMP_LOGI("Failed to build index from .index file; "
670 "falling back to stream indexing.");
671 index = build_index_from_stream_file(ds_file);
672 end:
673 return index;
674 }
675
676 BT_HIDDEN
677 struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
678 bt_self_component *self_comp)
679 {
680 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
681
682 if (!index) {
683 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
684 "Failed to allocate index");
685 goto error;
686 }
687
688 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
689 if (!index->entries) {
690 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
691 "Failed to allocate index entries.");
692 goto error;
693 }
694
695 goto end;
696
697 error:
698 ctf_fs_ds_index_destroy(index);
699 index = NULL;
700 end:
701 return index;
702 }
703
704 BT_HIDDEN
705 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
706 {
707 if (!ds_file) {
708 return;
709 }
710
711 bt_stream_put_ref(ds_file->stream);
712 (void) ds_file_munmap(ds_file);
713
714 if (ds_file->file) {
715 ctf_fs_file_destroy(ds_file->file);
716 }
717
718 g_free(ds_file);
719 }
720
721 BT_HIDDEN
722 bt_component_class_message_iterator_next_method_status ctf_fs_ds_file_next(
723 struct ctf_fs_ds_file *ds_file,
724 bt_message **msg)
725 {
726 enum bt_msg_iter_status msg_iter_status;
727 bt_component_class_message_iterator_next_method_status status;
728
729 msg_iter_status = bt_msg_iter_get_next_message(
730 ds_file->msg_iter, ds_file->pc_msg_iter, msg);
731
732 switch (msg_iter_status) {
733 case BT_MSG_ITER_STATUS_EOF:
734 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
735 break;
736 case BT_MSG_ITER_STATUS_OK:
737 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
738 break;
739 case BT_MSG_ITER_STATUS_AGAIN:
740 /*
741 * Should not make it this far as this is
742 * medium-specific; there is nothing for the user to do
743 * and it should have been handled upstream.
744 */
745 abort();
746 case BT_MSG_ITER_STATUS_INVAL:
747 case BT_MSG_ITER_STATUS_ERROR:
748 default:
749 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
750 break;
751 }
752 return status;
753 }
754
755 BT_HIDDEN
756 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
757 {
758 if (!index) {
759 return;
760 }
761
762 if (index->entries) {
763 g_ptr_array_free(index->entries, TRUE);
764 }
765 g_free(index);
766 }
This page took 0.04493 seconds and 4 git commands to generate.