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