Writer fix: don't skip first notification
[babeltrace.git] / plugins / ctf / fs / 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
ea0b4b9e 29#include <babeltrace/plugin/plugin-system.h>
5b29e799 30#include <babeltrace/ctf-ir/packet.h>
a4792757 31#include <babeltrace/ctf-ir/clock.h>
760051fa 32#include <babeltrace/plugin/notification/iterator.h>
5b29e799
JG
33#include <babeltrace/plugin/notification/stream.h>
34#include <babeltrace/plugin/notification/event.h>
35#include <babeltrace/plugin/notification/packet.h>
36#include <babeltrace/plugin/notification/heap.h>
ea0b4b9e
JG
37#include <glib.h>
38#include <assert.h>
56a1cced
JG
39#include <unistd.h>
40#include "fs.h"
413bc2c4
JG
41#include "metadata.h"
42#include "data-stream.h"
e7a4393b
JG
43#include "file.h"
44
45#define PRINT_ERR_STREAM ctf_fs->error_fp
46#define PRINT_PREFIX "ctf-fs"
47#include "print.h"
ea0b4b9e 48
78bb6992
MD
49BT_HIDDEN
50bool ctf_fs_debug;
ea0b4b9e 51
f48bc732
JG
52static
53enum bt_notification_iterator_status ctf_fs_iterator_next(
54 struct bt_notification_iterator *iterator);
55
ea0b4b9e 56static
760051fa
JG
57struct bt_notification *ctf_fs_iterator_get(
58 struct bt_notification_iterator *iterator)
ea0b4b9e 59{
5b29e799
JG
60 struct ctf_fs_iterator *ctf_it =
61 bt_notification_iterator_get_private_data(iterator);
d01e0f33 62
f48bc732
JG
63 if (!ctf_it->current_notification) {
64 (void) ctf_fs_iterator_next(iterator);
65 }
66
5b29e799 67 return bt_get(ctf_it->current_notification);
ea0b4b9e
JG
68}
69
70static
5b29e799
JG
71enum bt_notification_iterator_status ctf_fs_iterator_get_next_notification(
72 struct ctf_fs_iterator *it,
73 struct ctf_fs_stream *stream,
74 struct bt_notification **notification)
ea0b4b9e 75{
5b29e799 76 enum bt_ctf_notif_iter_status status;
d01e0f33 77 enum bt_notification_iterator_status ret;
d01e0f33 78
5b29e799
JG
79 if (stream->end_reached) {
80 status = BT_CTF_NOTIF_ITER_STATUS_EOF;
d01e0f33
JG
81 goto end;
82 }
83
5b29e799
JG
84 status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter,
85 notification);
86 if (status != BT_CTF_NOTIF_ITER_STATUS_OK &&
87 status != BT_CTF_NOTIF_ITER_STATUS_EOF) {
d01e0f33
JG
88 goto end;
89 }
90
5b29e799
JG
91 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
92 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
93 *notification = bt_notification_stream_end_create(
94 stream->stream);
95 if (!*notification) {
96 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
97 }
98 status = BT_CTF_NOTIF_ITER_STATUS_OK;
99 stream->end_reached = true;
100 }
d01e0f33 101end:
5b29e799
JG
102 switch (status) {
103 case BT_CTF_NOTIF_ITER_STATUS_EOF:
104 ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
105 break;
106 case BT_CTF_NOTIF_ITER_STATUS_OK:
107 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
108 break;
109 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
110 /*
111 * Should not make it this far as this is medium-specific;
112 * there is nothing for the user to do and it should have been
113 * handled upstream.
114 */
115 assert(0);
116 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
117 /* No argument provided by the user, so don't return INVAL. */
118 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
119 default:
120 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
121 break;
122 }
043e2020 123 return ret;
ea0b4b9e 124}
bfd20a42 125
5b29e799
JG
126/*
127 * Remove me. This is a temporary work-around due to our inhability to use
128 * libbabeltrace-ctf from libbabeltrace-plugin.
129 */
760051fa 130static
5b29e799
JG
131struct bt_ctf_stream *internal_bt_notification_get_stream(
132 struct bt_notification *notification)
760051fa 133{
5b29e799
JG
134 struct bt_ctf_stream *stream = NULL;
135
136 assert(notification);
137 switch (bt_notification_get_type(notification)) {
138 case BT_NOTIFICATION_TYPE_EVENT:
139 {
140 struct bt_ctf_event *event;
141
142 event = bt_notification_event_get_event(notification);
143 stream = bt_ctf_event_get_stream(event);
144 bt_put(event);
145 break;
146 }
147 case BT_NOTIFICATION_TYPE_PACKET_START:
148 {
149 struct bt_ctf_packet *packet;
150
151 packet = bt_notification_packet_start_get_packet(notification);
152 stream = bt_ctf_packet_get_stream(packet);
153 bt_put(packet);
154 break;
155 }
156 case BT_NOTIFICATION_TYPE_PACKET_END:
157 {
158 struct bt_ctf_packet *packet;
159
160 packet = bt_notification_packet_end_get_packet(notification);
161 stream = bt_ctf_packet_get_stream(packet);
162 bt_put(packet);
163 break;
164 }
165 case BT_NOTIFICATION_TYPE_STREAM_END:
166 stream = bt_notification_stream_end_get_stream(notification);
167 break;
168 default:
169 goto end;
170 }
171end:
172 return stream;
760051fa
JG
173}
174
175static
5b29e799 176enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it)
760051fa 177{
5b29e799
JG
178 size_t i, pending_streams_count = it->pending_streams->len;
179 enum bt_notification_iterator_status ret =
180 BT_NOTIFICATION_ITERATOR_STATUS_OK;
181
182 /* Insert one stream-associated notification for each stream. */
183 for (i = 0; i < pending_streams_count; i++) {
184 struct bt_notification *notification;
185 struct ctf_fs_stream *fs_stream;
186 struct bt_ctf_stream *stream;
187 size_t pending_stream_index = pending_streams_count - 1 - i;
188
189 fs_stream = g_ptr_array_index(it->pending_streams,
190 pending_stream_index);
191
192 do {
193 int heap_ret;
194
195 ret = ctf_fs_iterator_get_next_notification(
196 it, fs_stream, &notification);
197 if (ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END) {
198 printf_debug("Failed to populate heap at stream %zu\n",
199 pending_stream_index);
200 goto end;
201 }
202
203 stream = internal_bt_notification_get_stream(
204 notification);
205 if (stream) {
206 gboolean inserted;
207
208 /*
209 * Associate pending ctf_fs_stream to
210 * bt_ctf_stream. Ownership of stream
211 * is passed to the stream ht.
212 */
213 inserted = g_hash_table_insert(it->stream_ht,
214 stream, fs_stream);
215 if (!inserted) {
216 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
217 printf_debug("Failed to associate fs stream to ctf stream\n");
218 goto end;
219 }
220 }
221
222 heap_ret = bt_notification_heap_insert(
223 it->pending_notifications,
224 notification);
225 bt_put(notification);
226 if (heap_ret) {
227 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
228 printf_debug("Failed to insert notification in heap\n");
229 goto end;
230 }
231 } while (!stream && ret != BT_NOTIFICATION_ITERATOR_STATUS_END);
232 /*
233 * Set NULL so the destruction callback registered with the
234 * array is not invoked on the stream (its ownership was
235 * transferred to the streams hashtable).
236 */
237 g_ptr_array_index(it->pending_streams,
238 pending_stream_index) = NULL;
239 g_ptr_array_remove_index(it->pending_streams,
240 pending_stream_index);
241 }
760051fa 242
5b29e799
JG
243 g_ptr_array_free(it->pending_streams, TRUE);
244 it->pending_streams = NULL;
245end:
246 return ret;
760051fa
JG
247}
248
249static
5b29e799
JG
250enum bt_notification_iterator_status ctf_fs_iterator_next(
251 struct bt_notification_iterator *iterator)
4c1456f0 252{
5b29e799 253 int heap_ret;
a11bd504 254 struct bt_ctf_stream *stream = NULL;
5b29e799
JG
255 struct ctf_fs_stream *fs_stream;
256 struct bt_notification *notification;
257 struct bt_notification *next_stream_notification;
258 enum bt_notification_iterator_status ret =
259 BT_NOTIFICATION_ITERATOR_STATUS_OK;
260 struct ctf_fs_iterator *ctf_it =
261 bt_notification_iterator_get_private_data(iterator);
262
263 notification = bt_notification_heap_pop(ctf_it->pending_notifications);
264 if (!notification && !ctf_it->pending_streams) {
265 ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
760051fa
JG
266 goto end;
267 }
268
5b29e799
JG
269 if (!notification && ctf_it->pending_streams) {
270 /*
271 * Insert at one notification per stream in the heap and pop
272 * one.
273 */
274 ret = populate_heap(ctf_it);
275 if (ret) {
276 goto end;
277 }
278
279 notification = bt_notification_heap_pop(
280 ctf_it->pending_notifications);
281 if (!notification) {
282 ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
283 goto end;
284 }
760051fa
JG
285 }
286
5b29e799
JG
287 /* notification is set from here. */
288
289 stream = internal_bt_notification_get_stream(notification);
290 if (!stream) {
291 /*
292 * The current notification is not associated to a particular
293 * stream, there is no need to insert a new notification from
294 * a stream in the heap.
295 */
296 goto end;
760051fa
JG
297 }
298
5b29e799
JG
299 fs_stream = g_hash_table_lookup(ctf_it->stream_ht, stream);
300 if (!fs_stream) {
301 /* We have reached this stream's end. */
302 goto end;
760051fa
JG
303 }
304
5b29e799
JG
305 ret = ctf_fs_iterator_get_next_notification(ctf_it, fs_stream,
306 &next_stream_notification);
307 if ((ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END)) {
308 heap_ret = bt_notification_heap_insert(
309 ctf_it->pending_notifications, notification);
310
311 assert(!next_stream_notification);
312 if (heap_ret) {
313 /*
314 * We're dropping the most recent notification, but at
315 * this point, something is seriously wrong...
316 */
317 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
318 }
319 BT_PUT(notification);
320 goto end;
321 }
322
323 if (ret == BT_NOTIFICATION_ITERATOR_STATUS_END) {
324 gboolean success;
325
326 /* Remove stream. */
327 success = g_hash_table_remove(ctf_it->stream_ht, stream);
328 assert(success);
329 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
330 } else {
331 heap_ret = bt_notification_heap_insert(ctf_it->pending_notifications,
332 next_stream_notification);
333 BT_PUT(next_stream_notification);
334 if (heap_ret) {
335 /*
336 * We're dropping the most recent notification...
337 */
338 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
339 }
760051fa 340 }
5b29e799
JG
341
342 /*
343 * Ensure that the stream is removed from both pending_streams and
344 * the streams hashtable on reception of the "end of stream"
345 * notification.
346 */
760051fa 347end:
5b29e799 348 BT_MOVE(ctf_it->current_notification, notification);
33bf10b7 349 bt_put(stream);
760051fa
JG
350 return ret;
351}
352
760051fa 353static
5b29e799 354void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it)
760051fa 355{
5b29e799
JG
356 bt_put(ctf_it->current_notification);
357 bt_put(ctf_it->pending_notifications);
358 if (ctf_it->pending_streams) {
359 g_ptr_array_free(ctf_it->pending_streams, TRUE);
56a1cced 360 }
5b29e799
JG
361 if (ctf_it->stream_ht) {
362 g_hash_table_destroy(ctf_it->stream_ht);
c14d7e26 363 }
5b29e799 364 g_free(ctf_it);
760051fa
JG
365}
366
367static
5b29e799 368void ctf_fs_iterator_destroy(struct bt_notification_iterator *it)
760051fa 369{
5b29e799 370 void *data = bt_notification_iterator_get_private_data(it);
760051fa 371
5b29e799 372 ctf_fs_iterator_destroy_data(data);
4c1456f0
JG
373}
374
a4792757
JG
375static
376bool compare_event_notifications(struct bt_notification *a,
377 struct bt_notification *b)
378{
379 int ret;
380 struct bt_ctf_clock *clock;
381 struct bt_ctf_clock_value *a_clock_value, *b_clock_value;
382 struct bt_ctf_stream_class *a_stream_class;
383 struct bt_ctf_stream *a_stream;
384 struct bt_ctf_event *a_event, *b_event;
385 struct bt_ctf_trace *trace;
386 int64_t a_ts, b_ts;
387
388 // FIXME - assumes only one clock
389 a_event = bt_notification_event_get_event(a);
390 b_event = bt_notification_event_get_event(b);
391 assert(a_event);
392 assert(b_event);
393
394 a_stream = bt_ctf_event_get_stream(a_event);
395 assert(a_stream);
396 a_stream_class = bt_ctf_stream_get_class(a_stream);
397 assert(a_stream_class);
398 trace = bt_ctf_stream_class_get_trace(a_stream_class);
399 assert(trace);
400
401 clock = bt_ctf_trace_get_clock(trace, 0);
402 a_clock_value = bt_ctf_event_get_clock_value(a_event, clock);
403 b_clock_value = bt_ctf_event_get_clock_value(b_event, clock);
404 assert(a_clock_value);
405 assert(b_clock_value);
406
407 ret = bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value, &a_ts);
408 assert(!ret);
409 ret = bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value, &b_ts);
410 assert(!ret);
411
412 bt_put(a_event);
413 bt_put(b_event);
414 bt_put(a_clock_value);
415 bt_put(b_clock_value);
416 bt_put(a_stream);
417 bt_put(a_stream_class);
418 bt_put(clock);
419 bt_put(trace);
420 return a_ts < b_ts;
421}
422
e7a4393b 423static
5b29e799
JG
424bool compare_notifications(struct bt_notification *a, struct bt_notification *b,
425 void *unused)
426{
a4792757
JG
427 static int notification_priorities[] = {
428 [BT_NOTIFICATION_TYPE_NEW_TRACE] = 0,
429 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS] = 1,
430 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS] = 2,
431 [BT_NOTIFICATION_TYPE_PACKET_START] = 3,
432 [BT_NOTIFICATION_TYPE_PACKET_END] = 4,
433 [BT_NOTIFICATION_TYPE_EVENT] = 5,
434 [BT_NOTIFICATION_TYPE_END_OF_TRACE] = 6,
435 };
436 int a_prio, b_prio;
437 enum bt_notification_type a_type, b_type;
438
439 assert(a && b);
440 a_type = bt_notification_get_type(a);
441 b_type = bt_notification_get_type(b);
442 assert(a_type > BT_NOTIFICATION_TYPE_ALL);
443 assert(a_type < BT_NOTIFICATION_TYPE_NR);
444 assert(b_type > BT_NOTIFICATION_TYPE_ALL);
445 assert(b_type < BT_NOTIFICATION_TYPE_NR);
446
447 a_prio = notification_priorities[a_type];
448 b_prio = notification_priorities[b_type];
449
450 if (likely((a_type == b_type) && a_type == BT_NOTIFICATION_TYPE_EVENT)) {
451 return compare_event_notifications(a, b);
452 }
453
454 if (unlikely(a_prio != b_prio)) {
455 return a_prio < b_prio;
456 }
457
458 /* Notification types are equal, but not of type "event". */
459 switch (a_type) {
460 case BT_NOTIFICATION_TYPE_PACKET_START:
461 case BT_NOTIFICATION_TYPE_PACKET_END:
462 case BT_NOTIFICATION_TYPE_STREAM_END:
463 {
464 int64_t a_sc_id, b_sc_id;
465 struct bt_ctf_stream *a_stream, *b_stream;
466 struct bt_ctf_stream_class *a_sc, *b_sc;
467
468 a_stream = internal_bt_notification_get_stream(a);
469 b_stream = internal_bt_notification_get_stream(b);
470 assert(a_stream && b_stream);
471
472 a_sc = bt_ctf_stream_get_class(a_stream);
473 b_sc = bt_ctf_stream_get_class(b_stream);
474 assert(a_sc && b_sc);
475
476 a_sc_id = bt_ctf_stream_class_get_id(a_sc);
477 b_sc_id = bt_ctf_stream_class_get_id(b_sc);
478 assert(a_sc_id >= 0 && b_sc_id >= 0);
479 bt_put(a_sc);
480 bt_put(a_stream);
481 bt_put(b_sc);
482 bt_put(b_stream);
483 return a_sc_id < b_sc_id;
484 }
485 case BT_NOTIFICATION_TYPE_NEW_TRACE:
486 case BT_NOTIFICATION_TYPE_END_OF_TRACE:
487 /* Impossible to have two separate traces. */
488 default:
489 assert(0);
490 }
491
492 assert(0);
5b29e799
JG
493 return a < b;
494}
495
496static
497void stream_destroy(void *stream)
498{
499 ctf_fs_stream_destroy((struct ctf_fs_stream *) stream);
500}
501
502static
503int open_trace_streams(struct ctf_fs_component *ctf_fs,
504 struct ctf_fs_iterator *ctf_it)
e7a4393b
JG
505{
506 int ret = 0;
507 const char *name;
508 GError *error = NULL;
509 GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
510
511 if (!dir) {
512 PERR("Cannot open directory \"%s\": %s (code %d)\n",
513 ctf_fs->trace_path->str, error->message,
514 error->code);
515 goto error;
516 }
517
518 while ((name = g_dir_read_name(dir))) {
519 struct ctf_fs_file *file = NULL;
520 struct ctf_fs_stream *stream = NULL;
521
522 if (!strcmp(name, CTF_FS_METADATA_FILENAME)) {
523 /* Ignore the metadata stream. */
524 PDBG("Ignoring metadata file \"%s\"\n",
525 name);
526 continue;
527 }
528
529 if (name[0] == '.') {
530 PDBG("Ignoring hidden file \"%s\"\n",
531 name);
532 continue;
533 }
534
535 /* Create the file. */
536 file = ctf_fs_file_create(ctf_fs);
537 if (!file) {
538 PERR("Cannot create stream file object\n");
539 goto error;
540 }
541
542 /* Create full path string. */
543 g_string_append_printf(file->path, "%s/%s",
544 ctf_fs->trace_path->str, name);
545 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
546 PDBG("Ignoring non-regular file \"%s\"\n", name);
547 ctf_fs_file_destroy(file);
548 continue;
549 }
550
551 /* Open the file. */
552 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
553 ctf_fs_file_destroy(file);
554 goto error;
555 }
556
557 /* Create a private stream; file ownership is passed to it. */
558 stream = ctf_fs_stream_create(ctf_fs, file);
559 if (!stream) {
560 ctf_fs_file_destroy(file);
561 goto error;
562 }
563
5b29e799 564 g_ptr_array_add(ctf_it->pending_streams, stream);
e7a4393b
JG
565 }
566
567 goto end;
568error:
569 ret = -1;
570end:
571 if (dir) {
572 g_dir_close(dir);
573 dir = NULL;
574 }
575 if (error) {
576 g_error_free(error);
577 }
578 return ret;
579}
580
581static
5b29e799
JG
582enum bt_component_status ctf_fs_iterator_init(struct bt_component *source,
583 struct bt_notification_iterator *it)
e7a4393b 584{
5b29e799
JG
585 struct ctf_fs_iterator *ctf_it;
586 struct ctf_fs_component *ctf_fs;
587 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
588
589 assert(source && it);
590
591 ctf_fs = bt_component_get_private_data(source);
592 if (!ctf_fs) {
593 ret = BT_COMPONENT_STATUS_INVALID;
594 goto end;
595 }
596
597 ctf_it = g_new0(struct ctf_fs_iterator, 1);
598 if (!ctf_it) {
599 ret = BT_COMPONENT_STATUS_NOMEM;
600 goto end;
601 }
602
603 ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash,
604 g_direct_equal, bt_put, stream_destroy);
605 if (!ctf_it->stream_ht) {
606 goto error;
607 }
608 ctf_it->pending_streams = g_ptr_array_new_with_free_func(
609 stream_destroy);
610 if (!ctf_it->pending_streams) {
611 goto error;
612 }
613 ctf_it->pending_notifications = bt_notification_heap_create(
614 compare_notifications, NULL);
615 if (!ctf_it->pending_notifications) {
616 goto error;
617 }
618
619 ret = open_trace_streams(ctf_fs, ctf_it);
620 if (ret) {
621 goto error;
622 }
623
624 ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get);
625 if (ret) {
626 goto error;
627 }
628
629 ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next);
630 if (ret) {
631 goto error;
632 }
633
634 ret = bt_notification_iterator_set_destroy_cb(it,
635 ctf_fs_iterator_destroy);
636 if (ret) {
637 goto error;
638 }
639
640 ret = bt_notification_iterator_set_private_data(it, ctf_it);
641 if (ret) {
642 goto error;
643 }
f48bc732 644
5b29e799
JG
645end:
646 return ret;
647error:
648 (void) bt_notification_iterator_set_private_data(it, NULL);
649 ctf_fs_iterator_destroy_data(ctf_it);
650 goto end;
651}
652
653static
654void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs)
655{
656 if (ctf_fs->trace_path) {
657 g_string_free(ctf_fs->trace_path, TRUE);
658 }
659 if (ctf_fs->metadata) {
660 ctf_fs_metadata_fini(ctf_fs->metadata);
661 g_free(ctf_fs->metadata);
662 }
663 g_free(ctf_fs);
664}
665
666static
667void ctf_fs_destroy(struct bt_component *component)
668{
669 void *data = bt_component_get_private_data(component);
670
671 ctf_fs_destroy_data(data);
e7a4393b
JG
672}
673
56a1cced
JG
674static
675struct ctf_fs_component *ctf_fs_create(struct bt_value *params)
676{
677 struct ctf_fs_component *ctf_fs;
1ef09eb5 678 struct bt_value *value = NULL;
56a1cced
JG
679 const char *path;
680 enum bt_value_status ret;
681
682 ctf_fs = g_new0(struct ctf_fs_component, 1);
683 if (!ctf_fs) {
684 goto end;
685 }
686
687 /* FIXME: should probably look for a source URI */
688 value = bt_value_map_get(params, "path");
689 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
690 goto error;
691 }
692
693 ret = bt_value_string_get(value, &path);
694 if (ret != BT_VALUE_STATUS_OK) {
695 goto error;
696 }
697
698 ctf_fs->trace_path = g_string_new(path);
699 if (!ctf_fs->trace_path) {
700 goto error;
701 }
56a1cced
JG
702 ctf_fs->error_fp = stderr;
703 ctf_fs->page_size = (size_t) getpagesize();
e7a4393b
JG
704
705 // FIXME: check error.
5b29e799
JG
706 ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1);
707 if (!ctf_fs->metadata) {
e7a4393b
JG
708 goto error;
709 }
5b29e799 710 ctf_fs_metadata_set_trace(ctf_fs);
1ef09eb5
JG
711 goto end;
712
56a1cced
JG
713error:
714 ctf_fs_destroy_data(ctf_fs);
e7a4393b 715 ctf_fs = NULL;
1ef09eb5
JG
716end:
717 BT_PUT(value);
56a1cced
JG
718 return ctf_fs;
719}
720
ea0b4b9e
JG
721BT_HIDDEN
722enum bt_component_status ctf_fs_init(struct bt_component *source,
5c80adeb 723 struct bt_value *params)
ea0b4b9e
JG
724{
725 struct ctf_fs_component *ctf_fs;
726 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
727
728 assert(source);
729 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
730 ctf_fs = ctf_fs_create(params);
731 if (!ctf_fs) {
732 ret = BT_COMPONENT_STATUS_NOMEM;
733 goto end;
734 }
4c1456f0 735
ea0b4b9e
JG
736 ret = bt_component_set_destroy_cb(source, ctf_fs_destroy);
737 if (ret != BT_COMPONENT_STATUS_OK) {
738 goto error;
739 }
740
741 ret = bt_component_set_private_data(source, ctf_fs);
742 if (ret != BT_COMPONENT_STATUS_OK) {
743 goto error;
744 }
745
746 ret = bt_component_source_set_iterator_init_cb(source,
747 ctf_fs_iterator_init);
748 if (ret != BT_COMPONENT_STATUS_OK) {
749 goto error;
750 }
751end:
752 return ret;
753error:
754 (void) bt_component_set_private_data(source, NULL);
760051fa 755 ctf_fs_destroy_data(ctf_fs);
ea0b4b9e
JG
756 return ret;
757}
This page took 0.059688 seconds and 4 git commands to generate.