Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
f3bc2010 | 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
5b29e799 | 29 | #include <babeltrace/ctf-ir/packet.h> |
ac0c6bdd | 30 | #include <babeltrace/ctf-ir/clock-class.h> |
4f1f88a6 | 31 | #include <babeltrace/graph/private-port.h> |
b2e0c907 | 32 | #include <babeltrace/graph/private-component.h> |
4f1f88a6 PP |
33 | #include <babeltrace/graph/private-component-source.h> |
34 | #include <babeltrace/graph/private-notification-iterator.h> | |
b2e0c907 PP |
35 | #include <babeltrace/graph/component.h> |
36 | #include <babeltrace/graph/notification-iterator.h> | |
599faa1c | 37 | #include <babeltrace/graph/clock-class-priority-map.h> |
7d61fa8e | 38 | #include <plugins-common.h> |
ea0b4b9e JG |
39 | #include <glib.h> |
40 | #include <assert.h> | |
56a1cced JG |
41 | #include <unistd.h> |
42 | #include "fs.h" | |
413bc2c4 JG |
43 | #include "metadata.h" |
44 | #include "data-stream.h" | |
e7a4393b | 45 | #include "file.h" |
1e649dff | 46 | #include "../common/metadata/decoder.h" |
e7a4393b JG |
47 | |
48 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
49 | #define PRINT_PREFIX "ctf-fs" | |
50 | #include "print.h" | |
33f93973 | 51 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
ea0b4b9e | 52 | |
78bb6992 MD |
53 | BT_HIDDEN |
54 | bool ctf_fs_debug; | |
ea0b4b9e | 55 | |
41a2b7ae | 56 | struct bt_notification_iterator_next_return ctf_fs_iterator_next( |
4f1f88a6 | 57 | struct bt_private_notification_iterator *iterator) |
ea0b4b9e | 58 | { |
4f1f88a6 PP |
59 | struct ctf_fs_stream *fs_stream = |
60 | bt_private_notification_iterator_get_user_data(iterator); | |
d01e0f33 | 61 | |
4f1f88a6 | 62 | return ctf_fs_stream_next(fs_stream); |
ea0b4b9e | 63 | } |
bfd20a42 | 64 | |
4f1f88a6 | 65 | void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it) |
760051fa | 66 | { |
4f1f88a6 PP |
67 | void *ctf_fs_stream = |
68 | bt_private_notification_iterator_get_user_data(it); | |
760051fa | 69 | |
4f1f88a6 | 70 | ctf_fs_stream_destroy(ctf_fs_stream); |
760051fa JG |
71 | } |
72 | ||
4f1f88a6 PP |
73 | enum bt_notification_iterator_status ctf_fs_iterator_init( |
74 | struct bt_private_notification_iterator *it, | |
75 | struct bt_private_port *port) | |
4c1456f0 | 76 | { |
4f1f88a6 PP |
77 | struct ctf_fs_stream *stream = NULL; |
78 | struct ctf_fs_component *ctf_fs; | |
79 | struct ctf_fs_port_data *port_data; | |
80 | struct bt_private_component *priv_comp = | |
81 | bt_private_notification_iterator_get_private_component(it); | |
82 | enum bt_notification_iterator_status ret = | |
83 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
760051fa | 84 | |
4f1f88a6 | 85 | assert(priv_comp); |
5b29e799 | 86 | |
4f1f88a6 PP |
87 | ctf_fs = bt_private_component_get_user_data(priv_comp); |
88 | if (!ctf_fs) { | |
fe8ad2b6 | 89 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
4f1f88a6 | 90 | goto error; |
760051fa JG |
91 | } |
92 | ||
4f1f88a6 PP |
93 | port_data = bt_private_port_get_user_data(port); |
94 | if (!port_data) { | |
fe8ad2b6 | 95 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
4f1f88a6 PP |
96 | goto error; |
97 | } | |
5b29e799 | 98 | |
4f1f88a6 | 99 | stream = ctf_fs_stream_create(ctf_fs, port_data->path->str); |
5b29e799 | 100 | if (!stream) { |
4f1f88a6 | 101 | goto error; |
760051fa JG |
102 | } |
103 | ||
4f1f88a6 PP |
104 | ret = bt_private_notification_iterator_set_user_data(it, stream); |
105 | if (ret) { | |
106 | goto error; | |
760051fa JG |
107 | } |
108 | ||
4f1f88a6 PP |
109 | stream = NULL; |
110 | goto end; | |
5b29e799 | 111 | |
4f1f88a6 PP |
112 | error: |
113 | (void) bt_private_notification_iterator_set_user_data(it, NULL); | |
114 | ||
115 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
116 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
760051fa | 117 | } |
5b29e799 | 118 | |
760051fa | 119 | end: |
4f1f88a6 PP |
120 | ctf_fs_stream_destroy(stream); |
121 | bt_put(priv_comp); | |
760051fa JG |
122 | return ret; |
123 | } | |
124 | ||
760051fa | 125 | static |
4f1f88a6 | 126 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) |
760051fa | 127 | { |
4f1f88a6 | 128 | if (!ctf_fs) { |
8fa760ba JG |
129 | return; |
130 | } | |
4f1f88a6 PP |
131 | |
132 | if (ctf_fs->trace_path) { | |
133 | g_string_free(ctf_fs->trace_path, TRUE); | |
56a1cced | 134 | } |
4f1f88a6 PP |
135 | |
136 | if (ctf_fs->port_data) { | |
137 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 138 | } |
760051fa | 139 | |
4f1f88a6 PP |
140 | if (ctf_fs->metadata) { |
141 | ctf_fs_metadata_fini(ctf_fs->metadata); | |
142 | g_free(ctf_fs->metadata); | |
143 | } | |
760051fa | 144 | |
599faa1c | 145 | bt_put(ctf_fs->cc_prio_map); |
4f1f88a6 | 146 | g_free(ctf_fs); |
4c1456f0 JG |
147 | } |
148 | ||
4f1f88a6 | 149 | void ctf_fs_finalize(struct bt_private_component *component) |
a4792757 | 150 | { |
4f1f88a6 PP |
151 | void *data = bt_private_component_get_user_data(component); |
152 | ||
153 | ctf_fs_destroy_data(data); | |
a4792757 JG |
154 | } |
155 | ||
e7a4393b | 156 | static |
4f1f88a6 PP |
157 | void port_data_destroy(void *data) { |
158 | struct ctf_fs_port_data *port_data = data; | |
159 | ||
160 | if (!port_data) { | |
161 | return; | |
162 | } | |
163 | ||
164 | if (port_data->path) { | |
165 | g_string_free(port_data->path, TRUE); | |
166 | } | |
167 | ||
168 | g_free(port_data); | |
5b29e799 JG |
169 | } |
170 | ||
171 | static | |
4f1f88a6 PP |
172 | int create_one_port(struct ctf_fs_component *ctf_fs, |
173 | const char *stream_basename, const char *stream_path) | |
5b29e799 | 174 | { |
4f1f88a6 PP |
175 | int ret = 0; |
176 | struct bt_private_port *port = NULL; | |
177 | struct ctf_fs_port_data *port_data = NULL; | |
178 | GString *port_name = NULL; | |
179 | ||
180 | port_name = g_string_new(NULL); | |
181 | if (!port_name) { | |
182 | goto error; | |
183 | } | |
184 | ||
185 | /* Assign the name for the new output port */ | |
186 | g_string_assign(port_name, ""); | |
187 | g_string_printf(port_name, "trace0-stream-%s", stream_basename); | |
188 | PDBG("Creating one port named `%s` associated with path `%s`\n", | |
189 | port_name->str, stream_path); | |
190 | ||
191 | /* Create output port for this file */ | |
4f1f88a6 PP |
192 | port_data = g_new0(struct ctf_fs_port_data, 1); |
193 | if (!port_data) { | |
194 | goto error; | |
195 | } | |
196 | ||
197 | port_data->path = g_string_new(stream_path); | |
198 | if (!port_data->path) { | |
199 | goto error; | |
200 | } | |
201 | ||
3e9b0023 PP |
202 | port = bt_private_component_source_add_output_private_port( |
203 | ctf_fs->priv_comp, port_name->str, port_data); | |
204 | if (!port) { | |
4f1f88a6 PP |
205 | goto error; |
206 | } | |
207 | ||
208 | g_ptr_array_add(ctf_fs->port_data, port_data); | |
209 | port_data = NULL; | |
210 | goto end; | |
211 | ||
212 | error: | |
213 | ret = -1; | |
214 | ||
215 | end: | |
216 | if (port_name) { | |
217 | g_string_free(port_name, TRUE); | |
218 | } | |
219 | ||
220 | bt_put(port); | |
221 | port_data_destroy(port_data); | |
222 | return ret; | |
5b29e799 JG |
223 | } |
224 | ||
225 | static | |
4f1f88a6 | 226 | int create_ports(struct ctf_fs_component *ctf_fs) |
e7a4393b JG |
227 | { |
228 | int ret = 0; | |
4f1f88a6 | 229 | const char *basename; |
e7a4393b | 230 | GError *error = NULL; |
4f1f88a6 | 231 | GDir *dir = NULL; |
4f1f88a6 | 232 | struct ctf_fs_file *file = NULL; |
e7a4393b | 233 | |
4f1f88a6 PP |
234 | /* Create one output port for each stream file */ |
235 | dir = g_dir_open(ctf_fs->trace_path->str, 0, &error); | |
e7a4393b | 236 | if (!dir) { |
4f1f88a6 PP |
237 | PERR("Cannot open directory `%s`: %s (code %d)\n", |
238 | ctf_fs->trace_path->str, error->message, | |
239 | error->code); | |
e7a4393b JG |
240 | goto error; |
241 | } | |
242 | ||
4f1f88a6 PP |
243 | while ((basename = g_dir_read_name(dir))) { |
244 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { | |
e7a4393b | 245 | /* Ignore the metadata stream. */ |
4f1f88a6 | 246 | PDBG("Ignoring metadata file `%s`\n", basename); |
e7a4393b JG |
247 | continue; |
248 | } | |
249 | ||
4f1f88a6 PP |
250 | if (basename[0] == '.') { |
251 | PDBG("Ignoring hidden file `%s`\n", basename); | |
e7a4393b JG |
252 | continue; |
253 | } | |
254 | ||
255 | /* Create the file. */ | |
256 | file = ctf_fs_file_create(ctf_fs); | |
257 | if (!file) { | |
4f1f88a6 PP |
258 | PERR("Cannot create stream file object for file `%s`\n", |
259 | basename); | |
e7a4393b JG |
260 | goto error; |
261 | } | |
262 | ||
263 | /* Create full path string. */ | |
264 | g_string_append_printf(file->path, "%s/%s", | |
4f1f88a6 | 265 | ctf_fs->trace_path->str, basename); |
e7a4393b | 266 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
4f1f88a6 | 267 | PDBG("Ignoring non-regular file `%s`\n", basename); |
e7a4393b | 268 | ctf_fs_file_destroy(file); |
4f1f88a6 | 269 | file = NULL; |
e7a4393b JG |
270 | continue; |
271 | } | |
272 | ||
4f1f88a6 PP |
273 | ret = ctf_fs_file_open(ctf_fs, file, "rb"); |
274 | if (ret) { | |
275 | PERR("Cannot open stream file `%s`\n", basename); | |
e7a4393b JG |
276 | goto error; |
277 | } | |
278 | ||
9fa0891a JG |
279 | if (file->size == 0) { |
280 | /* Skip empty stream. */ | |
4f1f88a6 | 281 | PDBG("Ignoring empty file `%s`\n", basename); |
9fa0891a | 282 | ctf_fs_file_destroy(file); |
4f1f88a6 | 283 | file = NULL; |
9fa0891a JG |
284 | continue; |
285 | } | |
286 | ||
4f1f88a6 PP |
287 | ret = create_one_port(ctf_fs, basename, file->path->str); |
288 | if (ret) { | |
289 | PERR("Cannot create output port for file `%s`\n", | |
290 | basename); | |
e7a4393b JG |
291 | goto error; |
292 | } | |
293 | ||
4f1f88a6 PP |
294 | ctf_fs_file_destroy(file); |
295 | file = NULL; | |
e7a4393b JG |
296 | } |
297 | ||
298 | goto end; | |
4f1f88a6 | 299 | |
e7a4393b JG |
300 | error: |
301 | ret = -1; | |
4f1f88a6 | 302 | |
e7a4393b JG |
303 | end: |
304 | if (dir) { | |
305 | g_dir_close(dir); | |
306 | dir = NULL; | |
307 | } | |
4f1f88a6 | 308 | |
e7a4393b JG |
309 | if (error) { |
310 | g_error_free(error); | |
311 | } | |
5b29e799 | 312 | |
4f1f88a6 | 313 | ctf_fs_file_destroy(file); |
91457551 | 314 | return ret; |
5b29e799 JG |
315 | } |
316 | ||
599faa1c PP |
317 | static |
318 | int create_cc_prio_map(struct ctf_fs_component *ctf_fs) | |
319 | { | |
320 | int ret = 0; | |
321 | size_t i; | |
322 | int count; | |
323 | ||
324 | assert(ctf_fs); | |
325 | ctf_fs->cc_prio_map = bt_clock_class_priority_map_create(); | |
326 | if (!ctf_fs->cc_prio_map) { | |
327 | ret = -1; | |
328 | goto end; | |
329 | } | |
330 | ||
331 | count = bt_ctf_trace_get_clock_class_count(ctf_fs->metadata->trace); | |
332 | assert(count >= 0); | |
333 | ||
334 | for (i = 0; i < count; i++) { | |
335 | struct bt_ctf_clock_class *clock_class = | |
9ac68eb1 PP |
336 | bt_ctf_trace_get_clock_class_by_index( |
337 | ctf_fs->metadata->trace, i); | |
599faa1c PP |
338 | |
339 | assert(clock_class); | |
340 | ret = bt_clock_class_priority_map_add_clock_class( | |
341 | ctf_fs->cc_prio_map, clock_class, 0); | |
342 | BT_PUT(clock_class); | |
343 | ||
344 | if (ret) { | |
345 | goto end; | |
346 | } | |
347 | } | |
348 | ||
349 | end: | |
350 | return ret; | |
351 | } | |
352 | ||
5b29e799 | 353 | static |
4f1f88a6 PP |
354 | struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp, |
355 | struct bt_value *params) | |
56a1cced JG |
356 | { |
357 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 358 | struct bt_value *value = NULL; |
56a1cced | 359 | const char *path; |
4f1f88a6 | 360 | int ret; |
56a1cced JG |
361 | |
362 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
363 | if (!ctf_fs) { | |
364 | goto end; | |
365 | } | |
366 | ||
4f1f88a6 PP |
367 | /* |
368 | * We don't need to get a new reference here because as long as | |
369 | * our private ctf_fs_component object exists, the containing | |
370 | * private component should also exist. | |
371 | */ | |
372 | ctf_fs->priv_comp = priv_comp; | |
373 | ||
56a1cced JG |
374 | /* FIXME: should probably look for a source URI */ |
375 | value = bt_value_map_get(params, "path"); | |
376 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
377 | goto error; | |
378 | } | |
379 | ||
380 | ret = bt_value_string_get(value, &path); | |
4f1f88a6 PP |
381 | if (ret) { |
382 | goto error; | |
383 | } | |
384 | ||
385 | ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); | |
386 | if (!ctf_fs->port_data) { | |
56a1cced JG |
387 | goto error; |
388 | } | |
389 | ||
390 | ctf_fs->trace_path = g_string_new(path); | |
391 | if (!ctf_fs->trace_path) { | |
92540773 | 392 | BT_PUT(value); |
56a1cced JG |
393 | goto error; |
394 | } | |
92540773 JD |
395 | bt_put(value); |
396 | ||
397 | value = bt_value_map_get(params, "offset-s"); | |
398 | if (value) { | |
399 | int64_t offset; | |
400 | ||
401 | if (!bt_value_is_integer(value)) { | |
402 | fprintf(stderr, | |
403 | "offset-s should be an integer\n"); | |
404 | goto error; | |
405 | } | |
406 | ret = bt_value_integer_get(value, &offset); | |
407 | if (ret != BT_VALUE_STATUS_OK) { | |
408 | fprintf(stderr, | |
409 | "Failed to get offset-s value\n"); | |
410 | goto error; | |
411 | } | |
412 | ctf_fs->options.clock_offset = offset; | |
413 | bt_put(value); | |
414 | } | |
415 | ||
416 | value = bt_value_map_get(params, "offset-ns"); | |
417 | if (value) { | |
418 | int64_t offset; | |
419 | ||
420 | if (!bt_value_is_integer(value)) { | |
421 | fprintf(stderr, | |
422 | "offset-ns should be an integer\n"); | |
423 | goto error; | |
424 | } | |
425 | ret = bt_value_integer_get(value, &offset); | |
426 | if (ret != BT_VALUE_STATUS_OK) { | |
427 | fprintf(stderr, | |
428 | "Failed to get offset-ns value\n"); | |
429 | goto error; | |
430 | } | |
431 | ctf_fs->options.clock_offset_ns = offset; | |
432 | bt_put(value); | |
433 | } | |
434 | ||
56a1cced JG |
435 | ctf_fs->error_fp = stderr; |
436 | ctf_fs->page_size = (size_t) getpagesize(); | |
e7a4393b JG |
437 | |
438 | // FIXME: check error. | |
5b29e799 JG |
439 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
440 | if (!ctf_fs->metadata) { | |
e7a4393b JG |
441 | goto error; |
442 | } | |
4f1f88a6 PP |
443 | |
444 | ret = ctf_fs_metadata_set_trace(ctf_fs); | |
445 | if (ret) { | |
446 | goto error; | |
447 | } | |
448 | ||
599faa1c PP |
449 | ret = create_cc_prio_map(ctf_fs); |
450 | if (ret) { | |
451 | goto error; | |
452 | } | |
453 | ||
4f1f88a6 PP |
454 | ret = create_ports(ctf_fs); |
455 | if (ret) { | |
456 | goto error; | |
457 | } | |
458 | ||
1ef09eb5 JG |
459 | goto end; |
460 | ||
56a1cced JG |
461 | error: |
462 | ctf_fs_destroy_data(ctf_fs); | |
e7a4393b | 463 | ctf_fs = NULL; |
1ef09eb5 | 464 | end: |
56a1cced JG |
465 | return ctf_fs; |
466 | } | |
467 | ||
ea0b4b9e | 468 | BT_HIDDEN |
4f1f88a6 | 469 | enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp, |
7d61fa8e | 470 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
ea0b4b9e JG |
471 | { |
472 | struct ctf_fs_component *ctf_fs; | |
473 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
474 | ||
4f1f88a6 | 475 | assert(priv_comp); |
ea0b4b9e | 476 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; |
4f1f88a6 | 477 | ctf_fs = ctf_fs_create(priv_comp, params); |
ea0b4b9e JG |
478 | if (!ctf_fs) { |
479 | ret = BT_COMPONENT_STATUS_NOMEM; | |
480 | goto end; | |
481 | } | |
4c1456f0 | 482 | |
4f1f88a6 | 483 | ret = bt_private_component_set_user_data(priv_comp, ctf_fs); |
ea0b4b9e JG |
484 | if (ret != BT_COMPONENT_STATUS_OK) { |
485 | goto error; | |
486 | } | |
ea0b4b9e JG |
487 | end: |
488 | return ret; | |
489 | error: | |
4f1f88a6 | 490 | (void) bt_private_component_set_user_data(priv_comp, NULL); |
760051fa | 491 | ctf_fs_destroy_data(ctf_fs); |
ea0b4b9e JG |
492 | return ret; |
493 | } | |
33f93973 PP |
494 | |
495 | BT_HIDDEN | |
a67681c1 PP |
496 | struct bt_value *ctf_fs_query(struct bt_component_class *comp_class, |
497 | const char *object, struct bt_value *params) | |
33f93973 PP |
498 | { |
499 | struct bt_value *results = NULL; | |
500 | struct bt_value *path_value = NULL; | |
501 | char *metadata_text = NULL; | |
502 | FILE *metadata_fp = NULL; | |
503 | GString *g_metadata_text = NULL; | |
504 | ||
a67681c1 | 505 | if (strcmp(object, "metadata-info") == 0) { |
33f93973 PP |
506 | int ret; |
507 | int bo; | |
508 | const char *path; | |
509 | bool is_packetized; | |
510 | ||
511 | results = bt_value_map_create(); | |
512 | if (!results) { | |
513 | goto error; | |
514 | } | |
515 | ||
516 | if (!bt_value_is_map(params)) { | |
517 | fprintf(stderr, | |
a67681c1 | 518 | "Query parameters is not a map value object\n"); |
33f93973 PP |
519 | goto error; |
520 | } | |
521 | ||
522 | path_value = bt_value_map_get(params, "path"); | |
523 | ret = bt_value_string_get(path_value, &path); | |
524 | if (ret) { | |
525 | fprintf(stderr, | |
526 | "Cannot get `path` string parameter\n"); | |
527 | goto error; | |
528 | } | |
529 | ||
530 | assert(path); | |
531 | metadata_fp = ctf_fs_metadata_open_file(path); | |
532 | if (!metadata_fp) { | |
533 | fprintf(stderr, | |
534 | "Cannot open trace at path `%s`\n", path); | |
535 | goto error; | |
536 | } | |
537 | ||
1e649dff PP |
538 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, |
539 | &bo); | |
33f93973 PP |
540 | |
541 | if (is_packetized) { | |
1e649dff PP |
542 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( |
543 | metadata_fp, &metadata_text, bo); | |
33f93973 PP |
544 | if (ret) { |
545 | fprintf(stderr, | |
546 | "Cannot decode packetized metadata file\n"); | |
547 | goto error; | |
548 | } | |
549 | } else { | |
550 | long filesize; | |
551 | ||
552 | fseek(metadata_fp, 0, SEEK_END); | |
553 | filesize = ftell(metadata_fp); | |
554 | rewind(metadata_fp); | |
555 | metadata_text = malloc(filesize + 1); | |
556 | if (!metadata_text) { | |
557 | fprintf(stderr, | |
558 | "Cannot allocate buffer for metadata text\n"); | |
559 | goto error; | |
560 | } | |
561 | ||
562 | if (fread(metadata_text, filesize, 1, metadata_fp) != | |
563 | 1) { | |
564 | fprintf(stderr, | |
565 | "Cannot read metadata file\n"); | |
566 | goto error; | |
567 | } | |
568 | ||
569 | metadata_text[filesize] = '\0'; | |
570 | } | |
571 | ||
572 | g_metadata_text = g_string_new(NULL); | |
573 | if (!g_metadata_text) { | |
574 | goto error; | |
575 | } | |
576 | ||
577 | if (strncmp(metadata_text, METADATA_TEXT_SIG, | |
578 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { | |
579 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); | |
580 | g_string_append(g_metadata_text, " */\n\n"); | |
581 | } | |
582 | ||
583 | g_string_append(g_metadata_text, metadata_text); | |
584 | ||
585 | ret = bt_value_map_insert_string(results, "text", | |
586 | g_metadata_text->str); | |
587 | if (ret) { | |
588 | fprintf(stderr, "Cannot insert metadata text into results\n"); | |
589 | goto error; | |
590 | } | |
591 | ||
592 | ret = bt_value_map_insert_bool(results, "is-packetized", | |
593 | is_packetized); | |
594 | if (ret) { | |
595 | fprintf(stderr, "Cannot insert is packetized into results\n"); | |
596 | goto error; | |
597 | } | |
598 | } else { | |
a67681c1 | 599 | fprintf(stderr, "Unknown query object `%s`\n", object); |
33f93973 PP |
600 | goto error; |
601 | } | |
602 | ||
603 | goto end; | |
604 | ||
605 | error: | |
606 | BT_PUT(results); | |
607 | ||
608 | end: | |
609 | bt_put(path_value); | |
610 | free(metadata_text); | |
611 | ||
612 | if (g_metadata_text) { | |
613 | g_string_free(g_metadata_text, TRUE); | |
614 | } | |
615 | ||
616 | if (metadata_fp) { | |
617 | fclose(metadata_fp); | |
618 | } | |
619 | return results; | |
620 | } |