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