| 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/plugin/plugin-system.h> |
| 30 | #include <babeltrace/ctf-ir/packet.h> |
| 31 | #include <babeltrace/ctf-ir/clock.h> |
| 32 | #include <babeltrace/plugin/notification/iterator.h> |
| 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> |
| 37 | #include <glib.h> |
| 38 | #include <assert.h> |
| 39 | #include <unistd.h> |
| 40 | #include "fs.h" |
| 41 | #include "metadata.h" |
| 42 | #include "data-stream.h" |
| 43 | #include "file.h" |
| 44 | |
| 45 | #define PRINT_ERR_STREAM ctf_fs->error_fp |
| 46 | #define PRINT_PREFIX "ctf-fs" |
| 47 | #include "print.h" |
| 48 | |
| 49 | BT_HIDDEN |
| 50 | bool ctf_fs_debug; |
| 51 | |
| 52 | static |
| 53 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
| 54 | struct bt_notification_iterator *iterator); |
| 55 | |
| 56 | static |
| 57 | struct bt_notification *ctf_fs_iterator_get( |
| 58 | struct bt_notification_iterator *iterator) |
| 59 | { |
| 60 | struct ctf_fs_iterator *ctf_it = |
| 61 | bt_notification_iterator_get_private_data(iterator); |
| 62 | |
| 63 | if (!ctf_it->current_notification) { |
| 64 | (void) ctf_fs_iterator_next(iterator); |
| 65 | } |
| 66 | |
| 67 | return bt_get(ctf_it->current_notification); |
| 68 | } |
| 69 | |
| 70 | static |
| 71 | enum 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) |
| 75 | { |
| 76 | enum bt_ctf_notif_iter_status status; |
| 77 | enum bt_notification_iterator_status ret; |
| 78 | |
| 79 | if (stream->end_reached) { |
| 80 | status = BT_CTF_NOTIF_ITER_STATUS_EOF; |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 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) { |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 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 | } |
| 101 | end: |
| 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 | } |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Remove me. This is a temporary work-around due to our inhability to use |
| 128 | * libbabeltrace-ctf from libbabeltrace-plugin. |
| 129 | */ |
| 130 | static |
| 131 | struct bt_ctf_stream *internal_bt_notification_get_stream( |
| 132 | struct bt_notification *notification) |
| 133 | { |
| 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_BEGIN: |
| 148 | { |
| 149 | struct bt_ctf_packet *packet; |
| 150 | |
| 151 | packet = bt_notification_packet_begin_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 | } |
| 171 | end: |
| 172 | return stream; |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it) |
| 177 | { |
| 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, ¬ification); |
| 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 | } |
| 242 | |
| 243 | g_ptr_array_free(it->pending_streams, TRUE); |
| 244 | it->pending_streams = NULL; |
| 245 | end: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static |
| 250 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
| 251 | struct bt_notification_iterator *iterator) |
| 252 | { |
| 253 | int heap_ret; |
| 254 | struct bt_ctf_stream *stream = NULL; |
| 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; |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 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 | } |
| 285 | } |
| 286 | |
| 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; |
| 297 | } |
| 298 | |
| 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; |
| 303 | } |
| 304 | |
| 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 | } |
| 340 | } |
| 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 | */ |
| 347 | end: |
| 348 | BT_MOVE(ctf_it->current_notification, notification); |
| 349 | bt_put(stream); |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | static |
| 354 | void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it) |
| 355 | { |
| 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); |
| 360 | } |
| 361 | if (ctf_it->stream_ht) { |
| 362 | g_hash_table_destroy(ctf_it->stream_ht); |
| 363 | } |
| 364 | g_free(ctf_it); |
| 365 | } |
| 366 | |
| 367 | static |
| 368 | void ctf_fs_iterator_destroy(struct bt_notification_iterator *it) |
| 369 | { |
| 370 | void *data = bt_notification_iterator_get_private_data(it); |
| 371 | |
| 372 | ctf_fs_iterator_destroy_data(data); |
| 373 | } |
| 374 | |
| 375 | static |
| 376 | bool 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 | |
| 423 | static |
| 424 | bool compare_notifications(struct bt_notification *a, struct bt_notification *b, |
| 425 | void *unused) |
| 426 | { |
| 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_BEGIN] = 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_BEGIN: |
| 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); |
| 493 | return a < b; |
| 494 | } |
| 495 | |
| 496 | static |
| 497 | void stream_destroy(void *stream) |
| 498 | { |
| 499 | ctf_fs_stream_destroy((struct ctf_fs_stream *) stream); |
| 500 | } |
| 501 | |
| 502 | static |
| 503 | int open_trace_streams(struct ctf_fs_component *ctf_fs, |
| 504 | struct ctf_fs_iterator *ctf_it) |
| 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 | if (file->size == 0) { |
| 558 | /* Skip empty stream. */ |
| 559 | ctf_fs_file_destroy(file); |
| 560 | continue; |
| 561 | } |
| 562 | |
| 563 | /* Create a private stream; file ownership is passed to it. */ |
| 564 | stream = ctf_fs_stream_create(ctf_fs, file); |
| 565 | if (!stream) { |
| 566 | ctf_fs_file_destroy(file); |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | g_ptr_array_add(ctf_it->pending_streams, stream); |
| 571 | } |
| 572 | |
| 573 | goto end; |
| 574 | error: |
| 575 | ret = -1; |
| 576 | end: |
| 577 | if (dir) { |
| 578 | g_dir_close(dir); |
| 579 | dir = NULL; |
| 580 | } |
| 581 | if (error) { |
| 582 | g_error_free(error); |
| 583 | } |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | static |
| 588 | enum bt_component_status ctf_fs_iterator_init(struct bt_component *source, |
| 589 | struct bt_notification_iterator *it) |
| 590 | { |
| 591 | struct ctf_fs_iterator *ctf_it; |
| 592 | struct ctf_fs_component *ctf_fs; |
| 593 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 594 | |
| 595 | assert(source && it); |
| 596 | |
| 597 | ctf_fs = bt_component_get_private_data(source); |
| 598 | if (!ctf_fs) { |
| 599 | ret = BT_COMPONENT_STATUS_INVALID; |
| 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | ctf_it = g_new0(struct ctf_fs_iterator, 1); |
| 604 | if (!ctf_it) { |
| 605 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 606 | goto end; |
| 607 | } |
| 608 | |
| 609 | ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash, |
| 610 | g_direct_equal, bt_put, stream_destroy); |
| 611 | if (!ctf_it->stream_ht) { |
| 612 | goto error; |
| 613 | } |
| 614 | ctf_it->pending_streams = g_ptr_array_new_with_free_func( |
| 615 | stream_destroy); |
| 616 | if (!ctf_it->pending_streams) { |
| 617 | goto error; |
| 618 | } |
| 619 | ctf_it->pending_notifications = bt_notification_heap_create( |
| 620 | compare_notifications, NULL); |
| 621 | if (!ctf_it->pending_notifications) { |
| 622 | goto error; |
| 623 | } |
| 624 | |
| 625 | ret = open_trace_streams(ctf_fs, ctf_it); |
| 626 | if (ret) { |
| 627 | goto error; |
| 628 | } |
| 629 | |
| 630 | ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get); |
| 631 | if (ret) { |
| 632 | goto error; |
| 633 | } |
| 634 | |
| 635 | ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next); |
| 636 | if (ret) { |
| 637 | goto error; |
| 638 | } |
| 639 | |
| 640 | ret = bt_notification_iterator_set_destroy_cb(it, |
| 641 | ctf_fs_iterator_destroy); |
| 642 | if (ret) { |
| 643 | goto error; |
| 644 | } |
| 645 | |
| 646 | ret = bt_notification_iterator_set_private_data(it, ctf_it); |
| 647 | if (ret) { |
| 648 | goto error; |
| 649 | } |
| 650 | |
| 651 | end: |
| 652 | return ret; |
| 653 | error: |
| 654 | (void) bt_notification_iterator_set_private_data(it, NULL); |
| 655 | ctf_fs_iterator_destroy_data(ctf_it); |
| 656 | goto end; |
| 657 | } |
| 658 | |
| 659 | static |
| 660 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) |
| 661 | { |
| 662 | if (ctf_fs->trace_path) { |
| 663 | g_string_free(ctf_fs->trace_path, TRUE); |
| 664 | } |
| 665 | if (ctf_fs->metadata) { |
| 666 | ctf_fs_metadata_fini(ctf_fs->metadata); |
| 667 | g_free(ctf_fs->metadata); |
| 668 | } |
| 669 | g_free(ctf_fs); |
| 670 | } |
| 671 | |
| 672 | static |
| 673 | void ctf_fs_destroy(struct bt_component *component) |
| 674 | { |
| 675 | void *data = bt_component_get_private_data(component); |
| 676 | |
| 677 | ctf_fs_destroy_data(data); |
| 678 | } |
| 679 | |
| 680 | static |
| 681 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) |
| 682 | { |
| 683 | struct ctf_fs_component *ctf_fs; |
| 684 | struct bt_value *value = NULL; |
| 685 | const char *path; |
| 686 | enum bt_value_status ret; |
| 687 | |
| 688 | ctf_fs = g_new0(struct ctf_fs_component, 1); |
| 689 | if (!ctf_fs) { |
| 690 | goto end; |
| 691 | } |
| 692 | |
| 693 | /* FIXME: should probably look for a source URI */ |
| 694 | value = bt_value_map_get(params, "path"); |
| 695 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { |
| 696 | goto error; |
| 697 | } |
| 698 | |
| 699 | ret = bt_value_string_get(value, &path); |
| 700 | if (ret != BT_VALUE_STATUS_OK) { |
| 701 | goto error; |
| 702 | } |
| 703 | |
| 704 | ctf_fs->trace_path = g_string_new(path); |
| 705 | if (!ctf_fs->trace_path) { |
| 706 | goto error; |
| 707 | } |
| 708 | ctf_fs->error_fp = stderr; |
| 709 | ctf_fs->page_size = (size_t) getpagesize(); |
| 710 | |
| 711 | // FIXME: check error. |
| 712 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
| 713 | if (!ctf_fs->metadata) { |
| 714 | goto error; |
| 715 | } |
| 716 | ctf_fs_metadata_set_trace(ctf_fs); |
| 717 | goto end; |
| 718 | |
| 719 | error: |
| 720 | ctf_fs_destroy_data(ctf_fs); |
| 721 | ctf_fs = NULL; |
| 722 | end: |
| 723 | BT_PUT(value); |
| 724 | return ctf_fs; |
| 725 | } |
| 726 | |
| 727 | BT_HIDDEN |
| 728 | enum bt_component_status ctf_fs_init(struct bt_component *source, |
| 729 | struct bt_value *params) |
| 730 | { |
| 731 | struct ctf_fs_component *ctf_fs; |
| 732 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 733 | |
| 734 | assert(source); |
| 735 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; |
| 736 | ctf_fs = ctf_fs_create(params); |
| 737 | if (!ctf_fs) { |
| 738 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 739 | goto end; |
| 740 | } |
| 741 | |
| 742 | ret = bt_component_set_destroy_cb(source, ctf_fs_destroy); |
| 743 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 744 | goto error; |
| 745 | } |
| 746 | |
| 747 | ret = bt_component_set_private_data(source, ctf_fs); |
| 748 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 749 | goto error; |
| 750 | } |
| 751 | |
| 752 | ret = bt_component_source_set_iterator_init_cb(source, |
| 753 | ctf_fs_iterator_init); |
| 754 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 755 | goto error; |
| 756 | } |
| 757 | end: |
| 758 | return ret; |
| 759 | error: |
| 760 | (void) bt_component_set_private_data(source, NULL); |
| 761 | ctf_fs_destroy_data(ctf_fs); |
| 762 | return ret; |
| 763 | } |