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