Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / plugins / ctf / fs-src / fs.c
CommitLineData
7a278c8e 1/*
ea0b4b9e 2 * fs.c
7a278c8e 3 *
ea0b4b9e 4 * Babeltrace CTF file system Reader Component
7a278c8e 5 *
1a9f7075 6 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
f3bc2010 7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7a278c8e 8 *
7a278c8e
JG
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
4bd72b60 28#include <babeltrace/common-internal.h>
9d408fca 29#include <babeltrace/babeltrace.h>
7d61fa8e 30#include <plugins-common.h>
ea0b4b9e 31#include <glib.h>
f6ccaed9 32#include <babeltrace/assert-internal.h>
94cf822e 33#include <inttypes.h>
c55a9f58 34#include <stdbool.h>
56a1cced 35#include "fs.h"
413bc2c4 36#include "metadata.h"
94cf822e 37#include "data-stream-file.h"
e7a4393b 38#include "file.h"
1e649dff 39#include "../common/metadata/decoder.h"
6de92955 40#include "../common/notif-iter/notif-iter.h"
a429c321 41#include "../common/utils/utils.h"
04c0ba87 42#include "query.h"
e7a4393b 43
55314f2a
JG
44#define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
45#include "logging.h"
ea0b4b9e 46
94cf822e
PP
47static
48int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data *notif_iter_data)
49{
50 struct ctf_fs_ds_file_info *ds_file_info;
51 int ret = 0;
52
f6ccaed9 53 BT_ASSERT(notif_iter_data->ds_file_info_index <
94cf822e
PP
54 notif_iter_data->ds_file_group->ds_file_infos->len);
55 ds_file_info = g_ptr_array_index(
56 notif_iter_data->ds_file_group->ds_file_infos,
57 notif_iter_data->ds_file_info_index);
58
59 ctf_fs_ds_file_destroy(notif_iter_data->ds_file);
60 notif_iter_data->ds_file = ctf_fs_ds_file_create(
61 notif_iter_data->ds_file_group->ctf_fs_trace,
6de92955 62 notif_iter_data->notif_iter,
94cf822e 63 notif_iter_data->ds_file_group->stream,
97ade20b 64 ds_file_info->path->str);
94cf822e
PP
65 if (!notif_iter_data->ds_file) {
66 ret = -1;
67 }
68
69 return ret;
70}
71
72static
73void ctf_fs_notif_iter_data_destroy(
74 struct ctf_fs_notif_iter_data *notif_iter_data)
75{
76 if (!notif_iter_data) {
77 return;
78 }
79
80 ctf_fs_ds_file_destroy(notif_iter_data->ds_file);
6de92955
PP
81
82 if (notif_iter_data->notif_iter) {
50842bdc 83 bt_notif_iter_destroy(notif_iter_data->notif_iter);
6de92955
PP
84 }
85
94cf822e
PP
86 g_free(notif_iter_data);
87}
88
90157d89
PP
89struct bt_notification_iterator_next_method_return ctf_fs_iterator_next(
90 struct bt_private_connection_private_notification_iterator *iterator)
ea0b4b9e 91{
90157d89 92 struct bt_notification_iterator_next_method_return next_ret;
94cf822e 93 struct ctf_fs_notif_iter_data *notif_iter_data =
90157d89 94 bt_private_connection_private_notification_iterator_get_user_data(iterator);
94cf822e
PP
95 int ret;
96
f6ccaed9 97 BT_ASSERT(notif_iter_data->ds_file);
94cf822e 98 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
f42867e2
PP
99
100 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK &&
101 bt_notification_get_type(next_ret.notification) ==
102 BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
103 if (notif_iter_data->skip_stream_begin_notifs) {
104 /*
105 * We already emitted a
106 * BT_NOTIFICATION_TYPE_STREAM_BEGIN
107 * notification: skip this one, get a new one.
108 */
109 BT_PUT(next_ret.notification);
110 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
111 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END);
112 goto end;
113 } else {
114 /*
115 * First BT_NOTIFICATION_TYPE_STREAM_BEGIN
116 * notification: skip all following.
117 */
118 notif_iter_data->skip_stream_begin_notifs = true;
119 goto end;
120 }
121 }
122
123 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK &&
124 bt_notification_get_type(next_ret.notification) ==
125 BT_NOTIFICATION_TYPE_STREAM_END) {
94cf822e
PP
126 notif_iter_data->ds_file_info_index++;
127
128 if (notif_iter_data->ds_file_info_index ==
129 notif_iter_data->ds_file_group->ds_file_infos->len) {
130 /*
131 * No more stream files to read: we reached the
f42867e2
PP
132 * real end. Emit this
133 * BT_NOTIFICATION_TYPE_STREAM_END notification.
134 * The next time ctf_fs_iterator_next() is
135 * called for this notification iterator,
136 * ctf_fs_ds_file_next() will return
137 * BT_NOTIFICATION_ITERATOR_STATUS_END().
94cf822e
PP
138 */
139 goto end;
140 }
141
f42867e2
PP
142 BT_PUT(next_ret.notification);
143 bt_notif_iter_reset(notif_iter_data->notif_iter);
144
94cf822e
PP
145 /*
146 * Open and start reading the next stream file within
147 * our stream file group.
148 */
149 ret = notif_iter_data_set_current_ds_file(notif_iter_data);
150 if (ret) {
151 next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
152 goto end;
153 }
154
155 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
156
157 /*
f42867e2
PP
158 * If we get a notification, we expect to get a
159 * BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
160 * because the iterator's state machine emits one before
161 * even requesting the first block of data from the
162 * medium. Skip this notification because we're not
163 * really starting a new stream here, and try getting a
164 * new notification (which, if it works, is a
165 * BT_NOTIFICATION_TYPE_PACKET_BEGIN one). We're sure to
166 * get at least one pair of
167 * BT_NOTIFICATION_TYPE_PACKET_BEGIN and
168 * BT_NOTIFICATION_TYPE_PACKET_END notifications in the
169 * case of a single, empty packet. We know there's at
170 * least one packet because the stream file group does
171 * not contain empty stream files.
94cf822e 172 */
f42867e2
PP
173 BT_ASSERT(notif_iter_data->skip_stream_begin_notifs);
174
175 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
176 BT_ASSERT(bt_notification_get_type(next_ret.notification) ==
177 BT_NOTIFICATION_TYPE_STREAM_BEGIN);
178 BT_PUT(next_ret.notification);
179 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
180 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END);
181 }
94cf822e 182 }
d01e0f33 183
94cf822e
PP
184end:
185 return next_ret;
ea0b4b9e 186}
bfd20a42 187
90157d89 188void ctf_fs_iterator_finalize(struct bt_private_connection_private_notification_iterator *it)
760051fa 189{
94cf822e 190 void *notif_iter_data =
90157d89 191 bt_private_connection_private_notification_iterator_get_user_data(it);
760051fa 192
94cf822e 193 ctf_fs_notif_iter_data_destroy(notif_iter_data);
760051fa
JG
194}
195
4f1f88a6 196enum bt_notification_iterator_status ctf_fs_iterator_init(
90157d89 197 struct bt_private_connection_private_notification_iterator *it,
4f1f88a6 198 struct bt_private_port *port)
4c1456f0 199{
4f1f88a6 200 struct ctf_fs_port_data *port_data;
94cf822e 201 struct ctf_fs_notif_iter_data *notif_iter_data = NULL;
4f1f88a6
PP
202 enum bt_notification_iterator_status ret =
203 BT_NOTIFICATION_ITERATOR_STATUS_OK;
94cf822e 204 int iret;
760051fa 205
4f1f88a6
PP
206 port_data = bt_private_port_get_user_data(port);
207 if (!port_data) {
fe8ad2b6 208 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
4f1f88a6
PP
209 goto error;
210 }
5b29e799 211
94cf822e
PP
212 notif_iter_data = g_new0(struct ctf_fs_notif_iter_data, 1);
213 if (!notif_iter_data) {
214 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
215 goto error;
216 }
217
50842bdc 218 notif_iter_data->notif_iter = bt_notif_iter_create(
6de92955
PP
219 port_data->ds_file_group->ctf_fs_trace->metadata->trace,
220 bt_common_get_page_size() * 8,
221 ctf_fs_ds_file_medops, NULL);
222 if (!notif_iter_data->notif_iter) {
223 BT_LOGE_STR("Cannot create a CTF notification iterator.");
224 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
225 goto error;
226 }
227
94cf822e
PP
228 notif_iter_data->ds_file_group = port_data->ds_file_group;
229 iret = notif_iter_data_set_current_ds_file(notif_iter_data);
230 if (iret) {
231 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
4f1f88a6 232 goto error;
760051fa
JG
233 }
234
90157d89 235 ret = bt_private_connection_private_notification_iterator_set_user_data(it, notif_iter_data);
7401e2d1 236 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
4f1f88a6 237 goto error;
760051fa
JG
238 }
239
94cf822e 240 notif_iter_data = NULL;
4f1f88a6 241 goto end;
5b29e799 242
4f1f88a6 243error:
90157d89 244 (void) bt_private_connection_private_notification_iterator_set_user_data(it, NULL);
4f1f88a6 245
760051fa 246end:
94cf822e 247 ctf_fs_notif_iter_data_destroy(notif_iter_data);
760051fa
JG
248 return ret;
249}
250
760051fa 251static
1a9f7075 252void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
760051fa 253{
4f1f88a6 254 if (!ctf_fs) {
8fa760ba
JG
255 return;
256 }
4f1f88a6 257
1a9f7075
PP
258 if (ctf_fs->traces) {
259 g_ptr_array_free(ctf_fs->traces, TRUE);
56a1cced 260 }
4f1f88a6
PP
261
262 if (ctf_fs->port_data) {
263 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 264 }
760051fa 265
1a9f7075
PP
266 g_free(ctf_fs);
267}
268
97ade20b
JG
269BT_HIDDEN
270void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
1a9f7075 271{
1a9f7075
PP
272 if (!ctf_fs_trace) {
273 return;
274 }
275
94cf822e
PP
276 if (ctf_fs_trace->ds_file_groups) {
277 g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE);
278 }
279
1a9f7075
PP
280 if (ctf_fs_trace->path) {
281 g_string_free(ctf_fs_trace->path, TRUE);
4f1f88a6 282 }
760051fa 283
1a9f7075
PP
284 if (ctf_fs_trace->name) {
285 g_string_free(ctf_fs_trace->name, TRUE);
286 }
287
288 if (ctf_fs_trace->metadata) {
289 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
290 g_free(ctf_fs_trace->metadata);
291 }
292
293 bt_put(ctf_fs_trace->cc_prio_map);
294 g_free(ctf_fs_trace);
4c1456f0
JG
295}
296
97ade20b
JG
297static
298void ctf_fs_trace_destroy_notifier(void *data)
299{
300 struct ctf_fs_trace *trace = data;
301 ctf_fs_trace_destroy(trace);
302}
303
4f1f88a6 304void ctf_fs_finalize(struct bt_private_component *component)
a4792757 305{
4f1f88a6
PP
306 void *data = bt_private_component_get_user_data(component);
307
1a9f7075 308 ctf_fs_destroy(data);
a4792757
JG
309}
310
e7a4393b 311static
4f1f88a6
PP
312void port_data_destroy(void *data) {
313 struct ctf_fs_port_data *port_data = data;
314
315 if (!port_data) {
316 return;
317 }
318
4f1f88a6 319 g_free(port_data);
5b29e799
JG
320}
321
547eacf1
PP
322static
323GString *get_stream_instance_unique_name(
324 struct ctf_fs_ds_file_group *ds_file_group)
325{
326 GString *name;
327 struct ctf_fs_ds_file_info *ds_file_info;
328
329 name = g_string_new(NULL);
330 if (!name) {
331 goto end;
332 }
333
334 /*
335 * If there's more than one stream file in the stream file
336 * group, the first (earliest) stream file's path is used as
337 * the stream's unique name.
338 */
f6ccaed9 339 BT_ASSERT(ds_file_group->ds_file_infos->len > 0);
547eacf1
PP
340 ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0);
341 g_string_assign(name, ds_file_info->path->str);
342
343end:
344 return name;
345}
346
5b29e799 347static
55314f2a
JG
348int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
349 struct ctf_fs_trace *ctf_fs_trace,
94cf822e 350 struct ctf_fs_ds_file_group *ds_file_group)
5b29e799 351{
4f1f88a6 352 int ret = 0;
4f1f88a6
PP
353 struct ctf_fs_port_data *port_data = NULL;
354 GString *port_name = NULL;
355
547eacf1 356 port_name = get_stream_instance_unique_name(ds_file_group);
4f1f88a6
PP
357 if (!port_name) {
358 goto error;
359 }
360
55314f2a 361 BT_LOGD("Creating one port named `%s`", port_name->str);
4f1f88a6
PP
362
363 /* Create output port for this file */
4f1f88a6
PP
364 port_data = g_new0(struct ctf_fs_port_data, 1);
365 if (!port_data) {
366 goto error;
367 }
368
94cf822e 369 port_data->ds_file_group = ds_file_group;
147337a3
PP
370 ret = bt_private_component_source_add_output_private_port(
371 ctf_fs->priv_comp, port_name->str, port_data, NULL);
372 if (ret) {
4f1f88a6
PP
373 goto error;
374 }
375
376 g_ptr_array_add(ctf_fs->port_data, port_data);
377 port_data = NULL;
378 goto end;
379
380error:
381 ret = -1;
382
383end:
384 if (port_name) {
385 g_string_free(port_name, TRUE);
386 }
387
4f1f88a6
PP
388 port_data_destroy(port_data);
389 return ret;
5b29e799
JG
390}
391
392static
55314f2a
JG
393int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
394 struct ctf_fs_trace *ctf_fs_trace)
94cf822e
PP
395{
396 int ret = 0;
94cf822e
PP
397 size_t i;
398
399 /* Create one output port for each stream file group */
400 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
401 struct ctf_fs_ds_file_group *ds_file_group =
402 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
403
55314f2a
JG
404 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
405 ds_file_group);
94cf822e 406 if (ret) {
55314f2a 407 BT_LOGE("Cannot create output port.");
94cf822e
PP
408 goto end;
409 }
410 }
411
412end:
413 return ret;
414}
415
05afcecd 416static
94cf822e 417uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace *ctf_fs_trace,
50842bdc 418 struct bt_field *packet_header_field)
94cf822e 419{
50842bdc 420 struct bt_field *stream_instance_id_field = NULL;
94cf822e
PP
421 uint64_t stream_instance_id = -1ULL;
422 int ret;
423
424 if (!packet_header_field) {
425 goto end;
426 }
427
50842bdc 428 stream_instance_id_field = bt_field_structure_get_field_by_name(
94cf822e
PP
429 packet_header_field, "stream_instance_id");
430 if (!stream_instance_id_field) {
431 goto end;
432 }
433
3dca2276 434 ret = bt_field_integer_unsigned_get_value(stream_instance_id_field,
94cf822e
PP
435 &stream_instance_id);
436 if (ret) {
437 stream_instance_id = -1ULL;
438 goto end;
439 }
440
441end:
442 bt_put(stream_instance_id_field);
443 return stream_instance_id;
444}
445
94cf822e
PP
446uint64_t get_packet_context_timestamp_begin_ns(
447 struct ctf_fs_trace *ctf_fs_trace,
50842bdc 448 struct bt_field *packet_context_field)
94cf822e
PP
449{
450 int ret;
50842bdc
PP
451 struct bt_field *timestamp_begin_field = NULL;
452 struct bt_field_type *timestamp_begin_ft = NULL;
94cf822e
PP
453 uint64_t timestamp_begin_raw_value = -1ULL;
454 uint64_t timestamp_begin_ns = -1ULL;
455 int64_t timestamp_begin_ns_signed;
50842bdc
PP
456 struct bt_clock_class *timestamp_begin_clock_class = NULL;
457 struct bt_clock_value *clock_value = NULL;
94cf822e
PP
458
459 if (!packet_context_field) {
460 goto end;
461 }
462
50842bdc 463 timestamp_begin_field = bt_field_structure_get_field_by_name(
94cf822e
PP
464 packet_context_field, "timestamp_begin");
465 if (!timestamp_begin_field) {
466 goto end;
467 }
468
50842bdc 469 timestamp_begin_ft = bt_field_get_type(timestamp_begin_field);
f6ccaed9 470 BT_ASSERT(timestamp_begin_ft);
94cf822e 471 timestamp_begin_clock_class =
50842bdc 472 bt_field_type_integer_get_mapped_clock_class(timestamp_begin_ft);
94cf822e
PP
473 if (!timestamp_begin_clock_class) {
474 goto end;
475 }
476
3dca2276 477 ret = bt_field_integer_unsigned_get_value(timestamp_begin_field,
94cf822e
PP
478 &timestamp_begin_raw_value);
479 if (ret) {
480 goto end;
481 }
482
50842bdc 483 clock_value = bt_clock_value_create(timestamp_begin_clock_class,
94cf822e
PP
484 timestamp_begin_raw_value);
485 if (!clock_value) {
486 goto end;
487 }
488
50842bdc 489 ret = bt_clock_value_get_value_ns_from_epoch(clock_value,
94cf822e
PP
490 &timestamp_begin_ns_signed);
491 if (ret) {
492 goto end;
493 }
494
495 timestamp_begin_ns = (uint64_t) timestamp_begin_ns_signed;
496
497end:
498 bt_put(timestamp_begin_field);
499 bt_put(timestamp_begin_ft);
500 bt_put(timestamp_begin_clock_class);
501 bt_put(clock_value);
502 return timestamp_begin_ns;
503}
504
505static
506void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
507{
508 if (!ds_file_info) {
509 return;
510 }
511
512 if (ds_file_info->path) {
513 g_string_free(ds_file_info->path, TRUE);
514 }
515
97ade20b 516 ctf_fs_ds_index_destroy(ds_file_info->index);
94cf822e
PP
517 g_free(ds_file_info);
518}
519
520static
521struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
97ade20b 522 uint64_t begin_ns, struct ctf_fs_ds_index *index)
94cf822e
PP
523{
524 struct ctf_fs_ds_file_info *ds_file_info;
525
526 ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
527 if (!ds_file_info) {
528 goto end;
529 }
530
531 ds_file_info->path = g_string_new(path);
532 if (!ds_file_info->path) {
533 ctf_fs_ds_file_info_destroy(ds_file_info);
534 ds_file_info = NULL;
535 goto end;
536 }
537
538 ds_file_info->begin_ns = begin_ns;
97ade20b
JG
539 ds_file_info->index = index;
540 index = NULL;
94cf822e
PP
541
542end:
97ade20b 543 ctf_fs_ds_index_destroy(index);
94cf822e
PP
544 return ds_file_info;
545}
546
547static
548void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
549{
550 if (!ds_file_group) {
551 return;
552 }
553
554 if (ds_file_group->ds_file_infos) {
555 g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
556 }
557
558 bt_put(ds_file_group->stream);
547eacf1 559 bt_put(ds_file_group->stream_class);
94cf822e
PP
560 g_free(ds_file_group);
561}
562
563static
564struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
565 struct ctf_fs_trace *ctf_fs_trace,
50842bdc 566 struct bt_stream_class *stream_class,
94cf822e
PP
567 uint64_t stream_instance_id)
568{
569 struct ctf_fs_ds_file_group *ds_file_group;
570
94cf822e
PP
571 ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
572 if (!ds_file_group) {
573 goto error;
574 }
575
576 ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
577 (GDestroyNotify) ctf_fs_ds_file_info_destroy);
578 if (!ds_file_group->ds_file_infos) {
579 goto error;
580 }
581
547eacf1 582 ds_file_group->stream_id = stream_instance_id;
f6ccaed9 583 BT_ASSERT(stream_class);
547eacf1 584 ds_file_group->stream_class = bt_get(stream_class);
94cf822e 585 ds_file_group->ctf_fs_trace = ctf_fs_trace;
94cf822e
PP
586 goto end;
587
588error:
589 ctf_fs_ds_file_group_destroy(ds_file_group);
590 ds_file_group = NULL;
591
592end:
593 return ds_file_group;
594}
595
8bf7105e
MJ
596/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
597static
598void array_insert(GPtrArray *array, gpointer element, size_t pos)
599{
600 size_t original_array_len = array->len;
601
602 /* Allocate an unused element at the end of the array. */
603 g_ptr_array_add(array, NULL);
604
605 /* If we are not inserting at the end, move the elements by one. */
606 if (pos < original_array_len) {
607 memmove(&(array->pdata[pos + 1]),
608 &(array->pdata[pos]),
609 (original_array_len - pos) * sizeof(gpointer));
610 }
611
612 /* Insert the value and bump the array len */
613 array->pdata[pos] = element;
614}
615
94cf822e
PP
616static
617int ctf_fs_ds_file_group_add_ds_file_info(
618 struct ctf_fs_ds_file_group *ds_file_group,
97ade20b
JG
619 const char *path, uint64_t begin_ns,
620 struct ctf_fs_ds_index *index)
94cf822e
PP
621{
622 struct ctf_fs_ds_file_info *ds_file_info;
623 gint i = 0;
624 int ret = 0;
625
97ade20b
JG
626 /* Onwership of index is transferred. */
627 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index);
628 index = NULL;
94cf822e
PP
629 if (!ds_file_info) {
630 goto error;
631 }
632
633 /* Find a spot to insert this one */
634 for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
635 struct ctf_fs_ds_file_info *other_ds_file_info =
636 g_ptr_array_index(ds_file_group->ds_file_infos, i);
637
638 if (begin_ns < other_ds_file_info->begin_ns) {
639 break;
640 }
641 }
642
8bf7105e 643 array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
94cf822e
PP
644 ds_file_info = NULL;
645 goto end;
646
647error:
648 ctf_fs_ds_file_info_destroy(ds_file_info);
97ade20b 649 ctf_fs_ds_index_destroy(index);
94cf822e 650 ret = -1;
94cf822e
PP
651end:
652 return ret;
653}
654
655static
656int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
657 const char *path)
658{
50842bdc
PP
659 struct bt_field *packet_header_field = NULL;
660 struct bt_field *packet_context_field = NULL;
661 struct bt_stream_class *stream_class = NULL;
94cf822e
PP
662 uint64_t stream_instance_id = -1ULL;
663 uint64_t begin_ns = -1ULL;
664 struct ctf_fs_ds_file_group *ds_file_group = NULL;
665 bool add_group = false;
666 int ret;
667 size_t i;
6de92955 668 struct ctf_fs_ds_file *ds_file = NULL;
97ade20b 669 struct ctf_fs_ds_index *index = NULL;
50842bdc 670 struct bt_notif_iter *notif_iter = NULL;
94cf822e 671
50842bdc 672 notif_iter = bt_notif_iter_create(ctf_fs_trace->metadata->trace,
6de92955
PP
673 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL);
674 if (!notif_iter) {
675 BT_LOGE_STR("Cannot create a CTF notification iterator.");
676 goto error;
677 }
678
679 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, notif_iter, NULL, path);
97ade20b
JG
680 if (!ds_file) {
681 goto error;
682 }
683
684 ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
685 &packet_header_field, &packet_context_field);
94cf822e 686 if (ret) {
55314f2a 687 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
94cf822e
PP
688 path);
689 goto error;
690 }
691
692 stream_instance_id = get_packet_header_stream_instance_id(ctf_fs_trace,
693 packet_header_field);
694 begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace,
695 packet_context_field);
a429c321
JG
696 stream_class = ctf_utils_stream_class_from_packet_header(
697 ctf_fs_trace->metadata->trace, packet_header_field);
94cf822e
PP
698 if (!stream_class) {
699 goto error;
700 }
701
97ade20b
JG
702 index = ctf_fs_ds_file_build_index(ds_file);
703 if (!index) {
704 BT_LOGW("Failed to index CTF stream file \'%s\'",
705 ds_file->file->path->str);
706 }
707
94cf822e
PP
708 if (begin_ns == -1ULL) {
709 /*
710 * No beggining timestamp to sort the stream files
711 * within a stream file group, so consider that this
712 * file must be the only one within its group.
713 */
714 stream_instance_id = -1ULL;
715 }
716
717 if (stream_instance_id == -1ULL) {
718 /*
719 * No stream instance ID or no beginning timestamp:
720 * create a unique stream file group for this stream
721 * file because, even if there's a stream instance ID,
722 * there's no timestamp to order the file within its
723 * group.
724 */
94cf822e
PP
725 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
726 stream_class, stream_instance_id);
727 if (!ds_file_group) {
728 goto error;
729 }
730
731 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
547eacf1 732 path, begin_ns, index);
97ade20b
JG
733 /* Ownership of index is transferred. */
734 index = NULL;
94cf822e
PP
735 if (ret) {
736 goto error;
737 }
738
739 add_group = true;
740 goto end;
741 }
742
f6ccaed9
PP
743 BT_ASSERT(stream_instance_id != -1ULL);
744 BT_ASSERT(begin_ns != -1ULL);
94cf822e
PP
745
746 /* Find an existing stream file group with this ID */
747 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
94cf822e
PP
748 ds_file_group = g_ptr_array_index(
749 ctf_fs_trace->ds_file_groups, i);
94cf822e 750
547eacf1
PP
751 if (ds_file_group->stream_class == stream_class &&
752 ds_file_group->stream_id ==
753 stream_instance_id) {
94cf822e
PP
754 break;
755 }
756
757 ds_file_group = NULL;
758 }
759
760 if (!ds_file_group) {
761 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
762 stream_class, stream_instance_id);
763 if (!ds_file_group) {
764 goto error;
765 }
766
767 add_group = true;
768 }
769
547eacf1
PP
770 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path,
771 begin_ns, index);
97ade20b 772 index = NULL;
94cf822e
PP
773 if (ret) {
774 goto error;
775 }
776
777 goto end;
778
779error:
780 ctf_fs_ds_file_group_destroy(ds_file_group);
781 ret = -1;
782
783end:
784 if (add_group && ds_file_group) {
785 g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
786 }
547eacf1 787
97ade20b 788 ctf_fs_ds_file_destroy(ds_file);
6de92955
PP
789
790 if (notif_iter) {
50842bdc 791 bt_notif_iter_destroy(notif_iter);
6de92955
PP
792 }
793
97ade20b 794 ctf_fs_ds_index_destroy(index);
94cf822e
PP
795 bt_put(packet_header_field);
796 bt_put(packet_context_field);
797 bt_put(stream_class);
798 return ret;
799}
800
801static
802int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
e7a4393b
JG
803{
804 int ret = 0;
4f1f88a6 805 const char *basename;
e7a4393b 806 GError *error = NULL;
4f1f88a6 807 GDir *dir = NULL;
547eacf1 808 size_t i;
e7a4393b 809
94cf822e 810 /* Check each file in the path directory, except specific ones */
1a9f7075 811 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
e7a4393b 812 if (!dir) {
55314f2a 813 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075 814 ctf_fs_trace->path->str, error->message,
4f1f88a6 815 error->code);
e7a4393b
JG
816 goto error;
817 }
818
4f1f88a6 819 while ((basename = g_dir_read_name(dir))) {
94cf822e
PP
820 struct ctf_fs_file *file;
821
4f1f88a6 822 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
e7a4393b 823 /* Ignore the metadata stream. */
3743a302 824 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 825 ctf_fs_trace->path->str, basename);
e7a4393b
JG
826 continue;
827 }
828
4f1f88a6 829 if (basename[0] == '.') {
3743a302 830 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 831 ctf_fs_trace->path->str, basename);
e7a4393b
JG
832 continue;
833 }
834
835 /* Create the file. */
55314f2a 836 file = ctf_fs_file_create();
e7a4393b 837 if (!file) {
3743a302 838 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 839 ctf_fs_trace->path->str, basename);
e7a4393b
JG
840 goto error;
841 }
842
843 /* Create full path string. */
3743a302 844 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
1a9f7075 845 ctf_fs_trace->path->str, basename);
e7a4393b 846 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
55314f2a 847 BT_LOGD("Ignoring non-regular file `%s`",
1a9f7075 848 file->path->str);
e7a4393b 849 ctf_fs_file_destroy(file);
4f1f88a6 850 file = NULL;
e7a4393b
JG
851 continue;
852 }
853
55314f2a 854 ret = ctf_fs_file_open(file, "rb");
4f1f88a6 855 if (ret) {
55314f2a 856 BT_LOGE("Cannot open stream file `%s`", file->path->str);
e7a4393b
JG
857 goto error;
858 }
859
9fa0891a
JG
860 if (file->size == 0) {
861 /* Skip empty stream. */
55314f2a 862 BT_LOGD("Ignoring empty file `%s`", file->path->str);
9fa0891a
JG
863 ctf_fs_file_destroy(file);
864 continue;
865 }
866
94cf822e
PP
867 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
868 file->path->str);
4f1f88a6 869 if (ret) {
89b08333 870 BT_LOGE("Cannot add stream file `%s` to stream file group",
1a9f7075 871 file->path->str);
94cf822e 872 ctf_fs_file_destroy(file);
e7a4393b
JG
873 goto error;
874 }
875
4f1f88a6 876 ctf_fs_file_destroy(file);
e7a4393b
JG
877 }
878
547eacf1
PP
879 /*
880 * At this point, DS file groupes are created, but their
881 * associated stream objects do not exist yet. This is because
882 * we need to name the created stream object with the data
883 * stream file's path. We have everything we need here to do
884 * this.
885 */
886 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
887 struct ctf_fs_ds_file_group *ds_file_group =
888 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
889 GString *name = get_stream_instance_unique_name(ds_file_group);
890
891 if (!name) {
892 goto error;
893 }
894
895 if (ds_file_group->stream_id == -1ULL) {
3dca2276 896 /* No stream ID: use 0 */
50842bdc 897 ds_file_group->stream = bt_stream_create(
3dca2276
PP
898 ds_file_group->stream_class, name->str,
899 ctf_fs_trace->next_stream_id);
900 ctf_fs_trace->next_stream_id++;
547eacf1
PP
901 } else {
902 /* Specific stream ID */
3dca2276 903 ds_file_group->stream = bt_stream_create(
547eacf1
PP
904 ds_file_group->stream_class, name->str,
905 ds_file_group->stream_id);
906 }
907
908 g_string_free(name, TRUE);
909
7742909c 910 if (!ds_file_group->stream) {
547eacf1
PP
911 BT_LOGE("Cannot create stream for DS file group: "
912 "addr=%p, stream-name=\"%s\"",
913 ds_file_group, name->str);
914 goto error;
915 }
916 }
917
e7a4393b 918 goto end;
4f1f88a6 919
e7a4393b
JG
920error:
921 ret = -1;
4f1f88a6 922
e7a4393b
JG
923end:
924 if (dir) {
925 g_dir_close(dir);
926 dir = NULL;
927 }
4f1f88a6 928
e7a4393b
JG
929 if (error) {
930 g_error_free(error);
931 }
5b29e799 932
91457551 933 return ret;
5b29e799
JG
934}
935
599faa1c 936static
1a9f7075 937int create_cc_prio_map(struct ctf_fs_trace *ctf_fs_trace)
599faa1c
PP
938{
939 int ret = 0;
940 size_t i;
941 int count;
942
f6ccaed9 943 BT_ASSERT(ctf_fs_trace);
1a9f7075
PP
944 ctf_fs_trace->cc_prio_map = bt_clock_class_priority_map_create();
945 if (!ctf_fs_trace->cc_prio_map) {
599faa1c
PP
946 ret = -1;
947 goto end;
948 }
949
50842bdc 950 count = bt_trace_get_clock_class_count(
1a9f7075 951 ctf_fs_trace->metadata->trace);
f6ccaed9 952 BT_ASSERT(count >= 0);
599faa1c
PP
953
954 for (i = 0; i < count; i++) {
50842bdc
PP
955 struct bt_clock_class *clock_class =
956 bt_trace_get_clock_class_by_index(
1a9f7075 957 ctf_fs_trace->metadata->trace, i);
599faa1c 958
f6ccaed9 959 BT_ASSERT(clock_class);
599faa1c 960 ret = bt_clock_class_priority_map_add_clock_class(
1a9f7075 961 ctf_fs_trace->cc_prio_map, clock_class, 0);
599faa1c
PP
962 BT_PUT(clock_class);
963
964 if (ret) {
965 goto end;
966 }
967 }
968
969end:
970 return ret;
971}
972
97ade20b 973BT_HIDDEN
55314f2a 974struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name,
a2a54545 975 struct ctf_fs_metadata_config *metadata_config)
1a9f7075
PP
976{
977 struct ctf_fs_trace *ctf_fs_trace;
978 int ret;
979
980 ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
981 if (!ctf_fs_trace) {
982 goto end;
983 }
984
1a9f7075
PP
985 ctf_fs_trace->path = g_string_new(path);
986 if (!ctf_fs_trace->path) {
987 goto error;
988 }
989
990 ctf_fs_trace->name = g_string_new(name);
991 if (!ctf_fs_trace->name) {
992 goto error;
993 }
994
995 ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
996 if (!ctf_fs_trace->metadata) {
997 goto error;
998 }
999
94cf822e
PP
1000 ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func(
1001 (GDestroyNotify) ctf_fs_ds_file_group_destroy);
1002 if (!ctf_fs_trace->ds_file_groups) {
1003 goto error;
1004 }
1005
a2a54545 1006 ret = ctf_fs_metadata_set_trace(ctf_fs_trace, metadata_config);
1a9f7075
PP
1007 if (ret) {
1008 goto error;
1009 }
1010
94cf822e
PP
1011 ret = create_ds_file_groups(ctf_fs_trace);
1012 if (ret) {
1013 goto error;
1014 }
1015
1a9f7075
PP
1016 ret = create_cc_prio_map(ctf_fs_trace);
1017 if (ret) {
1018 goto error;
1019 }
1020
27d1a78b
PP
1021 /*
1022 * create_ds_file_groups() created all the streams that this
1023 * trace needs. There won't be any more. Therefore it is safe to
1024 * make this trace static.
1025 */
50842bdc 1026 (void) bt_trace_set_is_static(ctf_fs_trace->metadata->trace);
27d1a78b 1027
1a9f7075
PP
1028 goto end;
1029
1030error:
1031 ctf_fs_trace_destroy(ctf_fs_trace);
1032 ctf_fs_trace = NULL;
1033end:
1034 return ctf_fs_trace;
1035}
1036
1037static
1038int path_is_ctf_trace(const char *path)
1039{
1040 GString *metadata_path = g_string_new(NULL);
1041 int ret = 0;
1042
1043 if (!metadata_path) {
1044 ret = -1;
1045 goto end;
1046 }
1047
3743a302 1048 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
1a9f7075
PP
1049
1050 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
1051 ret = 1;
1052 goto end;
1053 }
1054
1055end:
1056 g_string_free(metadata_path, TRUE);
1057 return ret;
1058}
1059
1060static
55314f2a 1061int add_trace_path(GList **trace_paths, const char *path)
1a9f7075 1062{
4bd72b60 1063 GString *norm_path = NULL;
1a9f7075 1064 int ret = 0;
1a9f7075 1065
4bd72b60
PP
1066 norm_path = bt_common_normalize_path(path, NULL);
1067 if (!norm_path) {
55314f2a 1068 BT_LOGE("Failed to normalize path `%s`.", path);
1a9f7075
PP
1069 ret = -1;
1070 goto end;
1071 }
1072
3743a302 1073 // FIXME: Remove or ifdef for __MINGW32__
4bd72b60 1074 if (strcmp(norm_path->str, "/") == 0) {
55314f2a 1075 BT_LOGE("Opening a trace in `/` is not supported.");
1a9f7075
PP
1076 ret = -1;
1077 goto end;
1078 }
1079
4bd72b60 1080 *trace_paths = g_list_prepend(*trace_paths, norm_path);
f6ccaed9 1081 BT_ASSERT(*trace_paths);
4bd72b60 1082 norm_path = NULL;
1a9f7075
PP
1083
1084end:
4bd72b60
PP
1085 if (norm_path) {
1086 g_string_free(norm_path, TRUE);
1087 }
1088
1a9f7075
PP
1089 return ret;
1090}
1091
55314f2a
JG
1092BT_HIDDEN
1093int ctf_fs_find_traces(GList **trace_paths, const char *start_path)
1a9f7075
PP
1094{
1095 int ret;
1096 GError *error = NULL;
1097 GDir *dir = NULL;
1098 const char *basename = NULL;
1099
1100 /* Check if the starting path is a CTF trace itself */
1101 ret = path_is_ctf_trace(start_path);
1102 if (ret < 0) {
1103 goto end;
1104 }
1105
1106 if (ret) {
1107 /*
4bd72b60
PP
1108 * Stop recursion: a CTF trace cannot contain another
1109 * CTF trace.
1a9f7075 1110 */
55314f2a 1111 ret = add_trace_path(trace_paths, start_path);
1a9f7075
PP
1112 goto end;
1113 }
1114
1115 /* Look for subdirectories */
1116 if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) {
1117 /* Starting path is not a directory: end of recursion */
1118 goto end;
1119 }
1120
1121 dir = g_dir_open(start_path, 0, &error);
1122 if (!dir) {
1123 if (error->code == G_FILE_ERROR_ACCES) {
55314f2a 1124 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1a9f7075
PP
1125 start_path, error->message, error->code);
1126 goto end;
1127 }
1128
55314f2a 1129 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075
PP
1130 start_path, error->message, error->code);
1131 ret = -1;
1132 goto end;
1133 }
1134
1135 while ((basename = g_dir_read_name(dir))) {
1136 GString *sub_path = g_string_new(NULL);
1137
1138 if (!sub_path) {
1139 ret = -1;
1140 goto end;
1141 }
1142
3743a302 1143 g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename);
55314f2a 1144 ret = ctf_fs_find_traces(trace_paths, sub_path->str);
1a9f7075
PP
1145 g_string_free(sub_path, TRUE);
1146 if (ret) {
1147 goto end;
1148 }
1149 }
1150
1151end:
1152 if (dir) {
1153 g_dir_close(dir);
1154 }
1155
1156 if (error) {
1157 g_error_free(error);
1158 }
1159
1160 return ret;
1161}
1162
55314f2a
JG
1163BT_HIDDEN
1164GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) {
1a9f7075 1165 GList *trace_names = NULL;
1a9f7075 1166 GList *node;
4bd72b60
PP
1167 const char *last_sep;
1168 size_t base_dist;
1a9f7075
PP
1169
1170 /*
4bd72b60
PP
1171 * At this point we know that all the trace paths are
1172 * normalized, and so is the base path. This means that
1173 * they are absolute and they don't end with a separator.
1174 * We can simply find the location of the last separator
1175 * in the base path, which gives us the name of the actual
1176 * directory to look into, and use this location as the
1177 * start of each trace name within each trace path.
1178 *
1179 * For example:
1180 *
1181 * Base path: /home/user/my-traces/some-trace
1182 * Trace paths:
1183 * - /home/user/my-traces/some-trace/host1/trace1
1184 * - /home/user/my-traces/some-trace/host1/trace2
1185 * - /home/user/my-traces/some-trace/host2/trace
1186 * - /home/user/my-traces/some-trace/other-trace
1187 *
1188 * In this case the trace names are:
1189 *
1190 * - some-trace/host1/trace1
1191 * - some-trace/host1/trace2
1192 * - some-trace/host2/trace
1193 * - some-trace/other-trace
1a9f7075 1194 */
4bd72b60 1195 last_sep = strrchr(base_path, G_DIR_SEPARATOR);
1a9f7075 1196
4bd72b60 1197 /* We know there's at least one separator */
f6ccaed9 1198 BT_ASSERT(last_sep);
1a9f7075 1199
4bd72b60
PP
1200 /* Distance to base */
1201 base_dist = last_sep - base_path + 1;
1a9f7075
PP
1202
1203 /* Create the trace names */
1204 for (node = trace_paths; node; node = g_list_next(node)) {
1205 GString *trace_name = g_string_new(NULL);
1206 GString *trace_path = node->data;
1207
f6ccaed9 1208 BT_ASSERT(trace_name);
4bd72b60 1209 g_string_assign(trace_name, &trace_path->str[base_dist]);
1a9f7075
PP
1210 trace_names = g_list_append(trace_names, trace_name);
1211 }
1212
1213 return trace_names;
1214}
1215
1216static
1217int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs,
1218 const char *path_param)
1219{
1220 struct ctf_fs_trace *ctf_fs_trace = NULL;
1221 int ret = 0;
4bd72b60 1222 GString *norm_path = NULL;
1a9f7075
PP
1223 GList *trace_paths = NULL;
1224 GList *trace_names = NULL;
1225 GList *tp_node;
1226 GList *tn_node;
1227
4bd72b60
PP
1228 norm_path = bt_common_normalize_path(path_param, NULL);
1229 if (!norm_path) {
55314f2a 1230 BT_LOGE("Failed to normalize path: `%s`.",
4bd72b60
PP
1231 path_param);
1232 goto error;
1233 }
1234
55314f2a 1235 ret = ctf_fs_find_traces(&trace_paths, norm_path->str);
1a9f7075
PP
1236 if (ret) {
1237 goto error;
1238 }
1239
1240 if (!trace_paths) {
55314f2a 1241 BT_LOGE("No CTF traces recursively found in `%s`.",
1a9f7075
PP
1242 path_param);
1243 goto error;
1244 }
1245
55314f2a 1246 trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str);
1a9f7075 1247 if (!trace_names) {
55314f2a 1248 BT_LOGE("Cannot create trace names from trace paths.");
1a9f7075
PP
1249 goto error;
1250 }
1251
1252 for (tp_node = trace_paths, tn_node = trace_names; tp_node;
1253 tp_node = g_list_next(tp_node),
1254 tn_node = g_list_next(tn_node)) {
1255 GString *trace_path = tp_node->data;
1256 GString *trace_name = tn_node->data;
1257
55314f2a 1258 ctf_fs_trace = ctf_fs_trace_create(trace_path->str,
a2a54545 1259 trace_name->str, &ctf_fs->metadata_config);
1a9f7075 1260 if (!ctf_fs_trace) {
55314f2a 1261 BT_LOGE("Cannot create trace for `%s`.",
1a9f7075
PP
1262 trace_path->str);
1263 goto error;
1264 }
52c5fe74 1265
55314f2a
JG
1266 ret = create_ports_for_trace(ctf_fs, ctf_fs_trace);
1267 if (ret) {
1268 goto error;
1269 }
1270
52c5fe74
PP
1271 g_ptr_array_add(ctf_fs->traces, ctf_fs_trace);
1272 ctf_fs_trace = NULL;
1a9f7075
PP
1273 }
1274
1a9f7075
PP
1275 goto end;
1276
1277error:
1278 ret = -1;
1279 ctf_fs_trace_destroy(ctf_fs_trace);
1280
1281end:
1282 for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
1283 if (tp_node->data) {
1284 g_string_free(tp_node->data, TRUE);
1285 }
1286 }
1287
1288 for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
1289 if (tn_node->data) {
1290 g_string_free(tn_node->data, TRUE);
1291 }
1292 }
1293
1294 if (trace_paths) {
1295 g_list_free(trace_paths);
1296 }
1297
1298 if (trace_names) {
1299 g_list_free(trace_names);
1300 }
1301
4bd72b60
PP
1302 if (norm_path) {
1303 g_string_free(norm_path, TRUE);
1304 }
1305
1a9f7075
PP
1306 return ret;
1307}
1308
5b29e799 1309static
4f1f88a6
PP
1310struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
1311 struct bt_value *params)
56a1cced
JG
1312{
1313 struct ctf_fs_component *ctf_fs;
1ef09eb5 1314 struct bt_value *value = NULL;
1a9f7075 1315 const char *path_param;
3faba6d4
MD
1316 enum bt_component_status ret;
1317 enum bt_value_status value_ret;
56a1cced
JG
1318
1319 ctf_fs = g_new0(struct ctf_fs_component, 1);
1320 if (!ctf_fs) {
1321 goto end;
1322 }
1323
1a9f7075 1324 ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
f6ccaed9 1325 BT_ASSERT(ret == BT_COMPONENT_STATUS_OK);
1a9f7075 1326
4f1f88a6
PP
1327 /*
1328 * We don't need to get a new reference here because as long as
1329 * our private ctf_fs_component object exists, the containing
1330 * private component should also exist.
1331 */
1332 ctf_fs->priv_comp = priv_comp;
56a1cced 1333 value = bt_value_map_get(params, "path");
f6ccaed9 1334 if (value && !bt_value_is_string(value)) {
56a1cced
JG
1335 goto error;
1336 }
1337
3faba6d4 1338 value_ret = bt_value_string_get(value, &path_param);
f6ccaed9 1339 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
1a9f7075 1340 BT_PUT(value);
a2a54545 1341 value = bt_value_map_get(params, "clock-class-offset-s");
92540773 1342 if (value) {
a2a54545
PP
1343 if (!bt_value_is_integer(value)) {
1344 BT_LOGE("clock-class-offset-s should be an integer");
1345 goto error;
1346 }
3faba6d4 1347 value_ret = bt_value_integer_get(value,
a2a54545 1348 &ctf_fs->metadata_config.clock_class_offset_s);
f6ccaed9 1349 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
a2a54545
PP
1350 BT_PUT(value);
1351 }
92540773 1352
a2a54545
PP
1353 value = bt_value_map_get(params, "clock-class-offset-ns");
1354 if (value) {
92540773 1355 if (!bt_value_is_integer(value)) {
a2a54545 1356 BT_LOGE("clock-class-offset-ns should be an integer");
92540773
JD
1357 goto error;
1358 }
3faba6d4 1359 value_ret = bt_value_integer_get(value,
a2a54545 1360 &ctf_fs->metadata_config.clock_class_offset_ns);
f6ccaed9 1361 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
1a9f7075 1362 BT_PUT(value);
92540773
JD
1363 }
1364
1a9f7075
PP
1365 ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy);
1366 if (!ctf_fs->port_data) {
4f1f88a6
PP
1367 goto error;
1368 }
1369
97ade20b
JG
1370 ctf_fs->traces = g_ptr_array_new_with_free_func(
1371 ctf_fs_trace_destroy_notifier);
1a9f7075 1372 if (!ctf_fs->traces) {
599faa1c
PP
1373 goto error;
1374 }
1375
3faba6d4 1376 if (create_ctf_fs_traces(ctf_fs, path_param)) {
4f1f88a6
PP
1377 goto error;
1378 }
1379
1ef09eb5
JG
1380 goto end;
1381
56a1cced 1382error:
1a9f7075 1383 ctf_fs_destroy(ctf_fs);
e7a4393b 1384 ctf_fs = NULL;
1a9f7075 1385 ret = bt_private_component_set_user_data(priv_comp, NULL);
f6ccaed9 1386 BT_ASSERT(ret == BT_COMPONENT_STATUS_OK);
1a9f7075 1387
1ef09eb5 1388end:
1a9f7075 1389 bt_put(value);
56a1cced
JG
1390 return ctf_fs;
1391}
1392
ea0b4b9e 1393BT_HIDDEN
4f1f88a6 1394enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
7d61fa8e 1395 struct bt_value *params, UNUSED_VAR void *init_method_data)
ea0b4b9e
JG
1396{
1397 struct ctf_fs_component *ctf_fs;
1398 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1399
4f1f88a6 1400 ctf_fs = ctf_fs_create(priv_comp, params);
ea0b4b9e 1401 if (!ctf_fs) {
1a9f7075 1402 ret = BT_COMPONENT_STATUS_ERROR;
ea0b4b9e 1403 }
4c1456f0 1404
ea0b4b9e
JG
1405 return ret;
1406}
33f93973
PP
1407
1408BT_HIDDEN
90157d89 1409struct bt_component_class_query_method_return ctf_fs_query(
c7eee084
PP
1410 struct bt_component_class *comp_class,
1411 struct bt_query_executor *query_exec,
a67681c1 1412 const char *object, struct bt_value *params)
33f93973 1413{
90157d89 1414 struct bt_component_class_query_method_return ret = {
c7eee084
PP
1415 .result = NULL,
1416 .status = BT_QUERY_STATUS_OK,
1417 };
33f93973 1418
04c0ba87 1419 if (!strcmp(object, "metadata-info")) {
c7eee084 1420 ret = metadata_info_query(comp_class, params);
97ade20b 1421 } else if (!strcmp(object, "trace-info")) {
c7eee084 1422 ret = trace_info_query(comp_class, params);
33f93973 1423 } else {
55314f2a 1424 BT_LOGE("Unknown query object `%s`", object);
c7eee084 1425 ret.status = BT_QUERY_STATUS_INVALID_OBJECT;
04c0ba87 1426 goto end;
33f93973 1427 }
33f93973 1428end:
c7eee084 1429 return ret;
33f93973 1430}
This page took 0.111588 seconds and 4 git commands to generate.