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