Do not use `bool` type; use new `bt_bool` instead
[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 *
f3bc2010 6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7a278c8e
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
5b29e799 29#include <babeltrace/ctf-ir/packet.h>
ac0c6bdd 30#include <babeltrace/ctf-ir/clock-class.h>
4f1f88a6 31#include <babeltrace/graph/private-port.h>
b2e0c907 32#include <babeltrace/graph/private-component.h>
4f1f88a6
PP
33#include <babeltrace/graph/private-component-source.h>
34#include <babeltrace/graph/private-notification-iterator.h>
b2e0c907
PP
35#include <babeltrace/graph/component.h>
36#include <babeltrace/graph/notification-iterator.h>
599faa1c 37#include <babeltrace/graph/clock-class-priority-map.h>
7d61fa8e 38#include <plugins-common.h>
ea0b4b9e
JG
39#include <glib.h>
40#include <assert.h>
c55a9f58 41#include <stdbool.h>
56a1cced
JG
42#include <unistd.h>
43#include "fs.h"
413bc2c4
JG
44#include "metadata.h"
45#include "data-stream.h"
e7a4393b 46#include "file.h"
1e649dff 47#include "../common/metadata/decoder.h"
e7a4393b
JG
48
49#define PRINT_ERR_STREAM ctf_fs->error_fp
50#define PRINT_PREFIX "ctf-fs"
51#include "print.h"
33f93973 52#define METADATA_TEXT_SIG "/* CTF 1.8"
ea0b4b9e 53
78bb6992
MD
54BT_HIDDEN
55bool ctf_fs_debug;
ea0b4b9e 56
41a2b7ae 57struct bt_notification_iterator_next_return ctf_fs_iterator_next(
4f1f88a6 58 struct bt_private_notification_iterator *iterator)
ea0b4b9e 59{
4f1f88a6
PP
60 struct ctf_fs_stream *fs_stream =
61 bt_private_notification_iterator_get_user_data(iterator);
d01e0f33 62
4f1f88a6 63 return ctf_fs_stream_next(fs_stream);
ea0b4b9e 64}
bfd20a42 65
4f1f88a6 66void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it)
760051fa 67{
4f1f88a6
PP
68 void *ctf_fs_stream =
69 bt_private_notification_iterator_get_user_data(it);
760051fa 70
4f1f88a6 71 ctf_fs_stream_destroy(ctf_fs_stream);
760051fa
JG
72}
73
4f1f88a6
PP
74enum bt_notification_iterator_status ctf_fs_iterator_init(
75 struct bt_private_notification_iterator *it,
76 struct bt_private_port *port)
4c1456f0 77{
4f1f88a6
PP
78 struct ctf_fs_stream *stream = NULL;
79 struct ctf_fs_component *ctf_fs;
80 struct ctf_fs_port_data *port_data;
81 struct bt_private_component *priv_comp =
82 bt_private_notification_iterator_get_private_component(it);
83 enum bt_notification_iterator_status ret =
84 BT_NOTIFICATION_ITERATOR_STATUS_OK;
760051fa 85
4f1f88a6 86 assert(priv_comp);
5b29e799 87
4f1f88a6
PP
88 ctf_fs = bt_private_component_get_user_data(priv_comp);
89 if (!ctf_fs) {
fe8ad2b6 90 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
4f1f88a6 91 goto error;
760051fa
JG
92 }
93
4f1f88a6
PP
94 port_data = bt_private_port_get_user_data(port);
95 if (!port_data) {
fe8ad2b6 96 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
4f1f88a6
PP
97 goto error;
98 }
5b29e799 99
4f1f88a6 100 stream = ctf_fs_stream_create(ctf_fs, port_data->path->str);
5b29e799 101 if (!stream) {
4f1f88a6 102 goto error;
760051fa
JG
103 }
104
4f1f88a6
PP
105 ret = bt_private_notification_iterator_set_user_data(it, stream);
106 if (ret) {
107 goto error;
760051fa
JG
108 }
109
4f1f88a6
PP
110 stream = NULL;
111 goto end;
5b29e799 112
4f1f88a6
PP
113error:
114 (void) bt_private_notification_iterator_set_user_data(it, NULL);
115
116 if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
117 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
760051fa 118 }
5b29e799 119
760051fa 120end:
4f1f88a6
PP
121 ctf_fs_stream_destroy(stream);
122 bt_put(priv_comp);
760051fa
JG
123 return ret;
124}
125
760051fa 126static
4f1f88a6 127void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs)
760051fa 128{
4f1f88a6 129 if (!ctf_fs) {
8fa760ba
JG
130 return;
131 }
4f1f88a6
PP
132
133 if (ctf_fs->trace_path) {
134 g_string_free(ctf_fs->trace_path, TRUE);
56a1cced 135 }
4f1f88a6
PP
136
137 if (ctf_fs->port_data) {
138 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 139 }
760051fa 140
4f1f88a6
PP
141 if (ctf_fs->metadata) {
142 ctf_fs_metadata_fini(ctf_fs->metadata);
143 g_free(ctf_fs->metadata);
144 }
760051fa 145
599faa1c 146 bt_put(ctf_fs->cc_prio_map);
4f1f88a6 147 g_free(ctf_fs);
4c1456f0
JG
148}
149
4f1f88a6 150void ctf_fs_finalize(struct bt_private_component *component)
a4792757 151{
4f1f88a6
PP
152 void *data = bt_private_component_get_user_data(component);
153
154 ctf_fs_destroy_data(data);
a4792757
JG
155}
156
e7a4393b 157static
4f1f88a6
PP
158void port_data_destroy(void *data) {
159 struct ctf_fs_port_data *port_data = data;
160
161 if (!port_data) {
162 return;
163 }
164
165 if (port_data->path) {
166 g_string_free(port_data->path, TRUE);
167 }
168
169 g_free(port_data);
5b29e799
JG
170}
171
172static
4f1f88a6
PP
173int create_one_port(struct ctf_fs_component *ctf_fs,
174 const char *stream_basename, const char *stream_path)
5b29e799 175{
4f1f88a6
PP
176 int ret = 0;
177 struct bt_private_port *port = NULL;
178 struct ctf_fs_port_data *port_data = NULL;
179 GString *port_name = NULL;
180
181 port_name = g_string_new(NULL);
182 if (!port_name) {
183 goto error;
184 }
185
186 /* Assign the name for the new output port */
187 g_string_assign(port_name, "");
188 g_string_printf(port_name, "trace0-stream-%s", stream_basename);
189 PDBG("Creating one port named `%s` associated with path `%s`\n",
190 port_name->str, stream_path);
191
192 /* Create output port for this file */
4f1f88a6
PP
193 port_data = g_new0(struct ctf_fs_port_data, 1);
194 if (!port_data) {
195 goto error;
196 }
197
198 port_data->path = g_string_new(stream_path);
199 if (!port_data->path) {
200 goto error;
201 }
202
3e9b0023
PP
203 port = bt_private_component_source_add_output_private_port(
204 ctf_fs->priv_comp, port_name->str, port_data);
205 if (!port) {
4f1f88a6
PP
206 goto error;
207 }
208
209 g_ptr_array_add(ctf_fs->port_data, port_data);
210 port_data = NULL;
211 goto end;
212
213error:
214 ret = -1;
215
216end:
217 if (port_name) {
218 g_string_free(port_name, TRUE);
219 }
220
221 bt_put(port);
222 port_data_destroy(port_data);
223 return ret;
5b29e799
JG
224}
225
226static
4f1f88a6 227int create_ports(struct ctf_fs_component *ctf_fs)
e7a4393b
JG
228{
229 int ret = 0;
4f1f88a6 230 const char *basename;
e7a4393b 231 GError *error = NULL;
4f1f88a6 232 GDir *dir = NULL;
4f1f88a6 233 struct ctf_fs_file *file = NULL;
e7a4393b 234
4f1f88a6
PP
235 /* Create one output port for each stream file */
236 dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
e7a4393b 237 if (!dir) {
4f1f88a6
PP
238 PERR("Cannot open directory `%s`: %s (code %d)\n",
239 ctf_fs->trace_path->str, error->message,
240 error->code);
e7a4393b
JG
241 goto error;
242 }
243
4f1f88a6
PP
244 while ((basename = g_dir_read_name(dir))) {
245 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
e7a4393b 246 /* Ignore the metadata stream. */
4f1f88a6 247 PDBG("Ignoring metadata file `%s`\n", basename);
e7a4393b
JG
248 continue;
249 }
250
4f1f88a6
PP
251 if (basename[0] == '.') {
252 PDBG("Ignoring hidden file `%s`\n", basename);
e7a4393b
JG
253 continue;
254 }
255
256 /* Create the file. */
257 file = ctf_fs_file_create(ctf_fs);
258 if (!file) {
4f1f88a6
PP
259 PERR("Cannot create stream file object for file `%s`\n",
260 basename);
e7a4393b
JG
261 goto error;
262 }
263
264 /* Create full path string. */
265 g_string_append_printf(file->path, "%s/%s",
4f1f88a6 266 ctf_fs->trace_path->str, basename);
e7a4393b 267 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
4f1f88a6 268 PDBG("Ignoring non-regular file `%s`\n", basename);
e7a4393b 269 ctf_fs_file_destroy(file);
4f1f88a6 270 file = NULL;
e7a4393b
JG
271 continue;
272 }
273
4f1f88a6
PP
274 ret = ctf_fs_file_open(ctf_fs, file, "rb");
275 if (ret) {
276 PERR("Cannot open stream file `%s`\n", basename);
e7a4393b
JG
277 goto error;
278 }
279
9fa0891a
JG
280 if (file->size == 0) {
281 /* Skip empty stream. */
4f1f88a6 282 PDBG("Ignoring empty file `%s`\n", basename);
9fa0891a 283 ctf_fs_file_destroy(file);
4f1f88a6 284 file = NULL;
9fa0891a
JG
285 continue;
286 }
287
4f1f88a6
PP
288 ret = create_one_port(ctf_fs, basename, file->path->str);
289 if (ret) {
290 PERR("Cannot create output port for file `%s`\n",
291 basename);
e7a4393b
JG
292 goto error;
293 }
294
4f1f88a6
PP
295 ctf_fs_file_destroy(file);
296 file = NULL;
e7a4393b
JG
297 }
298
299 goto end;
4f1f88a6 300
e7a4393b
JG
301error:
302 ret = -1;
4f1f88a6 303
e7a4393b
JG
304end:
305 if (dir) {
306 g_dir_close(dir);
307 dir = NULL;
308 }
4f1f88a6 309
e7a4393b
JG
310 if (error) {
311 g_error_free(error);
312 }
5b29e799 313
4f1f88a6 314 ctf_fs_file_destroy(file);
91457551 315 return ret;
5b29e799
JG
316}
317
599faa1c
PP
318static
319int create_cc_prio_map(struct ctf_fs_component *ctf_fs)
320{
321 int ret = 0;
322 size_t i;
323 int count;
324
325 assert(ctf_fs);
326 ctf_fs->cc_prio_map = bt_clock_class_priority_map_create();
327 if (!ctf_fs->cc_prio_map) {
328 ret = -1;
329 goto end;
330 }
331
332 count = bt_ctf_trace_get_clock_class_count(ctf_fs->metadata->trace);
333 assert(count >= 0);
334
335 for (i = 0; i < count; i++) {
336 struct bt_ctf_clock_class *clock_class =
9ac68eb1
PP
337 bt_ctf_trace_get_clock_class_by_index(
338 ctf_fs->metadata->trace, i);
599faa1c
PP
339
340 assert(clock_class);
341 ret = bt_clock_class_priority_map_add_clock_class(
342 ctf_fs->cc_prio_map, clock_class, 0);
343 BT_PUT(clock_class);
344
345 if (ret) {
346 goto end;
347 }
348 }
349
350end:
351 return ret;
352}
353
5b29e799 354static
4f1f88a6
PP
355struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
356 struct bt_value *params)
56a1cced
JG
357{
358 struct ctf_fs_component *ctf_fs;
1ef09eb5 359 struct bt_value *value = NULL;
56a1cced 360 const char *path;
4f1f88a6 361 int ret;
56a1cced
JG
362
363 ctf_fs = g_new0(struct ctf_fs_component, 1);
364 if (!ctf_fs) {
365 goto end;
366 }
367
4f1f88a6
PP
368 /*
369 * We don't need to get a new reference here because as long as
370 * our private ctf_fs_component object exists, the containing
371 * private component should also exist.
372 */
373 ctf_fs->priv_comp = priv_comp;
374
56a1cced
JG
375 /* FIXME: should probably look for a source URI */
376 value = bt_value_map_get(params, "path");
377 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
378 goto error;
379 }
380
381 ret = bt_value_string_get(value, &path);
4f1f88a6
PP
382 if (ret) {
383 goto error;
384 }
385
386 ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy);
387 if (!ctf_fs->port_data) {
56a1cced
JG
388 goto error;
389 }
390
391 ctf_fs->trace_path = g_string_new(path);
392 if (!ctf_fs->trace_path) {
92540773 393 BT_PUT(value);
56a1cced
JG
394 goto error;
395 }
92540773
JD
396 bt_put(value);
397
398 value = bt_value_map_get(params, "offset-s");
399 if (value) {
400 int64_t offset;
401
402 if (!bt_value_is_integer(value)) {
403 fprintf(stderr,
404 "offset-s should be an integer\n");
405 goto error;
406 }
407 ret = bt_value_integer_get(value, &offset);
408 if (ret != BT_VALUE_STATUS_OK) {
409 fprintf(stderr,
410 "Failed to get offset-s value\n");
411 goto error;
412 }
413 ctf_fs->options.clock_offset = offset;
414 bt_put(value);
415 }
416
417 value = bt_value_map_get(params, "offset-ns");
418 if (value) {
419 int64_t offset;
420
421 if (!bt_value_is_integer(value)) {
422 fprintf(stderr,
423 "offset-ns should be an integer\n");
424 goto error;
425 }
426 ret = bt_value_integer_get(value, &offset);
427 if (ret != BT_VALUE_STATUS_OK) {
428 fprintf(stderr,
429 "Failed to get offset-ns value\n");
430 goto error;
431 }
432 ctf_fs->options.clock_offset_ns = offset;
433 bt_put(value);
434 }
435
56a1cced
JG
436 ctf_fs->error_fp = stderr;
437 ctf_fs->page_size = (size_t) getpagesize();
e7a4393b
JG
438
439 // FIXME: check error.
5b29e799
JG
440 ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1);
441 if (!ctf_fs->metadata) {
e7a4393b
JG
442 goto error;
443 }
4f1f88a6
PP
444
445 ret = ctf_fs_metadata_set_trace(ctf_fs);
446 if (ret) {
447 goto error;
448 }
449
599faa1c
PP
450 ret = create_cc_prio_map(ctf_fs);
451 if (ret) {
452 goto error;
453 }
454
4f1f88a6
PP
455 ret = create_ports(ctf_fs);
456 if (ret) {
457 goto error;
458 }
459
1ef09eb5
JG
460 goto end;
461
56a1cced
JG
462error:
463 ctf_fs_destroy_data(ctf_fs);
e7a4393b 464 ctf_fs = NULL;
1ef09eb5 465end:
56a1cced
JG
466 return ctf_fs;
467}
468
ea0b4b9e 469BT_HIDDEN
4f1f88a6 470enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
7d61fa8e 471 struct bt_value *params, UNUSED_VAR void *init_method_data)
ea0b4b9e
JG
472{
473 struct ctf_fs_component *ctf_fs;
474 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
475
4f1f88a6 476 assert(priv_comp);
ea0b4b9e 477 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
4f1f88a6 478 ctf_fs = ctf_fs_create(priv_comp, params);
ea0b4b9e
JG
479 if (!ctf_fs) {
480 ret = BT_COMPONENT_STATUS_NOMEM;
481 goto end;
482 }
4c1456f0 483
4f1f88a6 484 ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
ea0b4b9e
JG
485 if (ret != BT_COMPONENT_STATUS_OK) {
486 goto error;
487 }
ea0b4b9e
JG
488end:
489 return ret;
490error:
4f1f88a6 491 (void) bt_private_component_set_user_data(priv_comp, NULL);
760051fa 492 ctf_fs_destroy_data(ctf_fs);
ea0b4b9e
JG
493 return ret;
494}
33f93973
PP
495
496BT_HIDDEN
a67681c1
PP
497struct bt_value *ctf_fs_query(struct bt_component_class *comp_class,
498 const char *object, struct bt_value *params)
33f93973
PP
499{
500 struct bt_value *results = NULL;
501 struct bt_value *path_value = NULL;
502 char *metadata_text = NULL;
503 FILE *metadata_fp = NULL;
504 GString *g_metadata_text = NULL;
505
a67681c1 506 if (strcmp(object, "metadata-info") == 0) {
33f93973
PP
507 int ret;
508 int bo;
509 const char *path;
510 bool is_packetized;
511
512 results = bt_value_map_create();
513 if (!results) {
514 goto error;
515 }
516
517 if (!bt_value_is_map(params)) {
518 fprintf(stderr,
a67681c1 519 "Query parameters is not a map value object\n");
33f93973
PP
520 goto error;
521 }
522
523 path_value = bt_value_map_get(params, "path");
524 ret = bt_value_string_get(path_value, &path);
525 if (ret) {
526 fprintf(stderr,
527 "Cannot get `path` string parameter\n");
528 goto error;
529 }
530
531 assert(path);
532 metadata_fp = ctf_fs_metadata_open_file(path);
533 if (!metadata_fp) {
534 fprintf(stderr,
535 "Cannot open trace at path `%s`\n", path);
536 goto error;
537 }
538
1e649dff
PP
539 is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
540 &bo);
33f93973
PP
541
542 if (is_packetized) {
1e649dff
PP
543 ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
544 metadata_fp, &metadata_text, bo);
33f93973
PP
545 if (ret) {
546 fprintf(stderr,
547 "Cannot decode packetized metadata file\n");
548 goto error;
549 }
550 } else {
551 long filesize;
552
553 fseek(metadata_fp, 0, SEEK_END);
554 filesize = ftell(metadata_fp);
555 rewind(metadata_fp);
556 metadata_text = malloc(filesize + 1);
557 if (!metadata_text) {
558 fprintf(stderr,
559 "Cannot allocate buffer for metadata text\n");
560 goto error;
561 }
562
563 if (fread(metadata_text, filesize, 1, metadata_fp) !=
564 1) {
565 fprintf(stderr,
566 "Cannot read metadata file\n");
567 goto error;
568 }
569
570 metadata_text[filesize] = '\0';
571 }
572
573 g_metadata_text = g_string_new(NULL);
574 if (!g_metadata_text) {
575 goto error;
576 }
577
578 if (strncmp(metadata_text, METADATA_TEXT_SIG,
579 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
580 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
581 g_string_append(g_metadata_text, " */\n\n");
582 }
583
584 g_string_append(g_metadata_text, metadata_text);
585
586 ret = bt_value_map_insert_string(results, "text",
587 g_metadata_text->str);
588 if (ret) {
589 fprintf(stderr, "Cannot insert metadata text into results\n");
590 goto error;
591 }
592
593 ret = bt_value_map_insert_bool(results, "is-packetized",
594 is_packetized);
595 if (ret) {
596 fprintf(stderr, "Cannot insert is packetized into results\n");
597 goto error;
598 }
599 } else {
a67681c1 600 fprintf(stderr, "Unknown query object `%s`\n", object);
33f93973
PP
601 goto error;
602 }
603
604 goto end;
605
606error:
607 BT_PUT(results);
608
609end:
610 bt_put(path_value);
611 free(metadata_text);
612
613 if (g_metadata_text) {
614 g_string_free(g_metadata_text, TRUE);
615 }
616
617 if (metadata_fp) {
618 fclose(metadata_fp);
619 }
620 return results;
621}
This page took 0.057699 seconds and 4 git commands to generate.