Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
1a9f7075 | 6 | * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com> |
f3bc2010 | 7 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e | 8 | * |
7a278c8e JG |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
4bd72b60 | 28 | #include <babeltrace/common-internal.h> |
9d408fca | 29 | #include <babeltrace/babeltrace.h> |
7d61fa8e | 30 | #include <plugins-common.h> |
ea0b4b9e | 31 | #include <glib.h> |
f6ccaed9 | 32 | #include <babeltrace/assert-internal.h> |
94cf822e | 33 | #include <inttypes.h> |
c55a9f58 | 34 | #include <stdbool.h> |
56a1cced | 35 | #include "fs.h" |
413bc2c4 | 36 | #include "metadata.h" |
94cf822e | 37 | #include "data-stream-file.h" |
e7a4393b | 38 | #include "file.h" |
1e649dff | 39 | #include "../common/metadata/decoder.h" |
c14831f0 | 40 | #include "../common/msg-iter/msg-iter.h" |
a429c321 | 41 | #include "../common/utils/utils.h" |
04c0ba87 | 42 | #include "query.h" |
e7a4393b | 43 | |
55314f2a JG |
44 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC" |
45 | #include "logging.h" | |
ea0b4b9e | 46 | |
94cf822e | 47 | static |
c14831f0 | 48 | int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data *msg_iter_data) |
94cf822e PP |
49 | { |
50 | struct ctf_fs_ds_file_info *ds_file_info; | |
51 | int ret = 0; | |
52 | ||
c14831f0 PP |
53 | BT_ASSERT(msg_iter_data->ds_file_info_index < |
54 | msg_iter_data->ds_file_group->ds_file_infos->len); | |
94cf822e | 55 | ds_file_info = g_ptr_array_index( |
c14831f0 PP |
56 | msg_iter_data->ds_file_group->ds_file_infos, |
57 | msg_iter_data->ds_file_info_index); | |
58 | ||
59 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); | |
60 | msg_iter_data->ds_file = ctf_fs_ds_file_create( | |
61 | msg_iter_data->ds_file_group->ctf_fs_trace, | |
62 | msg_iter_data->pc_msg_iter, | |
63 | msg_iter_data->msg_iter, | |
64 | msg_iter_data->ds_file_group->stream, | |
97ade20b | 65 | ds_file_info->path->str); |
c14831f0 | 66 | if (!msg_iter_data->ds_file) { |
94cf822e PP |
67 | ret = -1; |
68 | } | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | static | |
c14831f0 PP |
74 | void ctf_fs_msg_iter_data_destroy( |
75 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
94cf822e | 76 | { |
c14831f0 | 77 | if (!msg_iter_data) { |
94cf822e PP |
78 | return; |
79 | } | |
80 | ||
c14831f0 | 81 | ctf_fs_ds_file_destroy(msg_iter_data->ds_file); |
6de92955 | 82 | |
c14831f0 PP |
83 | if (msg_iter_data->msg_iter) { |
84 | bt_msg_iter_destroy(msg_iter_data->msg_iter); | |
6de92955 PP |
85 | } |
86 | ||
c14831f0 | 87 | g_free(msg_iter_data); |
94cf822e PP |
88 | } |
89 | ||
d26376f5 PP |
90 | static |
91 | void set_msg_iter_emits_stream_beginning_end_messages( | |
92 | struct ctf_fs_msg_iter_data *msg_iter_data) | |
93 | { | |
94 | bt_msg_iter_set_emit_stream_beginning_message( | |
95 | msg_iter_data->ds_file->msg_iter, | |
96 | msg_iter_data->ds_file_info_index == 0); | |
97 | bt_msg_iter_set_emit_stream_end_message( | |
98 | msg_iter_data->ds_file->msg_iter, | |
99 | msg_iter_data->ds_file_info_index == | |
100 | msg_iter_data->ds_file_group->ds_file_infos->len - 1); | |
101 | } | |
102 | ||
d4393e08 | 103 | static |
b7c5b62e | 104 | bt_self_message_iterator_status ctf_fs_iterator_next_one( |
c14831f0 | 105 | struct ctf_fs_msg_iter_data *msg_iter_data, |
d26376f5 | 106 | const bt_message **out_msg) |
ea0b4b9e | 107 | { |
b7c5b62e | 108 | bt_self_message_iterator_status status; |
94cf822e | 109 | |
c14831f0 | 110 | BT_ASSERT(msg_iter_data->ds_file); |
f42867e2 | 111 | |
d26376f5 PP |
112 | while (true) { |
113 | bt_message *msg; | |
114 | ||
115 | status = ctf_fs_ds_file_next(msg_iter_data->ds_file, &msg); | |
116 | switch (status) { | |
117 | case BT_SELF_MESSAGE_ITERATOR_STATUS_OK: | |
118 | *out_msg = msg; | |
119 | msg = NULL; | |
f42867e2 | 120 | goto end; |
d26376f5 PP |
121 | case BT_SELF_MESSAGE_ITERATOR_STATUS_END: |
122 | { | |
123 | int ret; | |
f42867e2 | 124 | |
d26376f5 PP |
125 | if (msg_iter_data->ds_file_info_index == |
126 | msg_iter_data->ds_file_group->ds_file_infos->len - 1) { | |
127 | /* End of all group's stream files */ | |
128 | goto end; | |
129 | } | |
130 | ||
131 | msg_iter_data->ds_file_info_index++; | |
e79d34df PP |
132 | bt_msg_iter_reset_for_next_stream_file( |
133 | msg_iter_data->msg_iter); | |
d26376f5 PP |
134 | set_msg_iter_emits_stream_beginning_end_messages( |
135 | msg_iter_data); | |
94cf822e | 136 | |
94cf822e | 137 | /* |
d26376f5 PP |
138 | * Open and start reading the next stream file |
139 | * within our stream file group. | |
94cf822e | 140 | */ |
d26376f5 PP |
141 | ret = msg_iter_data_set_current_ds_file(msg_iter_data); |
142 | if (ret) { | |
143 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Continue the loop to get the next message */ | |
148 | break; | |
94cf822e | 149 | } |
d26376f5 | 150 | default: |
94cf822e PP |
151 | goto end; |
152 | } | |
94cf822e | 153 | } |
d01e0f33 | 154 | |
94cf822e | 155 | end: |
d4393e08 PP |
156 | return status; |
157 | } | |
158 | ||
159 | BT_HIDDEN | |
b7c5b62e | 160 | bt_self_message_iterator_status ctf_fs_iterator_next( |
c14831f0 PP |
161 | bt_self_message_iterator *iterator, |
162 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
163 | uint64_t *count) |
164 | { | |
b7c5b62e | 165 | bt_self_message_iterator_status status = |
c14831f0 PP |
166 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
167 | struct ctf_fs_msg_iter_data *msg_iter_data = | |
168 | bt_self_message_iterator_get_data(iterator); | |
d4393e08 PP |
169 | uint64_t i = 0; |
170 | ||
c14831f0 PP |
171 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
172 | status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]); | |
173 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
d4393e08 PP |
174 | i++; |
175 | } | |
176 | } | |
177 | ||
178 | if (i > 0) { | |
179 | /* | |
180 | * Even if ctf_fs_iterator_next_one() returned something | |
c14831f0 PP |
181 | * else than BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we |
182 | * accumulated message objects in the output | |
183 | * message array, so we need to return | |
184 | * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they are | |
d4393e08 | 185 | * transfered to downstream. This other status occurs |
c14831f0 | 186 | * again the next time muxer_msg_iter_do_next() is |
d4393e08 | 187 | * called, possibly without any accumulated |
c14831f0 | 188 | * message, in which case we'll return it. |
d4393e08 PP |
189 | */ |
190 | *count = i; | |
c14831f0 | 191 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
192 | } |
193 | ||
194 | return status; | |
ea0b4b9e | 195 | } |
bfd20a42 | 196 | |
c14831f0 | 197 | void ctf_fs_iterator_finalize(bt_self_message_iterator *it) |
760051fa | 198 | { |
c14831f0 PP |
199 | ctf_fs_msg_iter_data_destroy( |
200 | bt_self_message_iterator_get_data(it)); | |
760051fa JG |
201 | } |
202 | ||
b7c5b62e | 203 | bt_self_message_iterator_status ctf_fs_iterator_init( |
c14831f0 | 204 | bt_self_message_iterator *self_msg_iter, |
b9879703 PP |
205 | bt_self_component_source *self_comp, |
206 | bt_self_component_port_output *self_port) | |
4c1456f0 | 207 | { |
4f1f88a6 | 208 | struct ctf_fs_port_data *port_data; |
c14831f0 | 209 | struct ctf_fs_msg_iter_data *msg_iter_data = NULL; |
b7c5b62e | 210 | bt_self_message_iterator_status ret = |
c14831f0 | 211 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
94cf822e | 212 | int iret; |
760051fa | 213 | |
d27a81b2 | 214 | port_data = bt_self_component_port_get_data( |
27c0f01f | 215 | bt_self_component_port_output_as_self_component_port( |
d27a81b2 PP |
216 | self_port)); |
217 | BT_ASSERT(port_data); | |
c14831f0 PP |
218 | msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1); |
219 | if (!msg_iter_data) { | |
220 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
94cf822e PP |
221 | goto error; |
222 | } | |
223 | ||
c14831f0 PP |
224 | msg_iter_data->pc_msg_iter = self_msg_iter; |
225 | msg_iter_data->msg_iter = bt_msg_iter_create( | |
61f59358 | 226 | port_data->ds_file_group->ctf_fs_trace->metadata->tc, |
6de92955 PP |
227 | bt_common_get_page_size() * 8, |
228 | ctf_fs_ds_file_medops, NULL); | |
c14831f0 PP |
229 | if (!msg_iter_data->msg_iter) { |
230 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
231 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; | |
6de92955 PP |
232 | goto error; |
233 | } | |
234 | ||
c14831f0 PP |
235 | msg_iter_data->ds_file_group = port_data->ds_file_group; |
236 | iret = msg_iter_data_set_current_ds_file(msg_iter_data); | |
94cf822e | 237 | if (iret) { |
c14831f0 | 238 | ret = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
4f1f88a6 | 239 | goto error; |
760051fa JG |
240 | } |
241 | ||
d26376f5 | 242 | set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data); |
c14831f0 PP |
243 | bt_self_message_iterator_set_data(self_msg_iter, |
244 | msg_iter_data); | |
245 | if (ret != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
4f1f88a6 | 246 | goto error; |
760051fa JG |
247 | } |
248 | ||
c14831f0 | 249 | msg_iter_data = NULL; |
4f1f88a6 | 250 | goto end; |
5b29e799 | 251 | |
4f1f88a6 | 252 | error: |
c14831f0 | 253 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
4f1f88a6 | 254 | |
760051fa | 255 | end: |
c14831f0 | 256 | ctf_fs_msg_iter_data_destroy(msg_iter_data); |
760051fa JG |
257 | return ret; |
258 | } | |
259 | ||
760051fa | 260 | static |
1a9f7075 | 261 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
760051fa | 262 | { |
4f1f88a6 | 263 | if (!ctf_fs) { |
8fa760ba JG |
264 | return; |
265 | } | |
4f1f88a6 | 266 | |
1a9f7075 PP |
267 | if (ctf_fs->traces) { |
268 | g_ptr_array_free(ctf_fs->traces, TRUE); | |
56a1cced | 269 | } |
4f1f88a6 PP |
270 | |
271 | if (ctf_fs->port_data) { | |
272 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 273 | } |
760051fa | 274 | |
1a9f7075 PP |
275 | g_free(ctf_fs); |
276 | } | |
277 | ||
97ade20b JG |
278 | BT_HIDDEN |
279 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) | |
1a9f7075 | 280 | { |
1a9f7075 PP |
281 | if (!ctf_fs_trace) { |
282 | return; | |
283 | } | |
284 | ||
94cf822e PP |
285 | if (ctf_fs_trace->ds_file_groups) { |
286 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); | |
287 | } | |
288 | ||
317e35ec | 289 | BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace); |
3b4c8c43 | 290 | |
1a9f7075 PP |
291 | if (ctf_fs_trace->path) { |
292 | g_string_free(ctf_fs_trace->path, TRUE); | |
4f1f88a6 | 293 | } |
760051fa | 294 | |
1a9f7075 PP |
295 | if (ctf_fs_trace->name) { |
296 | g_string_free(ctf_fs_trace->name, TRUE); | |
297 | } | |
298 | ||
299 | if (ctf_fs_trace->metadata) { | |
300 | ctf_fs_metadata_fini(ctf_fs_trace->metadata); | |
301 | g_free(ctf_fs_trace->metadata); | |
302 | } | |
303 | ||
1a9f7075 | 304 | g_free(ctf_fs_trace); |
4c1456f0 JG |
305 | } |
306 | ||
97ade20b | 307 | static |
7f57aa08 | 308 | void ctf_fs_trace_destroy_notifier(void *data) |
97ade20b JG |
309 | { |
310 | struct ctf_fs_trace *trace = data; | |
311 | ctf_fs_trace_destroy(trace); | |
312 | } | |
313 | ||
b9879703 | 314 | void ctf_fs_finalize(bt_self_component_source *component) |
a4792757 | 315 | { |
d27a81b2 | 316 | ctf_fs_destroy(bt_self_component_get_data( |
27c0f01f | 317 | bt_self_component_source_as_self_component(component))); |
a4792757 JG |
318 | } |
319 | ||
e7a4393b | 320 | static |
4f1f88a6 PP |
321 | void port_data_destroy(void *data) { |
322 | struct ctf_fs_port_data *port_data = data; | |
323 | ||
324 | if (!port_data) { | |
325 | return; | |
326 | } | |
327 | ||
4f1f88a6 | 328 | g_free(port_data); |
5b29e799 JG |
329 | } |
330 | ||
547eacf1 PP |
331 | static |
332 | GString *get_stream_instance_unique_name( | |
333 | struct ctf_fs_ds_file_group *ds_file_group) | |
334 | { | |
335 | GString *name; | |
336 | struct ctf_fs_ds_file_info *ds_file_info; | |
337 | ||
338 | name = g_string_new(NULL); | |
339 | if (!name) { | |
340 | goto end; | |
341 | } | |
342 | ||
343 | /* | |
344 | * If there's more than one stream file in the stream file | |
345 | * group, the first (earliest) stream file's path is used as | |
346 | * the stream's unique name. | |
347 | */ | |
f6ccaed9 | 348 | BT_ASSERT(ds_file_group->ds_file_infos->len > 0); |
547eacf1 PP |
349 | ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0); |
350 | g_string_assign(name, ds_file_info->path->str); | |
351 | ||
352 | end: | |
353 | return name; | |
354 | } | |
355 | ||
5b29e799 | 356 | static |
55314f2a JG |
357 | int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, |
358 | struct ctf_fs_trace *ctf_fs_trace, | |
94cf822e | 359 | struct ctf_fs_ds_file_group *ds_file_group) |
5b29e799 | 360 | { |
4f1f88a6 | 361 | int ret = 0; |
4f1f88a6 PP |
362 | struct ctf_fs_port_data *port_data = NULL; |
363 | GString *port_name = NULL; | |
364 | ||
547eacf1 | 365 | port_name = get_stream_instance_unique_name(ds_file_group); |
4f1f88a6 PP |
366 | if (!port_name) { |
367 | goto error; | |
368 | } | |
369 | ||
55314f2a | 370 | BT_LOGD("Creating one port named `%s`", port_name->str); |
4f1f88a6 PP |
371 | |
372 | /* Create output port for this file */ | |
4f1f88a6 PP |
373 | port_data = g_new0(struct ctf_fs_port_data, 1); |
374 | if (!port_data) { | |
375 | goto error; | |
376 | } | |
377 | ||
5c563278 | 378 | port_data->ctf_fs = ctf_fs; |
94cf822e | 379 | port_data->ds_file_group = ds_file_group; |
d27a81b2 PP |
380 | ret = bt_self_component_source_add_output_port( |
381 | ctf_fs->self_comp, port_name->str, port_data, NULL); | |
147337a3 | 382 | if (ret) { |
4f1f88a6 PP |
383 | goto error; |
384 | } | |
385 | ||
386 | g_ptr_array_add(ctf_fs->port_data, port_data); | |
387 | port_data = NULL; | |
388 | goto end; | |
389 | ||
390 | error: | |
391 | ret = -1; | |
392 | ||
393 | end: | |
394 | if (port_name) { | |
395 | g_string_free(port_name, TRUE); | |
396 | } | |
397 | ||
4f1f88a6 PP |
398 | port_data_destroy(port_data); |
399 | return ret; | |
5b29e799 JG |
400 | } |
401 | ||
402 | static | |
55314f2a JG |
403 | int create_ports_for_trace(struct ctf_fs_component *ctf_fs, |
404 | struct ctf_fs_trace *ctf_fs_trace) | |
94cf822e PP |
405 | { |
406 | int ret = 0; | |
94cf822e PP |
407 | size_t i; |
408 | ||
409 | /* Create one output port for each stream file group */ | |
410 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
411 | struct ctf_fs_ds_file_group *ds_file_group = | |
412 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
413 | ||
55314f2a JG |
414 | ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, |
415 | ds_file_group); | |
94cf822e | 416 | if (ret) { |
55314f2a | 417 | BT_LOGE("Cannot create output port."); |
94cf822e PP |
418 | goto end; |
419 | } | |
420 | } | |
421 | ||
422 | end: | |
423 | return ret; | |
424 | } | |
425 | ||
94cf822e PP |
426 | static |
427 | void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info) | |
428 | { | |
429 | if (!ds_file_info) { | |
430 | return; | |
431 | } | |
432 | ||
433 | if (ds_file_info->path) { | |
434 | g_string_free(ds_file_info->path, TRUE); | |
435 | } | |
436 | ||
97ade20b | 437 | ctf_fs_ds_index_destroy(ds_file_info->index); |
94cf822e PP |
438 | g_free(ds_file_info); |
439 | } | |
440 | ||
441 | static | |
442 | struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, | |
61f59358 | 443 | int64_t begin_ns, struct ctf_fs_ds_index *index) |
94cf822e PP |
444 | { |
445 | struct ctf_fs_ds_file_info *ds_file_info; | |
446 | ||
447 | ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1); | |
448 | if (!ds_file_info) { | |
449 | goto end; | |
450 | } | |
451 | ||
452 | ds_file_info->path = g_string_new(path); | |
453 | if (!ds_file_info->path) { | |
454 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
455 | ds_file_info = NULL; | |
456 | goto end; | |
457 | } | |
458 | ||
459 | ds_file_info->begin_ns = begin_ns; | |
97ade20b JG |
460 | ds_file_info->index = index; |
461 | index = NULL; | |
94cf822e PP |
462 | |
463 | end: | |
97ade20b | 464 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
465 | return ds_file_info; |
466 | } | |
467 | ||
468 | static | |
469 | void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) | |
470 | { | |
471 | if (!ds_file_group) { | |
472 | return; | |
473 | } | |
474 | ||
475 | if (ds_file_group->ds_file_infos) { | |
476 | g_ptr_array_free(ds_file_group->ds_file_infos, TRUE); | |
477 | } | |
478 | ||
317e35ec PP |
479 | bt_stream_put_ref(ds_file_group->stream); |
480 | bt_stream_class_put_ref(ds_file_group->stream_class); | |
94cf822e PP |
481 | g_free(ds_file_group); |
482 | } | |
483 | ||
484 | static | |
485 | struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( | |
486 | struct ctf_fs_trace *ctf_fs_trace, | |
b9879703 | 487 | bt_stream_class *stream_class, |
94cf822e PP |
488 | uint64_t stream_instance_id) |
489 | { | |
490 | struct ctf_fs_ds_file_group *ds_file_group; | |
491 | ||
94cf822e PP |
492 | ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1); |
493 | if (!ds_file_group) { | |
494 | goto error; | |
495 | } | |
496 | ||
497 | ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func( | |
498 | (GDestroyNotify) ctf_fs_ds_file_info_destroy); | |
499 | if (!ds_file_group->ds_file_infos) { | |
500 | goto error; | |
501 | } | |
502 | ||
547eacf1 | 503 | ds_file_group->stream_id = stream_instance_id; |
f6ccaed9 | 504 | BT_ASSERT(stream_class); |
5e8988cb | 505 | ds_file_group->stream_class = stream_class; |
317e35ec | 506 | bt_stream_class_get_ref(ds_file_group->stream_class); |
94cf822e | 507 | ds_file_group->ctf_fs_trace = ctf_fs_trace; |
94cf822e PP |
508 | goto end; |
509 | ||
510 | error: | |
511 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
512 | ds_file_group = NULL; | |
513 | ||
514 | end: | |
515 | return ds_file_group; | |
516 | } | |
517 | ||
8bf7105e MJ |
518 | /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */ |
519 | static | |
520 | void array_insert(GPtrArray *array, gpointer element, size_t pos) | |
521 | { | |
522 | size_t original_array_len = array->len; | |
523 | ||
524 | /* Allocate an unused element at the end of the array. */ | |
525 | g_ptr_array_add(array, NULL); | |
526 | ||
527 | /* If we are not inserting at the end, move the elements by one. */ | |
528 | if (pos < original_array_len) { | |
529 | memmove(&(array->pdata[pos + 1]), | |
530 | &(array->pdata[pos]), | |
531 | (original_array_len - pos) * sizeof(gpointer)); | |
532 | } | |
533 | ||
534 | /* Insert the value and bump the array len */ | |
535 | array->pdata[pos] = element; | |
536 | } | |
537 | ||
94cf822e PP |
538 | static |
539 | int ctf_fs_ds_file_group_add_ds_file_info( | |
540 | struct ctf_fs_ds_file_group *ds_file_group, | |
61f59358 | 541 | const char *path, int64_t begin_ns, |
97ade20b | 542 | struct ctf_fs_ds_index *index) |
94cf822e PP |
543 | { |
544 | struct ctf_fs_ds_file_info *ds_file_info; | |
545 | gint i = 0; | |
546 | int ret = 0; | |
547 | ||
97ade20b JG |
548 | /* Onwership of index is transferred. */ |
549 | ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index); | |
550 | index = NULL; | |
94cf822e PP |
551 | if (!ds_file_info) { |
552 | goto error; | |
553 | } | |
554 | ||
555 | /* Find a spot to insert this one */ | |
556 | for (i = 0; i < ds_file_group->ds_file_infos->len; i++) { | |
557 | struct ctf_fs_ds_file_info *other_ds_file_info = | |
558 | g_ptr_array_index(ds_file_group->ds_file_infos, i); | |
559 | ||
560 | if (begin_ns < other_ds_file_info->begin_ns) { | |
561 | break; | |
562 | } | |
563 | } | |
564 | ||
8bf7105e | 565 | array_insert(ds_file_group->ds_file_infos, ds_file_info, i); |
94cf822e PP |
566 | ds_file_info = NULL; |
567 | goto end; | |
568 | ||
569 | error: | |
570 | ctf_fs_ds_file_info_destroy(ds_file_info); | |
97ade20b | 571 | ctf_fs_ds_index_destroy(index); |
94cf822e | 572 | ret = -1; |
94cf822e PP |
573 | end: |
574 | return ret; | |
575 | } | |
576 | ||
577 | static | |
578 | int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, | |
351946b4 | 579 | const char *path) |
94cf822e | 580 | { |
b9879703 | 581 | bt_stream_class *stream_class = NULL; |
61f59358 PP |
582 | int64_t stream_instance_id = -1; |
583 | int64_t begin_ns = -1; | |
94cf822e PP |
584 | struct ctf_fs_ds_file_group *ds_file_group = NULL; |
585 | bool add_group = false; | |
586 | int ret; | |
587 | size_t i; | |
6de92955 | 588 | struct ctf_fs_ds_file *ds_file = NULL; |
97ade20b | 589 | struct ctf_fs_ds_index *index = NULL; |
c14831f0 | 590 | struct bt_msg_iter *msg_iter = NULL; |
61f59358 | 591 | struct ctf_stream_class *sc = NULL; |
c14831f0 | 592 | struct bt_msg_iter_packet_properties props; |
94cf822e | 593 | |
c14831f0 | 594 | msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc, |
6de92955 | 595 | bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL); |
c14831f0 PP |
596 | if (!msg_iter) { |
597 | BT_LOGE_STR("Cannot create a CTF message iterator."); | |
6de92955 PP |
598 | goto error; |
599 | } | |
600 | ||
c14831f0 | 601 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter, |
5c563278 | 602 | NULL, path); |
97ade20b JG |
603 | if (!ds_file) { |
604 | goto error; | |
605 | } | |
606 | ||
8f5d08f2 | 607 | ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props); |
94cf822e | 608 | if (ret) { |
55314f2a | 609 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
94cf822e PP |
610 | path); |
611 | goto error; | |
612 | } | |
613 | ||
61f59358 PP |
614 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, |
615 | props.stream_class_id); | |
616 | BT_ASSERT(sc); | |
617 | stream_class = sc->ir_sc; | |
618 | BT_ASSERT(stream_class); | |
619 | stream_instance_id = props.data_stream_id; | |
620 | ||
621 | if (props.snapshots.beginning_clock != UINT64_C(-1)) { | |
622 | BT_ASSERT(sc->default_clock_class); | |
00d7a6f2 PP |
623 | ret = bt_util_clock_cycles_to_ns_from_origin( |
624 | props.snapshots.beginning_clock, | |
625 | sc->default_clock_class->frequency, | |
626 | sc->default_clock_class->offset_seconds, | |
627 | sc->default_clock_class->offset_cycles, &begin_ns); | |
61f59358 PP |
628 | if (ret) { |
629 | BT_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).", | |
630 | path); | |
631 | goto error; | |
632 | } | |
94cf822e PP |
633 | } |
634 | ||
97ade20b JG |
635 | index = ctf_fs_ds_file_build_index(ds_file); |
636 | if (!index) { | |
637 | BT_LOGW("Failed to index CTF stream file \'%s\'", | |
638 | ds_file->file->path->str); | |
639 | } | |
640 | ||
61f59358 | 641 | if (begin_ns == -1) { |
94cf822e PP |
642 | /* |
643 | * No beggining timestamp to sort the stream files | |
644 | * within a stream file group, so consider that this | |
645 | * file must be the only one within its group. | |
646 | */ | |
61f59358 | 647 | stream_instance_id = -1; |
94cf822e PP |
648 | } |
649 | ||
61f59358 | 650 | if (stream_instance_id == -1) { |
94cf822e PP |
651 | /* |
652 | * No stream instance ID or no beginning timestamp: | |
653 | * create a unique stream file group for this stream | |
654 | * file because, even if there's a stream instance ID, | |
655 | * there's no timestamp to order the file within its | |
656 | * group. | |
657 | */ | |
94cf822e PP |
658 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
659 | stream_class, stream_instance_id); | |
660 | if (!ds_file_group) { | |
661 | goto error; | |
662 | } | |
663 | ||
664 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, | |
547eacf1 | 665 | path, begin_ns, index); |
97ade20b JG |
666 | /* Ownership of index is transferred. */ |
667 | index = NULL; | |
94cf822e PP |
668 | if (ret) { |
669 | goto error; | |
670 | } | |
671 | ||
672 | add_group = true; | |
673 | goto end; | |
674 | } | |
675 | ||
61f59358 PP |
676 | BT_ASSERT(stream_instance_id != -1); |
677 | BT_ASSERT(begin_ns != -1); | |
94cf822e PP |
678 | |
679 | /* Find an existing stream file group with this ID */ | |
680 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
94cf822e PP |
681 | ds_file_group = g_ptr_array_index( |
682 | ctf_fs_trace->ds_file_groups, i); | |
94cf822e | 683 | |
547eacf1 PP |
684 | if (ds_file_group->stream_class == stream_class && |
685 | ds_file_group->stream_id == | |
686 | stream_instance_id) { | |
94cf822e PP |
687 | break; |
688 | } | |
689 | ||
690 | ds_file_group = NULL; | |
691 | } | |
692 | ||
693 | if (!ds_file_group) { | |
694 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, | |
695 | stream_class, stream_instance_id); | |
696 | if (!ds_file_group) { | |
697 | goto error; | |
698 | } | |
699 | ||
700 | add_group = true; | |
701 | } | |
702 | ||
547eacf1 PP |
703 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path, |
704 | begin_ns, index); | |
97ade20b | 705 | index = NULL; |
94cf822e PP |
706 | if (ret) { |
707 | goto error; | |
708 | } | |
709 | ||
710 | goto end; | |
711 | ||
712 | error: | |
713 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
714 | ret = -1; | |
715 | ||
716 | end: | |
717 | if (add_group && ds_file_group) { | |
718 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); | |
719 | } | |
547eacf1 | 720 | |
97ade20b | 721 | ctf_fs_ds_file_destroy(ds_file); |
6de92955 | 722 | |
c14831f0 PP |
723 | if (msg_iter) { |
724 | bt_msg_iter_destroy(msg_iter); | |
6de92955 PP |
725 | } |
726 | ||
97ade20b | 727 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
728 | return ret; |
729 | } | |
730 | ||
731 | static | |
351946b4 | 732 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) |
e7a4393b JG |
733 | { |
734 | int ret = 0; | |
4f1f88a6 | 735 | const char *basename; |
e7a4393b | 736 | GError *error = NULL; |
4f1f88a6 | 737 | GDir *dir = NULL; |
547eacf1 | 738 | size_t i; |
e7a4393b | 739 | |
94cf822e | 740 | /* Check each file in the path directory, except specific ones */ |
1a9f7075 | 741 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
e7a4393b | 742 | if (!dir) { |
55314f2a | 743 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 | 744 | ctf_fs_trace->path->str, error->message, |
4f1f88a6 | 745 | error->code); |
e7a4393b JG |
746 | goto error; |
747 | } | |
748 | ||
4f1f88a6 | 749 | while ((basename = g_dir_read_name(dir))) { |
94cf822e PP |
750 | struct ctf_fs_file *file; |
751 | ||
4f1f88a6 | 752 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
e7a4393b | 753 | /* Ignore the metadata stream. */ |
3743a302 | 754 | BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 755 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
756 | continue; |
757 | } | |
758 | ||
4f1f88a6 | 759 | if (basename[0] == '.') { |
3743a302 | 760 | BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 761 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
762 | continue; |
763 | } | |
764 | ||
765 | /* Create the file. */ | |
55314f2a | 766 | file = ctf_fs_file_create(); |
e7a4393b | 767 | if (!file) { |
3743a302 | 768 | BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 769 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
770 | goto error; |
771 | } | |
772 | ||
773 | /* Create full path string. */ | |
3743a302 | 774 | g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s", |
1a9f7075 | 775 | ctf_fs_trace->path->str, basename); |
e7a4393b | 776 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
55314f2a | 777 | BT_LOGD("Ignoring non-regular file `%s`", |
1a9f7075 | 778 | file->path->str); |
e7a4393b | 779 | ctf_fs_file_destroy(file); |
4f1f88a6 | 780 | file = NULL; |
e7a4393b JG |
781 | continue; |
782 | } | |
783 | ||
55314f2a | 784 | ret = ctf_fs_file_open(file, "rb"); |
4f1f88a6 | 785 | if (ret) { |
55314f2a | 786 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
e7a4393b JG |
787 | goto error; |
788 | } | |
789 | ||
9fa0891a JG |
790 | if (file->size == 0) { |
791 | /* Skip empty stream. */ | |
55314f2a | 792 | BT_LOGD("Ignoring empty file `%s`", file->path->str); |
9fa0891a JG |
793 | ctf_fs_file_destroy(file); |
794 | continue; | |
795 | } | |
796 | ||
351946b4 | 797 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
94cf822e | 798 | file->path->str); |
4f1f88a6 | 799 | if (ret) { |
89b08333 | 800 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
1a9f7075 | 801 | file->path->str); |
94cf822e | 802 | ctf_fs_file_destroy(file); |
e7a4393b JG |
803 | goto error; |
804 | } | |
805 | ||
4f1f88a6 | 806 | ctf_fs_file_destroy(file); |
e7a4393b JG |
807 | } |
808 | ||
8f5d08f2 PP |
809 | if (!ctf_fs_trace->trace) { |
810 | goto end; | |
811 | } | |
812 | ||
547eacf1 PP |
813 | /* |
814 | * At this point, DS file groupes are created, but their | |
815 | * associated stream objects do not exist yet. This is because | |
816 | * we need to name the created stream object with the data | |
817 | * stream file's path. We have everything we need here to do | |
818 | * this. | |
819 | */ | |
820 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
821 | struct ctf_fs_ds_file_group *ds_file_group = | |
822 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
823 | GString *name = get_stream_instance_unique_name(ds_file_group); | |
824 | ||
825 | if (!name) { | |
826 | goto error; | |
827 | } | |
828 | ||
61f59358 | 829 | if (ds_file_group->stream_id == UINT64_C(-1)) { |
3dca2276 | 830 | /* No stream ID: use 0 */ |
dadf29b9 | 831 | ds_file_group->stream = bt_stream_create_with_id( |
61f59358 | 832 | ds_file_group->stream_class, |
3b4c8c43 | 833 | ctf_fs_trace->trace, |
3dca2276 PP |
834 | ctf_fs_trace->next_stream_id); |
835 | ctf_fs_trace->next_stream_id++; | |
547eacf1 PP |
836 | } else { |
837 | /* Specific stream ID */ | |
dadf29b9 | 838 | ds_file_group->stream = bt_stream_create_with_id( |
61f59358 | 839 | ds_file_group->stream_class, |
3b4c8c43 | 840 | ctf_fs_trace->trace, |
61f59358 | 841 | (uint64_t) ds_file_group->stream_id); |
547eacf1 PP |
842 | } |
843 | ||
7742909c | 844 | if (!ds_file_group->stream) { |
547eacf1 PP |
845 | BT_LOGE("Cannot create stream for DS file group: " |
846 | "addr=%p, stream-name=\"%s\"", | |
847 | ds_file_group, name->str); | |
61f59358 PP |
848 | g_string_free(name, TRUE); |
849 | goto error; | |
850 | } | |
851 | ||
dadf29b9 | 852 | ret = bt_stream_set_name(ds_file_group->stream, |
351946b4 | 853 | name->str); |
61f59358 PP |
854 | if (ret) { |
855 | BT_LOGE("Cannot set stream's name: " | |
856 | "addr=%p, stream-name=\"%s\"", | |
857 | ds_file_group->stream, name->str); | |
858 | g_string_free(name, TRUE); | |
547eacf1 PP |
859 | goto error; |
860 | } | |
61f59358 PP |
861 | |
862 | g_string_free(name, TRUE); | |
547eacf1 PP |
863 | } |
864 | ||
e7a4393b | 865 | goto end; |
4f1f88a6 | 866 | |
e7a4393b JG |
867 | error: |
868 | ret = -1; | |
4f1f88a6 | 869 | |
e7a4393b JG |
870 | end: |
871 | if (dir) { | |
872 | g_dir_close(dir); | |
873 | dir = NULL; | |
874 | } | |
4f1f88a6 | 875 | |
e7a4393b JG |
876 | if (error) { |
877 | g_error_free(error); | |
878 | } | |
5b29e799 | 879 | |
91457551 | 880 | return ret; |
5b29e799 JG |
881 | } |
882 | ||
3b4c8c43 | 883 | static |
b9879703 | 884 | int set_trace_name(bt_trace *trace, const char *name_suffix) |
3b4c8c43 PP |
885 | { |
886 | int ret = 0; | |
b9879703 PP |
887 | const bt_trace_class *tc = bt_trace_borrow_class_const(trace); |
888 | const bt_value *val; | |
3b4c8c43 PP |
889 | GString *name; |
890 | ||
891 | name = g_string_new(NULL); | |
892 | if (!name) { | |
893 | BT_LOGE_STR("Failed to allocate a GString."); | |
894 | ret = -1; | |
895 | goto end; | |
896 | } | |
897 | ||
898 | /* | |
899 | * Check if we have a trace environment string value named `hostname`. | |
900 | * If so, use it as the trace name's prefix. | |
901 | */ | |
902 | val = bt_trace_class_borrow_environment_entry_value_by_name_const( | |
903 | tc, "hostname"); | |
904 | if (val && bt_value_is_string(val)) { | |
905 | g_string_append(name, bt_value_string_get(val)); | |
906 | ||
907 | if (name_suffix) { | |
908 | g_string_append_c(name, G_DIR_SEPARATOR); | |
909 | } | |
910 | } | |
911 | ||
912 | if (name_suffix) { | |
913 | g_string_append(name, name_suffix); | |
914 | } | |
915 | ||
916 | ret = bt_trace_set_name(trace, name->str); | |
917 | if (ret) { | |
918 | goto end; | |
919 | } | |
920 | ||
921 | goto end; | |
922 | ||
923 | end: | |
924 | if (name) { | |
925 | g_string_free(name, TRUE); | |
926 | } | |
927 | ||
928 | return ret; | |
929 | } | |
930 | ||
97ade20b | 931 | BT_HIDDEN |
8f5d08f2 PP |
932 | struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component_source *self_comp, |
933 | const char *path, const char *name, | |
351946b4 | 934 | struct ctf_fs_metadata_config *metadata_config) |
1a9f7075 PP |
935 | { |
936 | struct ctf_fs_trace *ctf_fs_trace; | |
937 | int ret; | |
938 | ||
939 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); | |
940 | if (!ctf_fs_trace) { | |
941 | goto end; | |
942 | } | |
943 | ||
1a9f7075 PP |
944 | ctf_fs_trace->path = g_string_new(path); |
945 | if (!ctf_fs_trace->path) { | |
946 | goto error; | |
947 | } | |
948 | ||
949 | ctf_fs_trace->name = g_string_new(name); | |
950 | if (!ctf_fs_trace->name) { | |
951 | goto error; | |
952 | } | |
953 | ||
954 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); | |
955 | if (!ctf_fs_trace->metadata) { | |
956 | goto error; | |
957 | } | |
958 | ||
61f59358 | 959 | ctf_fs_metadata_init(ctf_fs_trace->metadata); |
94cf822e PP |
960 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
961 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); | |
962 | if (!ctf_fs_trace->ds_file_groups) { | |
963 | goto error; | |
964 | } | |
965 | ||
8f5d08f2 PP |
966 | ret = ctf_fs_metadata_set_trace_class(self_comp, |
967 | ctf_fs_trace, metadata_config); | |
3b4c8c43 PP |
968 | if (ret) { |
969 | goto error; | |
970 | } | |
971 | ||
8f5d08f2 PP |
972 | if (ctf_fs_trace->metadata->trace_class) { |
973 | ctf_fs_trace->trace = | |
974 | bt_trace_create(ctf_fs_trace->metadata->trace_class); | |
975 | if (!ctf_fs_trace->trace) { | |
976 | goto error; | |
977 | } | |
3b4c8c43 PP |
978 | } |
979 | ||
8f5d08f2 PP |
980 | if (ctf_fs_trace->trace) { |
981 | ret = set_trace_name(ctf_fs_trace->trace, name); | |
982 | if (ret) { | |
983 | goto error; | |
984 | } | |
1a9f7075 PP |
985 | } |
986 | ||
351946b4 | 987 | ret = create_ds_file_groups(ctf_fs_trace); |
94cf822e PP |
988 | if (ret) { |
989 | goto error; | |
990 | } | |
991 | ||
27d1a78b PP |
992 | /* |
993 | * create_ds_file_groups() created all the streams that this | |
994 | * trace needs. There won't be any more. Therefore it is safe to | |
995 | * make this trace static. | |
996 | */ | |
8f5d08f2 PP |
997 | if (ctf_fs_trace->trace) { |
998 | (void) bt_trace_make_static(ctf_fs_trace->trace); | |
999 | } | |
27d1a78b | 1000 | |
1a9f7075 PP |
1001 | goto end; |
1002 | ||
1003 | error: | |
1004 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1005 | ctf_fs_trace = NULL; | |
61f59358 | 1006 | |
1a9f7075 PP |
1007 | end: |
1008 | return ctf_fs_trace; | |
1009 | } | |
1010 | ||
1011 | static | |
1012 | int path_is_ctf_trace(const char *path) | |
1013 | { | |
1014 | GString *metadata_path = g_string_new(NULL); | |
1015 | int ret = 0; | |
1016 | ||
1017 | if (!metadata_path) { | |
1018 | ret = -1; | |
1019 | goto end; | |
1020 | } | |
1021 | ||
3743a302 | 1022 | g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME); |
1a9f7075 PP |
1023 | |
1024 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { | |
1025 | ret = 1; | |
1026 | goto end; | |
1027 | } | |
1028 | ||
1029 | end: | |
1030 | g_string_free(metadata_path, TRUE); | |
1031 | return ret; | |
1032 | } | |
1033 | ||
1034 | static | |
55314f2a | 1035 | int add_trace_path(GList **trace_paths, const char *path) |
1a9f7075 | 1036 | { |
4bd72b60 | 1037 | GString *norm_path = NULL; |
1a9f7075 | 1038 | int ret = 0; |
1a9f7075 | 1039 | |
4bd72b60 PP |
1040 | norm_path = bt_common_normalize_path(path, NULL); |
1041 | if (!norm_path) { | |
55314f2a | 1042 | BT_LOGE("Failed to normalize path `%s`.", path); |
1a9f7075 PP |
1043 | ret = -1; |
1044 | goto end; | |
1045 | } | |
1046 | ||
3743a302 | 1047 | // FIXME: Remove or ifdef for __MINGW32__ |
4bd72b60 | 1048 | if (strcmp(norm_path->str, "/") == 0) { |
55314f2a | 1049 | BT_LOGE("Opening a trace in `/` is not supported."); |
1a9f7075 PP |
1050 | ret = -1; |
1051 | goto end; | |
1052 | } | |
1053 | ||
4bd72b60 | 1054 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
f6ccaed9 | 1055 | BT_ASSERT(*trace_paths); |
4bd72b60 | 1056 | norm_path = NULL; |
1a9f7075 PP |
1057 | |
1058 | end: | |
4bd72b60 PP |
1059 | if (norm_path) { |
1060 | g_string_free(norm_path, TRUE); | |
1061 | } | |
1062 | ||
1a9f7075 PP |
1063 | return ret; |
1064 | } | |
1065 | ||
55314f2a JG |
1066 | BT_HIDDEN |
1067 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) | |
1a9f7075 PP |
1068 | { |
1069 | int ret; | |
1070 | GError *error = NULL; | |
1071 | GDir *dir = NULL; | |
1072 | const char *basename = NULL; | |
1073 | ||
1074 | /* Check if the starting path is a CTF trace itself */ | |
1075 | ret = path_is_ctf_trace(start_path); | |
1076 | if (ret < 0) { | |
1077 | goto end; | |
1078 | } | |
1079 | ||
1080 | if (ret) { | |
1081 | /* | |
4bd72b60 PP |
1082 | * Stop recursion: a CTF trace cannot contain another |
1083 | * CTF trace. | |
1a9f7075 | 1084 | */ |
55314f2a | 1085 | ret = add_trace_path(trace_paths, start_path); |
1a9f7075 PP |
1086 | goto end; |
1087 | } | |
1088 | ||
1089 | /* Look for subdirectories */ | |
1090 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { | |
1091 | /* Starting path is not a directory: end of recursion */ | |
1092 | goto end; | |
1093 | } | |
1094 | ||
1095 | dir = g_dir_open(start_path, 0, &error); | |
1096 | if (!dir) { | |
1097 | if (error->code == G_FILE_ERROR_ACCES) { | |
55314f2a | 1098 | BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing", |
1a9f7075 PP |
1099 | start_path, error->message, error->code); |
1100 | goto end; | |
1101 | } | |
1102 | ||
55314f2a | 1103 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 PP |
1104 | start_path, error->message, error->code); |
1105 | ret = -1; | |
1106 | goto end; | |
1107 | } | |
1108 | ||
1109 | while ((basename = g_dir_read_name(dir))) { | |
1110 | GString *sub_path = g_string_new(NULL); | |
1111 | ||
1112 | if (!sub_path) { | |
1113 | ret = -1; | |
1114 | goto end; | |
1115 | } | |
1116 | ||
3743a302 | 1117 | g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename); |
55314f2a | 1118 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
1a9f7075 PP |
1119 | g_string_free(sub_path, TRUE); |
1120 | if (ret) { | |
1121 | goto end; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | end: | |
1126 | if (dir) { | |
1127 | g_dir_close(dir); | |
1128 | } | |
1129 | ||
1130 | if (error) { | |
1131 | g_error_free(error); | |
1132 | } | |
1133 | ||
1134 | return ret; | |
1135 | } | |
1136 | ||
55314f2a JG |
1137 | BT_HIDDEN |
1138 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { | |
1a9f7075 | 1139 | GList *trace_names = NULL; |
1a9f7075 | 1140 | GList *node; |
4bd72b60 PP |
1141 | const char *last_sep; |
1142 | size_t base_dist; | |
1a9f7075 PP |
1143 | |
1144 | /* | |
4bd72b60 PP |
1145 | * At this point we know that all the trace paths are |
1146 | * normalized, and so is the base path. This means that | |
1147 | * they are absolute and they don't end with a separator. | |
1148 | * We can simply find the location of the last separator | |
1149 | * in the base path, which gives us the name of the actual | |
1150 | * directory to look into, and use this location as the | |
1151 | * start of each trace name within each trace path. | |
1152 | * | |
1153 | * For example: | |
1154 | * | |
1155 | * Base path: /home/user/my-traces/some-trace | |
1156 | * Trace paths: | |
1157 | * - /home/user/my-traces/some-trace/host1/trace1 | |
1158 | * - /home/user/my-traces/some-trace/host1/trace2 | |
1159 | * - /home/user/my-traces/some-trace/host2/trace | |
1160 | * - /home/user/my-traces/some-trace/other-trace | |
1161 | * | |
1162 | * In this case the trace names are: | |
1163 | * | |
1164 | * - some-trace/host1/trace1 | |
1165 | * - some-trace/host1/trace2 | |
1166 | * - some-trace/host2/trace | |
1167 | * - some-trace/other-trace | |
1a9f7075 | 1168 | */ |
4bd72b60 | 1169 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
1a9f7075 | 1170 | |
4bd72b60 | 1171 | /* We know there's at least one separator */ |
f6ccaed9 | 1172 | BT_ASSERT(last_sep); |
1a9f7075 | 1173 | |
4bd72b60 PP |
1174 | /* Distance to base */ |
1175 | base_dist = last_sep - base_path + 1; | |
1a9f7075 PP |
1176 | |
1177 | /* Create the trace names */ | |
1178 | for (node = trace_paths; node; node = g_list_next(node)) { | |
1179 | GString *trace_name = g_string_new(NULL); | |
1180 | GString *trace_path = node->data; | |
1181 | ||
f6ccaed9 | 1182 | BT_ASSERT(trace_name); |
4bd72b60 | 1183 | g_string_assign(trace_name, &trace_path->str[base_dist]); |
1a9f7075 PP |
1184 | trace_names = g_list_append(trace_names, trace_name); |
1185 | } | |
1186 | ||
1187 | return trace_names; | |
1188 | } | |
1189 | ||
1190 | static | |
8f5d08f2 PP |
1191 | int create_ctf_fs_traces(bt_self_component_source *self_comp, |
1192 | struct ctf_fs_component *ctf_fs, | |
1a9f7075 PP |
1193 | const char *path_param) |
1194 | { | |
1195 | struct ctf_fs_trace *ctf_fs_trace = NULL; | |
1196 | int ret = 0; | |
4bd72b60 | 1197 | GString *norm_path = NULL; |
1a9f7075 PP |
1198 | GList *trace_paths = NULL; |
1199 | GList *trace_names = NULL; | |
1200 | GList *tp_node; | |
1201 | GList *tn_node; | |
1202 | ||
4bd72b60 PP |
1203 | norm_path = bt_common_normalize_path(path_param, NULL); |
1204 | if (!norm_path) { | |
55314f2a | 1205 | BT_LOGE("Failed to normalize path: `%s`.", |
4bd72b60 PP |
1206 | path_param); |
1207 | goto error; | |
1208 | } | |
1209 | ||
55314f2a | 1210 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
1a9f7075 PP |
1211 | if (ret) { |
1212 | goto error; | |
1213 | } | |
1214 | ||
1215 | if (!trace_paths) { | |
55314f2a | 1216 | BT_LOGE("No CTF traces recursively found in `%s`.", |
1a9f7075 PP |
1217 | path_param); |
1218 | goto error; | |
1219 | } | |
1220 | ||
55314f2a | 1221 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
1a9f7075 | 1222 | if (!trace_names) { |
55314f2a | 1223 | BT_LOGE("Cannot create trace names from trace paths."); |
1a9f7075 PP |
1224 | goto error; |
1225 | } | |
1226 | ||
1227 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; | |
1228 | tp_node = g_list_next(tp_node), | |
1229 | tn_node = g_list_next(tn_node)) { | |
1230 | GString *trace_path = tp_node->data; | |
1231 | GString *trace_name = tn_node->data; | |
1232 | ||
8f5d08f2 PP |
1233 | ctf_fs_trace = ctf_fs_trace_create(self_comp, |
1234 | trace_path->str, trace_name->str, | |
1235 | &ctf_fs->metadata_config); | |
1a9f7075 | 1236 | if (!ctf_fs_trace) { |
55314f2a | 1237 | BT_LOGE("Cannot create trace for `%s`.", |
1a9f7075 PP |
1238 | trace_path->str); |
1239 | goto error; | |
1240 | } | |
52c5fe74 | 1241 | |
55314f2a JG |
1242 | ret = create_ports_for_trace(ctf_fs, ctf_fs_trace); |
1243 | if (ret) { | |
1244 | goto error; | |
1245 | } | |
1246 | ||
52c5fe74 PP |
1247 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); |
1248 | ctf_fs_trace = NULL; | |
1a9f7075 PP |
1249 | } |
1250 | ||
1a9f7075 PP |
1251 | goto end; |
1252 | ||
1253 | error: | |
1254 | ret = -1; | |
1255 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1256 | ||
1257 | end: | |
1258 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { | |
1259 | if (tp_node->data) { | |
1260 | g_string_free(tp_node->data, TRUE); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { | |
1265 | if (tn_node->data) { | |
1266 | g_string_free(tn_node->data, TRUE); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | if (trace_paths) { | |
1271 | g_list_free(trace_paths); | |
1272 | } | |
1273 | ||
1274 | if (trace_names) { | |
1275 | g_list_free(trace_names); | |
1276 | } | |
1277 | ||
4bd72b60 PP |
1278 | if (norm_path) { |
1279 | g_string_free(norm_path, TRUE); | |
1280 | } | |
1281 | ||
1a9f7075 PP |
1282 | return ret; |
1283 | } | |
1284 | ||
5b29e799 | 1285 | static |
d27a81b2 | 1286 | struct ctf_fs_component *ctf_fs_create( |
b9879703 PP |
1287 | bt_self_component_source *self_comp, |
1288 | const bt_value *params) | |
56a1cced JG |
1289 | { |
1290 | struct ctf_fs_component *ctf_fs; | |
b9879703 | 1291 | const bt_value *value = NULL; |
1a9f7075 | 1292 | const char *path_param; |
56a1cced JG |
1293 | |
1294 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
1295 | if (!ctf_fs) { | |
1296 | goto end; | |
1297 | } | |
1298 | ||
d27a81b2 | 1299 | bt_self_component_set_data( |
27c0f01f | 1300 | bt_self_component_source_as_self_component(self_comp), |
d27a81b2 | 1301 | ctf_fs); |
1a9f7075 | 1302 | |
4f1f88a6 PP |
1303 | /* |
1304 | * We don't need to get a new reference here because as long as | |
1305 | * our private ctf_fs_component object exists, the containing | |
1306 | * private component should also exist. | |
1307 | */ | |
d27a81b2 | 1308 | ctf_fs->self_comp = self_comp; |
82fa4539 | 1309 | value = bt_value_map_borrow_entry_value_const(params, "path"); |
f6ccaed9 | 1310 | if (value && !bt_value_is_string(value)) { |
56a1cced JG |
1311 | goto error; |
1312 | } | |
1313 | ||
605c62ba | 1314 | path_param = bt_value_string_get(value); |
82fa4539 PP |
1315 | value = bt_value_map_borrow_entry_value_const(params, |
1316 | "clock-class-offset-s"); | |
92540773 | 1317 | if (value) { |
a2a54545 PP |
1318 | if (!bt_value_is_integer(value)) { |
1319 | BT_LOGE("clock-class-offset-s should be an integer"); | |
1320 | goto error; | |
1321 | } | |
605c62ba | 1322 | ctf_fs->metadata_config.clock_class_offset_s = bt_value_integer_get(value); |
a2a54545 | 1323 | } |
92540773 | 1324 | |
82fa4539 PP |
1325 | value = bt_value_map_borrow_entry_value_const(params, |
1326 | "clock-class-offset-ns"); | |
a2a54545 | 1327 | if (value) { |
92540773 | 1328 | if (!bt_value_is_integer(value)) { |
a2a54545 | 1329 | BT_LOGE("clock-class-offset-ns should be an integer"); |
92540773 JD |
1330 | goto error; |
1331 | } | |
605c62ba | 1332 | ctf_fs->metadata_config.clock_class_offset_ns = bt_value_integer_get(value); |
92540773 JD |
1333 | } |
1334 | ||
1a9f7075 PP |
1335 | ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); |
1336 | if (!ctf_fs->port_data) { | |
4f1f88a6 PP |
1337 | goto error; |
1338 | } | |
1339 | ||
97ade20b | 1340 | ctf_fs->traces = g_ptr_array_new_with_free_func( |
7f57aa08 | 1341 | ctf_fs_trace_destroy_notifier); |
1a9f7075 | 1342 | if (!ctf_fs->traces) { |
599faa1c PP |
1343 | goto error; |
1344 | } | |
1345 | ||
8f5d08f2 | 1346 | if (create_ctf_fs_traces(self_comp, ctf_fs, path_param)) { |
4f1f88a6 PP |
1347 | goto error; |
1348 | } | |
1349 | ||
1ef09eb5 JG |
1350 | goto end; |
1351 | ||
56a1cced | 1352 | error: |
1a9f7075 | 1353 | ctf_fs_destroy(ctf_fs); |
e7a4393b | 1354 | ctf_fs = NULL; |
d27a81b2 | 1355 | bt_self_component_set_data( |
27c0f01f | 1356 | bt_self_component_source_as_self_component(self_comp), |
d27a81b2 | 1357 | NULL); |
1a9f7075 | 1358 | |
1ef09eb5 | 1359 | end: |
56a1cced JG |
1360 | return ctf_fs; |
1361 | } | |
1362 | ||
ea0b4b9e | 1363 | BT_HIDDEN |
b7c5b62e | 1364 | bt_self_component_status ctf_fs_init( |
b9879703 PP |
1365 | bt_self_component_source *self_comp, |
1366 | const bt_value *params, UNUSED_VAR void *init_method_data) | |
ea0b4b9e JG |
1367 | { |
1368 | struct ctf_fs_component *ctf_fs; | |
b7c5b62e | 1369 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
ea0b4b9e | 1370 | |
d27a81b2 | 1371 | ctf_fs = ctf_fs_create(self_comp, params); |
ea0b4b9e | 1372 | if (!ctf_fs) { |
d27a81b2 | 1373 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
ea0b4b9e | 1374 | } |
4c1456f0 | 1375 | |
ea0b4b9e JG |
1376 | return ret; |
1377 | } | |
33f93973 PP |
1378 | |
1379 | BT_HIDDEN | |
b7c5b62e | 1380 | bt_query_status ctf_fs_query( |
b9879703 PP |
1381 | bt_self_component_class_source *comp_class, |
1382 | const bt_query_executor *query_exec, | |
1383 | const char *object, const bt_value *params, | |
1384 | const bt_value **result) | |
33f93973 | 1385 | { |
b7c5b62e | 1386 | bt_query_status status = BT_QUERY_STATUS_OK; |
33f93973 | 1387 | |
04c0ba87 | 1388 | if (!strcmp(object, "metadata-info")) { |
d27a81b2 | 1389 | status = metadata_info_query(comp_class, params, result); |
97ade20b | 1390 | } else if (!strcmp(object, "trace-info")) { |
d27a81b2 | 1391 | status = trace_info_query(comp_class, params, result); |
33f93973 | 1392 | } else { |
55314f2a | 1393 | BT_LOGE("Unknown query object `%s`", object); |
d27a81b2 | 1394 | status = BT_QUERY_STATUS_INVALID_OBJECT; |
04c0ba87 | 1395 | goto end; |
33f93973 | 1396 | } |
33f93973 | 1397 | end: |
d27a81b2 | 1398 | return status; |
33f93973 | 1399 | } |