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 | ||
98903a3e 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> | |
578e048b | 33 | #include "common/assert.h" |
04c0ba87 JG |
34 | #include "metadata.h" |
35 | #include "../common/metadata/decoder.h" | |
578e048b | 36 | #include "common/common.h" |
91d81473 | 37 | #include "common/macros.h" |
3fadfbc0 | 38 | #include <babeltrace2/babeltrace.h> |
97ade20b | 39 | #include "fs.h" |
55314f2a | 40 | |
04c0ba87 JG |
41 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
42 | ||
97ade20b JG |
43 | struct range { |
44 | int64_t begin_ns; | |
45 | int64_t end_ns; | |
46 | bool set; | |
47 | }; | |
48 | ||
04c0ba87 | 49 | BT_HIDDEN |
d24d5663 | 50 | bt_component_class_query_method_status metadata_info_query( |
b19ff26f | 51 | bt_self_component_class_source *comp_class, |
98903a3e | 52 | const bt_value *params, bt_logging_level log_level, |
b19ff26f | 53 | const bt_value **user_result) |
04c0ba87 | 54 | { |
d24d5663 PP |
55 | bt_component_class_query_method_status status = |
56 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
b19ff26f PP |
57 | bt_value *result = NULL; |
58 | const bt_value *path_value = NULL; | |
04c0ba87 JG |
59 | char *metadata_text = NULL; |
60 | FILE *metadata_fp = NULL; | |
61 | GString *g_metadata_text = NULL; | |
62 | int ret; | |
63 | int bo; | |
64 | const char *path; | |
65 | bool is_packetized; | |
66 | ||
05e21286 | 67 | result = bt_value_map_create(); |
da91b29a | 68 | if (!result) { |
d24d5663 | 69 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
04c0ba87 JG |
70 | goto error; |
71 | } | |
72 | ||
f6ccaed9 PP |
73 | BT_ASSERT(params); |
74 | ||
04c0ba87 | 75 | if (!bt_value_is_map(params)) { |
f7d0e29c | 76 | BT_LOGE_STR("Query parameters is not a map value object."); |
d24d5663 | 77 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS; |
04c0ba87 JG |
78 | goto error; |
79 | } | |
80 | ||
05e21286 | 81 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
07330b50 FD |
82 | if (!path_value) { |
83 | BT_LOGE_STR("Mandatory `path` parameter missing"); | |
d24d5663 | 84 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS; |
07330b50 FD |
85 | goto error; |
86 | } | |
87 | ||
88 | if (!bt_value_is_string(path_value)) { | |
89 | BT_LOGE_STR("`path` parameter is required to be a string value"); | |
d24d5663 | 90 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS; |
07330b50 FD |
91 | goto error; |
92 | } | |
93 | ||
601b0d3c | 94 | path = bt_value_string_get(path_value); |
04c0ba87 | 95 | |
f6ccaed9 | 96 | BT_ASSERT(path); |
04c0ba87 JG |
97 | metadata_fp = ctf_fs_metadata_open_file(path); |
98 | if (!metadata_fp) { | |
f7d0e29c | 99 | BT_LOGE("Cannot open trace metadata: path=\"%s\".", path); |
04c0ba87 JG |
100 | goto error; |
101 | } | |
102 | ||
103 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, | |
98903a3e | 104 | &bo, log_level, NULL); |
04c0ba87 JG |
105 | |
106 | if (is_packetized) { | |
107 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( | |
3c8252a5 PP |
108 | metadata_fp, &metadata_text, bo, NULL, NULL, |
109 | log_level, NULL); | |
04c0ba87 | 110 | if (ret) { |
f7d0e29c JG |
111 | BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"", |
112 | path); | |
04c0ba87 JG |
113 | goto error; |
114 | } | |
115 | } else { | |
116 | long filesize; | |
117 | ||
677e1a21 MD |
118 | ret = fseek(metadata_fp, 0, SEEK_END); |
119 | if (ret) { | |
f7d0e29c JG |
120 | BT_LOGE_ERRNO("Failed to seek to the end of the metadata file", |
121 | ": path=\"%s\"", path); | |
677e1a21 MD |
122 | goto error; |
123 | } | |
04c0ba87 | 124 | filesize = ftell(metadata_fp); |
677e1a21 | 125 | if (filesize < 0) { |
f7d0e29c JG |
126 | BT_LOGE_ERRNO("Failed to get the current position in the metadata file", |
127 | ": path=\"%s\"", path); | |
677e1a21 MD |
128 | goto error; |
129 | } | |
04c0ba87 JG |
130 | rewind(metadata_fp); |
131 | metadata_text = malloc(filesize + 1); | |
132 | if (!metadata_text) { | |
f7d0e29c | 133 | BT_LOGE_STR("Cannot allocate buffer for metadata text."); |
04c0ba87 JG |
134 | goto error; |
135 | } | |
136 | ||
137 | if (fread(metadata_text, filesize, 1, metadata_fp) != 1) { | |
f7d0e29c JG |
138 | BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"", |
139 | path); | |
04c0ba87 JG |
140 | goto error; |
141 | } | |
142 | ||
143 | metadata_text[filesize] = '\0'; | |
144 | } | |
145 | ||
146 | g_metadata_text = g_string_new(NULL); | |
147 | if (!g_metadata_text) { | |
148 | goto error; | |
149 | } | |
150 | ||
151 | if (strncmp(metadata_text, METADATA_TEXT_SIG, | |
152 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { | |
153 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); | |
154 | g_string_append(g_metadata_text, " */\n\n"); | |
155 | } | |
156 | ||
157 | g_string_append(g_metadata_text, metadata_text); | |
158 | ||
05e21286 | 159 | ret = bt_value_map_insert_string_entry(result, "text", |
04c0ba87 JG |
160 | g_metadata_text->str); |
161 | if (ret) { | |
f7d0e29c | 162 | BT_LOGE_STR("Cannot insert metadata text into query result."); |
04c0ba87 JG |
163 | goto error; |
164 | } | |
165 | ||
05e21286 | 166 | ret = bt_value_map_insert_bool_entry(result, "is-packetized", |
04c0ba87 JG |
167 | is_packetized); |
168 | if (ret) { | |
f7d0e29c | 169 | BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result."); |
04c0ba87 JG |
170 | goto error; |
171 | } | |
172 | ||
173 | goto end; | |
174 | ||
175 | error: | |
c5b9b441 | 176 | BT_VALUE_PUT_REF_AND_RESET(result); |
d94d92ac | 177 | result = NULL; |
c7eee084 | 178 | |
d94d92ac | 179 | if (status >= 0) { |
d24d5663 | 180 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
c7eee084 | 181 | } |
04c0ba87 JG |
182 | |
183 | end: | |
04c0ba87 JG |
184 | free(metadata_text); |
185 | ||
186 | if (g_metadata_text) { | |
187 | g_string_free(g_metadata_text, TRUE); | |
188 | } | |
189 | ||
190 | if (metadata_fp) { | |
191 | fclose(metadata_fp); | |
192 | } | |
c7eee084 | 193 | |
05e21286 | 194 | *user_result = result; |
d94d92ac | 195 | return status; |
97ade20b | 196 | } |
9ec238a8 | 197 | |
97ade20b | 198 | static |
b19ff26f | 199 | int add_range(bt_value *info, struct range *range, |
97ade20b JG |
200 | const char *range_name) |
201 | { | |
202 | int ret = 0; | |
d24d5663 | 203 | bt_value_map_insert_entry_status status; |
b19ff26f | 204 | bt_value *range_map = NULL; |
97ade20b JG |
205 | |
206 | if (!range->set) { | |
207 | /* Not an error. */ | |
208 | goto end; | |
209 | } | |
210 | ||
05e21286 | 211 | range_map = bt_value_map_create(); |
97ade20b JG |
212 | if (!range_map) { |
213 | ret = -1; | |
214 | goto end; | |
215 | } | |
216 | ||
fdd3a2da | 217 | status = bt_value_map_insert_signed_integer_entry(range_map, "begin", |
97ade20b | 218 | range->begin_ns); |
d24d5663 | 219 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
220 | ret = -1; |
221 | goto end; | |
222 | } | |
223 | ||
fdd3a2da | 224 | status = bt_value_map_insert_signed_integer_entry(range_map, "end", |
97ade20b | 225 | range->end_ns); |
d24d5663 | 226 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
227 | ret = -1; |
228 | goto end; | |
229 | } | |
230 | ||
05e21286 PP |
231 | status = bt_value_map_insert_entry(info, range_name, |
232 | range_map); | |
d24d5663 | 233 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
234 | ret = -1; |
235 | goto end; | |
236 | } | |
da91b29a | 237 | |
97ade20b | 238 | end: |
c5b9b441 | 239 | bt_value_put_ref(range_map); |
97ade20b JG |
240 | return ret; |
241 | } | |
242 | ||
243 | static | |
60363f2f | 244 | int add_stream_ids(bt_value *info, struct ctf_fs_ds_file_group *ds_file_group) |
97ade20b JG |
245 | { |
246 | int ret = 0; | |
d24d5663 | 247 | bt_value_map_insert_entry_status status; |
97ade20b | 248 | |
60363f2f | 249 | if (ds_file_group->stream_id != UINT64_C(-1)) { |
fdd3a2da PP |
250 | status = bt_value_map_insert_unsigned_integer_entry(info, "id", |
251 | ds_file_group->stream_id); | |
d24d5663 | 252 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
253 | ret = -1; |
254 | goto end; | |
255 | } | |
256 | } | |
257 | ||
fdd3a2da PP |
258 | status = bt_value_map_insert_unsigned_integer_entry(info, "class-id", |
259 | ds_file_group->sc->id); | |
d24d5663 | 260 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
261 | ret = -1; |
262 | goto end; | |
263 | } | |
0b29603d | 264 | |
97ade20b | 265 | end: |
97ade20b JG |
266 | return ret; |
267 | } | |
268 | ||
269 | static | |
270 | int populate_stream_info(struct ctf_fs_ds_file_group *group, | |
b19ff26f | 271 | bt_value *group_info, struct range *stream_range) |
97ade20b JG |
272 | { |
273 | int ret = 0; | |
274 | size_t file_idx; | |
d24d5663 PP |
275 | bt_value_map_insert_entry_status insert_status; |
276 | bt_value_array_append_element_status append_status; | |
b19ff26f | 277 | bt_value *file_paths; |
b1c001b6 | 278 | struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry; |
a38d7650 | 279 | gchar *port_name = NULL; |
97ade20b | 280 | |
05e21286 | 281 | file_paths = bt_value_array_create(); |
97ade20b JG |
282 | if (!file_paths) { |
283 | ret = -1; | |
284 | goto end; | |
285 | } | |
286 | ||
287 | for (file_idx = 0; file_idx < group->ds_file_infos->len; file_idx++) { | |
97ade20b | 288 | struct ctf_fs_ds_file_info *info = |
60363f2f PP |
289 | g_ptr_array_index(group->ds_file_infos, |
290 | file_idx); | |
97ade20b | 291 | |
d24d5663 | 292 | append_status = bt_value_array_append_string_element(file_paths, |
97ade20b | 293 | info->path->str); |
d24d5663 | 294 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
97ade20b JG |
295 | ret = -1; |
296 | goto end; | |
297 | } | |
97ade20b JG |
298 | } |
299 | ||
b1c001b6 | 300 | /* |
7ed5243a FD |
301 | * Since each `struct ctf_fs_ds_file_group` has a sorted array of |
302 | * `struct ctf_fs_ds_index_entry`, we can compute the stream range from | |
303 | * the timestamp_begin of the first index entry and the timestamp_end | |
304 | * of the last index entry. | |
b1c001b6 | 305 | */ |
7ed5243a FD |
306 | BT_ASSERT(group->index); |
307 | BT_ASSERT(group->index->entries); | |
308 | BT_ASSERT(group->index->entries->len > 0); | |
b1c001b6 | 309 | |
7ed5243a FD |
310 | /* First entry. */ |
311 | first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
312 | group->index->entries, 0); | |
b1c001b6 | 313 | |
7ed5243a FD |
314 | /* Last entry. */ |
315 | last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( | |
316 | group->index->entries, group->index->entries->len - 1); | |
b1c001b6 FD |
317 | |
318 | stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns; | |
319 | stream_range->end_ns = last_ds_index_entry->timestamp_end_ns; | |
b1c001b6 | 320 | |
1d4ac4b6 FD |
321 | /* |
322 | * If any of the begin and end timestamps is not set it means that | |
323 | * packets don't include `timestamp_begin` _and_ `timestamp_end` fields | |
324 | * in their packet context so we can't set the range. | |
325 | */ | |
326 | stream_range->set = stream_range->begin_ns != UINT64_C(-1) && | |
327 | stream_range->end_ns != UINT64_C(-1); | |
328 | ||
329 | ret = add_range(group_info, stream_range, "range-ns"); | |
330 | if (ret) { | |
331 | goto end; | |
97ade20b JG |
332 | } |
333 | ||
d24d5663 | 334 | insert_status = bt_value_map_insert_entry(group_info, "paths", |
05e21286 | 335 | file_paths); |
d24d5663 | 336 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
337 | ret = -1; |
338 | goto end; | |
339 | } | |
340 | ||
60363f2f | 341 | ret = add_stream_ids(group_info, group); |
97ade20b JG |
342 | if (ret) { |
343 | goto end; | |
344 | } | |
a38d7650 SM |
345 | |
346 | port_name = ctf_fs_make_port_name(group); | |
347 | if (!port_name) { | |
348 | ret = -1; | |
349 | goto end; | |
350 | } | |
351 | ||
d24d5663 PP |
352 | insert_status = bt_value_map_insert_string_entry(group_info, |
353 | "port-name", port_name); | |
354 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
a38d7650 SM |
355 | ret = -1; |
356 | goto end; | |
357 | } | |
358 | ||
97ade20b | 359 | end: |
19bbdc9b | 360 | g_free(port_name); |
c5b9b441 | 361 | bt_value_put_ref(file_paths); |
97ade20b JG |
362 | return ret; |
363 | } | |
364 | ||
365 | static | |
f280892e | 366 | int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info) |
97ade20b JG |
367 | { |
368 | int ret = 0; | |
369 | size_t group_idx; | |
d24d5663 PP |
370 | bt_value_map_insert_entry_status insert_status; |
371 | bt_value_array_append_element_status append_status; | |
f280892e | 372 | bt_value *file_groups = NULL; |
97ade20b JG |
373 | struct range trace_range = { |
374 | .begin_ns = INT64_MAX, | |
375 | .end_ns = 0, | |
376 | .set = false, | |
377 | }; | |
378 | struct range trace_intersection = { | |
379 | .begin_ns = 0, | |
380 | .end_ns = INT64_MAX, | |
381 | .set = false, | |
382 | }; | |
383 | ||
f280892e SM |
384 | BT_ASSERT(trace->ds_file_groups); |
385 | /* Add trace range info only if it contains streams. */ | |
386 | if (trace->ds_file_groups->len == 0) { | |
387 | ret = -1; | |
388 | goto end; | |
389 | } | |
390 | ||
05e21286 | 391 | file_groups = bt_value_array_create(); |
97ade20b JG |
392 | if (!file_groups) { |
393 | goto end; | |
394 | } | |
395 | ||
d24d5663 | 396 | insert_status = bt_value_map_insert_string_entry(trace_info, "name", |
f280892e | 397 | trace->name->str); |
d24d5663 | 398 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
399 | ret = -1; |
400 | goto end; | |
401 | } | |
d24d5663 | 402 | insert_status = bt_value_map_insert_string_entry(trace_info, "path", |
f280892e | 403 | trace->path->str); |
d24d5663 | 404 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
405 | ret = -1; |
406 | goto end; | |
407 | } | |
408 | ||
97ade20b JG |
409 | /* Find range of all stream groups, and of the trace. */ |
410 | for (group_idx = 0; group_idx < trace->ds_file_groups->len; | |
411 | group_idx++) { | |
b19ff26f | 412 | bt_value *group_info; |
97ade20b JG |
413 | struct range group_range = { .set = false }; |
414 | struct ctf_fs_ds_file_group *group = g_ptr_array_index( | |
415 | trace->ds_file_groups, group_idx); | |
416 | ||
05e21286 | 417 | group_info = bt_value_map_create(); |
97ade20b JG |
418 | if (!group_info) { |
419 | ret = -1; | |
420 | goto end; | |
421 | } | |
422 | ||
423 | ret = populate_stream_info(group, group_info, &group_range); | |
424 | if (ret) { | |
c5b9b441 | 425 | bt_value_put_ref(group_info); |
97ade20b JG |
426 | goto end; |
427 | } | |
428 | ||
d24d5663 PP |
429 | append_status = bt_value_array_append_element(file_groups, |
430 | group_info); | |
7233204d | 431 | bt_value_put_ref(group_info); |
d24d5663 | 432 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
7233204d FD |
433 | goto end; |
434 | } | |
435 | ||
97ade20b | 436 | if (group_range.set) { |
91d81473 | 437 | trace_range.begin_ns = MIN(trace_range.begin_ns, |
97ade20b | 438 | group_range.begin_ns); |
91d81473 | 439 | trace_range.end_ns = MAX(trace_range.end_ns, |
97ade20b JG |
440 | group_range.end_ns); |
441 | trace_range.set = true; | |
442 | ||
91d81473 | 443 | trace_intersection.begin_ns = MAX(trace_intersection.begin_ns, |
97ade20b | 444 | group_range.begin_ns); |
91d81473 | 445 | trace_intersection.end_ns = MIN(trace_intersection.end_ns, |
97ade20b JG |
446 | group_range.end_ns); |
447 | trace_intersection.set = true; | |
97ade20b JG |
448 | } |
449 | } | |
450 | ||
451 | ret = add_range(trace_info, &trace_range, "range-ns"); | |
452 | if (ret) { | |
453 | goto end; | |
454 | } | |
c6daf8b2 PP |
455 | |
456 | if (trace_intersection.begin_ns < trace_intersection.end_ns) { | |
457 | ret = add_range(trace_info, &trace_intersection, | |
458 | "intersection-range-ns"); | |
459 | if (ret) { | |
460 | goto end; | |
461 | } | |
97ade20b JG |
462 | } |
463 | ||
d24d5663 | 464 | insert_status = bt_value_map_insert_entry(trace_info, "streams", |
05e21286 | 465 | file_groups); |
c5b9b441 | 466 | BT_VALUE_PUT_REF_AND_RESET(file_groups); |
d24d5663 | 467 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
97ade20b JG |
468 | ret = -1; |
469 | goto end; | |
470 | } | |
471 | ||
472 | end: | |
c5b9b441 | 473 | bt_value_put_ref(file_groups); |
97ade20b JG |
474 | return ret; |
475 | } | |
476 | ||
477 | BT_HIDDEN | |
d24d5663 | 478 | bt_component_class_query_method_status trace_info_query( |
b19ff26f | 479 | bt_self_component_class_source *comp_class, |
98903a3e | 480 | const bt_value *params, bt_logging_level log_level, |
b19ff26f | 481 | const bt_value **user_result) |
97ade20b | 482 | { |
f280892e | 483 | struct ctf_fs_component *ctf_fs = NULL; |
d24d5663 PP |
484 | bt_component_class_query_method_status status = |
485 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
b19ff26f | 486 | bt_value *result = NULL; |
73760435 | 487 | const bt_value *inputs_value = NULL; |
97ade20b | 488 | int ret = 0; |
f280892e | 489 | guint i; |
97ade20b | 490 | |
f6ccaed9 PP |
491 | BT_ASSERT(params); |
492 | ||
97ade20b JG |
493 | if (!bt_value_is_map(params)) { |
494 | BT_LOGE("Query parameters is not a map value object."); | |
d24d5663 | 495 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS; |
97ade20b JG |
496 | goto error; |
497 | } | |
498 | ||
4c65a157 | 499 | ctf_fs = ctf_fs_component_create(log_level, NULL); |
d907165c | 500 | if (!ctf_fs) { |
97ade20b JG |
501 | goto error; |
502 | } | |
97ade20b | 503 | |
73760435 | 504 | if (!read_src_fs_parameters(params, &inputs_value, ctf_fs)) { |
d24d5663 | 505 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS; |
97ade20b JG |
506 | goto error; |
507 | } | |
508 | ||
73760435 | 509 | if (ctf_fs_component_create_ctf_fs_traces(NULL, ctf_fs, inputs_value)) { |
97ade20b JG |
510 | goto error; |
511 | } | |
512 | ||
05e21286 | 513 | result = bt_value_array_create(); |
da91b29a | 514 | if (!result) { |
d24d5663 | 515 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
97ade20b JG |
516 | goto error; |
517 | } | |
518 | ||
f280892e SM |
519 | for (i = 0; i < ctf_fs->traces->len; i++) { |
520 | struct ctf_fs_trace *trace; | |
b19ff26f | 521 | bt_value *trace_info; |
d24d5663 | 522 | bt_value_array_append_element_status append_status; |
f280892e SM |
523 | |
524 | trace = g_ptr_array_index(ctf_fs->traces, i); | |
525 | BT_ASSERT(trace); | |
97ade20b | 526 | |
05e21286 | 527 | trace_info = bt_value_map_create(); |
97ade20b JG |
528 | if (!trace_info) { |
529 | BT_LOGE("Failed to create trace info map."); | |
530 | goto error; | |
531 | } | |
532 | ||
f280892e | 533 | ret = populate_trace_info(trace, trace_info); |
97ade20b | 534 | if (ret) { |
c5b9b441 | 535 | bt_value_put_ref(trace_info); |
97ade20b JG |
536 | goto error; |
537 | } | |
538 | ||
d24d5663 PP |
539 | append_status = bt_value_array_append_element(result, |
540 | trace_info); | |
c5b9b441 | 541 | bt_value_put_ref(trace_info); |
d24d5663 | 542 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
97ade20b JG |
543 | goto error; |
544 | } | |
545 | } | |
546 | ||
547 | goto end; | |
548 | ||
549 | error: | |
c5b9b441 | 550 | BT_VALUE_PUT_REF_AND_RESET(result); |
d94d92ac | 551 | result = NULL; |
c7eee084 | 552 | |
d94d92ac | 553 | if (status >= 0) { |
d24d5663 | 554 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
c7eee084 PP |
555 | } |
556 | ||
97ade20b | 557 | end: |
f280892e SM |
558 | if (ctf_fs) { |
559 | ctf_fs_destroy(ctf_fs); | |
560 | ctf_fs = NULL; | |
97ade20b | 561 | } |
d94d92ac | 562 | |
05e21286 | 563 | *user_result = result; |
d94d92ac | 564 | return status; |
04c0ba87 | 565 | } |
73760435 SM |
566 | |
567 | BT_HIDDEN | |
568 | bt_component_class_query_method_status support_info_query( | |
569 | bt_self_component_class_source *comp_class, | |
570 | const bt_value *params, bt_logging_level log_level, | |
571 | const bt_value **user_result) | |
572 | { | |
573 | const bt_value *input_type_value; | |
574 | const char *input_type; | |
575 | bt_component_class_query_method_status status; | |
576 | double weight = 0; | |
577 | gchar *metadata_path = NULL; | |
578 | bt_value *result = NULL; | |
579 | ||
580 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
581 | BT_ASSERT(input_type_value); | |
582 | BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING); | |
583 | input_type = bt_value_string_get(input_type_value); | |
584 | ||
585 | result = bt_value_map_create(); | |
586 | if (!result) { | |
587 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
588 | goto end; | |
589 | } | |
590 | ||
591 | if (strcmp(input_type, "directory") == 0) { | |
592 | const bt_value *input_value; | |
593 | const char *path; | |
594 | ||
595 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
596 | BT_ASSERT(input_value); | |
597 | BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING); | |
598 | path = bt_value_string_get(input_value); | |
599 | ||
600 | metadata_path = g_build_filename(path, CTF_FS_METADATA_FILENAME, NULL); | |
601 | if (!metadata_path) { | |
602 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
603 | goto end; | |
604 | } | |
605 | ||
606 | /* | |
607 | * If the metadata file exists in this directory, consider it to | |
608 | * be a CTF trace. | |
609 | */ | |
610 | if (g_file_test(metadata_path, G_FILE_TEST_EXISTS)) { | |
611 | weight = 0.5; | |
612 | } | |
613 | } | |
614 | ||
615 | if (bt_value_map_insert_real_entry(result, "weight", weight) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
616 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
617 | goto end; | |
618 | } | |
619 | ||
620 | /* | |
621 | * Use the arbitrary constant string "ctf" as the group, such that all | |
622 | * found ctf traces are passed to the same instance of src.ctf.fs. | |
623 | */ | |
624 | if (bt_value_map_insert_string_entry(result, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
625 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
626 | goto end; | |
627 | } | |
628 | ||
629 | *user_result = result; | |
630 | result = NULL; | |
631 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
632 | ||
633 | end: | |
634 | g_free(metadata_path); | |
635 | bt_value_put_ref(result); | |
636 | ||
637 | return status; | |
638 | } |