cli: print current thread's error causes, if any, before exiting
[babeltrace.git] / src / 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
4c65a157 28#define BT_COMP_LOG_SELF_COMP self_comp
98903a3e
PP
29#define BT_LOG_OUTPUT_LEVEL log_level
30#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
4c65a157 31#include "plugins/comp-logging.h"
98903a3e 32
578e048b 33#include "common/common.h"
3fadfbc0 34#include <babeltrace2/babeltrace.h>
578e048b 35#include "compat/uuid.h"
ea0b4b9e 36#include <glib.h>
578e048b 37#include "common/assert.h"
94cf822e 38#include <inttypes.h>
c55a9f58 39#include <stdbool.h>
56a1cced 40#include "fs.h"
413bc2c4 41#include "metadata.h"
94cf822e 42#include "data-stream-file.h"
e7a4393b 43#include "file.h"
1e649dff 44#include "../common/metadata/decoder.h"
d6e69534 45#include "../common/msg-iter/msg-iter.h"
04c0ba87 46#include "query.h"
e7a4393b 47
94cf822e 48static
d6e69534 49int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e
PP
50{
51 struct ctf_fs_ds_file_info *ds_file_info;
52 int ret = 0;
53
d6e69534
PP
54 BT_ASSERT(msg_iter_data->ds_file_info_index <
55 msg_iter_data->ds_file_group->ds_file_infos->len);
94cf822e 56 ds_file_info = g_ptr_array_index(
d6e69534
PP
57 msg_iter_data->ds_file_group->ds_file_infos,
58 msg_iter_data->ds_file_info_index);
59
60 ctf_fs_ds_file_destroy(msg_iter_data->ds_file);
61 msg_iter_data->ds_file = ctf_fs_ds_file_create(
62 msg_iter_data->ds_file_group->ctf_fs_trace,
63 msg_iter_data->pc_msg_iter,
64 msg_iter_data->msg_iter,
65 msg_iter_data->ds_file_group->stream,
98903a3e
PP
66 ds_file_info->path->str,
67 msg_iter_data->log_level);
d6e69534 68 if (!msg_iter_data->ds_file) {
94cf822e
PP
69 ret = -1;
70 }
71
72 return ret;
73}
74
75static
d6e69534
PP
76void ctf_fs_msg_iter_data_destroy(
77 struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e 78{
d6e69534 79 if (!msg_iter_data) {
94cf822e
PP
80 return;
81 }
82
d6e69534 83 ctf_fs_ds_file_destroy(msg_iter_data->ds_file);
6de92955 84
d6e69534
PP
85 if (msg_iter_data->msg_iter) {
86 bt_msg_iter_destroy(msg_iter_data->msg_iter);
6de92955
PP
87 }
88
d6e69534 89 g_free(msg_iter_data);
94cf822e
PP
90}
91
fc917f65
PP
92static
93void set_msg_iter_emits_stream_beginning_end_messages(
94 struct ctf_fs_msg_iter_data *msg_iter_data)
95{
96 bt_msg_iter_set_emit_stream_beginning_message(
97 msg_iter_data->ds_file->msg_iter,
98 msg_iter_data->ds_file_info_index == 0);
99 bt_msg_iter_set_emit_stream_end_message(
100 msg_iter_data->ds_file->msg_iter,
101 msg_iter_data->ds_file_info_index ==
102 msg_iter_data->ds_file_group->ds_file_infos->len - 1);
103}
104
d4393e08 105static
d24d5663 106bt_component_class_message_iterator_next_method_status ctf_fs_iterator_next_one(
d6e69534 107 struct ctf_fs_msg_iter_data *msg_iter_data,
fc917f65 108 const bt_message **out_msg)
ea0b4b9e 109{
d24d5663 110 bt_component_class_message_iterator_next_method_status status;
94cf822e 111
d6e69534 112 BT_ASSERT(msg_iter_data->ds_file);
f42867e2 113
fc917f65
PP
114 while (true) {
115 bt_message *msg;
116
117 status = ctf_fs_ds_file_next(msg_iter_data->ds_file, &msg);
118 switch (status) {
d24d5663 119 case BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK:
fc917f65
PP
120 *out_msg = msg;
121 msg = NULL;
f42867e2 122 goto end;
d24d5663 123 case BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END:
fc917f65
PP
124 {
125 int ret;
f42867e2 126
fc917f65
PP
127 if (msg_iter_data->ds_file_info_index ==
128 msg_iter_data->ds_file_group->ds_file_infos->len - 1) {
129 /* End of all group's stream files */
130 goto end;
131 }
132
133 msg_iter_data->ds_file_info_index++;
495490c5
PP
134 bt_msg_iter_reset_for_next_stream_file(
135 msg_iter_data->msg_iter);
fc917f65
PP
136 set_msg_iter_emits_stream_beginning_end_messages(
137 msg_iter_data);
94cf822e 138
94cf822e 139 /*
fc917f65
PP
140 * Open and start reading the next stream file
141 * within our stream file group.
94cf822e 142 */
fc917f65
PP
143 ret = msg_iter_data_set_current_ds_file(msg_iter_data);
144 if (ret) {
d24d5663 145 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
fc917f65
PP
146 goto end;
147 }
148
149 /* Continue the loop to get the next message */
150 break;
94cf822e 151 }
fc917f65 152 default:
94cf822e
PP
153 goto end;
154 }
94cf822e 155 }
d01e0f33 156
94cf822e 157end:
d4393e08
PP
158 return status;
159}
160
161BT_HIDDEN
d24d5663 162bt_component_class_message_iterator_next_method_status ctf_fs_iterator_next(
d6e69534
PP
163 bt_self_message_iterator *iterator,
164 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
165 uint64_t *count)
166{
d24d5663
PP
167 bt_component_class_message_iterator_next_method_status status =
168 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d6e69534
PP
169 struct ctf_fs_msg_iter_data *msg_iter_data =
170 bt_self_message_iterator_get_data(iterator);
d4393e08
PP
171 uint64_t i = 0;
172
d24d5663
PP
173 while (i < capacity &&
174 status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d6e69534 175 status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
d24d5663 176 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d4393e08
PP
177 i++;
178 }
179 }
180
181 if (i > 0) {
182 /*
183 * Even if ctf_fs_iterator_next_one() returned something
d24d5663 184 * else than BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
d6e69534
PP
185 * accumulated message objects in the output
186 * message array, so we need to return
d24d5663 187 * BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
d4393e08 188 * transfered to downstream. This other status occurs
d6e69534 189 * again the next time muxer_msg_iter_do_next() is
d4393e08 190 * called, possibly without any accumulated
d6e69534 191 * message, in which case we'll return it.
d4393e08
PP
192 */
193 *count = i;
d24d5663 194 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d4393e08
PP
195 }
196
197 return status;
ea0b4b9e 198}
bfd20a42 199
6a9bb5e9
PP
200static
201int ctf_fs_iterator_reset(struct ctf_fs_msg_iter_data *msg_iter_data)
202{
203 int ret;
204
205 msg_iter_data->ds_file_info_index = 0;
206 ret = msg_iter_data_set_current_ds_file(msg_iter_data);
207 if (ret) {
208 goto end;
209 }
210
211 bt_msg_iter_reset(msg_iter_data->msg_iter);
212 set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data);
213
214end:
215 return ret;
216}
217
218BT_HIDDEN
d24d5663
PP
219bt_component_class_message_iterator_seek_beginning_method_status
220ctf_fs_iterator_seek_beginning(bt_self_message_iterator *it)
6a9bb5e9
PP
221{
222 struct ctf_fs_msg_iter_data *msg_iter_data =
223 bt_self_message_iterator_get_data(it);
d24d5663
PP
224 bt_component_class_message_iterator_seek_beginning_method_status status =
225 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
6a9bb5e9
PP
226
227 BT_ASSERT(msg_iter_data);
228 if (ctf_fs_iterator_reset(msg_iter_data)) {
d24d5663 229 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_ERROR;
6a9bb5e9
PP
230 }
231
232 return status;
233}
234
235BT_HIDDEN
d6e69534 236void ctf_fs_iterator_finalize(bt_self_message_iterator *it)
760051fa 237{
d6e69534
PP
238 ctf_fs_msg_iter_data_destroy(
239 bt_self_message_iterator_get_data(it));
760051fa
JG
240}
241
6a9bb5e9 242BT_HIDDEN
d24d5663 243bt_component_class_message_iterator_init_method_status ctf_fs_iterator_init(
d6e69534 244 bt_self_message_iterator *self_msg_iter,
4c65a157 245 bt_self_component_source *self_comp_src,
b19ff26f 246 bt_self_component_port_output *self_port)
4c1456f0 247{
4f1f88a6 248 struct ctf_fs_port_data *port_data;
d6e69534 249 struct ctf_fs_msg_iter_data *msg_iter_data = NULL;
d24d5663
PP
250 bt_component_class_message_iterator_init_method_status ret =
251 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
98903a3e 252 bt_logging_level log_level;
4c65a157 253 bt_self_component *self_comp;
760051fa 254
d94d92ac 255 port_data = bt_self_component_port_get_data(
707b7d35 256 bt_self_component_port_output_as_self_component_port(
d94d92ac
PP
257 self_port));
258 BT_ASSERT(port_data);
98903a3e 259 log_level = port_data->ctf_fs->log_level;
4c65a157 260 self_comp = port_data->ctf_fs->self_comp;
d6e69534
PP
261 msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1);
262 if (!msg_iter_data) {
9275bef4 263 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
94cf822e
PP
264 goto error;
265 }
266
98903a3e 267 msg_iter_data->log_level = log_level;
4c65a157 268 msg_iter_data->self_comp = self_comp;
d6e69534
PP
269 msg_iter_data->pc_msg_iter = self_msg_iter;
270 msg_iter_data->msg_iter = bt_msg_iter_create(
44c440bc 271 port_data->ds_file_group->ctf_fs_trace->metadata->tc,
98903a3e 272 bt_common_get_page_size(msg_iter_data->log_level) * 8,
4c65a157
PP
273 ctf_fs_ds_file_medops, NULL, msg_iter_data->log_level,
274 self_comp);
d6e69534 275 if (!msg_iter_data->msg_iter) {
4c65a157 276 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
d24d5663 277 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
6de92955
PP
278 goto error;
279 }
280
d6e69534 281 msg_iter_data->ds_file_group = port_data->ds_file_group;
6a9bb5e9 282 if (ctf_fs_iterator_reset(msg_iter_data)) {
d24d5663 283 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
4f1f88a6 284 goto error;
760051fa
JG
285 }
286
d6e69534
PP
287 bt_self_message_iterator_set_data(self_msg_iter,
288 msg_iter_data);
d24d5663 289 if (ret != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK) {
4f1f88a6 290 goto error;
760051fa
JG
291 }
292
d6e69534 293 msg_iter_data = NULL;
4f1f88a6 294 goto end;
5b29e799 295
4f1f88a6 296error:
d6e69534 297 bt_self_message_iterator_set_data(self_msg_iter, NULL);
4f1f88a6 298
760051fa 299end:
d6e69534 300 ctf_fs_msg_iter_data_destroy(msg_iter_data);
760051fa
JG
301 return ret;
302}
303
f280892e 304BT_HIDDEN
1a9f7075 305void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
760051fa 306{
4f1f88a6 307 if (!ctf_fs) {
8fa760ba
JG
308 return;
309 }
4f1f88a6 310
1a9f7075
PP
311 if (ctf_fs->traces) {
312 g_ptr_array_free(ctf_fs->traces, TRUE);
56a1cced 313 }
4f1f88a6
PP
314
315 if (ctf_fs->port_data) {
316 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 317 }
760051fa 318
1a9f7075
PP
319 g_free(ctf_fs);
320}
321
f280892e
SM
322static
323void port_data_destroy(struct ctf_fs_port_data *port_data)
324{
325 if (!port_data) {
326 return;
327 }
328
329 g_free(port_data);
330}
331
332static
333void port_data_destroy_notifier(void *data) {
334 port_data_destroy(data);
335}
336
337static
97ade20b 338void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
1a9f7075 339{
1a9f7075
PP
340 if (!ctf_fs_trace) {
341 return;
342 }
343
94cf822e
PP
344 if (ctf_fs_trace->ds_file_groups) {
345 g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE);
346 }
347
c5b9b441 348 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace);
862ca4ed 349
1a9f7075
PP
350 if (ctf_fs_trace->path) {
351 g_string_free(ctf_fs_trace->path, TRUE);
4f1f88a6 352 }
760051fa 353
1a9f7075
PP
354 if (ctf_fs_trace->name) {
355 g_string_free(ctf_fs_trace->name, TRUE);
356 }
357
358 if (ctf_fs_trace->metadata) {
359 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
360 g_free(ctf_fs_trace->metadata);
361 }
362
1a9f7075 363 g_free(ctf_fs_trace);
4c1456f0
JG
364}
365
97ade20b 366static
56a924f4 367void ctf_fs_trace_destroy_notifier(void *data)
97ade20b
JG
368{
369 struct ctf_fs_trace *trace = data;
370 ctf_fs_trace_destroy(trace);
371}
372
4c65a157
PP
373struct ctf_fs_component *ctf_fs_component_create(bt_logging_level log_level,
374 bt_self_component *self_comp)
a4792757 375{
f280892e 376 struct ctf_fs_component *ctf_fs;
a4792757 377
f280892e
SM
378 ctf_fs = g_new0(struct ctf_fs_component, 1);
379 if (!ctf_fs) {
380 goto error;
381 }
4f1f88a6 382
98903a3e 383 ctf_fs->log_level = log_level;
4c65a157 384 ctf_fs->self_comp = self_comp;
f280892e
SM
385 ctf_fs->port_data =
386 g_ptr_array_new_with_free_func(port_data_destroy_notifier);
387 if (!ctf_fs->port_data) {
388 goto error;
4f1f88a6
PP
389 }
390
f280892e
SM
391 ctf_fs->traces =
392 g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier);
393 if (!ctf_fs->traces) {
394 goto error;
395 }
396
397 goto end;
398
399error:
400 if (ctf_fs) {
401 ctf_fs_destroy(ctf_fs);
402 }
403
404end:
405 return ctf_fs;
406}
407
408void ctf_fs_finalize(bt_self_component_source *component)
409{
410 ctf_fs_destroy(bt_self_component_get_data(
411 bt_self_component_source_as_self_component(component)));
5b29e799
JG
412}
413
a38d7650 414gchar *ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group)
547eacf1 415{
a38d7650 416 GString *name = g_string_new(NULL);
547eacf1 417
a38d7650
SM
418 /*
419 * The unique port name is generated by concatenating unique identifiers
420 * for:
421 *
422 * - the trace
423 * - the stream class
424 * - the stream
425 */
426
427 /* For the trace, use the uuid if present, else the path. */
428 if (ds_file_group->ctf_fs_trace->metadata->tc->is_uuid_set) {
429 char uuid_str[BABELTRACE_UUID_STR_LEN];
430
431 bt_uuid_unparse(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str);
432 g_string_assign(name, uuid_str);
433 } else {
434 g_string_assign(name, ds_file_group->ctf_fs_trace->path->str);
547eacf1
PP
435 }
436
437 /*
a38d7650
SM
438 * For the stream class, use the id if present. We can omit this field
439 * otherwise, as there will only be a single stream class.
547eacf1 440 */
a38d7650
SM
441 if (ds_file_group->sc->id != UINT64_C(-1)) {
442 g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id);
443 }
547eacf1 444
a38d7650
SM
445 /* For the stream, use the id if present, else, use the path. */
446 if (ds_file_group->stream_id != UINT64_C(-1)) {
447 g_string_append_printf(name, " | %" PRIu64, ds_file_group->stream_id);
448 } else {
449 BT_ASSERT(ds_file_group->ds_file_infos->len == 1);
450 struct ctf_fs_ds_file_info *ds_file_info =
451 g_ptr_array_index(ds_file_group->ds_file_infos, 0);
452 g_string_append_printf(name, " | %s", ds_file_info->path->str);
453 }
454
455 return g_string_free(name, FALSE);
547eacf1
PP
456}
457
5b29e799 458static
55314f2a
JG
459int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
460 struct ctf_fs_trace *ctf_fs_trace,
94cf822e 461 struct ctf_fs_ds_file_group *ds_file_group)
5b29e799 462{
4f1f88a6 463 int ret = 0;
4f1f88a6 464 struct ctf_fs_port_data *port_data = NULL;
a38d7650 465 gchar *port_name;
98903a3e 466 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 467 bt_self_component *self_comp = ctf_fs->self_comp;
4f1f88a6 468
a38d7650 469 port_name = ctf_fs_make_port_name(ds_file_group);
4f1f88a6
PP
470 if (!port_name) {
471 goto error;
472 }
473
4c65a157 474 BT_COMP_LOGI("Creating one port named `%s`", port_name);
4f1f88a6
PP
475
476 /* Create output port for this file */
4f1f88a6
PP
477 port_data = g_new0(struct ctf_fs_port_data, 1);
478 if (!port_data) {
479 goto error;
480 }
481
5c563278 482 port_data->ctf_fs = ctf_fs;
94cf822e 483 port_data->ds_file_group = ds_file_group;
d94d92ac 484 ret = bt_self_component_source_add_output_port(
4c65a157 485 ctf_fs->self_comp_src, port_name, port_data, NULL);
147337a3 486 if (ret) {
4f1f88a6
PP
487 goto error;
488 }
489
490 g_ptr_array_add(ctf_fs->port_data, port_data);
491 port_data = NULL;
492 goto end;
493
494error:
495 ret = -1;
496
497end:
498 if (port_name) {
a38d7650 499 g_free(port_name);
4f1f88a6
PP
500 }
501
4f1f88a6
PP
502 port_data_destroy(port_data);
503 return ret;
5b29e799
JG
504}
505
506static
55314f2a
JG
507int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
508 struct ctf_fs_trace *ctf_fs_trace)
94cf822e
PP
509{
510 int ret = 0;
94cf822e 511 size_t i;
98903a3e 512 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 513 bt_self_component *self_comp = ctf_fs_trace->self_comp;
94cf822e
PP
514
515 /* Create one output port for each stream file group */
516 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
517 struct ctf_fs_ds_file_group *ds_file_group =
518 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
519
55314f2a
JG
520 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
521 ds_file_group);
94cf822e 522 if (ret) {
4c65a157 523 BT_COMP_LOGE("Cannot create output port.");
94cf822e
PP
524 goto end;
525 }
526 }
527
528end:
529 return ret;
530}
531
94cf822e
PP
532static
533void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
534{
535 if (!ds_file_info) {
536 return;
537 }
538
539 if (ds_file_info->path) {
540 g_string_free(ds_file_info->path, TRUE);
541 }
542
543 g_free(ds_file_info);
544}
545
546static
547struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
7ed5243a 548 int64_t begin_ns)
94cf822e
PP
549{
550 struct ctf_fs_ds_file_info *ds_file_info;
551
552 ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
553 if (!ds_file_info) {
554 goto end;
555 }
556
557 ds_file_info->path = g_string_new(path);
558 if (!ds_file_info->path) {
559 ctf_fs_ds_file_info_destroy(ds_file_info);
560 ds_file_info = NULL;
561 goto end;
562 }
563
564 ds_file_info->begin_ns = begin_ns;
565
566end:
567 return ds_file_info;
568}
569
570static
571void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
572{
573 if (!ds_file_group) {
574 return;
575 }
576
577 if (ds_file_group->ds_file_infos) {
578 g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
579 }
580
7ed5243a
FD
581 if (ds_file_group->index) {
582 if (ds_file_group->index->entries) {
583 g_ptr_array_free(ds_file_group->index->entries, TRUE);
584 }
585 g_free(ds_file_group->index);
586 }
587
c5b9b441 588 bt_stream_put_ref(ds_file_group->stream);
94cf822e
PP
589 g_free(ds_file_group);
590}
591
592static
593struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
594 struct ctf_fs_trace *ctf_fs_trace,
60363f2f 595 struct ctf_stream_class *sc,
7ed5243a
FD
596 uint64_t stream_instance_id,
597 struct ctf_fs_ds_index *index)
94cf822e
PP
598{
599 struct ctf_fs_ds_file_group *ds_file_group;
600
94cf822e
PP
601 ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
602 if (!ds_file_group) {
603 goto error;
604 }
605
606 ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
607 (GDestroyNotify) ctf_fs_ds_file_info_destroy);
608 if (!ds_file_group->ds_file_infos) {
609 goto error;
610 }
611
7ed5243a
FD
612 ds_file_group->index = index;
613
547eacf1 614 ds_file_group->stream_id = stream_instance_id;
60363f2f
PP
615 BT_ASSERT(sc);
616 ds_file_group->sc = sc;
94cf822e 617 ds_file_group->ctf_fs_trace = ctf_fs_trace;
94cf822e
PP
618 goto end;
619
620error:
621 ctf_fs_ds_file_group_destroy(ds_file_group);
7ed5243a 622 ctf_fs_ds_index_destroy(index);
94cf822e
PP
623 ds_file_group = NULL;
624
625end:
626 return ds_file_group;
627}
628
8bf7105e
MJ
629/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
630static
631void array_insert(GPtrArray *array, gpointer element, size_t pos)
632{
633 size_t original_array_len = array->len;
634
635 /* Allocate an unused element at the end of the array. */
636 g_ptr_array_add(array, NULL);
637
638 /* If we are not inserting at the end, move the elements by one. */
639 if (pos < original_array_len) {
640 memmove(&(array->pdata[pos + 1]),
641 &(array->pdata[pos]),
642 (original_array_len - pos) * sizeof(gpointer));
643 }
644
f897d50c 645 /* Insert the value. */
8bf7105e
MJ
646 array->pdata[pos] = element;
647}
648
41a65f30
SM
649/*
650 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
651 * place to keep it sorted.
652 */
653
654static
655void ds_file_group_insert_ds_file_info_sorted(
656 struct ctf_fs_ds_file_group *ds_file_group,
657 struct ctf_fs_ds_file_info *ds_file_info)
658{
659 guint i;
660
661 /* Find the spot where to insert this ds_file_info. */
662 for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
663 struct ctf_fs_ds_file_info *other_ds_file_info =
664 g_ptr_array_index(ds_file_group->ds_file_infos, i);
665
666 if (ds_file_info->begin_ns < other_ds_file_info->begin_ns) {
667 break;
668 }
669 }
670
671 array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
672}
673
7ed5243a
FD
674static
675void ds_file_group_insert_ds_index_entry_sorted(
676 struct ctf_fs_ds_file_group *ds_file_group,
677 struct ctf_fs_ds_index_entry *entry)
678{
679 guint i;
680
681 /* Find the spot where to insert this index entry. */
682 for (i = 0; i < ds_file_group->index->entries->len; i++) {
683 struct ctf_fs_ds_index_entry *other_entry = g_ptr_array_index(
684 ds_file_group->index->entries, i);
685
686 if (entry->timestamp_begin_ns < other_entry->timestamp_begin_ns) {
687 break;
688 }
689 }
690
691 array_insert(ds_file_group->index->entries, entry, i);
692}
693
41a65f30
SM
694/*
695 * Create a new ds_file_info using the provided path, begin_ns and index, then
696 * add it to ds_file_group's list of ds_file_infos.
697 */
698
94cf822e
PP
699static
700int ctf_fs_ds_file_group_add_ds_file_info(
701 struct ctf_fs_ds_file_group *ds_file_group,
7ed5243a 702 const char *path, int64_t begin_ns)
94cf822e
PP
703{
704 struct ctf_fs_ds_file_info *ds_file_info;
94cf822e
PP
705 int ret = 0;
706
7ed5243a 707 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns);
94cf822e
PP
708 if (!ds_file_info) {
709 goto error;
710 }
711
41a65f30 712 ds_file_group_insert_ds_file_info_sorted(ds_file_group, ds_file_info);
94cf822e 713
94cf822e
PP
714 ds_file_info = NULL;
715 goto end;
716
717error:
718 ctf_fs_ds_file_info_destroy(ds_file_info);
719 ret = -1;
94cf822e
PP
720end:
721 return ret;
722}
723
724static
725int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
e5be10ef 726 const char *path)
94cf822e 727{
44c440bc
PP
728 int64_t stream_instance_id = -1;
729 int64_t begin_ns = -1;
94cf822e
PP
730 struct ctf_fs_ds_file_group *ds_file_group = NULL;
731 bool add_group = false;
732 int ret;
733 size_t i;
6de92955 734 struct ctf_fs_ds_file *ds_file = NULL;
97ade20b 735 struct ctf_fs_ds_index *index = NULL;
d6e69534 736 struct bt_msg_iter *msg_iter = NULL;
44c440bc 737 struct ctf_stream_class *sc = NULL;
d6e69534 738 struct bt_msg_iter_packet_properties props;
98903a3e 739 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 740 bt_self_component *self_comp = ctf_fs_trace->self_comp;
94cf822e 741
d6e69534 742 msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc,
98903a3e 743 bt_common_get_page_size(log_level) * 8,
4c65a157 744 ctf_fs_ds_file_medops, NULL, log_level, self_comp);
d6e69534 745 if (!msg_iter) {
4c65a157 746 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
6de92955
PP
747 goto error;
748 }
749
d6e69534 750 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter,
98903a3e 751 NULL, path, log_level);
97ade20b
JG
752 if (!ds_file) {
753 goto error;
754 }
755
41693723 756 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
94cf822e 757 if (ret) {
4c65a157 758 BT_COMP_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
94cf822e
PP
759 path);
760 goto error;
761 }
762
44c440bc
PP
763 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
764 props.stream_class_id);
765 BT_ASSERT(sc);
44c440bc
PP
766 stream_instance_id = props.data_stream_id;
767
768 if (props.snapshots.beginning_clock != UINT64_C(-1)) {
769 BT_ASSERT(sc->default_clock_class);
0f2d58c9
PP
770 ret = bt_util_clock_cycles_to_ns_from_origin(
771 props.snapshots.beginning_clock,
772 sc->default_clock_class->frequency,
773 sc->default_clock_class->offset_seconds,
774 sc->default_clock_class->offset_cycles, &begin_ns);
44c440bc 775 if (ret) {
4c65a157 776 BT_COMP_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
44c440bc
PP
777 path);
778 goto error;
779 }
94cf822e
PP
780 }
781
97ade20b
JG
782 index = ctf_fs_ds_file_build_index(ds_file);
783 if (!index) {
4c65a157 784 BT_COMP_LOGW("Failed to index CTF stream file \'%s\'",
97ade20b
JG
785 ds_file->file->path->str);
786 }
787
44c440bc 788 if (begin_ns == -1) {
94cf822e
PP
789 /*
790 * No beggining timestamp to sort the stream files
791 * within a stream file group, so consider that this
792 * file must be the only one within its group.
793 */
44c440bc 794 stream_instance_id = -1;
94cf822e
PP
795 }
796
44c440bc 797 if (stream_instance_id == -1) {
94cf822e
PP
798 /*
799 * No stream instance ID or no beginning timestamp:
800 * create a unique stream file group for this stream
801 * file because, even if there's a stream instance ID,
802 * there's no timestamp to order the file within its
803 * group.
804 */
94cf822e 805 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
806 sc, UINT64_C(-1), index);
807 /* Ownership of index is transferred. */
808 index = NULL;
809
94cf822e
PP
810 if (!ds_file_group) {
811 goto error;
812 }
813
814 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
7ed5243a 815 path, begin_ns);
94cf822e
PP
816 if (ret) {
817 goto error;
818 }
819
820 add_group = true;
821 goto end;
822 }
823
44c440bc
PP
824 BT_ASSERT(stream_instance_id != -1);
825 BT_ASSERT(begin_ns != -1);
94cf822e
PP
826
827 /* Find an existing stream file group with this ID */
828 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
94cf822e
PP
829 ds_file_group = g_ptr_array_index(
830 ctf_fs_trace->ds_file_groups, i);
94cf822e 831
60363f2f 832 if (ds_file_group->sc == sc &&
547eacf1
PP
833 ds_file_group->stream_id ==
834 stream_instance_id) {
94cf822e
PP
835 break;
836 }
837
838 ds_file_group = NULL;
839 }
840
841 if (!ds_file_group) {
842 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
843 sc, stream_instance_id, index);
844 /* Ownership of index is transferred. */
845 index = NULL;
94cf822e
PP
846 if (!ds_file_group) {
847 goto error;
848 }
849
850 add_group = true;
851 }
852
547eacf1 853 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path,
7ed5243a 854 begin_ns);
94cf822e
PP
855 if (ret) {
856 goto error;
857 }
858
859 goto end;
860
861error:
862 ctf_fs_ds_file_group_destroy(ds_file_group);
90e1eb09 863 ds_file_group = NULL;
94cf822e
PP
864 ret = -1;
865
866end:
867 if (add_group && ds_file_group) {
868 g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
869 }
547eacf1 870
97ade20b 871 ctf_fs_ds_file_destroy(ds_file);
6de92955 872
d6e69534
PP
873 if (msg_iter) {
874 bt_msg_iter_destroy(msg_iter);
6de92955
PP
875 }
876
97ade20b 877 ctf_fs_ds_index_destroy(index);
94cf822e
PP
878 return ret;
879}
880
881static
e5be10ef 882int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
e7a4393b
JG
883{
884 int ret = 0;
4f1f88a6 885 const char *basename;
e7a4393b 886 GError *error = NULL;
4f1f88a6 887 GDir *dir = NULL;
98903a3e 888 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 889 bt_self_component *self_comp = ctf_fs_trace->self_comp;
e7a4393b 890
94cf822e 891 /* Check each file in the path directory, except specific ones */
1a9f7075 892 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
e7a4393b 893 if (!dir) {
4c65a157 894 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075 895 ctf_fs_trace->path->str, error->message,
4f1f88a6 896 error->code);
e7a4393b
JG
897 goto error;
898 }
899
4f1f88a6 900 while ((basename = g_dir_read_name(dir))) {
94cf822e
PP
901 struct ctf_fs_file *file;
902
4f1f88a6 903 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
e7a4393b 904 /* Ignore the metadata stream. */
4c65a157 905 BT_COMP_LOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 906 ctf_fs_trace->path->str, basename);
e7a4393b
JG
907 continue;
908 }
909
4f1f88a6 910 if (basename[0] == '.') {
4c65a157 911 BT_COMP_LOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 912 ctf_fs_trace->path->str, basename);
e7a4393b
JG
913 continue;
914 }
915
916 /* Create the file. */
4c65a157 917 file = ctf_fs_file_create(log_level, self_comp);
e7a4393b 918 if (!file) {
4c65a157 919 BT_COMP_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 920 ctf_fs_trace->path->str, basename);
e7a4393b
JG
921 goto error;
922 }
923
924 /* Create full path string. */
3743a302 925 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
1a9f7075 926 ctf_fs_trace->path->str, basename);
e7a4393b 927 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
4c65a157 928 BT_COMP_LOGI("Ignoring non-regular file `%s`",
1a9f7075 929 file->path->str);
e7a4393b 930 ctf_fs_file_destroy(file);
4f1f88a6 931 file = NULL;
e7a4393b
JG
932 continue;
933 }
934
55314f2a 935 ret = ctf_fs_file_open(file, "rb");
4f1f88a6 936 if (ret) {
4c65a157 937 BT_COMP_LOGE("Cannot open stream file `%s`", file->path->str);
e7a4393b
JG
938 goto error;
939 }
940
9fa0891a
JG
941 if (file->size == 0) {
942 /* Skip empty stream. */
4c65a157 943 BT_COMP_LOGI("Ignoring empty file `%s`", file->path->str);
9fa0891a
JG
944 ctf_fs_file_destroy(file);
945 continue;
946 }
947
e5be10ef 948 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
94cf822e 949 file->path->str);
4f1f88a6 950 if (ret) {
4c65a157 951 BT_COMP_LOGE("Cannot add stream file `%s` to stream file group",
1a9f7075 952 file->path->str);
94cf822e 953 ctf_fs_file_destroy(file);
e7a4393b
JG
954 goto error;
955 }
956
4f1f88a6 957 ctf_fs_file_destroy(file);
e7a4393b
JG
958 }
959
960 goto end;
4f1f88a6 961
e7a4393b
JG
962error:
963 ret = -1;
4f1f88a6 964
e7a4393b
JG
965end:
966 if (dir) {
967 g_dir_close(dir);
968 dir = NULL;
969 }
4f1f88a6 970
e7a4393b
JG
971 if (error) {
972 g_error_free(error);
973 }
5b29e799 974
91457551 975 return ret;
5b29e799
JG
976}
977
862ca4ed 978static
98903a3e 979int set_trace_name(bt_trace *trace, const char *name_suffix,
4c65a157 980 bt_logging_level log_level, bt_self_component *self_comp)
862ca4ed
PP
981{
982 int ret = 0;
b19ff26f
PP
983 const bt_trace_class *tc = bt_trace_borrow_class_const(trace);
984 const bt_value *val;
862ca4ed
PP
985 GString *name;
986
987 name = g_string_new(NULL);
988 if (!name) {
4c65a157 989 BT_COMP_LOGE_STR("Failed to allocate a GString.");
862ca4ed
PP
990 ret = -1;
991 goto end;
992 }
993
994 /*
995 * Check if we have a trace environment string value named `hostname`.
996 * If so, use it as the trace name's prefix.
997 */
998 val = bt_trace_class_borrow_environment_entry_value_by_name_const(
999 tc, "hostname");
1000 if (val && bt_value_is_string(val)) {
1001 g_string_append(name, bt_value_string_get(val));
1002
1003 if (name_suffix) {
1004 g_string_append_c(name, G_DIR_SEPARATOR);
1005 }
1006 }
1007
1008 if (name_suffix) {
1009 g_string_append(name, name_suffix);
1010 }
1011
1012 ret = bt_trace_set_name(trace, name->str);
1013 if (ret) {
1014 goto end;
1015 }
1016
1017 goto end;
1018
1019end:
1020 if (name) {
1021 g_string_free(name, TRUE);
1022 }
1023
1024 return ret;
1025}
1026
f280892e 1027static
4c65a157 1028struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component *self_comp,
41693723 1029 const char *path, const char *name,
98903a3e
PP
1030 struct ctf_fs_metadata_config *metadata_config,
1031 bt_logging_level log_level)
1a9f7075
PP
1032{
1033 struct ctf_fs_trace *ctf_fs_trace;
1034 int ret;
1035
1036 ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
1037 if (!ctf_fs_trace) {
1038 goto end;
1039 }
1040
98903a3e 1041 ctf_fs_trace->log_level = log_level;
4c65a157 1042 ctf_fs_trace->self_comp = self_comp;
1a9f7075
PP
1043 ctf_fs_trace->path = g_string_new(path);
1044 if (!ctf_fs_trace->path) {
1045 goto error;
1046 }
1047
1048 ctf_fs_trace->name = g_string_new(name);
1049 if (!ctf_fs_trace->name) {
1050 goto error;
1051 }
1052
1053 ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
1054 if (!ctf_fs_trace->metadata) {
1055 goto error;
1056 }
1057
44c440bc 1058 ctf_fs_metadata_init(ctf_fs_trace->metadata);
94cf822e
PP
1059 ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func(
1060 (GDestroyNotify) ctf_fs_ds_file_group_destroy);
1061 if (!ctf_fs_trace->ds_file_groups) {
1062 goto error;
1063 }
1064
4c65a157
PP
1065 ret = ctf_fs_metadata_set_trace_class(self_comp, ctf_fs_trace,
1066 metadata_config);
862ca4ed
PP
1067 if (ret) {
1068 goto error;
1069 }
1070
41693723
PP
1071 if (ctf_fs_trace->metadata->trace_class) {
1072 ctf_fs_trace->trace =
1073 bt_trace_create(ctf_fs_trace->metadata->trace_class);
1074 if (!ctf_fs_trace->trace) {
1075 goto error;
1076 }
862ca4ed
PP
1077 }
1078
41693723 1079 if (ctf_fs_trace->trace) {
4c65a157
PP
1080 ret = set_trace_name(ctf_fs_trace->trace, name, log_level,
1081 self_comp);
41693723
PP
1082 if (ret) {
1083 goto error;
1084 }
1a9f7075
PP
1085 }
1086
e5be10ef 1087 ret = create_ds_file_groups(ctf_fs_trace);
94cf822e
PP
1088 if (ret) {
1089 goto error;
1090 }
1091
1a9f7075
PP
1092 goto end;
1093
1094error:
1095 ctf_fs_trace_destroy(ctf_fs_trace);
1096 ctf_fs_trace = NULL;
44c440bc 1097
1a9f7075
PP
1098end:
1099 return ctf_fs_trace;
1100}
1101
1102static
1103int path_is_ctf_trace(const char *path)
1104{
1105 GString *metadata_path = g_string_new(NULL);
1106 int ret = 0;
1107
1108 if (!metadata_path) {
1109 ret = -1;
1110 goto end;
1111 }
1112
3743a302 1113 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
1a9f7075
PP
1114
1115 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
1116 ret = 1;
1117 goto end;
1118 }
1119
1120end:
1121 g_string_free(metadata_path, TRUE);
1122 return ret;
1123}
1124
1125static
98903a3e 1126int add_trace_path(GList **trace_paths, const char *path,
4c65a157 1127 bt_logging_level log_level, bt_self_component *self_comp)
1a9f7075 1128{
4bd72b60 1129 GString *norm_path = NULL;
1a9f7075 1130 int ret = 0;
1a9f7075 1131
4bd72b60
PP
1132 norm_path = bt_common_normalize_path(path, NULL);
1133 if (!norm_path) {
4c65a157 1134 BT_COMP_LOGE("Failed to normalize path `%s`.", path);
1a9f7075
PP
1135 ret = -1;
1136 goto end;
1137 }
1138
3743a302 1139 // FIXME: Remove or ifdef for __MINGW32__
4bd72b60 1140 if (strcmp(norm_path->str, "/") == 0) {
4c65a157 1141 BT_COMP_LOGE("Opening a trace in `/` is not supported.");
1a9f7075
PP
1142 ret = -1;
1143 goto end;
1144 }
1145
4bd72b60 1146 *trace_paths = g_list_prepend(*trace_paths, norm_path);
f6ccaed9 1147 BT_ASSERT(*trace_paths);
4bd72b60 1148 norm_path = NULL;
1a9f7075
PP
1149
1150end:
4bd72b60
PP
1151 if (norm_path) {
1152 g_string_free(norm_path, TRUE);
1153 }
1154
1a9f7075
PP
1155 return ret;
1156}
1157
f280892e 1158static
98903a3e 1159int ctf_fs_find_traces(GList **trace_paths, const char *start_path,
4c65a157 1160 bt_logging_level log_level, bt_self_component *self_comp)
1a9f7075
PP
1161{
1162 int ret;
1163 GError *error = NULL;
1164 GDir *dir = NULL;
1165 const char *basename = NULL;
1166
1167 /* Check if the starting path is a CTF trace itself */
1168 ret = path_is_ctf_trace(start_path);
1169 if (ret < 0) {
1170 goto end;
1171 }
1172
1173 if (ret) {
1174 /*
4bd72b60
PP
1175 * Stop recursion: a CTF trace cannot contain another
1176 * CTF trace.
1a9f7075 1177 */
4c65a157
PP
1178 ret = add_trace_path(trace_paths, start_path, log_level,
1179 self_comp);
1a9f7075
PP
1180 goto end;
1181 }
1182
1183 /* Look for subdirectories */
1184 if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) {
1185 /* Starting path is not a directory: end of recursion */
1186 goto end;
1187 }
1188
1189 dir = g_dir_open(start_path, 0, &error);
1190 if (!dir) {
1191 if (error->code == G_FILE_ERROR_ACCES) {
4c65a157 1192 BT_COMP_LOGI("Cannot open directory `%s`: %s (code %d): continuing",
1a9f7075
PP
1193 start_path, error->message, error->code);
1194 goto end;
1195 }
1196
4c65a157 1197 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075
PP
1198 start_path, error->message, error->code);
1199 ret = -1;
1200 goto end;
1201 }
1202
1203 while ((basename = g_dir_read_name(dir))) {
1204 GString *sub_path = g_string_new(NULL);
1205
1206 if (!sub_path) {
1207 ret = -1;
1208 goto end;
1209 }
1210
3743a302 1211 g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename);
98903a3e 1212 ret = ctf_fs_find_traces(trace_paths, sub_path->str,
4c65a157 1213 log_level, self_comp);
1a9f7075
PP
1214 g_string_free(sub_path, TRUE);
1215 if (ret) {
1216 goto end;
1217 }
1218 }
1219
1220end:
1221 if (dir) {
1222 g_dir_close(dir);
1223 }
1224
1225 if (error) {
1226 g_error_free(error);
1227 }
1228
1229 return ret;
1230}
1231
f280892e 1232static
55314f2a 1233GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) {
1a9f7075 1234 GList *trace_names = NULL;
1a9f7075 1235 GList *node;
4bd72b60
PP
1236 const char *last_sep;
1237 size_t base_dist;
1a9f7075
PP
1238
1239 /*
4bd72b60
PP
1240 * At this point we know that all the trace paths are
1241 * normalized, and so is the base path. This means that
1242 * they are absolute and they don't end with a separator.
1243 * We can simply find the location of the last separator
1244 * in the base path, which gives us the name of the actual
1245 * directory to look into, and use this location as the
1246 * start of each trace name within each trace path.
1247 *
1248 * For example:
1249 *
1250 * Base path: /home/user/my-traces/some-trace
1251 * Trace paths:
1252 * - /home/user/my-traces/some-trace/host1/trace1
1253 * - /home/user/my-traces/some-trace/host1/trace2
1254 * - /home/user/my-traces/some-trace/host2/trace
1255 * - /home/user/my-traces/some-trace/other-trace
1256 *
1257 * In this case the trace names are:
1258 *
1259 * - some-trace/host1/trace1
1260 * - some-trace/host1/trace2
1261 * - some-trace/host2/trace
1262 * - some-trace/other-trace
1a9f7075 1263 */
4bd72b60 1264 last_sep = strrchr(base_path, G_DIR_SEPARATOR);
1a9f7075 1265
4bd72b60 1266 /* We know there's at least one separator */
f6ccaed9 1267 BT_ASSERT(last_sep);
1a9f7075 1268
4bd72b60
PP
1269 /* Distance to base */
1270 base_dist = last_sep - base_path + 1;
1a9f7075
PP
1271
1272 /* Create the trace names */
1273 for (node = trace_paths; node; node = g_list_next(node)) {
1274 GString *trace_name = g_string_new(NULL);
1275 GString *trace_path = node->data;
1276
f6ccaed9 1277 BT_ASSERT(trace_name);
4bd72b60 1278 g_string_assign(trace_name, &trace_path->str[base_dist]);
1a9f7075
PP
1279 trace_names = g_list_append(trace_names, trace_name);
1280 }
1281
1282 return trace_names;
1283}
1284
f280892e
SM
1285/* Helper for ctf_fs_component_create_ctf_fs_traces, to handle a single path/root. */
1286
1a9f7075 1287static
4c65a157 1288int ctf_fs_component_create_ctf_fs_traces_one_root(
41693723 1289 struct ctf_fs_component *ctf_fs,
1a9f7075
PP
1290 const char *path_param)
1291{
1292 struct ctf_fs_trace *ctf_fs_trace = NULL;
1293 int ret = 0;
4bd72b60 1294 GString *norm_path = NULL;
1a9f7075
PP
1295 GList *trace_paths = NULL;
1296 GList *trace_names = NULL;
1297 GList *tp_node;
1298 GList *tn_node;
98903a3e 1299 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1300 bt_self_component *self_comp = ctf_fs->self_comp;
1a9f7075 1301
4bd72b60
PP
1302 norm_path = bt_common_normalize_path(path_param, NULL);
1303 if (!norm_path) {
4c65a157 1304 BT_COMP_LOGE("Failed to normalize path: `%s`.",
4bd72b60
PP
1305 path_param);
1306 goto error;
1307 }
1308
4c65a157
PP
1309 ret = ctf_fs_find_traces(&trace_paths, norm_path->str, log_level,
1310 self_comp);
1a9f7075
PP
1311 if (ret) {
1312 goto error;
1313 }
1314
1315 if (!trace_paths) {
4c65a157 1316 BT_COMP_LOGE("No CTF traces recursively found in `%s`.",
1a9f7075 1317 path_param);
3fdffb55
PP
1318 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
1319 ctf_fs->self_comp,
1320 "No CTF traces recursively found in `%s`.", path_param);
1a9f7075
PP
1321 goto error;
1322 }
1323
55314f2a 1324 trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str);
1a9f7075 1325 if (!trace_names) {
4c65a157 1326 BT_COMP_LOGE("Cannot create trace names from trace paths.");
1a9f7075
PP
1327 goto error;
1328 }
1329
1330 for (tp_node = trace_paths, tn_node = trace_names; tp_node;
1331 tp_node = g_list_next(tp_node),
1332 tn_node = g_list_next(tn_node)) {
1333 GString *trace_path = tp_node->data;
1334 GString *trace_name = tn_node->data;
1335
41693723
PP
1336 ctf_fs_trace = ctf_fs_trace_create(self_comp,
1337 trace_path->str, trace_name->str,
98903a3e
PP
1338 &ctf_fs->metadata_config,
1339 log_level);
1a9f7075 1340 if (!ctf_fs_trace) {
4c65a157 1341 BT_COMP_LOGE("Cannot create trace for `%s`.",
1a9f7075
PP
1342 trace_path->str);
1343 goto error;
1344 }
52c5fe74
PP
1345
1346 g_ptr_array_add(ctf_fs->traces, ctf_fs_trace);
1347 ctf_fs_trace = NULL;
1a9f7075
PP
1348 }
1349
1a9f7075
PP
1350 goto end;
1351
1352error:
1353 ret = -1;
1354 ctf_fs_trace_destroy(ctf_fs_trace);
1355
1356end:
1357 for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
1358 if (tp_node->data) {
1359 g_string_free(tp_node->data, TRUE);
1360 }
1361 }
1362
1363 for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
1364 if (tn_node->data) {
1365 g_string_free(tn_node->data, TRUE);
1366 }
1367 }
1368
1369 if (trace_paths) {
1370 g_list_free(trace_paths);
1371 }
1372
1373 if (trace_names) {
1374 g_list_free(trace_names);
1375 }
1376
4bd72b60
PP
1377 if (norm_path) {
1378 g_string_free(norm_path, TRUE);
1379 }
1380
1a9f7075
PP
1381 return ret;
1382}
1383
41a65f30
SM
1384/* GCompareFunc to sort traces by UUID. */
1385
1386static
1387gint sort_traces_by_uuid(gconstpointer a, gconstpointer b)
1388{
1389 const struct ctf_fs_trace *trace_a = *((const struct ctf_fs_trace **) a);
1390 const struct ctf_fs_trace *trace_b = *((const struct ctf_fs_trace **) b);
1391
1392 bool trace_a_has_uuid = trace_a->metadata->tc->is_uuid_set;
1393 bool trace_b_has_uuid = trace_b->metadata->tc->is_uuid_set;
1394 gint ret;
1395
1396 /* Order traces without uuid first. */
1397 if (!trace_a_has_uuid && trace_b_has_uuid) {
1398 ret = -1;
1399 } else if (trace_a_has_uuid && !trace_b_has_uuid) {
1400 ret = 1;
1401 } else if (!trace_a_has_uuid && !trace_b_has_uuid) {
1402 ret = 0;
1403 } else {
1404 ret = bt_uuid_compare(trace_a->metadata->tc->uuid, trace_b->metadata->tc->uuid);
1405 }
1406
1407 return ret;
1408}
1409
1410/*
1411 * Count the number of stream and event classes defined by this trace's metadata.
1412 *
1413 * This is used to determine which metadata is the "latest", out of multiple
1414 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
1415 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
1416 * enough to just count the classes.
1417 */
1418
1419static
1420unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace *trace)
1421{
1422 unsigned int num = trace->metadata->tc->stream_classes->len;
1423 guint i;
1424
1425 for (i = 0; i < trace->metadata->tc->stream_classes->len; i++) {
1426 struct ctf_stream_class *sc = trace->metadata->tc->stream_classes->pdata[i];
1427 num += sc->event_classes->len;
1428 }
1429
1430 return num;
1431}
1432
1433/*
1434 * Merge the src ds_file_group into dest. This consists of merging their
1435 * ds_file_infos, making sure to keep the result sorted.
1436 */
1437
1438static
1439void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group *dest, struct ctf_fs_ds_file_group *src)
1440{
1441 guint i;
1442
1443 for (i = 0; i < src->ds_file_infos->len; i++) {
1444 struct ctf_fs_ds_file_info *ds_file_info =
1445 g_ptr_array_index(src->ds_file_infos, i);
1446
1447 /* Ownership of the ds_file_info is transferred to dest. */
1448 g_ptr_array_index(src->ds_file_infos, i) = NULL;
1449
1450 ds_file_group_insert_ds_file_info_sorted(dest, ds_file_info);
1451 }
41a65f30 1452
7ed5243a
FD
1453 /* Merge both indexes. */
1454 for (i = 0; i < src->index->entries->len; i++) {
1455 struct ctf_fs_ds_index_entry *entry = g_ptr_array_index(
1456 src->index->entries, i);
1457
1458 /*
1459 * Ownership of the ctf_fs_ds_index_entry is transferred to
1460 * dest.
1461 */
1462 g_ptr_array_index(src->index->entries, i) = NULL;
1463
1464 ds_file_group_insert_ds_index_entry_sorted(dest, entry);
1465 }
1466}
41a65f30
SM
1467/* Merge src_trace's data stream file groups into dest_trace's. */
1468
1469static
54ef52bd 1470int merge_matching_ctf_fs_ds_file_groups(
41a65f30
SM
1471 struct ctf_fs_trace *dest_trace,
1472 struct ctf_fs_trace *src_trace)
1473{
1474
1475 GPtrArray *dest = dest_trace->ds_file_groups;
1476 GPtrArray *src = src_trace->ds_file_groups;
1477 guint s_i;
54ef52bd 1478 int ret = 0;
41a65f30
SM
1479
1480 /*
1481 * Save the initial length of dest: we only want to check against the
1482 * original elements in the inner loop.
1483 */
1484 const guint dest_len = dest->len;
1485
1486 for (s_i = 0; s_i < src->len; s_i++) {
1487 struct ctf_fs_ds_file_group *src_group = g_ptr_array_index(src, s_i);
1488 struct ctf_fs_ds_file_group *dest_group = NULL;
1489
1490 /* A stream instance without ID can't match a stream in the other trace. */
1491 if (src_group->stream_id != -1) {
1492 guint d_i;
1493
1494 /* Let's search for a matching ds_file_group in the destination. */
1495 for (d_i = 0; d_i < dest_len; d_i++) {
1496 struct ctf_fs_ds_file_group *candidate_dest = g_ptr_array_index(dest, d_i);
1497
1498 /* Can't match a stream instance without ID. */
1499 if (candidate_dest->stream_id == -1) {
1500 continue;
1501 }
1502
1503 /*
1504 * If the two groups have the same stream instance id
1505 * and belong to the same stream class (stream instance
1506 * ids are per-stream class), they represent the same
1507 * stream instance.
1508 */
1509 if (candidate_dest->stream_id != src_group->stream_id ||
1510 candidate_dest->sc->id != src_group->sc->id) {
1511 continue;
1512 }
1513
1514 dest_group = candidate_dest;
1515 break;
1516 }
1517 }
1518
1519 /*
1520 * Didn't find a friend in dest to merge our src_group into?
7ed5243a
FD
1521 * Create a new empty one. This can happen if a stream was
1522 * active in the source trace chunk but not in the destination
1523 * trace chunk.
41a65f30
SM
1524 */
1525 if (!dest_group) {
1526 struct ctf_stream_class *sc;
7ed5243a 1527 struct ctf_fs_ds_index *index;
41a65f30
SM
1528
1529 sc = ctf_trace_class_borrow_stream_class_by_id(
1530 dest_trace->metadata->tc, src_group->sc->id);
1531 BT_ASSERT(sc);
1532
4c65a157
PP
1533 index = ctf_fs_ds_index_create(dest_trace->log_level,
1534 dest_trace->self_comp);
7ed5243a
FD
1535 if (!index) {
1536 ret = -1;
1537 goto end;
1538 }
1539
41a65f30 1540 dest_group = ctf_fs_ds_file_group_create(dest_trace, sc,
7ed5243a
FD
1541 src_group->stream_id, index);
1542 /* Ownership of index is transferred. */
1543 index = NULL;
54ef52bd
FD
1544 if (!dest_group) {
1545 ret = -1;
1546 goto end;
1547 }
41a65f30
SM
1548
1549 g_ptr_array_add(dest_trace->ds_file_groups, dest_group);
1550 }
1551
1552 BT_ASSERT(dest_group);
1553 merge_ctf_fs_ds_file_groups(dest_group, src_group);
1554 }
54ef52bd
FD
1555
1556end:
1557 return ret;
41a65f30
SM
1558}
1559
1560/*
1561 * Collapse the given traces, which must all share the same UUID, in a single
1562 * one.
1563 *
1564 * The trace with the most expansive metadata is chosen and all other traces
1565 * are merged into that one. The array slots of all the traces that get merged
1566 * in the chosen one are set to NULL, so only the slot of the chosen trace
1567 * remains non-NULL.
1568 */
1569
1570static
54ef52bd 1571int merge_ctf_fs_traces(struct ctf_fs_trace **traces, unsigned int num_traces)
41a65f30
SM
1572{
1573 unsigned int winner_count;
1574 struct ctf_fs_trace *winner;
1575 guint i;
54ef52bd 1576 int ret = 0;
41a65f30
SM
1577 char uuid_str[BABELTRACE_UUID_STR_LEN];
1578
1579 BT_ASSERT(num_traces >= 2);
1580
1581 winner_count = metadata_count_stream_and_event_classes(traces[0]);
1582 winner = traces[0];
1583
1584 /* Find the trace with the largest metadata. */
1585 for (i = 1; i < num_traces; i++) {
1586 struct ctf_fs_trace *candidate;
1587 unsigned int candidate_count;
1588
1589 candidate = traces[i];
1590
1591 /* A bit of sanity check. */
1592 BT_ASSERT(bt_uuid_compare(winner->metadata->tc->uuid, candidate->metadata->tc->uuid) == 0);
1593
1594 candidate_count = metadata_count_stream_and_event_classes(candidate);
1595
1596 if (candidate_count > winner_count) {
1597 winner_count = candidate_count;
1598 winner = candidate;
1599 }
1600 }
1601
1602 /* Merge all the other traces in the winning trace. */
1603 for (i = 0; i < num_traces; i++) {
1604 struct ctf_fs_trace *trace = traces[i];
1605
1606 /* Don't merge the winner into itself. */
1607 if (trace == winner) {
1608 continue;
1609 }
1610
1611 /* Merge trace's data stream file groups into winner's. */
54ef52bd
FD
1612 ret = merge_matching_ctf_fs_ds_file_groups(winner, trace);
1613 if (ret) {
1614 goto end;
1615 }
41a65f30
SM
1616
1617 /* Free the trace that got merged into winner, clear the slot in the array. */
1618 ctf_fs_trace_destroy(trace);
1619 traces[i] = NULL;
1620 }
1621
1622 /* Use the string representation of the UUID as the trace name. */
1623 bt_uuid_unparse(winner->metadata->tc->uuid, uuid_str);
1624 g_string_printf(winner->name, "%s", uuid_str);
54ef52bd
FD
1625
1626end:
1627 return ret;
41a65f30
SM
1628}
1629
1630/*
1631 * Merge all traces of `ctf_fs` that share the same UUID in a single trace.
1632 * Traces with no UUID are not merged.
1633 */
1634
1635static
54ef52bd 1636int merge_traces_with_same_uuid(struct ctf_fs_component *ctf_fs)
41a65f30
SM
1637{
1638 GPtrArray *traces = ctf_fs->traces;
1639 guint range_start_idx = 0;
1640 unsigned int num_traces = 0;
1641 guint i;
54ef52bd 1642 int ret = 0;
41a65f30
SM
1643
1644 /* Sort the traces by uuid, then collapse traces with the same uuid in a single one. */
1645 g_ptr_array_sort(traces, sort_traces_by_uuid);
1646
1647 /* Find ranges of consecutive traces that share the same UUID. */
1648 while (range_start_idx < traces->len) {
1649 guint range_len;
1650 struct ctf_fs_trace *range_start_trace = g_ptr_array_index(traces, range_start_idx);
1651
1652 /* Exclusive end of range. */
1653 guint range_end_exc_idx = range_start_idx + 1;
1654
1655 while (range_end_exc_idx < traces->len) {
1656 struct ctf_fs_trace *this_trace = g_ptr_array_index(traces, range_end_exc_idx);
1657
1658 if (!range_start_trace->metadata->tc->is_uuid_set ||
1659 (bt_uuid_compare(range_start_trace->metadata->tc->uuid, this_trace->metadata->tc->uuid) != 0)) {
1660 break;
1661 }
1662
1663 range_end_exc_idx++;
1664 }
1665
1666 /* If we have two or more traces with matching UUIDs, merge them. */
1667 range_len = range_end_exc_idx - range_start_idx;
1668 if (range_len > 1) {
1669 struct ctf_fs_trace **range_start = (struct ctf_fs_trace **) &traces->pdata[range_start_idx];
54ef52bd
FD
1670 ret = merge_ctf_fs_traces(range_start, range_len);
1671 if (ret) {
1672 goto end;
1673 }
41a65f30
SM
1674 }
1675
1676 num_traces++;
1677 range_start_idx = range_end_exc_idx;
1678 }
1679
1680 /* Clear any NULL slot (traces that got merged in another one) in the array. */
1681 for (i = 0; i < traces->len;) {
1682 if (g_ptr_array_index(traces, i) == NULL) {
1683 g_ptr_array_remove_index_fast(traces, i);
1684 } else {
1685 i++;
1686 }
1687 }
1688
1689 BT_ASSERT(num_traces == traces->len);
54ef52bd
FD
1690
1691end:
1692 return ret;
41a65f30
SM
1693}
1694
f280892e
SM
1695int ctf_fs_component_create_ctf_fs_traces(bt_self_component_source *self_comp,
1696 struct ctf_fs_component *ctf_fs,
1697 const bt_value *paths_value)
1698{
1699 int ret = 0;
1700 uint64_t i;
1701
1702 for (i = 0; i < bt_value_array_get_size(paths_value); i++) {
1703 const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i);
1704 const char *path = bt_value_string_get(path_value);
1705
4c65a157
PP
1706 ret = ctf_fs_component_create_ctf_fs_traces_one_root(ctf_fs,
1707 path);
f280892e
SM
1708 if (ret) {
1709 goto end;
1710 }
1711 }
1712
54ef52bd 1713 ret = merge_traces_with_same_uuid(ctf_fs);
41a65f30 1714
f280892e
SM
1715end:
1716 return ret;
1717}
1718
a38d7650
SM
1719static
1720GString *get_stream_instance_unique_name(
1721 struct ctf_fs_ds_file_group *ds_file_group)
1722{
1723 GString *name;
1724 struct ctf_fs_ds_file_info *ds_file_info;
1725
1726 name = g_string_new(NULL);
1727 if (!name) {
1728 goto end;
1729 }
1730
1731 /*
1732 * If there's more than one stream file in the stream file
1733 * group, the first (earliest) stream file's path is used as
1734 * the stream's unique name.
1735 */
1736 BT_ASSERT(ds_file_group->ds_file_infos->len > 0);
1737 ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0);
1738 g_string_assign(name, ds_file_info->path->str);
1739
1740end:
1741 return name;
1742}
1743
f280892e
SM
1744/* Create the IR stream objects for ctf_fs_trace. */
1745
1746static
1747int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace)
1748{
1749 int ret;
1750 GString *name = NULL;
1751 guint i;
98903a3e 1752 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 1753 bt_self_component *self_comp = ctf_fs_trace->self_comp;
f280892e
SM
1754
1755 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
1756 struct ctf_fs_ds_file_group *ds_file_group =
1757 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
1758 name = get_stream_instance_unique_name(ds_file_group);
1759
1760 if (!name) {
1761 goto error;
1762 }
1763
1764 if (ds_file_group->sc->ir_sc) {
1765 BT_ASSERT(ctf_fs_trace->trace);
1766
1767 if (ds_file_group->stream_id == UINT64_C(-1)) {
1768 /* No stream ID: use 0 */
1769 ds_file_group->stream = bt_stream_create_with_id(
1770 ds_file_group->sc->ir_sc,
1771 ctf_fs_trace->trace,
1772 ctf_fs_trace->next_stream_id);
1773 ctf_fs_trace->next_stream_id++;
1774 } else {
1775 /* Specific stream ID */
1776 ds_file_group->stream = bt_stream_create_with_id(
1777 ds_file_group->sc->ir_sc,
1778 ctf_fs_trace->trace,
1779 (uint64_t) ds_file_group->stream_id);
1780 }
1781 } else {
1782 ds_file_group->stream = NULL;
1783 }
1784
1785 if (!ds_file_group->stream) {
4c65a157 1786 BT_COMP_LOGE("Cannot create stream for DS file group: "
f280892e
SM
1787 "addr=%p, stream-name=\"%s\"",
1788 ds_file_group, name->str);
1789 goto error;
1790 }
1791
1792 ret = bt_stream_set_name(ds_file_group->stream,
1793 name->str);
1794 if (ret) {
4c65a157 1795 BT_COMP_LOGE("Cannot set stream's name: "
f280892e
SM
1796 "addr=%p, stream-name=\"%s\"",
1797 ds_file_group->stream, name->str);
1798 goto error;
1799 }
1800
1801 g_string_free(name, TRUE);
1802 name = NULL;
1803 }
1804
1805 ret = 0;
1806 goto end;
1807
1808error:
1809 ret = -1;
1810
1811end:
1812
1813 if (name) {
1814 g_string_free(name, TRUE);
1815 }
1816 return ret;
1817}
1818
d907165c
SM
1819/*
1820 * Validate the "paths" parameter passed to this component. It must be
1821 * present, and it must be an array of strings.
1822 */
1823
1824static
98903a3e
PP
1825bool validate_paths_parameter(struct ctf_fs_component *ctf_fs,
1826 const bt_value *paths)
f280892e
SM
1827{
1828 bool ret;
1829 bt_value_type type;
1830 uint64_t i;
98903a3e 1831 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1832 bt_self_component *self_comp = ctf_fs->self_comp;
f280892e
SM
1833
1834 if (!paths) {
4c65a157 1835 BT_COMP_LOGE("missing \"paths\" parameter");
f280892e
SM
1836 goto error;
1837 }
1838
1839 type = bt_value_get_type(paths);
1840 if (type != BT_VALUE_TYPE_ARRAY) {
4c65a157 1841 BT_COMP_LOGE("`paths` parameter: expecting array value: type=%s",
f280892e
SM
1842 bt_common_value_type_string(type));
1843 goto error;
1844 }
1845
1846 for (i = 0; i < bt_value_array_get_size(paths); i++) {
1847 const bt_value *elem;
1848
1849 elem = bt_value_array_borrow_element_by_index_const(paths, i);
1850 type = bt_value_get_type(elem);
1851 if (type != BT_VALUE_TYPE_STRING) {
4c65a157 1852 BT_COMP_LOGE("`paths` parameter: expecting string value: index=%" PRIu64 ", type=%s",
f280892e
SM
1853 i, bt_common_value_type_string(type));
1854 goto error;
1855 }
1856 }
1857
1858 ret = true;
1859 goto end;
1860
1861error:
1862 ret = false;
1863
1864end:
1865 return ret;
1866}
1867
d907165c
SM
1868bool read_src_fs_parameters(const bt_value *params,
1869 const bt_value **paths, struct ctf_fs_component *ctf_fs) {
1870 bool ret;
1871 const bt_value *value;
98903a3e 1872 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1873 bt_self_component *self_comp = ctf_fs->self_comp;
d907165c
SM
1874
1875 /* paths parameter */
1876 *paths = bt_value_map_borrow_entry_value_const(params, "paths");
98903a3e 1877 if (!validate_paths_parameter(ctf_fs, *paths)) {
d907165c
SM
1878 goto error;
1879 }
1880
1881 /* clock-class-offset-s parameter */
1882 value = bt_value_map_borrow_entry_value_const(params,
1883 "clock-class-offset-s");
1884 if (value) {
fdd3a2da 1885 if (!bt_value_is_signed_integer(value)) {
4c65a157 1886 BT_COMP_LOGE("clock-class-offset-s must be an integer");
d907165c
SM
1887 goto error;
1888 }
1889 ctf_fs->metadata_config.clock_class_offset_s =
fdd3a2da 1890 bt_value_signed_integer_get(value);
d907165c
SM
1891 }
1892
1893 /* clock-class-offset-ns parameter */
1894 value = bt_value_map_borrow_entry_value_const(params,
1895 "clock-class-offset-ns");
1896 if (value) {
fdd3a2da 1897 if (!bt_value_is_signed_integer(value)) {
4c65a157 1898 BT_COMP_LOGE("clock-class-offset-ns must be an integer");
d907165c
SM
1899 goto error;
1900 }
1901 ctf_fs->metadata_config.clock_class_offset_ns =
fdd3a2da 1902 bt_value_signed_integer_get(value);
d907165c
SM
1903 }
1904
1905
1906 ret = true;
1907 goto end;
1908
1909error:
1910 ret = false;
1911
1912end:
1913 return ret;
1914}
1915
5b29e799 1916static
d94d92ac 1917struct ctf_fs_component *ctf_fs_create(
98903a3e 1918 bt_self_component_source *self_comp_src,
b19ff26f 1919 const bt_value *params)
56a1cced 1920{
f280892e 1921 struct ctf_fs_component *ctf_fs = NULL;
f280892e
SM
1922 guint i;
1923 const bt_value *paths_value;
98903a3e
PP
1924 bt_self_component *self_comp =
1925 bt_self_component_source_as_self_component(self_comp_src);
56a1cced 1926
98903a3e 1927 ctf_fs = ctf_fs_component_create(bt_component_get_logging_level(
4c65a157 1928 bt_self_component_as_component(self_comp)), self_comp);
d907165c 1929 if (!ctf_fs) {
f280892e
SM
1930 goto error;
1931 }
1932
d907165c 1933 if (!read_src_fs_parameters(params, &paths_value, ctf_fs)) {
f280892e 1934 goto error;
56a1cced
JG
1935 }
1936
98903a3e 1937 bt_self_component_set_data(self_comp, ctf_fs);
4c65a157
PP
1938 ctf_fs->self_comp = self_comp;
1939 ctf_fs->self_comp_src = self_comp_src;
56a1cced 1940
98903a3e 1941 if (ctf_fs_component_create_ctf_fs_traces(self_comp_src, ctf_fs, paths_value)) {
4f1f88a6
PP
1942 goto error;
1943 }
1944
f280892e
SM
1945 for (i = 0; i < ctf_fs->traces->len; i++) {
1946 struct ctf_fs_trace *trace = g_ptr_array_index(ctf_fs->traces, i);
599faa1c 1947
f280892e
SM
1948 if (create_streams_for_trace(trace)) {
1949 goto error;
1950 }
1951
1952 if (create_ports_for_trace(ctf_fs, trace)) {
1953 goto error;
1954 }
4f1f88a6
PP
1955 }
1956
1ef09eb5
JG
1957 goto end;
1958
56a1cced 1959error:
1a9f7075 1960 ctf_fs_destroy(ctf_fs);
e7a4393b 1961 ctf_fs = NULL;
98903a3e 1962 bt_self_component_set_data(self_comp, NULL);
1a9f7075 1963
1ef09eb5 1964end:
56a1cced
JG
1965 return ctf_fs;
1966}
1967
ea0b4b9e 1968BT_HIDDEN
d24d5663 1969bt_component_class_init_method_status ctf_fs_init(
b19ff26f 1970 bt_self_component_source *self_comp,
c88dd1cb 1971 const bt_value *params, __attribute__((unused)) void *init_method_data)
ea0b4b9e
JG
1972{
1973 struct ctf_fs_component *ctf_fs;
d24d5663
PP
1974 bt_component_class_init_method_status ret =
1975 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
ea0b4b9e 1976
d94d92ac 1977 ctf_fs = ctf_fs_create(self_comp, params);
ea0b4b9e 1978 if (!ctf_fs) {
d24d5663 1979 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
ea0b4b9e 1980 }
4c1456f0 1981
ea0b4b9e
JG
1982 return ret;
1983}
33f93973
PP
1984
1985BT_HIDDEN
d24d5663 1986bt_component_class_query_method_status ctf_fs_query(
b19ff26f
PP
1987 bt_self_component_class_source *comp_class,
1988 const bt_query_executor *query_exec,
1989 const char *object, const bt_value *params,
98903a3e 1990 bt_logging_level log_level,
b19ff26f 1991 const bt_value **result)
33f93973 1992{
d24d5663
PP
1993 bt_component_class_query_method_status status =
1994 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
33f93973 1995
04c0ba87 1996 if (!strcmp(object, "metadata-info")) {
98903a3e
PP
1997 status = metadata_info_query(comp_class, params, log_level,
1998 result);
97ade20b 1999 } else if (!strcmp(object, "trace-info")) {
98903a3e
PP
2000 status = trace_info_query(comp_class, params, log_level,
2001 result);
33f93973 2002 } else {
55314f2a 2003 BT_LOGE("Unknown query object `%s`", object);
d24d5663 2004 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_OBJECT;
04c0ba87 2005 goto end;
33f93973 2006 }
33f93973 2007end:
d94d92ac 2008 return status;
33f93973 2009}
This page took 0.17153 seconds and 4 git commands to generate.