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