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