Logging: standardize logging tags
[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 #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 "compat/mman.h"
33 #include "compat/endian.h"
34 #include <babeltrace2/babeltrace.h>
35 #include "common/common.h"
36 #include "file.h"
37 #include "metadata.h"
38 #include "../common/msg-iter/msg-iter.h"
39 #include "common/assert.h"
40 #include "data-stream-file.h"
41 #include <string.h>
42
43 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/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 int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
261 uint64_t cycles, int64_t *ns)
262 {
263 return bt_util_clock_cycles_to_ns_from_origin(cycles,
264 clock_class->frequency, clock_class->offset_seconds,
265 clock_class->offset_cycles, ns);
266 }
267
268 static
269 struct ctf_fs_ds_index *build_index_from_idx_file(
270 struct ctf_fs_ds_file *ds_file)
271 {
272 int ret;
273 gchar *directory = NULL;
274 gchar *basename = NULL;
275 GString *index_basename = NULL;
276 gchar *index_file_path = NULL;
277 GMappedFile *mapped_file = NULL;
278 gsize filesize;
279 const char *mmap_begin = NULL, *file_pos = NULL;
280 const struct ctf_packet_index_file_hdr *header = NULL;
281 struct ctf_fs_ds_index *index = NULL;
282 struct ctf_fs_ds_index_entry *index_entry = NULL;
283 uint64_t total_packets_size = 0;
284 size_t file_index_entry_size;
285 size_t file_entry_count;
286 size_t i;
287 struct ctf_stream_class *sc;
288 struct bt_msg_iter_packet_properties props;
289
290 BT_LOGI("Building index from .idx file of stream file %s",
291 ds_file->file->path->str);
292 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
293 if (ret) {
294 BT_LOGI_STR("Cannot read first packet's header and context fields.");
295 goto error;
296 }
297
298 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
299 props.stream_class_id);
300 BT_ASSERT(sc);
301 if (!sc->default_clock_class) {
302 BT_LOGI_STR("Cannot find stream class's default clock class.");
303 goto error;
304 }
305
306 /* Look for index file in relative path index/name.idx. */
307 basename = g_path_get_basename(ds_file->file->path->str);
308 if (!basename) {
309 BT_LOGE("Cannot get the basename of datastream file %s",
310 ds_file->file->path->str);
311 goto error;
312 }
313
314 directory = g_path_get_dirname(ds_file->file->path->str);
315 if (!directory) {
316 BT_LOGE("Cannot get dirname of datastream file %s",
317 ds_file->file->path->str);
318 goto error;
319 }
320
321 index_basename = g_string_new(basename);
322 if (!index_basename) {
323 BT_LOGE_STR("Cannot allocate index file basename string");
324 goto error;
325 }
326
327 g_string_append(index_basename, ".idx");
328 index_file_path = g_build_filename(directory, "index",
329 index_basename->str, NULL);
330 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
331 if (!mapped_file) {
332 BT_LOGD("Cannot create new mapped file %s",
333 index_file_path);
334 goto error;
335 }
336
337 /*
338 * The g_mapped_file API limits us to 4GB files on 32-bit.
339 * Traces with such large indexes have never been seen in the wild,
340 * but this would need to be adjusted to support them.
341 */
342 filesize = g_mapped_file_get_length(mapped_file);
343 if (filesize < sizeof(*header)) {
344 BT_LOGW("Invalid LTTng trace index file: "
345 "file size (%zu bytes) < header size (%zu bytes)",
346 filesize, sizeof(*header));
347 goto error;
348 }
349
350 mmap_begin = g_mapped_file_get_contents(mapped_file);
351 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
352
353 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
354 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
355 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
356 goto error;
357 }
358
359 file_index_entry_size = be32toh(header->packet_index_len);
360 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
361 if ((filesize - sizeof(*header)) % file_index_entry_size) {
362 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
363 "(%zu bytes) is not a multiple of the index entry size "
364 "(%zu bytes)", (filesize - sizeof(*header)),
365 sizeof(*header));
366 goto error;
367 }
368
369 index = ctf_fs_ds_index_create();
370 if (!index) {
371 goto error;
372 }
373
374 for (i = 0; i < file_entry_count; i++) {
375 struct ctf_packet_index *file_index =
376 (struct ctf_packet_index *) file_pos;
377 uint64_t packet_size = be64toh(file_index->packet_size);
378
379 if (packet_size % CHAR_BIT) {
380 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
381 goto error;
382 }
383
384 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
385 if (!index_entry) {
386 goto error;
387 }
388
389 /* Convert size in bits to bytes. */
390 packet_size /= CHAR_BIT;
391 index_entry->packet_size = packet_size;
392
393 index_entry->offset = be64toh(file_index->offset);
394 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
395 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
396 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
397 (index_entry - 1)->offset, index_entry->offset);
398 goto error;
399 }
400
401 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
402 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
403 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
404 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
405 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
406 index_entry->timestamp_begin,
407 index_entry->timestamp_end);
408 goto error;
409 }
410
411 /* Convert the packet's bound to nanoseconds since Epoch. */
412 ret = convert_cycles_to_ns(sc->default_clock_class,
413 index_entry->timestamp_begin,
414 &index_entry->timestamp_begin_ns);
415 if (ret) {
416 BT_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
417 goto error;
418 }
419 ret = convert_cycles_to_ns(sc->default_clock_class,
420 index_entry->timestamp_end,
421 &index_entry->timestamp_end_ns);
422 if (ret) {
423 BT_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
424 goto error;
425 }
426
427 total_packets_size += packet_size;
428 file_pos += file_index_entry_size;
429
430 g_ptr_array_add(index->entries, index_entry);
431 }
432
433 /* Validate that the index addresses the complete stream. */
434 if (ds_file->file->size != total_packets_size) {
435 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
436 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
437 ds_file->file->size, total_packets_size);
438 goto error;
439 }
440 end:
441 g_free(directory);
442 g_free(basename);
443 g_free(index_file_path);
444 if (index_basename) {
445 g_string_free(index_basename, TRUE);
446 }
447 if (mapped_file) {
448 g_mapped_file_unref(mapped_file);
449 }
450 return index;
451 error:
452 ctf_fs_ds_index_destroy(index);
453 g_free(index_entry);
454 index = NULL;
455 goto end;
456 }
457
458 static
459 int init_index_entry(struct ctf_fs_ds_index_entry *entry,
460 struct ctf_fs_ds_file *ds_file,
461 struct bt_msg_iter_packet_properties *props,
462 off_t packet_size, off_t packet_offset)
463 {
464 int ret = 0;
465 struct ctf_stream_class *sc;
466
467 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
468 props->stream_class_id);
469 BT_ASSERT(sc);
470 BT_ASSERT(packet_offset >= 0);
471 entry->offset = packet_offset;
472 BT_ASSERT(packet_size >= 0);
473 entry->packet_size = packet_size;
474
475 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
476 /* Convert the packet's bound to nanoseconds since Epoch. */
477 ret = convert_cycles_to_ns(sc->default_clock_class,
478 props->snapshots.beginning_clock,
479 &entry->timestamp_begin_ns);
480 if (ret) {
481 BT_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
482 goto end;
483 }
484 } else {
485 entry->timestamp_begin_ns = UINT64_C(-1);
486 }
487
488 if (props->snapshots.end_clock != UINT64_C(-1)) {
489 ret = convert_cycles_to_ns(sc->default_clock_class,
490 props->snapshots.end_clock,
491 &entry->timestamp_end_ns);
492 if (ret) {
493 BT_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
494 goto end;
495 }
496 } else {
497 entry->timestamp_end_ns = UINT64_C(-1);
498 }
499
500 end:
501 return ret;
502 }
503
504 static
505 struct ctf_fs_ds_index *build_index_from_stream_file(
506 struct ctf_fs_ds_file *ds_file)
507 {
508 int ret;
509 struct ctf_fs_ds_index *index = NULL;
510 enum bt_msg_iter_status iter_status = BT_MSG_ITER_STATUS_OK;
511 off_t current_packet_offset_bytes = 0;
512
513 BT_LOGI("Indexing stream file %s", ds_file->file->path->str);
514
515 index = ctf_fs_ds_index_create();
516 if (!index) {
517 goto error;
518 }
519
520 do {
521 off_t current_packet_size_bytes;
522 struct ctf_fs_ds_index_entry *index_entry;
523 struct bt_msg_iter_packet_properties props;
524
525 if (current_packet_offset_bytes < 0) {
526 BT_LOGE_STR("Cannot get the current packet's offset.");
527 goto error;
528 } else if (current_packet_offset_bytes > ds_file->file->size) {
529 BT_LOGE_STR("Unexpected current packet's offset (larger than file).");
530 goto error;
531 } else if (current_packet_offset_bytes == ds_file->file->size) {
532 /* No more data */
533 break;
534 }
535
536 iter_status = bt_msg_iter_seek(ds_file->msg_iter,
537 current_packet_offset_bytes);
538 if (iter_status != BT_MSG_ITER_STATUS_OK) {
539 goto error;
540 }
541
542 iter_status = bt_msg_iter_get_packet_properties(
543 ds_file->msg_iter, &props);
544 if (iter_status != BT_MSG_ITER_STATUS_OK) {
545 goto error;
546 }
547
548 if (props.exp_packet_total_size >= 0) {
549 current_packet_size_bytes =
550 (uint64_t) props.exp_packet_total_size / 8;
551 } else {
552 current_packet_size_bytes = ds_file->file->size;
553 }
554
555 if (current_packet_offset_bytes + current_packet_size_bytes >
556 ds_file->file->size) {
557 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
558 "packet-offset=%jd, packet-size-bytes=%jd, "
559 "file-size=%jd",
560 ds_file->file->path->str,
561 current_packet_offset_bytes,
562 current_packet_size_bytes,
563 ds_file->file->size);
564 goto error;
565 }
566
567 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
568 if (!index_entry) {
569 BT_LOGE_STR("Failed to allocate a new index entry.");
570 goto error;
571 }
572
573 ret = init_index_entry(index_entry, ds_file, &props,
574 current_packet_size_bytes, current_packet_offset_bytes);
575 if (ret) {
576 g_free(index_entry);
577 goto error;
578 }
579
580 g_ptr_array_add(index->entries, index_entry);
581
582 current_packet_offset_bytes += current_packet_size_bytes;
583 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
584 "next-packet-offset=%jd",
585 current_packet_offset_bytes - current_packet_size_bytes,
586 current_packet_offset_bytes);
587
588 } while (iter_status == BT_MSG_ITER_STATUS_OK);
589
590 if (iter_status != BT_MSG_ITER_STATUS_OK) {
591 goto error;
592 }
593
594 end:
595 return index;
596
597 error:
598 ctf_fs_ds_index_destroy(index);
599 index = NULL;
600 goto end;
601 }
602
603 BT_HIDDEN
604 struct ctf_fs_ds_file *ctf_fs_ds_file_create(
605 struct ctf_fs_trace *ctf_fs_trace,
606 bt_self_message_iterator *pc_msg_iter,
607 struct bt_msg_iter *msg_iter,
608 bt_stream *stream, const char *path)
609 {
610 int ret;
611 const size_t page_size = bt_common_get_page_size();
612 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
613
614 if (!ds_file) {
615 goto error;
616 }
617
618 ds_file->pc_msg_iter = pc_msg_iter;
619 ds_file->file = ctf_fs_file_create();
620 if (!ds_file->file) {
621 goto error;
622 }
623
624 ds_file->stream = stream;
625 bt_stream_get_ref(ds_file->stream);
626 ds_file->metadata = ctf_fs_trace->metadata;
627 g_string_assign(ds_file->file->path, path);
628 ret = ctf_fs_file_open(ds_file->file, "rb");
629 if (ret) {
630 goto error;
631 }
632
633 ds_file->msg_iter = msg_iter;
634 bt_msg_iter_set_medops_data(ds_file->msg_iter, ds_file);
635 if (!ds_file->msg_iter) {
636 goto error;
637 }
638
639 ds_file->mmap_max_len = page_size * 2048;
640
641 goto end;
642
643 error:
644 /* Do not touch "borrowed" file. */
645 ctf_fs_ds_file_destroy(ds_file);
646 ds_file = NULL;
647
648 end:
649 return ds_file;
650 }
651
652 BT_HIDDEN
653 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
654 struct ctf_fs_ds_file *ds_file)
655 {
656 struct ctf_fs_ds_index *index;
657
658 index = build_index_from_idx_file(ds_file);
659 if (index) {
660 goto end;
661 }
662
663 BT_LOGI("Failed to build index from .index file; "
664 "falling back to stream indexing.");
665 index = build_index_from_stream_file(ds_file);
666 end:
667 return index;
668 }
669
670 BT_HIDDEN
671 struct ctf_fs_ds_index *ctf_fs_ds_index_create()
672 {
673 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
674
675 if (!index) {
676 BT_LOGE_STR("Failed to allocate index");
677 goto error;
678 }
679
680 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
681 if (!index->entries) {
682 BT_LOGE("Failed to allocate index entries.");
683 goto error;
684 }
685
686 goto end;
687
688 error:
689 ctf_fs_ds_index_destroy(index);
690 index = NULL;
691 end:
692 return index;
693 }
694
695 BT_HIDDEN
696 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
697 {
698 if (!ds_file) {
699 return;
700 }
701
702 bt_stream_put_ref(ds_file->stream);
703 (void) ds_file_munmap(ds_file);
704
705 if (ds_file->file) {
706 ctf_fs_file_destroy(ds_file->file);
707 }
708
709 g_free(ds_file);
710 }
711
712 BT_HIDDEN
713 bt_self_message_iterator_status ctf_fs_ds_file_next(
714 struct ctf_fs_ds_file *ds_file,
715 bt_message **msg)
716 {
717 enum bt_msg_iter_status msg_iter_status;
718 bt_self_message_iterator_status status;
719
720 msg_iter_status = bt_msg_iter_get_next_message(
721 ds_file->msg_iter, ds_file->pc_msg_iter, msg);
722
723 switch (msg_iter_status) {
724 case BT_MSG_ITER_STATUS_EOF:
725 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
726 break;
727 case BT_MSG_ITER_STATUS_OK:
728 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
729 break;
730 case BT_MSG_ITER_STATUS_AGAIN:
731 /*
732 * Should not make it this far as this is
733 * medium-specific; there is nothing for the user to do
734 * and it should have been handled upstream.
735 */
736 abort();
737 case BT_MSG_ITER_STATUS_INVAL:
738 case BT_MSG_ITER_STATUS_ERROR:
739 default:
740 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
741 break;
742 }
743 return status;
744 }
745
746 BT_HIDDEN
747 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
748 {
749 if (!index) {
750 return;
751 }
752
753 if (index->entries) {
754 g_ptr_array_free(index->entries, TRUE);
755 }
756 g_free(index);
757 }
This page took 0.060845 seconds and 4 git commands to generate.