| 1 | /* |
| 2 | * query.c |
| 3 | * |
| 4 | * Babeltrace CTF file system Reader Component queries |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "query.h" |
| 28 | #include <stdbool.h> |
| 29 | #include <assert.h> |
| 30 | #include "metadata.h" |
| 31 | #include "../common/metadata/decoder.h" |
| 32 | |
| 33 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
| 34 | |
| 35 | BT_HIDDEN |
| 36 | struct bt_value *metadata_info_query(struct bt_component_class *comp_class, |
| 37 | struct bt_value *params) |
| 38 | { |
| 39 | struct bt_value *results = NULL; |
| 40 | struct bt_value *path_value = NULL; |
| 41 | char *metadata_text = NULL; |
| 42 | FILE *metadata_fp = NULL; |
| 43 | GString *g_metadata_text = NULL; |
| 44 | int ret; |
| 45 | int bo; |
| 46 | const char *path; |
| 47 | bool is_packetized; |
| 48 | |
| 49 | results = bt_value_map_create(); |
| 50 | if (!results) { |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | if (!bt_value_is_map(params)) { |
| 55 | fprintf(stderr, |
| 56 | "Query parameters is not a map value object\n"); |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | path_value = bt_value_map_get(params, "path"); |
| 61 | ret = bt_value_string_get(path_value, &path); |
| 62 | if (ret) { |
| 63 | fprintf(stderr, |
| 64 | "Cannot get `path` string parameter\n"); |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | assert(path); |
| 69 | metadata_fp = ctf_fs_metadata_open_file(path); |
| 70 | if (!metadata_fp) { |
| 71 | fprintf(stderr, |
| 72 | "Cannot open trace at path `%s`\n", path); |
| 73 | goto error; |
| 74 | } |
| 75 | |
| 76 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, |
| 77 | &bo); |
| 78 | |
| 79 | if (is_packetized) { |
| 80 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( |
| 81 | metadata_fp, &metadata_text, bo); |
| 82 | if (ret) { |
| 83 | fprintf(stderr, |
| 84 | "Cannot decode packetized metadata file\n"); |
| 85 | goto error; |
| 86 | } |
| 87 | } else { |
| 88 | long filesize; |
| 89 | |
| 90 | fseek(metadata_fp, 0, SEEK_END); |
| 91 | filesize = ftell(metadata_fp); |
| 92 | rewind(metadata_fp); |
| 93 | metadata_text = malloc(filesize + 1); |
| 94 | if (!metadata_text) { |
| 95 | fprintf(stderr, |
| 96 | "Cannot allocate buffer for metadata text\n"); |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | if (fread(metadata_text, filesize, 1, metadata_fp) != 1) { |
| 101 | fprintf(stderr, |
| 102 | "Cannot read metadata file\n"); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | metadata_text[filesize] = '\0'; |
| 107 | } |
| 108 | |
| 109 | g_metadata_text = g_string_new(NULL); |
| 110 | if (!g_metadata_text) { |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | if (strncmp(metadata_text, METADATA_TEXT_SIG, |
| 115 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { |
| 116 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); |
| 117 | g_string_append(g_metadata_text, " */\n\n"); |
| 118 | } |
| 119 | |
| 120 | g_string_append(g_metadata_text, metadata_text); |
| 121 | |
| 122 | ret = bt_value_map_insert_string(results, "text", |
| 123 | g_metadata_text->str); |
| 124 | if (ret) { |
| 125 | fprintf(stderr, "Cannot insert metadata text into results\n"); |
| 126 | goto error; |
| 127 | } |
| 128 | |
| 129 | ret = bt_value_map_insert_bool(results, "is-packetized", |
| 130 | is_packetized); |
| 131 | if (ret) { |
| 132 | fprintf(stderr, "Cannot insert is packetized into results\n"); |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | goto end; |
| 137 | |
| 138 | error: |
| 139 | BT_PUT(results); |
| 140 | |
| 141 | end: |
| 142 | bt_put(path_value); |
| 143 | free(metadata_text); |
| 144 | |
| 145 | if (g_metadata_text) { |
| 146 | g_string_free(g_metadata_text, TRUE); |
| 147 | } |
| 148 | |
| 149 | if (metadata_fp) { |
| 150 | fclose(metadata_fp); |
| 151 | } |
| 152 | return results; |
| 153 | } |