src.ctf.fs: move internal util to ctf/common/utils
[babeltrace.git] / plugins / ctf / fs-src / data-stream-file.c
CommitLineData
e98a2d6e 1/*
94cf822e 2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
fc9a526c 3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
e98a2d6e
PP
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
e98a2d6e
PP
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
0fbb9a9f 25#include <stdlib.h>
e98a2d6e
PP
26#include <stdio.h>
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <glib.h>
31#include <inttypes.h>
8f76831a 32#include <babeltrace/compat/mman-internal.h>
e9383dfd 33#include <babeltrace/endian-internal.h>
e98a2d6e 34#include <babeltrace/ctf-ir/stream.h>
b2e0c907 35#include <babeltrace/graph/notification-iterator.h>
4f1f88a6
PP
36#include <babeltrace/graph/notification-stream.h>
37#include <babeltrace/graph/notification-event.h>
38#include <babeltrace/graph/notification-packet.h>
55314f2a 39#include <babeltrace/common-internal.h>
78586d8a
JG
40#include "file.h"
41#include "metadata.h"
42#include "../common/notif-iter/notif-iter.h"
43#include <assert.h>
94cf822e 44#include "data-stream-file.h"
e9383dfd 45#include <string.h>
e98a2d6e 46
55314f2a
JG
47#define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
48#include "logging.h"
e98a2d6e 49
4f1f88a6 50static inline
94cf822e 51size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 52{
2c701ca6 53 return ds_file->mmap_len - ds_file->request_offset;
e98a2d6e
PP
54}
55
5b29e799 56static
94cf822e 57int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 58{
fc9a526c 59 int ret = 0;
e98a2d6e 60
d238196d 61 if (!ds_file || !ds_file->mmap_addr) {
94cf822e
PP
62 goto end;
63 }
64
04394229 65 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
cd232764
JG
66 BT_LOGE_ERRNO("Cannot memory-unmap file",
67 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
94cf822e 68 ds_file->mmap_addr, ds_file->mmap_len,
cd232764
JG
69 ds_file->file->path->str,
70 ds_file->file ? ds_file->file->fp : NULL);
fc9a526c
JG
71 ret = -1;
72 goto end;
e98a2d6e 73 }
94cf822e
PP
74
75 ds_file->mmap_addr = NULL;
76
fc9a526c
JG
77end:
78 return ret;
e98a2d6e
PP
79}
80
5b29e799 81static
94cf822e
PP
82enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
83 struct ctf_fs_ds_file *ds_file)
e98a2d6e 84{
e0f6a64a
JG
85 enum bt_ctf_notif_iter_medium_status ret =
86 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
e98a2d6e
PP
87
88 /* Unmap old region */
94cf822e
PP
89 if (ds_file->mmap_addr) {
90 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
91 goto error;
92 }
93
2f5a009b 94 /*
2c701ca6 95 * mmap_len is guaranteed to be page-aligned except on the
2f5a009b
JG
96 * last mapping where it may not be possible (since the file's
97 * size itself may not be a page multiple).
98 */
2c701ca6 99 ds_file->mmap_offset += ds_file->mmap_len;
94cf822e 100 ds_file->request_offset = 0;
e98a2d6e
PP
101 }
102
2c701ca6 103 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
94cf822e 104 ds_file->mmap_max_len);
2c701ca6 105 if (ds_file->mmap_len == 0) {
e0f6a64a
JG
106 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
107 goto end;
108 }
e98a2d6e 109 /* Map new region */
94cf822e 110 assert(ds_file->mmap_len);
04394229 111 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
94cf822e
PP
112 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
113 ds_file->mmap_offset);
114 if (ds_file->mmap_addr == MAP_FAILED) {
1974687e 115 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
94cf822e 116 ds_file->mmap_len, ds_file->file->path->str,
1974687e 117 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
78586d8a 118 strerror(errno));
e98a2d6e
PP
119 goto error;
120 }
121
122 goto end;
e98a2d6e 123error:
94cf822e 124 ds_file_munmap(ds_file);
e0f6a64a 125 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
126end:
127 return ret;
128}
129
5b29e799
JG
130static
131enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
132 size_t request_sz, uint8_t **buffer_addr,
133 size_t *buffer_sz, void *data)
134{
135 enum bt_ctf_notif_iter_medium_status status =
136 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e 137 struct ctf_fs_ds_file *ds_file = data;
e98a2d6e
PP
138
139 if (request_sz == 0) {
140 goto end;
141 }
142
e98a2d6e 143 /* Check if we have at least one memory-mapped byte left */
94cf822e 144 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 145 /* Are we at the end of the file? */
94cf822e 146 if (ds_file->mmap_offset >= ds_file->file->size) {
55314f2a 147 BT_LOGD("Reached end of file \"%s\" (%p)",
94cf822e 148 ds_file->file->path->str, ds_file->file->fp);
e98a2d6e
PP
149 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
150 goto end;
151 }
152
94cf822e 153 status = ds_file_mmap_next(ds_file);
e0f6a64a
JG
154 switch (status) {
155 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
156 break;
157 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
158 goto end;
159 default:
55314f2a 160 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
94cf822e
PP
161 ds_file->file->path->str,
162 ds_file->file->fp);
e98a2d6e
PP
163 goto error;
164 }
165 }
166
94cf822e
PP
167 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
168 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
169 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
170 goto end;
171
172error:
173 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
174
175end:
176 return status;
177}
178
5b29e799
JG
179static
180struct bt_ctf_stream *medop_get_stream(
b92735af
PP
181 struct bt_ctf_stream_class *stream_class, uint64_t stream_id,
182 void *data)
e98a2d6e 183{
94cf822e
PP
184 struct ctf_fs_ds_file *ds_file = data;
185 struct bt_ctf_stream_class *ds_file_stream_class;
186 struct bt_ctf_stream *stream = NULL;
187
188 ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream);
189 bt_put(ds_file_stream_class);
190
191 if (stream_class != ds_file_stream_class) {
192 /*
193 * Not supported: two packets described by two different
194 * stream classes within the same data stream file.
195 */
196 goto end;
e98a2d6e
PP
197 }
198
94cf822e
PP
199 stream = ds_file->stream;
200
201end:
202 return stream;
e98a2d6e
PP
203}
204
6de92955
PP
205BT_HIDDEN
206struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops = {
e98a2d6e
PP
207 .request_bytes = medop_request_bytes,
208 .get_stream = medop_get_stream,
209};
6de92955 210
97ade20b
JG
211static
212struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length)
213{
214 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
215
216 if (!index) {
217 BT_LOGE_STR("Failed to allocate index");
218 goto error;
219 }
220
221 index->entries = g_array_sized_new(FALSE, TRUE,
222 sizeof(struct ctf_fs_ds_index_entry), length);
223 if (!index->entries) {
224 BT_LOGE("Failed to allocate %zu index entries.", length);
225 goto error;
226 }
227 g_array_set_size(index->entries, length);
228end:
229 return index;
230error:
231 ctf_fs_ds_index_destroy(index);
232 goto end;
233}
e98a2d6e 234
b6c3dcb2 235static
97ade20b
JG
236struct bt_ctf_clock_class *get_field_mapped_clock_class(
237 struct bt_ctf_field *field)
238{
239 struct bt_ctf_field_type *field_type;
240 struct bt_ctf_clock_class *clock_class = NULL;
241
242 field_type = bt_ctf_field_get_type(field);
243 if (!field_type) {
244 goto end;
245 }
246
247 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
248 field_type);
249 if (!clock_class) {
250 goto end;
251 }
252end:
253 bt_put(field_type);
254 return clock_class;
255}
256
257static
258int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file,
259 struct bt_ctf_clock_class **_timestamp_begin_cc,
260 struct bt_ctf_clock_class **_timestamp_end_cc)
261{
262 int ret;
89b08333 263 struct bt_ctf_field *timestamp_field = NULL;
97ade20b
JG
264 struct bt_ctf_field *packet_context_field = NULL;
265 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
266 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
267
268 ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
269 NULL, &packet_context_field);
270 if (ret || !packet_context_field) {
271 BT_LOGD("Cannot retrieve packet context field of stream \'%s\'",
272 ds_file->file->path->str);
273 ret = -1;
274 goto end;
275 }
276
277 timestamp_field = bt_ctf_field_structure_get_field_by_name(
278 packet_context_field, "timestamp_begin");
279 if (!timestamp_field) {
280 BT_LOGD("Cannot retrieve timestamp_begin field in packet context of stream \'%s\'",
281 ds_file->file->path->str);
282 ret = -1;
283 goto end;
284 }
285
286 timestamp_begin_cc = get_field_mapped_clock_class(timestamp_field);
287 if (!timestamp_begin_cc) {
288 BT_LOGD("Cannot retrieve the clock mapped to timestamp_begin of stream \'%s\'",
289 ds_file->file->path->str);
290 }
89b08333 291 BT_PUT(timestamp_field);
97ade20b
JG
292
293 timestamp_field = bt_ctf_field_structure_get_field_by_name(
294 packet_context_field, "timestamp_end");
295 if (!timestamp_field) {
296 BT_LOGD("Cannot retrieve timestamp_end field in packet context of stream \'%s\'",
297 ds_file->file->path->str);
298 ret = -1;
299 goto end;
300 }
301
302 timestamp_end_cc = get_field_mapped_clock_class(timestamp_field);
303 if (!timestamp_end_cc) {
304 BT_LOGD("Cannot retrieve the clock mapped to timestamp_end in stream \'%s\'",
305 ds_file->file->path->str);
306 }
307
308 if (_timestamp_begin_cc) {
309 *_timestamp_begin_cc = bt_get(timestamp_begin_cc);
310 }
311 if (_timestamp_end_cc) {
312 *_timestamp_end_cc = bt_get(timestamp_end_cc);
313 }
314end:
315 bt_put(packet_context_field);
316 bt_put(timestamp_field);
317 bt_put(timestamp_begin_cc);
318 bt_put(timestamp_end_cc);
319 return ret;
320}
321
322static
323int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class,
324 uint64_t cycles, int64_t *ns)
b6c3dcb2
JG
325{
326 int ret = 0;
97ade20b
JG
327 struct bt_ctf_clock_value *clock_value;
328
329 assert(ns);
330 clock_value = bt_ctf_clock_value_create(clock_class, cycles);
331 if (!clock_value) {
332 ret = -1;
333 goto end;
334 }
335
336 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
337 if (ret) {
338 goto end;
339 }
340end:
341 bt_put(clock_value);
342 return ret;
343}
344
345static
346struct ctf_fs_ds_index *build_index_from_idx_file(
347 struct ctf_fs_ds_file *ds_file)
348{
349 int ret;
b6c3dcb2
JG
350 gchar *directory = NULL;
351 gchar *basename = NULL;
352 GString *index_basename = NULL;
353 gchar *index_file_path = NULL;
354 GMappedFile *mapped_file = NULL;
355 gsize filesize;
97ade20b
JG
356 const char *mmap_begin = NULL, *file_pos = NULL;
357 const struct ctf_packet_index_file_hdr *header = NULL;
358 struct ctf_fs_ds_index *index = NULL;
359 struct ctf_fs_ds_index_entry *index_entry = NULL;
b6c3dcb2
JG
360 uint64_t total_packets_size = 0;
361 size_t file_index_entry_size;
362 size_t file_entry_count;
363 size_t i;
97ade20b
JG
364 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
365 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
366
367 BT_LOGD("Building index from .idx file of stream file %s",
368 ds_file->file->path->str);
369
370 ret = get_ds_file_packet_bounds_clock_classes(ds_file,
371 &timestamp_begin_cc, &timestamp_end_cc);
372 if (ret) {
373 BT_LOGD("Cannot get clock classes of \"timestamp_begin\" and \"timestamp_end\" fields");
374 goto error;
375 }
b6c3dcb2
JG
376
377 /* Look for index file in relative path index/name.idx. */
94cf822e 378 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 379 if (!basename) {
97ade20b 380 BT_LOGE("Cannot get the basename of datastream file %s",
55314f2a 381 ds_file->file->path->str);
97ade20b 382 goto error;
b6c3dcb2
JG
383 }
384
94cf822e 385 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 386 if (!directory) {
97ade20b 387 BT_LOGE("Cannot get dirname of datastream file %s",
55314f2a 388 ds_file->file->path->str);
97ade20b 389 goto error;
b6c3dcb2
JG
390 }
391
392 index_basename = g_string_new(basename);
393 if (!index_basename) {
97ade20b
JG
394 BT_LOGE("Cannot allocate index file basename string");
395 goto error;
b6c3dcb2
JG
396 }
397
398 g_string_append(index_basename, ".idx");
399 index_file_path = g_build_filename(directory, "index",
400 index_basename->str, NULL);
401 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 402 if (!mapped_file) {
97ade20b 403 BT_LOGD("Cannot create new mapped file %s",
55314f2a 404 index_file_path);
97ade20b 405 goto error;
06367fa3 406 }
d14940a7
JG
407
408 /*
409 * The g_mapped_file API limits us to 4GB files on 32-bit.
410 * Traces with such large indexes have never been seen in the wild,
411 * but this would need to be adjusted to support them.
412 */
b6c3dcb2
JG
413 filesize = g_mapped_file_get_length(mapped_file);
414 if (filesize < sizeof(*header)) {
d14940a7
JG
415 BT_LOGW("Invalid LTTng trace index file: "
416 "file size (%zu bytes) < header size (%zu bytes)",
417 filesize, sizeof(*header));
97ade20b 418 goto error;
b6c3dcb2
JG
419 }
420
421 mmap_begin = g_mapped_file_get_contents(mapped_file);
422 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
423
424 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
425 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
d14940a7 426 BT_LOGW("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 427 goto error;
b6c3dcb2 428 }
b6c3dcb2
JG
429
430 file_index_entry_size = be32toh(header->packet_index_len);
431 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 432 if ((filesize - sizeof(*header)) % file_index_entry_size) {
d14940a7
JG
433 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
434 "(%zu bytes) is not a multiple of the index entry size "
435 "(%zu bytes)", (filesize - sizeof(*header)),
436 sizeof(*header));
97ade20b 437 goto error;
b6c3dcb2
JG
438 }
439
97ade20b
JG
440 index = ctf_fs_ds_index_create(file_entry_count);
441 if (!index) {
442 goto error;
b6c3dcb2 443 }
97ade20b
JG
444
445 index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index(
446 index->entries, struct ctf_fs_ds_index_entry, 0);
b6c3dcb2
JG
447 for (i = 0; i < file_entry_count; i++) {
448 struct ctf_packet_index *file_index =
449 (struct ctf_packet_index *) file_pos;
450 uint64_t packet_size = be64toh(file_index->packet_size);
451
452 if (packet_size % CHAR_BIT) {
d14940a7 453 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 454 goto error;
b6c3dcb2
JG
455 }
456
457 /* Convert size in bits to bytes. */
458 packet_size /= CHAR_BIT;
97ade20b 459 index_entry->packet_size = packet_size;
b6c3dcb2 460
97ade20b
JG
461 index_entry->offset = be64toh(file_index->offset);
462 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
d14940a7
JG
463 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
464 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
465 (index_entry - 1)->offset, index_entry->offset);
97ade20b 466 goto error;
b6c3dcb2
JG
467 }
468
97ade20b
JG
469 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
470 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
471 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
d14940a7
JG
472 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
473 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
474 index_entry->timestamp_begin,
475 index_entry->timestamp_end);
97ade20b
JG
476 goto error;
477 }
478
479 /* Convert the packet's bound to nanoseconds since Epoch. */
480 ret = convert_cycles_to_ns(timestamp_begin_cc,
481 index_entry->timestamp_begin,
482 &index_entry->timestamp_begin_ns);
483 if (ret) {
d14940a7 484 BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
485 goto error;
486 }
487 ret = convert_cycles_to_ns(timestamp_end_cc,
488 index_entry->timestamp_end,
489 &index_entry->timestamp_end_ns);
490 if (ret) {
d14940a7 491 BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 492 goto error;
b6c3dcb2
JG
493 }
494
495 total_packets_size += packet_size;
496 file_pos += file_index_entry_size;
97ade20b 497 index_entry++;
b6c3dcb2
JG
498 }
499
500 /* Validate that the index addresses the complete stream. */
94cf822e 501 if (ds_file->file->size != total_packets_size) {
d14940a7
JG
502 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
503 "file_size=%" PRIu64 ", total_packets_size=%" PRIu64,
504 ds_file->file->size, total_packets_size);
97ade20b 505 goto error;
b6c3dcb2
JG
506 }
507end:
508 g_free(directory);
509 g_free(basename);
510 g_free(index_file_path);
06367fa3
JG
511 if (index_basename) {
512 g_string_free(index_basename, TRUE);
513 }
514 if (mapped_file) {
515 g_mapped_file_unref(mapped_file);
516 }
97ade20b
JG
517 bt_put(timestamp_begin_cc);
518 bt_put(timestamp_end_cc);
519 return index;
520error:
521 ctf_fs_ds_index_destroy(index);
522 index = NULL;
b6c3dcb2
JG
523 goto end;
524}
525
e7a4393b 526BT_HIDDEN
94cf822e
PP
527struct ctf_fs_ds_file *ctf_fs_ds_file_create(
528 struct ctf_fs_trace *ctf_fs_trace,
6de92955 529 struct bt_ctf_notif_iter *notif_iter,
97ade20b 530 struct bt_ctf_stream *stream, const char *path)
e98a2d6e 531{
b6c3dcb2 532 int ret;
55314f2a 533 const size_t page_size = bt_common_get_page_size();
94cf822e 534 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 535
94cf822e 536 if (!ds_file) {
e98a2d6e
PP
537 goto error;
538 }
539
55314f2a 540 ds_file->file = ctf_fs_file_create();
94cf822e 541 if (!ds_file->file) {
4f1f88a6
PP
542 goto error;
543 }
544
94cf822e
PP
545 ds_file->stream = bt_get(stream);
546 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
547 g_string_assign(ds_file->file->path, path);
55314f2a 548 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
549 if (ret) {
550 goto error;
551 }
552
6de92955
PP
553 ds_file->notif_iter = notif_iter;
554 bt_ctf_notif_iter_set_medops_data(ds_file->notif_iter, ds_file);
94cf822e 555 if (!ds_file->notif_iter) {
e98a2d6e
PP
556 goto error;
557 }
5b29e799 558
55314f2a 559 ds_file->mmap_max_len = page_size * 2048;
94cf822e 560
e98a2d6e 561 goto end;
1a9f7075 562
e98a2d6e 563error:
78586d8a 564 /* Do not touch "borrowed" file. */
94cf822e
PP
565 ctf_fs_ds_file_destroy(ds_file);
566 ds_file = NULL;
1a9f7075 567
e98a2d6e 568end:
94cf822e 569 return ds_file;
e98a2d6e
PP
570}
571
97ade20b
JG
572BT_HIDDEN
573struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
574 struct ctf_fs_ds_file *ds_file)
575{
576 return build_index_from_idx_file(ds_file);
577}
578
5b29e799 579BT_HIDDEN
94cf822e 580void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 581{
94cf822e 582 if (!ds_file) {
5b29e799 583 return;
043e2020
JG
584 }
585
94cf822e
PP
586 bt_put(ds_file->cc_prio_map);
587 bt_put(ds_file->stream);
588 (void) ds_file_munmap(ds_file);
0982a26d 589
94cf822e
PP
590 if (ds_file->file) {
591 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
592 }
593
94cf822e 594 g_free(ds_file);
e98a2d6e 595}
4f1f88a6
PP
596
597BT_HIDDEN
94cf822e
PP
598struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
599 struct ctf_fs_ds_file *ds_file)
4f1f88a6
PP
600{
601 enum bt_ctf_notif_iter_status notif_iter_status;
602 struct bt_notification_iterator_next_return ret = {
603 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
604 .notification = NULL,
605 };
606
8915b7a7 607 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
94cf822e 608 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
4f1f88a6 609
4f1f88a6
PP
610 switch (notif_iter_status) {
611 case BT_CTF_NOTIF_ITER_STATUS_EOF:
612 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
613 break;
614 case BT_CTF_NOTIF_ITER_STATUS_OK:
615 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
616 break;
617 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
618 /*
619 * Should not make it this far as this is
620 * medium-specific; there is nothing for the user to do
621 * and it should have been handled upstream.
622 */
0fbb9a9f 623 abort();
4f1f88a6
PP
624 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
625 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
626 default:
627 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
628 break;
629 }
630
631 return ret;
632}
94cf822e
PP
633
634BT_HIDDEN
635int ctf_fs_ds_file_get_packet_header_context_fields(
97ade20b 636 struct ctf_fs_ds_file *ds_file,
94cf822e
PP
637 struct bt_ctf_field **packet_header_field,
638 struct bt_ctf_field **packet_context_field)
639{
640 enum bt_ctf_notif_iter_status notif_iter_status;
94cf822e
PP
641 int ret = 0;
642
97ade20b 643 assert(ds_file);
94cf822e
PP
644 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
645 ds_file->notif_iter, packet_header_field, packet_context_field);
646 switch (notif_iter_status) {
647 case BT_CTF_NOTIF_ITER_STATUS_EOF:
648 case BT_CTF_NOTIF_ITER_STATUS_OK:
649 break;
650 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
0fbb9a9f 651 abort();
94cf822e
PP
652 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
653 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
654 default:
655 goto error;
656 break;
657 }
658
659 goto end;
660
661error:
662 ret = -1;
663
664 if (packet_header_field) {
665 bt_put(*packet_header_field);
666 }
667
668 if (packet_context_field) {
669 bt_put(*packet_context_field);
670 }
671
672end:
94cf822e
PP
673 return ret;
674}
97ade20b
JG
675
676BT_HIDDEN
677void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
678{
679 if (!index) {
680 return;
681 }
682
683 if (index->entries) {
684 g_array_free(index->entries, TRUE);
685 }
686 g_free(index);
687}
This page took 0.065297 seconds and 4 git commands to generate.