| 1 | /* |
| 2 | * Copyright (c) 2019 EfficiOS Inc. and Linux Foundation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "CLI-CFG-SRC-AUTO-DISC" |
| 24 | #include "logging.h" |
| 25 | |
| 26 | #include "babeltrace2-cfg-src-auto-disc.h" |
| 27 | #include "babeltrace2-plugins.h" |
| 28 | #include "babeltrace2-query.h" |
| 29 | #include "common/common.h" |
| 30 | |
| 31 | /* Finalize and free a `struct auto_source_discovery_result`. */ |
| 32 | |
| 33 | static |
| 34 | void auto_source_discovery_result_destroy(struct auto_source_discovery_result *res) |
| 35 | { |
| 36 | if (res) { |
| 37 | g_free(res->group); |
| 38 | bt_value_put_ref(res->inputs); |
| 39 | bt_value_put_ref(res->original_input_indices); |
| 40 | g_free(res); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /* Allocate and initialize a `struct auto_source_discovery_result`. */ |
| 45 | |
| 46 | static |
| 47 | struct auto_source_discovery_result *auto_source_discovery_result_create( |
| 48 | const char *plugin_name, const char *source_cc_name, |
| 49 | const char *group) |
| 50 | { |
| 51 | struct auto_source_discovery_result *res; |
| 52 | |
| 53 | res = g_new0(struct auto_source_discovery_result, 1); |
| 54 | if (!res) { |
| 55 | BT_CLI_LOGE_APPEND_CAUSE( |
| 56 | "Failed to allocate a auto_source_discovery_result structure."); |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | res->plugin_name = plugin_name; |
| 61 | res->source_cc_name = source_cc_name; |
| 62 | res->group = g_strdup(group); |
| 63 | if (group && !res->group) { |
| 64 | BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a string."); |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | res->inputs = bt_value_array_create(); |
| 69 | if (!res->inputs) { |
| 70 | BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value."); |
| 71 | goto error; |
| 72 | } |
| 73 | |
| 74 | res->original_input_indices = bt_value_array_create(); |
| 75 | if (!res->original_input_indices) { |
| 76 | BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value."); |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | goto end; |
| 81 | error: |
| 82 | auto_source_discovery_result_destroy(res); |
| 83 | |
| 84 | end: |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | /* Finalize a `struct auto_source_discovery`. */ |
| 89 | |
| 90 | void auto_source_discovery_fini(struct auto_source_discovery *auto_disc) |
| 91 | { |
| 92 | if (auto_disc->results) { |
| 93 | g_ptr_array_free(auto_disc->results, TRUE); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Initialize an already allocated `struct auto_source_discovery`. */ |
| 98 | |
| 99 | int auto_source_discovery_init(struct auto_source_discovery *auto_disc) |
| 100 | { |
| 101 | int status; |
| 102 | |
| 103 | auto_disc->results = g_ptr_array_new_with_free_func( |
| 104 | (GDestroyNotify) auto_source_discovery_result_destroy); |
| 105 | |
| 106 | if (!auto_disc->results) { |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | status = 0; |
| 111 | goto end; |
| 112 | |
| 113 | error: |
| 114 | auto_source_discovery_fini(auto_disc); |
| 115 | status = -1; |
| 116 | |
| 117 | end: |
| 118 | |
| 119 | return status; |
| 120 | } |
| 121 | |
| 122 | static |
| 123 | const bt_value *borrow_array_value_last_element_const(const bt_value *array) |
| 124 | { |
| 125 | uint64_t last_index = bt_value_array_get_size(array) - 1; |
| 126 | |
| 127 | return bt_value_array_borrow_element_by_index_const(array, last_index); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Assign `input` to source component class `source_cc_name` of plugin |
| 132 | * `plugin_name`, in the group with key `group`. |
| 133 | */ |
| 134 | |
| 135 | static |
| 136 | int auto_source_discovery_add(struct auto_source_discovery *auto_disc, |
| 137 | const char *plugin_name, |
| 138 | const char *source_cc_name, |
| 139 | const char *group, |
| 140 | const char *input, |
| 141 | uint64_t original_input_index) |
| 142 | { |
| 143 | int status; |
| 144 | bt_value_array_append_element_status append_status; |
| 145 | guint len; |
| 146 | guint i; |
| 147 | struct auto_source_discovery_result *res = NULL; |
| 148 | bool append_index; |
| 149 | |
| 150 | len = auto_disc->results->len; |
| 151 | i = len; |
| 152 | |
| 153 | if (group) { |
| 154 | for (i = 0; i < len; i++) { |
| 155 | res = g_ptr_array_index(auto_disc->results, i); |
| 156 | |
| 157 | if (strcmp(res->plugin_name, plugin_name) != 0) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if (strcmp(res->source_cc_name, source_cc_name) != 0) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | if (g_strcmp0(res->group, group) != 0) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (i == len) { |
| 174 | /* Add a new result entry. */ |
| 175 | res = auto_source_discovery_result_create(plugin_name, |
| 176 | source_cc_name, group); |
| 177 | if (!res) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | g_ptr_array_add(auto_disc->results, res); |
| 182 | } |
| 183 | |
| 184 | append_status = bt_value_array_append_string_element(res->inputs, input); |
| 185 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 186 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append a string value."); |
| 187 | goto error; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Append `original_input_index` to `original_input_indices` if not |
| 192 | * there already. We process the `inputs` array in order, so if it is |
| 193 | * present, it has to be the last element. |
| 194 | */ |
| 195 | if (bt_value_array_is_empty(res->original_input_indices)) { |
| 196 | append_index = true; |
| 197 | } else { |
| 198 | const bt_value *last_index_value; |
| 199 | uint64_t last_index; |
| 200 | |
| 201 | last_index_value = |
| 202 | borrow_array_value_last_element_const(res->original_input_indices); |
| 203 | last_index = bt_value_integer_unsigned_get(last_index_value); |
| 204 | |
| 205 | BT_ASSERT(last_index <= original_input_index); |
| 206 | |
| 207 | append_index = (last_index != original_input_index); |
| 208 | } |
| 209 | |
| 210 | if (append_index) { |
| 211 | append_status = bt_value_array_append_unsigned_integer_element( |
| 212 | res->original_input_indices, original_input_index); |
| 213 | |
| 214 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 215 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append an unsigned integer value."); |
| 216 | goto error; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | status = 0; |
| 221 | goto end; |
| 222 | |
| 223 | error: |
| 224 | status = -1; |
| 225 | |
| 226 | end: |
| 227 | return status; |
| 228 | } |
| 229 | |
| 230 | static |
| 231 | int convert_weight_value(const bt_value *weight_value, double *weight, |
| 232 | const char *plugin_name, const char *source_cc_name, |
| 233 | const char *input, const char *input_type) |
| 234 | { |
| 235 | enum bt_value_type weight_value_type; |
| 236 | int status; |
| 237 | |
| 238 | weight_value_type = bt_value_get_type(weight_value); |
| 239 | |
| 240 | if (weight_value_type == BT_VALUE_TYPE_REAL) { |
| 241 | *weight = bt_value_real_get(weight_value); |
| 242 | } else if (weight_value_type == BT_VALUE_TYPE_SIGNED_INTEGER) { |
| 243 | /* Accept signed integer as a convenience for "return 0" or "return 1" in Python. */ |
| 244 | *weight = bt_value_integer_signed_get(weight_value); |
| 245 | } else { |
| 246 | BT_LOGW("babeltrace.support-info query: unexpected type for weight: " |
| 247 | "component-class-name=source.%s.%s, input=%s, input-type=%s, " |
| 248 | "expected-entry-type=%s, actual-entry-type=%s", |
| 249 | plugin_name, source_cc_name, input, input_type, |
| 250 | bt_common_value_type_string(BT_VALUE_TYPE_REAL), |
| 251 | bt_common_value_type_string(bt_value_get_type(weight_value))); |
| 252 | goto error; |
| 253 | } |
| 254 | |
| 255 | if (*weight < 0.0 || *weight > 1.0) { |
| 256 | BT_LOGW("babeltrace.support-info query: weight value is out of range [0.0, 1.0]: " |
| 257 | "component-class-name=source.%s.%s, input=%s, input-type=%s, " |
| 258 | "weight=%f", |
| 259 | plugin_name, source_cc_name, input, input_type, *weight); |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | status = 0; |
| 264 | goto end; |
| 265 | |
| 266 | error: |
| 267 | status = -1; |
| 268 | |
| 269 | end: |
| 270 | return status; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Query all known source components to see if any of them can handle `input` |
| 275 | * as the given `type`(arbitrary string, directory or file). |
| 276 | * |
| 277 | * If `plugin_restrict` is non-NULL, only query source component classes provided |
| 278 | * by the plugin with that name. |
| 279 | * |
| 280 | * If `component_class_restrict` is non-NULL, only query source component classes |
| 281 | * with that name. |
| 282 | * |
| 283 | * Return: |
| 284 | * |
| 285 | * - > 0 on success, if no source component class has reported that it handles `input` |
| 286 | * - 0 on success, if a source component class has reported that it handles `input` |
| 287 | * - < 0 on failure (e.g. memory error) |
| 288 | */ |
| 289 | static |
| 290 | int support_info_query_all_sources(const char *input, |
| 291 | const char *input_type, |
| 292 | uint64_t original_input_index, |
| 293 | size_t plugin_count, |
| 294 | const char *plugin_restrict, |
| 295 | const char *component_class_restrict, |
| 296 | enum bt_logging_level log_level, |
| 297 | struct auto_source_discovery *auto_disc) |
| 298 | { |
| 299 | bt_value_map_insert_entry_status insert_status; |
| 300 | bt_value *query_params = NULL; |
| 301 | int status; |
| 302 | size_t i_plugins; |
| 303 | const struct bt_value *query_result = NULL; |
| 304 | struct { |
| 305 | const bt_component_class_source *source; |
| 306 | const bt_plugin *plugin; |
| 307 | const bt_value *group; |
| 308 | double weigth; |
| 309 | } winner = { NULL, NULL, NULL, 0 }; |
| 310 | |
| 311 | query_params = bt_value_map_create(); |
| 312 | if (!query_params) { |
| 313 | BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a map value."); |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | insert_status = bt_value_map_insert_string_entry(query_params, "input", input); |
| 318 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 319 | BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry."); |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | insert_status = bt_value_map_insert_string_entry(query_params, "type", input_type); |
| 324 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 325 | BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry."); |
| 326 | goto error; |
| 327 | } |
| 328 | |
| 329 | for (i_plugins = 0; i_plugins < plugin_count; i_plugins++) { |
| 330 | const bt_plugin *plugin; |
| 331 | const char *plugin_name; |
| 332 | uint64_t source_count; |
| 333 | uint64_t i_sources; |
| 334 | |
| 335 | plugin = borrow_loaded_plugin(i_plugins); |
| 336 | plugin_name = bt_plugin_get_name(plugin); |
| 337 | |
| 338 | /* |
| 339 | * If the search is restricted to a specific plugin, only consider |
| 340 | * the plugin with that name. |
| 341 | */ |
| 342 | if (plugin_restrict && strcmp(plugin_restrict, plugin_name) != 0) { |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | source_count = bt_plugin_get_source_component_class_count(plugin); |
| 347 | |
| 348 | for (i_sources = 0; i_sources < source_count; i_sources++) { |
| 349 | const bt_component_class_source *source_cc; |
| 350 | const bt_component_class *cc; |
| 351 | const char *source_cc_name; |
| 352 | bt_query_executor_query_status query_status; |
| 353 | |
| 354 | source_cc = bt_plugin_borrow_source_component_class_by_index_const(plugin, i_sources); |
| 355 | cc = bt_component_class_source_as_component_class_const(source_cc); |
| 356 | source_cc_name = bt_component_class_get_name(cc); |
| 357 | |
| 358 | /* |
| 359 | * If the search is restricted to a specific component class, only consider the |
| 360 | * component classes with that name. |
| 361 | */ |
| 362 | if (component_class_restrict && strcmp(component_class_restrict, source_cc_name) != 0) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | BT_LOGD("babeltrace.support-info query: before: component-class-name=source.%s.%s, input=%s, " |
| 367 | "type=%s", plugin_name, source_cc_name, input, input_type); |
| 368 | |
| 369 | BT_VALUE_PUT_REF_AND_RESET(query_result); |
| 370 | query_status = cli_query(cc, "babeltrace.support-info", |
| 371 | query_params, log_level, NULL, &query_result, |
| 372 | NULL); |
| 373 | |
| 374 | if (query_status == BT_QUERY_EXECUTOR_QUERY_STATUS_OK) { |
| 375 | double weight; |
| 376 | const bt_value *group_value = NULL; |
| 377 | enum bt_value_type query_result_type; |
| 378 | |
| 379 | BT_ASSERT(query_result); |
| 380 | |
| 381 | query_result_type = bt_value_get_type(query_result); |
| 382 | |
| 383 | if (query_result_type == BT_VALUE_TYPE_REAL || query_result_type == BT_VALUE_TYPE_SIGNED_INTEGER) { |
| 384 | if (convert_weight_value(query_result, &weight, plugin_name, source_cc_name, input, input_type) != 0) { |
| 385 | /* convert_weight_value has already warned. */ |
| 386 | continue; |
| 387 | } |
| 388 | } else if (query_result_type == BT_VALUE_TYPE_MAP) { |
| 389 | const bt_value *weight_value; |
| 390 | |
| 391 | if (!bt_value_map_has_entry(query_result, "weight")) { |
| 392 | BT_LOGW("babeltrace.support-info query: result is missing `weight` entry: " |
| 393 | "component-class-name=source.%s.%s, input=%s, input-type=%s", |
| 394 | bt_plugin_get_name(plugin), |
| 395 | bt_component_class_get_name(cc), input, |
| 396 | input_type); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | weight_value = bt_value_map_borrow_entry_value_const(query_result, "weight"); |
| 401 | BT_ASSERT(weight_value); |
| 402 | |
| 403 | if (convert_weight_value(weight_value, &weight, plugin_name, source_cc_name, input, input_type) != 0) { |
| 404 | /* convert_weight_value has already warned. */ |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | if (bt_value_map_has_entry(query_result, "group")) { |
| 409 | enum bt_value_type group_value_type; |
| 410 | |
| 411 | group_value = bt_value_map_borrow_entry_value_const(query_result, "group"); |
| 412 | BT_ASSERT(group_value); |
| 413 | |
| 414 | group_value_type = bt_value_get_type(group_value); |
| 415 | |
| 416 | if (group_value_type == BT_VALUE_TYPE_NULL) { |
| 417 | /* Do as if no value was passed. */ |
| 418 | group_value = NULL; |
| 419 | } else if (bt_value_get_type(group_value) != BT_VALUE_TYPE_STRING) { |
| 420 | BT_LOGW("babeltrace.support-info query: unexpected type for entry `group`: " |
| 421 | "component-class-name=source.%s.%s, input=%s, input-type=%s, " |
| 422 | "expected-entry-type=%s,%s, actual-entry-type=%s", |
| 423 | bt_plugin_get_name(plugin), |
| 424 | bt_component_class_get_name(cc), input, |
| 425 | input_type, |
| 426 | bt_common_value_type_string(BT_VALUE_TYPE_NULL), |
| 427 | bt_common_value_type_string(BT_VALUE_TYPE_STRING), |
| 428 | bt_common_value_type_string(bt_value_get_type(group_value))); |
| 429 | continue; |
| 430 | } |
| 431 | } |
| 432 | } else { |
| 433 | BT_LOGW("babeltrace.support-info query: unexpected result type: " |
| 434 | "component-class-name=source.%s.%s, input=%s, input-type=%s, " |
| 435 | "expected-types=%s,%s,%s, actual-type=%s", |
| 436 | bt_plugin_get_name(plugin), |
| 437 | bt_component_class_get_name(cc), input, |
| 438 | input_type, |
| 439 | bt_common_value_type_string(BT_VALUE_TYPE_REAL), |
| 440 | bt_common_value_type_string(BT_VALUE_TYPE_MAP), |
| 441 | bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER), |
| 442 | bt_common_value_type_string(bt_value_get_type(query_result))); |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | BT_LOGD("babeltrace.support-info query: success: component-class-name=source.%s.%s, input=%s, " |
| 447 | "type=%s, weight=%f\n", |
| 448 | bt_plugin_get_name(plugin), bt_component_class_get_name(cc), input, |
| 449 | input_type, weight); |
| 450 | |
| 451 | if (weight > winner.weigth) { |
| 452 | winner.source = source_cc; |
| 453 | winner.plugin = plugin; |
| 454 | |
| 455 | bt_value_put_ref(winner.group); |
| 456 | winner.group = group_value; |
| 457 | bt_value_get_ref(winner.group); |
| 458 | |
| 459 | winner.weigth = weight; |
| 460 | } |
| 461 | } else if (query_status == BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR) { |
| 462 | BT_CLI_LOGE_APPEND_CAUSE("babeltrace.support-info query failed."); |
| 463 | goto error; |
| 464 | } else if (query_status == BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR) { |
| 465 | BT_CLI_LOGE_APPEND_CAUSE("Memory error."); |
| 466 | goto error; |
| 467 | } else { |
| 468 | BT_LOGD("babeltrace.support-info query: failure: component-class-name=source.%s.%s, input=%s, " |
| 469 | "type=%s, status=%s\n", |
| 470 | bt_plugin_get_name(plugin), bt_component_class_get_name(cc), input, |
| 471 | input_type, |
| 472 | bt_common_func_status_string(query_status)); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (winner.source) { |
| 478 | const char *source_name; |
| 479 | const char *plugin_name; |
| 480 | const char *group; |
| 481 | |
| 482 | source_name = bt_component_class_get_name( |
| 483 | bt_component_class_source_as_component_class_const(winner.source)); |
| 484 | plugin_name = bt_plugin_get_name(winner.plugin); |
| 485 | group = winner.group ? bt_value_string_get(winner.group) : NULL; |
| 486 | |
| 487 | BT_LOGI("Input %s is awarded to component class source.%s.%s with weight %f", |
| 488 | input, plugin_name, source_name, winner.weigth); |
| 489 | |
| 490 | status = auto_source_discovery_add(auto_disc, plugin_name, |
| 491 | source_name, group, input, original_input_index); |
| 492 | if (status != 0) { |
| 493 | goto error; |
| 494 | } |
| 495 | } else { |
| 496 | BT_LOGI("Input %s (%s) was not recognized by any source component class.", |
| 497 | input, input_type); |
| 498 | status = 1; |
| 499 | } |
| 500 | |
| 501 | goto end; |
| 502 | |
| 503 | error: |
| 504 | status = -1; |
| 505 | |
| 506 | end: |
| 507 | bt_value_put_ref(query_result); |
| 508 | bt_value_put_ref(query_params); |
| 509 | bt_value_put_ref(winner.group); |
| 510 | |
| 511 | return status; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Look for a source component class that recognizes `input` as an arbitrary |
| 516 | * string. |
| 517 | * |
| 518 | * Same return value semantic as `support_info_query_all_sources`. |
| 519 | */ |
| 520 | |
| 521 | static |
| 522 | int auto_discover_source_for_input_as_string(const char *input, |
| 523 | uint64_t original_input_index, |
| 524 | size_t plugin_count, const char *plugin_restrict, |
| 525 | const char *component_class_restrict, |
| 526 | enum bt_logging_level log_level, |
| 527 | struct auto_source_discovery *auto_disc) |
| 528 | { |
| 529 | return support_info_query_all_sources(input, "string", |
| 530 | original_input_index, plugin_count, plugin_restrict, |
| 531 | component_class_restrict, log_level, auto_disc); |
| 532 | } |
| 533 | |
| 534 | static |
| 535 | int auto_discover_source_for_input_as_dir_or_file_rec(GString *input, |
| 536 | uint64_t original_input_index, |
| 537 | size_t plugin_count, const char *plugin_restrict, |
| 538 | const char *component_class_restrict, |
| 539 | enum bt_logging_level log_level, |
| 540 | struct auto_source_discovery *auto_disc) |
| 541 | { |
| 542 | int status; |
| 543 | GError *error = NULL; |
| 544 | |
| 545 | if (g_file_test(input->str, G_FILE_TEST_IS_REGULAR)) { |
| 546 | /* It's a file. */ |
| 547 | status = support_info_query_all_sources(input->str, |
| 548 | "file", original_input_index, plugin_count, |
| 549 | plugin_restrict, component_class_restrict, log_level, auto_disc); |
| 550 | } else if (g_file_test(input->str, G_FILE_TEST_IS_DIR)) { |
| 551 | GDir *dir; |
| 552 | const gchar *dirent; |
| 553 | gsize saved_input_len; |
| 554 | int dir_status = 1; |
| 555 | |
| 556 | /* It's a directory. */ |
| 557 | status = support_info_query_all_sources(input->str, |
| 558 | "directory", original_input_index, plugin_count, |
| 559 | plugin_restrict, component_class_restrict, log_level, |
| 560 | auto_disc); |
| 561 | |
| 562 | if (status < 0) { |
| 563 | /* Fatal error. */ |
| 564 | goto error; |
| 565 | } else if (status == 0) { |
| 566 | /* |
| 567 | * A component class claimed this input as a directory, |
| 568 | * don't recurse. |
| 569 | */ |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | dir = g_dir_open(input->str, 0, &error); |
| 574 | if (!dir) { |
| 575 | const char *fmt = "Failed to open directory %s: %s"; |
| 576 | BT_LOGW(fmt, input->str, error->message); |
| 577 | |
| 578 | if (error->code == G_FILE_ERROR_ACCES) { |
| 579 | /* This is not a fatal error, we just skip it. */ |
| 580 | status = 1; |
| 581 | goto end; |
| 582 | } else { |
| 583 | BT_CLI_LOGE_APPEND_CAUSE(fmt, input->str, |
| 584 | error->message); |
| 585 | goto error; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | saved_input_len = input->len; |
| 590 | |
| 591 | do { |
| 592 | errno = 0; |
| 593 | dirent = g_dir_read_name(dir); |
| 594 | if (dirent) { |
| 595 | g_string_append_c_inline(input, G_DIR_SEPARATOR); |
| 596 | g_string_append(input, dirent); |
| 597 | |
| 598 | status = auto_discover_source_for_input_as_dir_or_file_rec( |
| 599 | input, original_input_index, plugin_count, |
| 600 | plugin_restrict, component_class_restrict, |
| 601 | log_level, auto_disc); |
| 602 | |
| 603 | g_string_truncate(input, saved_input_len); |
| 604 | |
| 605 | if (status < 0) { |
| 606 | /* Fatal error. */ |
| 607 | goto error; |
| 608 | } else if (status == 0) { |
| 609 | dir_status = 0; |
| 610 | } |
| 611 | } else if (errno != 0) { |
| 612 | BT_LOGW_ERRNO("Failed to read directory entry", ": dir=%s", input->str); |
| 613 | goto error; |
| 614 | } |
| 615 | } while (dirent); |
| 616 | |
| 617 | status = dir_status; |
| 618 | |
| 619 | g_dir_close(dir); |
| 620 | } else { |
| 621 | BT_LOGD("Skipping %s, not a file or directory", input->str); |
| 622 | status = 1; |
| 623 | } |
| 624 | |
| 625 | goto end; |
| 626 | |
| 627 | error: |
| 628 | status = -1; |
| 629 | |
| 630 | end: |
| 631 | |
| 632 | if (error) { |
| 633 | g_error_free(error); |
| 634 | } |
| 635 | |
| 636 | return status; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Look for a source component class that recognizes `input` as a directory or |
| 641 | * file. If `input` is a directory and is not directly recognized, recurse and |
| 642 | * apply the same logic to children nodes. |
| 643 | * |
| 644 | * Same return value semantic as `support_info_query_all_sources`. |
| 645 | */ |
| 646 | |
| 647 | static |
| 648 | int auto_discover_source_for_input_as_dir_or_file(const char *input, |
| 649 | uint64_t original_input_index, |
| 650 | size_t plugin_count, const char *plugin_restrict, |
| 651 | const char *component_class_restrict, |
| 652 | enum bt_logging_level log_level, |
| 653 | struct auto_source_discovery *auto_disc) |
| 654 | { |
| 655 | GString *mutable_input; |
| 656 | int status; |
| 657 | |
| 658 | mutable_input = g_string_new(input); |
| 659 | if (!mutable_input) { |
| 660 | status = -1; |
| 661 | goto end; |
| 662 | } |
| 663 | |
| 664 | status = auto_discover_source_for_input_as_dir_or_file_rec( |
| 665 | mutable_input, original_input_index, plugin_count, plugin_restrict, |
| 666 | component_class_restrict, log_level, auto_disc); |
| 667 | |
| 668 | g_string_free(mutable_input, TRUE); |
| 669 | end: |
| 670 | return status; |
| 671 | } |
| 672 | |
| 673 | int auto_discover_source_components( |
| 674 | const bt_value *plugin_paths, |
| 675 | const bt_value *inputs, |
| 676 | const char *plugin_restrict, |
| 677 | const char *component_class_restrict, |
| 678 | enum bt_logging_level log_level, |
| 679 | struct auto_source_discovery *auto_disc) |
| 680 | { |
| 681 | uint64_t i_inputs, input_count; |
| 682 | int status; |
| 683 | size_t plugin_count; |
| 684 | |
| 685 | input_count = bt_value_array_get_size(inputs); |
| 686 | |
| 687 | status = require_loaded_plugins(plugin_paths); |
| 688 | if (status != 0) { |
| 689 | goto end; |
| 690 | } |
| 691 | |
| 692 | plugin_count = get_loaded_plugins_count(); |
| 693 | |
| 694 | for (i_inputs = 0; i_inputs < input_count; i_inputs++) { |
| 695 | const bt_value *input_value; |
| 696 | const char *input; |
| 697 | |
| 698 | input_value = bt_value_array_borrow_element_by_index_const(inputs, i_inputs); |
| 699 | input = bt_value_string_get(input_value); |
| 700 | status = auto_discover_source_for_input_as_string(input, i_inputs, |
| 701 | plugin_count, plugin_restrict, component_class_restrict, |
| 702 | log_level, auto_disc); |
| 703 | if (status < 0) { |
| 704 | /* Fatal error. */ |
| 705 | goto end; |
| 706 | } else if (status == 0) { |
| 707 | /* A component class has claimed this input as an arbitrary string. */ |
| 708 | continue; |
| 709 | } |
| 710 | |
| 711 | status = auto_discover_source_for_input_as_dir_or_file(input, |
| 712 | i_inputs, plugin_count, plugin_restrict, |
| 713 | component_class_restrict, log_level, auto_disc); |
| 714 | if (status < 0) { |
| 715 | /* Fatal error. */ |
| 716 | goto end; |
| 717 | } else if (status == 0) { |
| 718 | /* |
| 719 | * This input (or something under it) was recognized. |
| 720 | */ |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | BT_LOGW("No trace was found based on input `%s`.", input); |
| 725 | } |
| 726 | |
| 727 | status = 0; |
| 728 | end: |
| 729 | return status; |
| 730 | } |