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