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 | ||
3d731ea9 PP |
27 | #define BT_LOG_OUTPUT_LEVEL log_level |
28 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY" | |
29 | #include "logging/log.h" | |
30 | ||
04c0ba87 JG |
31 | #include "query.h" |
32 | #include <stdbool.h> | |
1d3426bc SM |
33 | #include <glib.h> |
34 | #include <glib/gstdio.h> | |
35 | #include <fcntl.h> | |
36 | #include <sys/types.h> | |
37 | #include <sys/stat.h> | |
57952005 | 38 | #include "common/assert.h" |
04c0ba87 JG |
39 | #include "metadata.h" |
40 | #include "../common/metadata/decoder.h" | |
57952005 | 41 | #include "common/common.h" |
85e7137b | 42 | #include "common/macros.h" |
71c5da58 | 43 | #include <babeltrace2/babeltrace.h> |
97ade20b | 44 | #include "fs.h" |
5c3441d8 | 45 | #include "logging/comp-logging.h" |
55314f2a | 46 | |
04c0ba87 JG |
47 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
48 | ||
97ade20b JG |
49 | struct range { |
50 | int64_t begin_ns; | |
51 | int64_t end_ns; | |
52 | bool set; | |
53 | }; | |
54 | ||
04c0ba87 | 55 | BT_HIDDEN |
fb25b9e3 | 56 | bt_component_class_query_method_status metadata_info_query( |
5c3441d8 | 57 | bt_self_component_class_source *self_comp_class_src, |
3d731ea9 | 58 | const bt_value *params, bt_logging_level log_level, |
8eee8ea2 | 59 | const bt_value **user_result) |
04c0ba87 | 60 | { |
fb25b9e3 PP |
61 | bt_component_class_query_method_status status = |
62 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
5c3441d8 SM |
63 | bt_self_component_class *self_comp_class = |
64 | bt_self_component_class_source_as_self_component_class(self_comp_class_src); | |
8eee8ea2 PP |
65 | bt_value *result = NULL; |
66 | const bt_value *path_value = NULL; | |
04c0ba87 | 67 | FILE *metadata_fp = NULL; |
04c0ba87 JG |
68 | int ret; |
69 | int bo; | |
70 | const char *path; | |
71 | bool is_packetized; | |
526c3a49 PP |
72 | struct ctf_metadata_decoder *decoder = NULL; |
73 | struct ctf_metadata_decoder_config decoder_cfg = { 0 }; | |
74 | enum ctf_metadata_decoder_status decoder_status; | |
04c0ba87 | 75 | |
ce141536 | 76 | result = bt_value_map_create(); |
17582c6d | 77 | if (!result) { |
fb25b9e3 | 78 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
04c0ba87 JG |
79 | goto error; |
80 | } | |
81 | ||
8b45963b PP |
82 | BT_ASSERT(params); |
83 | ||
04c0ba87 | 84 | if (!bt_value_is_map(params)) { |
5c3441d8 SM |
85 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
86 | "Query parameters is not a map value object."); | |
574dea68 | 87 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
04c0ba87 JG |
88 | goto error; |
89 | } | |
90 | ||
ce141536 | 91 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
43eacc52 | 92 | if (!path_value) { |
5c3441d8 SM |
93 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
94 | "Mandatory `path` parameter missing"); | |
574dea68 | 95 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
43eacc52 FD |
96 | goto error; |
97 | } | |
98 | ||
99 | if (!bt_value_is_string(path_value)) { | |
5c3441d8 SM |
100 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
101 | "`path` parameter is required to be a string value"); | |
574dea68 | 102 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
43eacc52 FD |
103 | goto error; |
104 | } | |
105 | ||
b5cdc106 | 106 | path = bt_value_string_get(path_value); |
04c0ba87 | 107 | |
8b45963b | 108 | BT_ASSERT(path); |
04c0ba87 JG |
109 | metadata_fp = ctf_fs_metadata_open_file(path); |
110 | if (!metadata_fp) { | |
5c3441d8 SM |
111 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
112 | "Cannot open trace metadata: path=\"%s\".", path); | |
04c0ba87 JG |
113 | goto error; |
114 | } | |
115 | ||
526c3a49 | 116 | ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, |
3d731ea9 | 117 | &bo, log_level, NULL); |
526c3a49 PP |
118 | if (ret) { |
119 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, | |
120 | "Cannot check whether or not the metadata stream is packetized: path=\"%s\".", | |
121 | path); | |
122 | goto error; | |
04c0ba87 JG |
123 | } |
124 | ||
526c3a49 PP |
125 | decoder_cfg.log_level = log_level; |
126 | decoder_cfg.keep_plain_text = true; | |
127 | decoder = ctf_metadata_decoder_create(&decoder_cfg); | |
128 | if (!decoder) { | |
129 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, | |
130 | "Cannot create metadata decoder: path=\"%s\".", path); | |
04c0ba87 JG |
131 | goto error; |
132 | } | |
133 | ||
526c3a49 PP |
134 | rewind(metadata_fp); |
135 | decoder_status = ctf_metadata_decoder_append_content(decoder, | |
136 | metadata_fp); | |
137 | if (decoder_status) { | |
138 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, | |
139 | "Cannot update metadata decoder's content: path=\"%s\".", | |
140 | path); | |
141 | goto error; | |
04c0ba87 JG |
142 | } |
143 | ||
ce141536 | 144 | ret = bt_value_map_insert_string_entry(result, "text", |
526c3a49 | 145 | ctf_metadata_decoder_get_text(decoder)); |
04c0ba87 | 146 | if (ret) { |
5c3441d8 SM |
147 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
148 | "Cannot insert metadata text into query result."); | |
04c0ba87 JG |
149 | goto error; |
150 | } | |
151 | ||
ce141536 | 152 | ret = bt_value_map_insert_bool_entry(result, "is-packetized", |
04c0ba87 JG |
153 | is_packetized); |
154 | if (ret) { | |
5c3441d8 SM |
155 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
156 | "Cannot insert \"is-packetized\" attribute into query result."); | |
04c0ba87 JG |
157 | goto error; |
158 | } | |
159 | ||
160 | goto end; | |
161 | ||
162 | error: | |
8c6884d9 | 163 | BT_VALUE_PUT_REF_AND_RESET(result); |
834e9996 | 164 | result = NULL; |
1286dcbb | 165 | |
834e9996 | 166 | if (status >= 0) { |
fb25b9e3 | 167 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
1286dcbb | 168 | } |
04c0ba87 JG |
169 | |
170 | end: | |
526c3a49 | 171 | ctf_metadata_decoder_destroy(decoder); |
04c0ba87 JG |
172 | |
173 | if (metadata_fp) { | |
174 | fclose(metadata_fp); | |
175 | } | |
1286dcbb | 176 | |
ce141536 | 177 | *user_result = result; |
834e9996 | 178 | return status; |
97ade20b | 179 | } |
13160acc | 180 | |
97ade20b | 181 | static |
8eee8ea2 | 182 | int add_range(bt_value *info, struct range *range, |
97ade20b JG |
183 | const char *range_name) |
184 | { | |
185 | int ret = 0; | |
fb25b9e3 | 186 | bt_value_map_insert_entry_status status; |
de37c3a9 | 187 | bt_value *range_map; |
97ade20b JG |
188 | |
189 | if (!range->set) { | |
190 | /* Not an error. */ | |
191 | goto end; | |
192 | } | |
193 | ||
de37c3a9 SM |
194 | status = bt_value_map_insert_empty_map_entry(info, range_name, |
195 | &range_map); | |
196 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
97ade20b JG |
197 | ret = -1; |
198 | goto end; | |
199 | } | |
200 | ||
68d9d039 | 201 | status = bt_value_map_insert_signed_integer_entry(range_map, "begin", |
97ade20b | 202 | range->begin_ns); |
fb25b9e3 | 203 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
204 | ret = -1; |
205 | goto end; | |
206 | } | |
207 | ||
68d9d039 | 208 | status = bt_value_map_insert_signed_integer_entry(range_map, "end", |
97ade20b | 209 | range->end_ns); |
fb25b9e3 | 210 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
211 | ret = -1; |
212 | goto end; | |
213 | } | |
214 | ||
97ade20b | 215 | end: |
97ade20b JG |
216 | return ret; |
217 | } | |
218 | ||
97ade20b JG |
219 | static |
220 | int populate_stream_info(struct ctf_fs_ds_file_group *group, | |
8eee8ea2 | 221 | bt_value *group_info, struct range *stream_range) |
97ade20b JG |
222 | { |
223 | int ret = 0; | |
fb25b9e3 | 224 | bt_value_map_insert_entry_status insert_status; |
1924bed7 | 225 | struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry; |
ddf49b27 | 226 | gchar *port_name = NULL; |
97ade20b | 227 | |
1924bed7 | 228 | /* |
779f4a23 FD |
229 | * Since each `struct ctf_fs_ds_file_group` has a sorted array of |
230 | * `struct ctf_fs_ds_index_entry`, we can compute the stream range from | |
231 | * the timestamp_begin of the first index entry and the timestamp_end | |
232 | * of the last index entry. | |
1924bed7 | 233 | */ |
779f4a23 FD |
234 | BT_ASSERT(group->index); |
235 | BT_ASSERT(group->index->entries); | |
236 | BT_ASSERT(group->index->entries->len > 0); | |
1924bed7 | 237 | |
779f4a23 FD |
238 | /* First entry. */ |
239 | first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
240 | group->index->entries, 0); | |
1924bed7 | 241 | |
779f4a23 FD |
242 | /* Last entry. */ |
243 | last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
244 | group->index->entries, group->index->entries->len - 1); | |
1924bed7 FD |
245 | |
246 | stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns; | |
247 | stream_range->end_ns = last_ds_index_entry->timestamp_end_ns; | |
1924bed7 | 248 | |
58117c6d FD |
249 | /* |
250 | * If any of the begin and end timestamps is not set it means that | |
251 | * packets don't include `timestamp_begin` _and_ `timestamp_end` fields | |
252 | * in their packet context so we can't set the range. | |
253 | */ | |
254 | stream_range->set = stream_range->begin_ns != UINT64_C(-1) && | |
255 | stream_range->end_ns != UINT64_C(-1); | |
256 | ||
257 | ret = add_range(group_info, stream_range, "range-ns"); | |
258 | if (ret) { | |
259 | goto end; | |
97ade20b JG |
260 | } |
261 | ||
ddf49b27 SM |
262 | port_name = ctf_fs_make_port_name(group); |
263 | if (!port_name) { | |
264 | ret = -1; | |
265 | goto end; | |
266 | } | |
267 | ||
fb25b9e3 PP |
268 | insert_status = bt_value_map_insert_string_entry(group_info, |
269 | "port-name", port_name); | |
270 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
ddf49b27 SM |
271 | ret = -1; |
272 | goto end; | |
273 | } | |
274 | ||
97ade20b | 275 | end: |
be1b2e7e | 276 | g_free(port_name); |
97ade20b JG |
277 | return ret; |
278 | } | |
279 | ||
280 | static | |
bf5b7748 | 281 | int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info) |
97ade20b JG |
282 | { |
283 | int ret = 0; | |
284 | size_t group_idx; | |
fb25b9e3 PP |
285 | bt_value_map_insert_entry_status insert_status; |
286 | bt_value_array_append_element_status append_status; | |
bf5b7748 | 287 | bt_value *file_groups = NULL; |
97ade20b JG |
288 | struct range trace_intersection = { |
289 | .begin_ns = 0, | |
290 | .end_ns = INT64_MAX, | |
291 | .set = false, | |
292 | }; | |
293 | ||
bf5b7748 SM |
294 | BT_ASSERT(trace->ds_file_groups); |
295 | /* Add trace range info only if it contains streams. */ | |
296 | if (trace->ds_file_groups->len == 0) { | |
297 | ret = -1; | |
298 | goto end; | |
299 | } | |
300 | ||
de37c3a9 SM |
301 | insert_status = bt_value_map_insert_empty_array_entry(trace_info, |
302 | "streams", &file_groups); | |
303 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
304 | ret = -1; | |
97ade20b JG |
305 | goto end; |
306 | } | |
307 | ||
97ade20b JG |
308 | /* Find range of all stream groups, and of the trace. */ |
309 | for (group_idx = 0; group_idx < trace->ds_file_groups->len; | |
310 | group_idx++) { | |
8eee8ea2 | 311 | bt_value *group_info; |
97ade20b JG |
312 | struct range group_range = { .set = false }; |
313 | struct ctf_fs_ds_file_group *group = g_ptr_array_index( | |
314 | trace->ds_file_groups, group_idx); | |
315 | ||
de37c3a9 SM |
316 | append_status = bt_value_array_append_empty_map_element( |
317 | file_groups, &group_info); | |
318 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
97ade20b JG |
319 | ret = -1; |
320 | goto end; | |
321 | } | |
322 | ||
323 | ret = populate_stream_info(group, group_info, &group_range); | |
324 | if (ret) { | |
ab19273b FD |
325 | goto end; |
326 | } | |
327 | ||
97ade20b | 328 | if (group_range.set) { |
85e7137b | 329 | trace_intersection.begin_ns = MAX(trace_intersection.begin_ns, |
97ade20b | 330 | group_range.begin_ns); |
85e7137b | 331 | trace_intersection.end_ns = MIN(trace_intersection.end_ns, |
97ade20b JG |
332 | group_range.end_ns); |
333 | trace_intersection.set = true; | |
97ade20b JG |
334 | } |
335 | } | |
336 | ||
8ea849b8 PP |
337 | if (trace_intersection.begin_ns < trace_intersection.end_ns) { |
338 | ret = add_range(trace_info, &trace_intersection, | |
339 | "intersection-range-ns"); | |
340 | if (ret) { | |
341 | goto end; | |
342 | } | |
97ade20b JG |
343 | } |
344 | ||
97ade20b | 345 | end: |
97ade20b JG |
346 | return ret; |
347 | } | |
348 | ||
349 | BT_HIDDEN | |
fb25b9e3 | 350 | bt_component_class_query_method_status trace_info_query( |
5c3441d8 | 351 | bt_self_component_class_source *self_comp_class_src, |
3d731ea9 | 352 | const bt_value *params, bt_logging_level log_level, |
8eee8ea2 | 353 | const bt_value **user_result) |
97ade20b | 354 | { |
bf5b7748 | 355 | struct ctf_fs_component *ctf_fs = NULL; |
fb25b9e3 PP |
356 | bt_component_class_query_method_status status = |
357 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
5c3441d8 SM |
358 | bt_self_component_class *self_comp_class = |
359 | bt_self_component_class_source_as_self_component_class( | |
360 | self_comp_class_src); | |
8eee8ea2 | 361 | bt_value *result = NULL; |
a1040187 | 362 | const bt_value *inputs_value = NULL; |
97ade20b | 363 | int ret = 0; |
bf5b7748 | 364 | guint i; |
97ade20b | 365 | |
8b45963b PP |
366 | BT_ASSERT(params); |
367 | ||
97ade20b | 368 | if (!bt_value_is_map(params)) { |
5c3441d8 SM |
369 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
370 | "Query parameters is not a map value object."); | |
574dea68 | 371 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
97ade20b JG |
372 | goto error; |
373 | } | |
374 | ||
f67894f9 | 375 | ctf_fs = ctf_fs_component_create(log_level, NULL); |
c80d4c65 | 376 | if (!ctf_fs) { |
97ade20b JG |
377 | goto error; |
378 | } | |
97ade20b | 379 | |
5c3441d8 SM |
380 | if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL, |
381 | self_comp_class)) { | |
574dea68 | 382 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
97ade20b JG |
383 | goto error; |
384 | } | |
385 | ||
5c3441d8 SM |
386 | if (ctf_fs_component_create_ctf_fs_traces(ctf_fs, inputs_value, NULL, |
387 | self_comp_class)) { | |
97ade20b JG |
388 | goto error; |
389 | } | |
390 | ||
ce141536 | 391 | result = bt_value_array_create(); |
17582c6d | 392 | if (!result) { |
fb25b9e3 | 393 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
97ade20b JG |
394 | goto error; |
395 | } | |
396 | ||
bf5b7748 SM |
397 | for (i = 0; i < ctf_fs->traces->len; i++) { |
398 | struct ctf_fs_trace *trace; | |
8eee8ea2 | 399 | bt_value *trace_info; |
fb25b9e3 | 400 | bt_value_array_append_element_status append_status; |
bf5b7748 SM |
401 | |
402 | trace = g_ptr_array_index(ctf_fs->traces, i); | |
403 | BT_ASSERT(trace); | |
97ade20b | 404 | |
de37c3a9 SM |
405 | append_status = bt_value_array_append_empty_map_element(result, |
406 | &trace_info); | |
407 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
5c3441d8 SM |
408 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
409 | "Failed to create trace info map."); | |
97ade20b JG |
410 | goto error; |
411 | } | |
412 | ||
bf5b7748 | 413 | ret = populate_trace_info(trace, trace_info); |
97ade20b | 414 | if (ret) { |
97ade20b JG |
415 | goto error; |
416 | } | |
417 | } | |
418 | ||
419 | goto end; | |
420 | ||
421 | error: | |
8c6884d9 | 422 | BT_VALUE_PUT_REF_AND_RESET(result); |
834e9996 | 423 | result = NULL; |
1286dcbb | 424 | |
834e9996 | 425 | if (status >= 0) { |
fb25b9e3 | 426 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
1286dcbb PP |
427 | } |
428 | ||
97ade20b | 429 | end: |
bf5b7748 SM |
430 | if (ctf_fs) { |
431 | ctf_fs_destroy(ctf_fs); | |
432 | ctf_fs = NULL; | |
97ade20b | 433 | } |
834e9996 | 434 | |
ce141536 | 435 | *user_result = result; |
834e9996 | 436 | return status; |
04c0ba87 | 437 | } |
a1040187 SM |
438 | |
439 | BT_HIDDEN | |
440 | bt_component_class_query_method_status support_info_query( | |
441 | bt_self_component_class_source *comp_class, | |
442 | const bt_value *params, bt_logging_level log_level, | |
443 | const bt_value **user_result) | |
444 | { | |
445 | const bt_value *input_type_value; | |
446 | const char *input_type; | |
447 | bt_component_class_query_method_status status; | |
1d3426bc | 448 | bt_value_map_insert_entry_status insert_entry_status; |
a1040187 SM |
449 | double weight = 0; |
450 | gchar *metadata_path = NULL; | |
451 | bt_value *result = NULL; | |
1d3426bc SM |
452 | struct ctf_metadata_decoder *metadata_decoder = NULL; |
453 | FILE *metadata_file = NULL; | |
454 | char uuid_str[BT_UUID_STR_LEN + 1]; | |
455 | bool has_uuid = false; | |
456 | const bt_value *input_value; | |
457 | const char *input; | |
a1040187 SM |
458 | |
459 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
460 | BT_ASSERT(input_type_value); | |
461 | BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING); | |
462 | input_type = bt_value_string_get(input_type_value); | |
463 | ||
1d3426bc SM |
464 | if (strcmp(input_type, "directory") != 0) { |
465 | goto create_result; | |
466 | } | |
467 | ||
468 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
469 | BT_ASSERT(input_value); | |
470 | BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING); | |
471 | input = bt_value_string_get(input_value); | |
472 | ||
473 | metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL); | |
474 | if (!metadata_path) { | |
a1040187 SM |
475 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
476 | goto end; | |
477 | } | |
478 | ||
1d3426bc SM |
479 | metadata_file = g_fopen(metadata_path, "r"); |
480 | if (metadata_file) { | |
481 | struct ctf_metadata_decoder_config metadata_decoder_config = { 0 }; | |
482 | enum ctf_metadata_decoder_status decoder_status; | |
483 | bt_uuid_t uuid; | |
a1040187 | 484 | |
1d3426bc | 485 | metadata_decoder_config.log_level = log_level; |
a1040187 | 486 | |
1d3426bc SM |
487 | metadata_decoder = ctf_metadata_decoder_create( |
488 | &metadata_decoder_config); | |
489 | if (!metadata_decoder) { | |
490 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
491 | goto end; | |
492 | } | |
493 | ||
494 | decoder_status = ctf_metadata_decoder_append_content( | |
495 | metadata_decoder, metadata_file); | |
496 | if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) { | |
497 | BT_LOGW("cannot append metadata content: metadata-decoder-status=%d", | |
498 | decoder_status); | |
499 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
a1040187 SM |
500 | goto end; |
501 | } | |
502 | ||
503 | /* | |
1d3426bc SM |
504 | * We were able to parse the metadata file, so we are |
505 | * confident it's a CTF trace. | |
a1040187 | 506 | */ |
1d3426bc SM |
507 | weight = 0.75; |
508 | ||
509 | /* If the trace has a UUID, return the stringified UUID as the group. */ | |
510 | if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) { | |
511 | bt_uuid_to_str(uuid, uuid_str); | |
512 | has_uuid = true; | |
a1040187 SM |
513 | } |
514 | } | |
515 | ||
1d3426bc SM |
516 | create_result: |
517 | result = bt_value_map_create(); | |
518 | if (!result) { | |
a1040187 SM |
519 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
520 | goto end; | |
521 | } | |
522 | ||
1d3426bc SM |
523 | insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight); |
524 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
525 | status = (int) insert_entry_status; | |
a1040187 SM |
526 | goto end; |
527 | } | |
528 | ||
1d3426bc SM |
529 | /* We are not supposed to have weight == 0 and a UUID. */ |
530 | BT_ASSERT(weight > 0 || !has_uuid); | |
531 | ||
532 | if (weight > 0 && has_uuid) { | |
533 | insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str); | |
534 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
535 | status = (int) insert_entry_status; | |
536 | goto end; | |
537 | } | |
538 | } | |
539 | ||
a1040187 SM |
540 | *user_result = result; |
541 | result = NULL; | |
542 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
543 | ||
544 | end: | |
545 | g_free(metadata_path); | |
546 | bt_value_put_ref(result); | |
1d3426bc | 547 | ctf_metadata_decoder_destroy(metadata_decoder); |
a1040187 SM |
548 | |
549 | return status; | |
550 | } |