Commit | Line | Data |
---|---|---|
04c0ba87 JG |
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" | |
55314f2a JG |
32 | #include <babeltrace/common-internal.h> |
33 | ||
34 | #define BT_LOG_TAG "PLUGIN-CTF-FS-QUERY-SRC" | |
35 | #include "logging.h" | |
04c0ba87 JG |
36 | |
37 | #define METADATA_TEXT_SIG "/* CTF 1.8" | |
38 | ||
39 | BT_HIDDEN | |
40 | struct bt_value *metadata_info_query(struct bt_component_class *comp_class, | |
41 | struct bt_value *params) | |
42 | { | |
43 | struct bt_value *results = NULL; | |
44 | struct bt_value *path_value = NULL; | |
45 | char *metadata_text = NULL; | |
46 | FILE *metadata_fp = NULL; | |
47 | GString *g_metadata_text = NULL; | |
48 | int ret; | |
49 | int bo; | |
50 | const char *path; | |
51 | bool is_packetized; | |
52 | ||
53 | results = bt_value_map_create(); | |
54 | if (!results) { | |
55 | goto error; | |
56 | } | |
57 | ||
58 | if (!bt_value_is_map(params)) { | |
59 | fprintf(stderr, | |
60 | "Query parameters is not a map value object\n"); | |
61 | goto error; | |
62 | } | |
63 | ||
64 | path_value = bt_value_map_get(params, "path"); | |
65 | ret = bt_value_string_get(path_value, &path); | |
66 | if (ret) { | |
67 | fprintf(stderr, | |
68 | "Cannot get `path` string parameter\n"); | |
69 | goto error; | |
70 | } | |
71 | ||
72 | assert(path); | |
73 | metadata_fp = ctf_fs_metadata_open_file(path); | |
74 | if (!metadata_fp) { | |
75 | fprintf(stderr, | |
76 | "Cannot open trace at path `%s`\n", path); | |
77 | goto error; | |
78 | } | |
79 | ||
80 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, | |
81 | &bo); | |
82 | ||
83 | if (is_packetized) { | |
84 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( | |
85 | metadata_fp, &metadata_text, bo); | |
86 | if (ret) { | |
87 | fprintf(stderr, | |
88 | "Cannot decode packetized metadata file\n"); | |
89 | goto error; | |
90 | } | |
91 | } else { | |
92 | long filesize; | |
93 | ||
94 | fseek(metadata_fp, 0, SEEK_END); | |
95 | filesize = ftell(metadata_fp); | |
96 | rewind(metadata_fp); | |
97 | metadata_text = malloc(filesize + 1); | |
98 | if (!metadata_text) { | |
99 | fprintf(stderr, | |
100 | "Cannot allocate buffer for metadata text\n"); | |
101 | goto error; | |
102 | } | |
103 | ||
104 | if (fread(metadata_text, filesize, 1, metadata_fp) != 1) { | |
105 | fprintf(stderr, | |
106 | "Cannot read metadata file\n"); | |
107 | goto error; | |
108 | } | |
109 | ||
110 | metadata_text[filesize] = '\0'; | |
111 | } | |
112 | ||
113 | g_metadata_text = g_string_new(NULL); | |
114 | if (!g_metadata_text) { | |
115 | goto error; | |
116 | } | |
117 | ||
118 | if (strncmp(metadata_text, METADATA_TEXT_SIG, | |
119 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { | |
120 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); | |
121 | g_string_append(g_metadata_text, " */\n\n"); | |
122 | } | |
123 | ||
124 | g_string_append(g_metadata_text, metadata_text); | |
125 | ||
126 | ret = bt_value_map_insert_string(results, "text", | |
127 | g_metadata_text->str); | |
128 | if (ret) { | |
129 | fprintf(stderr, "Cannot insert metadata text into results\n"); | |
130 | goto error; | |
131 | } | |
132 | ||
133 | ret = bt_value_map_insert_bool(results, "is-packetized", | |
134 | is_packetized); | |
135 | if (ret) { | |
136 | fprintf(stderr, "Cannot insert is packetized into results\n"); | |
137 | goto error; | |
138 | } | |
139 | ||
140 | goto end; | |
141 | ||
142 | error: | |
143 | BT_PUT(results); | |
144 | ||
145 | end: | |
146 | bt_put(path_value); | |
147 | free(metadata_text); | |
148 | ||
149 | if (g_metadata_text) { | |
150 | g_string_free(g_metadata_text, TRUE); | |
151 | } | |
152 | ||
153 | if (metadata_fp) { | |
154 | fclose(metadata_fp); | |
155 | } | |
156 | return results; | |
157 | } |