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