src.ctf.fs: use GCharUP to hold metadata path
[babeltrace.git] / src / plugins / ctf / fs-src / query.cpp
CommitLineData
04c0ba87 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
04c0ba87
JG
3 *
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
0235b0db 6 * Babeltrace CTF file system Reader Component queries
04c0ba87
JG
7 */
8
c802cacb
SM
9#include <glib.h>
10#include <glib/gstdio.h>
c802cacb
SM
11#include <sys/types.h>
12
13#include <babeltrace2/babeltrace.h>
14
5656cea5 15#include "../common/src/metadata/tsdl/decoder.hpp"
087cd0f5 16#include "fs.hpp"
c802cacb 17#include "query.hpp"
55314f2a 18
4164020e 19#define METADATA_TEXT_SIG "/* CTF 1.8"
04c0ba87 20
4164020e
SM
21struct range
22{
23 int64_t begin_ns = 0;
24 int64_t end_ns = 0;
25 bool set = false;
97ade20b
JG
26};
27
0f5c5d5c
SM
28bt_component_class_query_method_status metadata_info_query(const bt_value *params,
29 const bt2c::Logger& logger,
30 const bt_value **user_result)
04c0ba87 31{
4164020e 32 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
4164020e
SM
33 bt_value *result = NULL;
34 const bt_value *path_value = NULL;
35 FILE *metadata_fp = NULL;
36 int ret;
37 int bo;
38 const char *path;
39 bool is_packetized;
40 struct ctf_metadata_decoder *decoder = NULL;
0f5c5d5c 41 ctf_metadata_decoder_config decoder_cfg {logger};
4164020e 42 enum ctf_metadata_decoder_status decoder_status;
c719eabb
FD
43 GString *g_metadata_text = NULL;
44 const char *plaintext;
4164020e
SM
45
46 result = bt_value_map_create();
47 if (!result) {
48 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
49 goto error;
50 }
51
52 BT_ASSERT(params);
53
54 if (!bt_value_is_map(params)) {
0f5c5d5c 55 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
4164020e
SM
56 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
57 goto error;
58 }
59
60 path_value = bt_value_map_borrow_entry_value_const(params, "path");
61 if (!path_value) {
0f5c5d5c 62 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Mandatory `path` parameter missing");
4164020e
SM
63 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
64 goto error;
65 }
66
67 if (!bt_value_is_string(path_value)) {
0f5c5d5c 68 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`path` parameter is required to be a string value");
4164020e
SM
69 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
70 goto error;
71 }
72
73 path = bt_value_string_get(path_value);
74
75 BT_ASSERT(path);
0f5c5d5c 76 metadata_fp = ctf_fs_metadata_open_file(path, logger);
4164020e 77 if (!metadata_fp) {
0f5c5d5c 78 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
4164020e
SM
79 goto error;
80 }
81
0f5c5d5c 82 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, &bo, logger);
4164020e 83 if (ret) {
0f5c5d5c
SM
84 BT_CPPLOGE_APPEND_CAUSE_SPEC(
85 logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
86 path);
4164020e
SM
87 goto error;
88 }
89
4164020e
SM
90 decoder_cfg.keep_plain_text = true;
91 decoder = ctf_metadata_decoder_create(&decoder_cfg);
92 if (!decoder) {
0f5c5d5c 93 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot create metadata decoder: path=\"{}\".", path);
4164020e
SM
94 goto error;
95 }
96
97 rewind(metadata_fp);
98 decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp);
99 if (decoder_status) {
0f5c5d5c
SM
100 BT_CPPLOGE_APPEND_CAUSE_SPEC(
101 logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
4164020e
SM
102 goto error;
103 }
104
c719eabb
FD
105 plaintext = ctf_metadata_decoder_get_text(decoder);
106 g_metadata_text = g_string_new(NULL);
107
108 if (!g_metadata_text) {
109 goto error;
110 }
111
112 if (strncmp(plaintext, METADATA_TEXT_SIG, sizeof(METADATA_TEXT_SIG) - 1) != 0) {
113 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
114 g_string_append(g_metadata_text, " */\n\n");
115 }
116
117 g_string_append(g_metadata_text, plaintext);
118
119 ret = bt_value_map_insert_string_entry(result, "text", g_metadata_text->str);
4164020e 120 if (ret) {
0f5c5d5c 121 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
4164020e
SM
122 goto error;
123 }
124
125 ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized);
126 if (ret) {
0f5c5d5c
SM
127 BT_CPPLOGE_APPEND_CAUSE_SPEC(
128 logger, "Cannot insert \"is-packetized\" attribute into query result.");
4164020e
SM
129 goto error;
130 }
131
132 goto end;
04c0ba87
JG
133
134error:
4164020e
SM
135 BT_VALUE_PUT_REF_AND_RESET(result);
136 result = NULL;
c7eee084 137
4164020e
SM
138 if (status >= 0) {
139 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
140 }
04c0ba87
JG
141
142end:
c719eabb
FD
143 if (g_metadata_text) {
144 g_string_free(g_metadata_text, TRUE);
145 }
4164020e
SM
146 ctf_metadata_decoder_destroy(decoder);
147
148 if (metadata_fp) {
149 ret = fclose(metadata_fp);
150 if (ret) {
0f5c5d5c
SM
151 BT_CPPLOGE_ERRNO_SPEC(logger, "Cannot close metadata file stream", ": path=\"{}\"",
152 path);
4164020e
SM
153 }
154 }
155
156 *user_result = result;
157 return status;
97ade20b 158}
9ec238a8 159
4164020e 160static int add_range(bt_value *info, struct range *range, const char *range_name)
97ade20b 161{
4164020e
SM
162 int ret = 0;
163 bt_value_map_insert_entry_status status;
164 bt_value *range_map;
165
166 if (!range->set) {
167 /* Not an error. */
168 goto end;
169 }
170
171 status = bt_value_map_insert_empty_map_entry(info, range_name, &range_map);
172 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
173 ret = -1;
174 goto end;
175 }
176
177 status = bt_value_map_insert_signed_integer_entry(range_map, "begin", range->begin_ns);
178 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
179 ret = -1;
180 goto end;
181 }
182
183 status = bt_value_map_insert_signed_integer_entry(range_map, "end", range->end_ns);
184 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
185 ret = -1;
186 goto end;
187 }
97ade20b 188
97ade20b 189end:
4164020e 190 return ret;
97ade20b
JG
191}
192
4164020e
SM
193static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *group_info,
194 struct range *stream_range)
97ade20b 195{
4164020e
SM
196 int ret = 0;
197 bt_value_map_insert_entry_status insert_status;
198 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
49b956cc 199 bt2c::GCharUP port_name;
4164020e
SM
200
201 /*
202 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
203 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
204 * the timestamp_begin of the first index entry and the timestamp_end
205 * of the last index entry.
206 */
207 BT_ASSERT(group->index);
208 BT_ASSERT(group->index->entries);
209 BT_ASSERT(group->index->entries->len > 0);
210
211 /* First entry. */
212 first_ds_index_entry =
213 (struct ctf_fs_ds_index_entry *) g_ptr_array_index(group->index->entries, 0);
214
215 /* Last entry. */
216 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
217 group->index->entries, group->index->entries->len - 1);
218
219 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
220 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
221
222 /*
223 * If any of the begin and end timestamps is not set it means that
224 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
225 * in their packet context so we can't set the range.
226 */
227 stream_range->set =
228 stream_range->begin_ns != UINT64_C(-1) && stream_range->end_ns != UINT64_C(-1);
229
230 ret = add_range(group_info, stream_range, "range-ns");
231 if (ret) {
232 goto end;
233 }
234
235 port_name = ctf_fs_make_port_name(group);
236 if (!port_name) {
237 ret = -1;
238 goto end;
239 }
240
49b956cc 241 insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name.get());
4164020e
SM
242 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
243 ret = -1;
244 goto end;
245 }
a38d7650 246
97ade20b 247end:
4164020e 248 return ret;
97ade20b
JG
249}
250
4164020e 251static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
0f5c5d5c 252 const bt2c::Logger& logger)
97ade20b 253{
4164020e
SM
254 int ret = 0;
255 size_t group_idx;
256 bt_value_map_insert_entry_status insert_status;
257 bt_value_array_append_element_status append_status;
258 bt_value *file_groups = NULL;
259
260 BT_ASSERT(trace->ds_file_groups);
261 /* Add trace range info only if it contains streams. */
262 if (trace->ds_file_groups->len == 0) {
263 ret = -1;
0f5c5d5c
SM
264 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
265 trace->path->str);
4164020e
SM
266 goto end;
267 }
268
269 insert_status = bt_value_map_insert_empty_array_entry(trace_info, "stream-infos", &file_groups);
270 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
271 ret = -1;
272 goto end;
273 }
274
275 /* Find range of all stream groups, and of the trace. */
276 for (group_idx = 0; group_idx < trace->ds_file_groups->len; group_idx++) {
277 bt_value *group_info;
278 range group_range;
279 ctf_fs_ds_file_group *group =
280 (ctf_fs_ds_file_group *) g_ptr_array_index(trace->ds_file_groups, group_idx);
281
282 append_status = bt_value_array_append_empty_map_element(file_groups, &group_info);
283 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
284 ret = -1;
285 goto end;
286 }
287
288 ret = populate_stream_info(group, group_info, &group_range);
289 if (ret) {
290 goto end;
291 }
292 }
97ade20b 293
97ade20b 294end:
4164020e 295 return ret;
97ade20b
JG
296}
297
4164020e 298bt_component_class_query_method_status
0f5c5d5c 299trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
97ade20b 300{
f340a3e8 301 ctf_fs_component::UP ctf_fs;
4164020e 302 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
4164020e
SM
303 bt_value *result = NULL;
304 const bt_value *inputs_value = NULL;
305 const bt_value *trace_name_value;
306 int ret = 0;
307 bt_value *trace_info = NULL;
308 bt_value_array_append_element_status append_status;
309
310 BT_ASSERT(params);
311
312 if (!bt_value_is_map(params)) {
0f5c5d5c 313 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
4164020e
SM
314 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
315 goto error;
316 }
317
0f5c5d5c 318 ctf_fs = ctf_fs_component_create(logger);
4164020e
SM
319 if (!ctf_fs) {
320 goto error;
321 }
322
f340a3e8 323 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
4164020e
SM
324 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
325 goto error;
326 }
327
f340a3e8 328 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
4164020e
SM
329 goto error;
330 }
331
332 result = bt_value_array_create();
333 if (!result) {
334 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
335 goto error;
336 }
337
338 append_status = bt_value_array_append_empty_map_element(result, &trace_info);
339 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
0f5c5d5c 340 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
4164020e
SM
341 goto error;
342 }
343
0f5c5d5c 344 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
4164020e
SM
345 if (ret) {
346 goto error;
347 }
348
349 goto end;
97ade20b
JG
350
351error:
4164020e 352 BT_VALUE_PUT_REF_AND_RESET(result);
c7eee084 353
4164020e
SM
354 if (status >= 0) {
355 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
356 }
c7eee084 357
97ade20b 358end:
4164020e
SM
359 *user_result = result;
360 return status;
04c0ba87 361}
73760435 362
4164020e 363bt_component_class_query_method_status
0f5c5d5c 364support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
73760435 365{
4164020e
SM
366 const bt_value *input_type_value;
367 const char *input_type;
368 bt_component_class_query_method_status status;
369 bt_value_map_insert_entry_status insert_entry_status;
370 double weight = 0;
9372bebb 371 bt2c::GCharUP metadata_path;
4164020e
SM
372 bt_value *result = NULL;
373 struct ctf_metadata_decoder *metadata_decoder = NULL;
374 FILE *metadata_file = NULL;
375 char uuid_str[BT_UUID_STR_LEN + 1];
376 bool has_uuid = false;
377 const bt_value *input_value;
378 const char *input;
379
380 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
381 BT_ASSERT(input_type_value);
382 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
383 input_type = bt_value_string_get(input_type_value);
384
385 if (strcmp(input_type, "directory") != 0) {
386 goto create_result;
387 }
388
389 input_value = bt_value_map_borrow_entry_value_const(params, "input");
390 BT_ASSERT(input_value);
391 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
392 input = bt_value_string_get(input_value);
393
9372bebb 394 metadata_path.reset(g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL));
4164020e
SM
395 if (!metadata_path) {
396 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
397 goto end;
398 }
399
9372bebb 400 metadata_file = g_fopen(metadata_path.get(), "rb");
4164020e 401 if (metadata_file) {
4164020e
SM
402 enum ctf_metadata_decoder_status decoder_status;
403 bt_uuid_t uuid;
404
0f5c5d5c 405 ctf_metadata_decoder_config metadata_decoder_config {logger};
4164020e
SM
406
407 metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config);
408 if (!metadata_decoder) {
409 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
410 goto end;
411 }
412
413 decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file);
414 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
0f5c5d5c
SM
415 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
416 decoder_status);
4164020e
SM
417 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
418 goto end;
419 }
420
421 /*
422 * We were able to parse the metadata file, so we are
423 * confident it's a CTF trace.
424 */
425 weight = 0.75;
426
427 /* If the trace has a UUID, return the stringified UUID as the group. */
428 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) {
429 bt_uuid_to_str(uuid, uuid_str);
430 has_uuid = true;
431 }
432 }
73760435 433
493917ba 434create_result:
4164020e
SM
435 result = bt_value_map_create();
436 if (!result) {
437 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
438 goto end;
439 }
440
441 insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight);
442 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
443 status = (bt_component_class_query_method_status) insert_entry_status;
444 goto end;
445 }
446
447 /* We are not supposed to have weight == 0 and a UUID. */
448 BT_ASSERT(weight > 0 || !has_uuid);
449
450 if (weight > 0 && has_uuid) {
451 insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str);
452 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
453 status = (bt_component_class_query_method_status) insert_entry_status;
454 goto end;
455 }
456 }
457
458 *user_result = result;
459 result = NULL;
460 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
73760435
SM
461
462end:
4164020e
SM
463 bt_value_put_ref(result);
464 ctf_metadata_decoder_destroy(metadata_decoder);
73760435 465
4164020e 466 return status;
73760435 467}
This page took 0.112681 seconds and 4 git commands to generate.