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