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