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" |
6de92955 | 40 | #include "../common/notif-iter/notif-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 PP |
47 | static |
48 | int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data *notif_iter_data) | |
49 | { | |
50 | struct ctf_fs_ds_file_info *ds_file_info; | |
51 | int ret = 0; | |
52 | ||
f6ccaed9 | 53 | BT_ASSERT(notif_iter_data->ds_file_info_index < |
94cf822e PP |
54 | notif_iter_data->ds_file_group->ds_file_infos->len); |
55 | ds_file_info = g_ptr_array_index( | |
56 | notif_iter_data->ds_file_group->ds_file_infos, | |
57 | notif_iter_data->ds_file_info_index); | |
58 | ||
59 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); | |
60 | notif_iter_data->ds_file = ctf_fs_ds_file_create( | |
61 | notif_iter_data->ds_file_group->ctf_fs_trace, | |
f0010051 | 62 | notif_iter_data->pc_notif_iter, |
6de92955 | 63 | notif_iter_data->notif_iter, |
94cf822e | 64 | notif_iter_data->ds_file_group->stream, |
97ade20b | 65 | ds_file_info->path->str); |
94cf822e PP |
66 | if (!notif_iter_data->ds_file) { |
67 | ret = -1; | |
68 | } | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | static | |
74 | void ctf_fs_notif_iter_data_destroy( | |
75 | struct ctf_fs_notif_iter_data *notif_iter_data) | |
76 | { | |
77 | if (!notif_iter_data) { | |
78 | return; | |
79 | } | |
80 | ||
81 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); | |
6de92955 PP |
82 | |
83 | if (notif_iter_data->notif_iter) { | |
50842bdc | 84 | bt_notif_iter_destroy(notif_iter_data->notif_iter); |
6de92955 PP |
85 | } |
86 | ||
94cf822e PP |
87 | g_free(notif_iter_data); |
88 | } | |
89 | ||
d4393e08 | 90 | static |
d94d92ac | 91 | enum bt_self_notification_iterator_status ctf_fs_iterator_next_one( |
d4393e08 PP |
92 | struct ctf_fs_notif_iter_data *notif_iter_data, |
93 | struct bt_notification **notif) | |
ea0b4b9e | 94 | { |
d94d92ac | 95 | enum bt_self_notification_iterator_status status; |
e5be10ef | 96 | struct bt_private_notification *priv_notif; |
94cf822e PP |
97 | int ret; |
98 | ||
f6ccaed9 | 99 | BT_ASSERT(notif_iter_data->ds_file); |
e5be10ef | 100 | status = ctf_fs_ds_file_next(notif_iter_data->ds_file, &priv_notif); |
707b7d35 | 101 | *notif = bt_private_notification_as_notification(priv_notif); |
f42867e2 | 102 | |
d94d92ac | 103 | if (status == BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK && |
d4393e08 | 104 | bt_notification_get_type(*notif) == |
f42867e2 PP |
105 | BT_NOTIFICATION_TYPE_STREAM_BEGIN) { |
106 | if (notif_iter_data->skip_stream_begin_notifs) { | |
107 | /* | |
108 | * We already emitted a | |
109 | * BT_NOTIFICATION_TYPE_STREAM_BEGIN | |
110 | * notification: skip this one, get a new one. | |
111 | */ | |
65300d60 | 112 | BT_OBJECT_PUT_REF_AND_RESET(*notif); |
d4393e08 | 113 | status = ctf_fs_ds_file_next(notif_iter_data->ds_file, |
e5be10ef | 114 | &priv_notif); |
707b7d35 | 115 | *notif = bt_private_notification_as_notification(priv_notif); |
d94d92ac | 116 | BT_ASSERT(status != BT_SELF_NOTIFICATION_ITERATOR_STATUS_END); |
f42867e2 PP |
117 | goto end; |
118 | } else { | |
119 | /* | |
120 | * First BT_NOTIFICATION_TYPE_STREAM_BEGIN | |
121 | * notification: skip all following. | |
122 | */ | |
123 | notif_iter_data->skip_stream_begin_notifs = true; | |
124 | goto end; | |
125 | } | |
126 | } | |
127 | ||
d94d92ac | 128 | if (status == BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK && |
d4393e08 | 129 | bt_notification_get_type(*notif) == |
f42867e2 | 130 | BT_NOTIFICATION_TYPE_STREAM_END) { |
94cf822e PP |
131 | notif_iter_data->ds_file_info_index++; |
132 | ||
133 | if (notif_iter_data->ds_file_info_index == | |
134 | notif_iter_data->ds_file_group->ds_file_infos->len) { | |
135 | /* | |
136 | * No more stream files to read: we reached the | |
f42867e2 PP |
137 | * real end. Emit this |
138 | * BT_NOTIFICATION_TYPE_STREAM_END notification. | |
139 | * The next time ctf_fs_iterator_next() is | |
140 | * called for this notification iterator, | |
141 | * ctf_fs_ds_file_next() will return | |
d94d92ac | 142 | * BT_SELF_NOTIFICATION_ITERATOR_STATUS_END(). |
94cf822e PP |
143 | */ |
144 | goto end; | |
145 | } | |
146 | ||
65300d60 | 147 | BT_OBJECT_PUT_REF_AND_RESET(*notif); |
f42867e2 PP |
148 | bt_notif_iter_reset(notif_iter_data->notif_iter); |
149 | ||
94cf822e PP |
150 | /* |
151 | * Open and start reading the next stream file within | |
152 | * our stream file group. | |
153 | */ | |
154 | ret = notif_iter_data_set_current_ds_file(notif_iter_data); | |
155 | if (ret) { | |
d94d92ac | 156 | status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_ERROR; |
94cf822e PP |
157 | goto end; |
158 | } | |
159 | ||
e5be10ef | 160 | status = ctf_fs_ds_file_next(notif_iter_data->ds_file, &priv_notif); |
707b7d35 | 161 | *notif = bt_private_notification_as_notification(priv_notif); |
94cf822e PP |
162 | |
163 | /* | |
f42867e2 PP |
164 | * If we get a notification, we expect to get a |
165 | * BT_NOTIFICATION_TYPE_STREAM_BEGIN notification | |
166 | * because the iterator's state machine emits one before | |
167 | * even requesting the first block of data from the | |
168 | * medium. Skip this notification because we're not | |
169 | * really starting a new stream here, and try getting a | |
170 | * new notification (which, if it works, is a | |
171 | * BT_NOTIFICATION_TYPE_PACKET_BEGIN one). We're sure to | |
172 | * get at least one pair of | |
173 | * BT_NOTIFICATION_TYPE_PACKET_BEGIN and | |
174 | * BT_NOTIFICATION_TYPE_PACKET_END notifications in the | |
175 | * case of a single, empty packet. We know there's at | |
176 | * least one packet because the stream file group does | |
177 | * not contain empty stream files. | |
94cf822e | 178 | */ |
f42867e2 PP |
179 | BT_ASSERT(notif_iter_data->skip_stream_begin_notifs); |
180 | ||
d94d92ac | 181 | if (status == BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK) { |
d4393e08 | 182 | BT_ASSERT(bt_notification_get_type(*notif) == |
f42867e2 | 183 | BT_NOTIFICATION_TYPE_STREAM_BEGIN); |
65300d60 | 184 | BT_OBJECT_PUT_REF_AND_RESET(*notif); |
d4393e08 | 185 | status = ctf_fs_ds_file_next(notif_iter_data->ds_file, |
e5be10ef | 186 | &priv_notif); |
707b7d35 | 187 | *notif = bt_private_notification_as_notification(priv_notif); |
d94d92ac | 188 | BT_ASSERT(status != BT_SELF_NOTIFICATION_ITERATOR_STATUS_END); |
f42867e2 | 189 | } |
94cf822e | 190 | } |
d01e0f33 | 191 | |
94cf822e | 192 | end: |
d4393e08 PP |
193 | return status; |
194 | } | |
195 | ||
196 | BT_HIDDEN | |
d94d92ac PP |
197 | enum bt_self_notification_iterator_status ctf_fs_iterator_next( |
198 | struct bt_self_notification_iterator *iterator, | |
d4393e08 PP |
199 | bt_notification_array notifs, uint64_t capacity, |
200 | uint64_t *count) | |
201 | { | |
d94d92ac PP |
202 | enum bt_self_notification_iterator_status status = |
203 | BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; | |
d4393e08 | 204 | struct ctf_fs_notif_iter_data *notif_iter_data = |
d94d92ac | 205 | bt_self_notification_iterator_get_data(iterator); |
d4393e08 PP |
206 | uint64_t i = 0; |
207 | ||
d94d92ac | 208 | while (i < capacity && status == BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK) { |
d4393e08 | 209 | status = ctf_fs_iterator_next_one(notif_iter_data, ¬ifs[i]); |
d94d92ac | 210 | if (status == BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK) { |
d4393e08 PP |
211 | i++; |
212 | } | |
213 | } | |
214 | ||
215 | if (i > 0) { | |
216 | /* | |
217 | * Even if ctf_fs_iterator_next_one() returned something | |
d94d92ac | 218 | * else than BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK, we |
d4393e08 PP |
219 | * accumulated notification objects in the output |
220 | * notification array, so we need to return | |
d94d92ac | 221 | * BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK so that they are |
d4393e08 PP |
222 | * transfered to downstream. This other status occurs |
223 | * again the next time muxer_notif_iter_do_next() is | |
224 | * called, possibly without any accumulated | |
225 | * notification, in which case we'll return it. | |
226 | */ | |
227 | *count = i; | |
d94d92ac | 228 | status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; |
d4393e08 PP |
229 | } |
230 | ||
231 | return status; | |
ea0b4b9e | 232 | } |
bfd20a42 | 233 | |
d94d92ac | 234 | void ctf_fs_iterator_finalize(struct bt_self_notification_iterator *it) |
760051fa | 235 | { |
d94d92ac PP |
236 | ctf_fs_notif_iter_data_destroy( |
237 | bt_self_notification_iterator_get_data(it)); | |
760051fa JG |
238 | } |
239 | ||
d94d92ac PP |
240 | enum bt_self_notification_iterator_status ctf_fs_iterator_init( |
241 | struct bt_self_notification_iterator *self_notif_iter, | |
242 | struct bt_self_component_source *self_comp, | |
243 | struct bt_self_component_port_output *self_port) | |
4c1456f0 | 244 | { |
4f1f88a6 | 245 | struct ctf_fs_port_data *port_data; |
94cf822e | 246 | struct ctf_fs_notif_iter_data *notif_iter_data = NULL; |
d94d92ac PP |
247 | enum bt_self_notification_iterator_status ret = |
248 | BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; | |
94cf822e | 249 | int iret; |
760051fa | 250 | |
d94d92ac | 251 | port_data = bt_self_component_port_get_data( |
707b7d35 | 252 | bt_self_component_port_output_as_self_component_port( |
d94d92ac PP |
253 | self_port)); |
254 | BT_ASSERT(port_data); | |
94cf822e PP |
255 | notif_iter_data = g_new0(struct ctf_fs_notif_iter_data, 1); |
256 | if (!notif_iter_data) { | |
d94d92ac | 257 | ret = BT_SELF_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
94cf822e PP |
258 | goto error; |
259 | } | |
260 | ||
d94d92ac | 261 | notif_iter_data->pc_notif_iter = self_notif_iter; |
50842bdc | 262 | notif_iter_data->notif_iter = bt_notif_iter_create( |
44c440bc | 263 | port_data->ds_file_group->ctf_fs_trace->metadata->tc, |
6de92955 PP |
264 | bt_common_get_page_size() * 8, |
265 | ctf_fs_ds_file_medops, NULL); | |
266 | if (!notif_iter_data->notif_iter) { | |
267 | BT_LOGE_STR("Cannot create a CTF notification iterator."); | |
d94d92ac | 268 | ret = BT_SELF_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
6de92955 PP |
269 | goto error; |
270 | } | |
271 | ||
94cf822e PP |
272 | notif_iter_data->ds_file_group = port_data->ds_file_group; |
273 | iret = notif_iter_data_set_current_ds_file(notif_iter_data); | |
274 | if (iret) { | |
d94d92ac | 275 | ret = BT_SELF_NOTIFICATION_ITERATOR_STATUS_ERROR; |
4f1f88a6 | 276 | goto error; |
760051fa JG |
277 | } |
278 | ||
d94d92ac PP |
279 | bt_self_notification_iterator_set_data(self_notif_iter, |
280 | notif_iter_data); | |
281 | if (ret != BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK) { | |
4f1f88a6 | 282 | goto error; |
760051fa JG |
283 | } |
284 | ||
94cf822e | 285 | notif_iter_data = NULL; |
4f1f88a6 | 286 | goto end; |
5b29e799 | 287 | |
4f1f88a6 | 288 | error: |
d94d92ac | 289 | bt_self_notification_iterator_set_data(self_notif_iter, NULL); |
4f1f88a6 | 290 | |
760051fa | 291 | end: |
94cf822e | 292 | ctf_fs_notif_iter_data_destroy(notif_iter_data); |
760051fa JG |
293 | return ret; |
294 | } | |
295 | ||
760051fa | 296 | static |
1a9f7075 | 297 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
760051fa | 298 | { |
4f1f88a6 | 299 | if (!ctf_fs) { |
8fa760ba JG |
300 | return; |
301 | } | |
4f1f88a6 | 302 | |
1a9f7075 PP |
303 | if (ctf_fs->traces) { |
304 | g_ptr_array_free(ctf_fs->traces, TRUE); | |
56a1cced | 305 | } |
4f1f88a6 PP |
306 | |
307 | if (ctf_fs->port_data) { | |
308 | g_ptr_array_free(ctf_fs->port_data, TRUE); | |
c14d7e26 | 309 | } |
760051fa | 310 | |
1a9f7075 PP |
311 | g_free(ctf_fs); |
312 | } | |
313 | ||
97ade20b JG |
314 | BT_HIDDEN |
315 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) | |
1a9f7075 | 316 | { |
1a9f7075 PP |
317 | if (!ctf_fs_trace) { |
318 | return; | |
319 | } | |
320 | ||
94cf822e PP |
321 | if (ctf_fs_trace->ds_file_groups) { |
322 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); | |
323 | } | |
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 JG |
341 | static |
342 | void ctf_fs_trace_destroy_notifier(void *data) | |
343 | { | |
344 | struct ctf_fs_trace *trace = data; | |
345 | ctf_fs_trace_destroy(trace); | |
346 | } | |
347 | ||
d94d92ac | 348 | void ctf_fs_finalize(struct bt_self_component_source *component) |
a4792757 | 349 | { |
d94d92ac | 350 | ctf_fs_destroy(bt_self_component_get_data( |
707b7d35 | 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; |
d94d92ac 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, | |
44c440bc | 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 | ||
65300d60 PP |
513 | bt_object_put_ref(ds_file_group->stream); |
514 | bt_object_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, | |
e5be10ef | 521 | struct bt_private_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); |
398454ed PP |
539 | ds_file_group->stream_class = stream_class; |
540 | bt_object_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, | |
44c440bc | 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, | |
e5be10ef | 613 | const char *path) |
94cf822e | 614 | { |
e5be10ef | 615 | struct bt_private_stream_class *stream_class = NULL; |
44c440bc 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; |
50842bdc | 624 | struct bt_notif_iter *notif_iter = NULL; |
44c440bc PP |
625 | struct ctf_stream_class *sc = NULL; |
626 | struct bt_notif_iter_packet_properties props; | |
94cf822e | 627 | |
44c440bc | 628 | notif_iter = bt_notif_iter_create(ctf_fs_trace->metadata->tc, |
6de92955 PP |
629 | bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL); |
630 | if (!notif_iter) { | |
631 | BT_LOGE_STR("Cannot create a CTF notification iterator."); | |
632 | goto error; | |
633 | } | |
634 | ||
f0010051 | 635 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, notif_iter, |
5c563278 | 636 | NULL, path); |
97ade20b JG |
637 | if (!ds_file) { |
638 | goto error; | |
639 | } | |
640 | ||
312c056a | 641 | ret = ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file, |
44c440bc | 642 | NULL, NULL); |
94cf822e | 643 | if (ret) { |
55314f2a | 644 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
94cf822e PP |
645 | path); |
646 | goto error; | |
647 | } | |
648 | ||
44c440bc PP |
649 | ret = bt_notif_iter_get_packet_properties(ds_file->notif_iter, &props); |
650 | BT_ASSERT(ret == 0); | |
651 | sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, | |
652 | props.stream_class_id); | |
653 | BT_ASSERT(sc); | |
654 | stream_class = sc->ir_sc; | |
655 | BT_ASSERT(stream_class); | |
656 | stream_instance_id = props.data_stream_id; | |
657 | ||
658 | if (props.snapshots.beginning_clock != UINT64_C(-1)) { | |
659 | BT_ASSERT(sc->default_clock_class); | |
660 | ret = bt_clock_class_cycles_to_ns_from_origin( | |
707b7d35 | 661 | bt_private_clock_class_as_clock_class( |
e5be10ef | 662 | sc->default_clock_class), |
44c440bc PP |
663 | props.snapshots.beginning_clock, &begin_ns); |
664 | if (ret) { | |
665 | BT_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).", | |
666 | path); | |
667 | goto error; | |
668 | } | |
94cf822e PP |
669 | } |
670 | ||
97ade20b JG |
671 | index = ctf_fs_ds_file_build_index(ds_file); |
672 | if (!index) { | |
673 | BT_LOGW("Failed to index CTF stream file \'%s\'", | |
674 | ds_file->file->path->str); | |
675 | } | |
676 | ||
44c440bc | 677 | if (begin_ns == -1) { |
94cf822e PP |
678 | /* |
679 | * No beggining timestamp to sort the stream files | |
680 | * within a stream file group, so consider that this | |
681 | * file must be the only one within its group. | |
682 | */ | |
44c440bc | 683 | stream_instance_id = -1; |
94cf822e PP |
684 | } |
685 | ||
44c440bc | 686 | if (stream_instance_id == -1) { |
94cf822e PP |
687 | /* |
688 | * No stream instance ID or no beginning timestamp: | |
689 | * create a unique stream file group for this stream | |
690 | * file because, even if there's a stream instance ID, | |
691 | * there's no timestamp to order the file within its | |
692 | * group. | |
693 | */ | |
94cf822e PP |
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 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, | |
547eacf1 | 701 | path, begin_ns, index); |
97ade20b JG |
702 | /* Ownership of index is transferred. */ |
703 | index = NULL; | |
94cf822e PP |
704 | if (ret) { |
705 | goto error; | |
706 | } | |
707 | ||
708 | add_group = true; | |
709 | goto end; | |
710 | } | |
711 | ||
44c440bc PP |
712 | BT_ASSERT(stream_instance_id != -1); |
713 | BT_ASSERT(begin_ns != -1); | |
94cf822e PP |
714 | |
715 | /* Find an existing stream file group with this ID */ | |
716 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
94cf822e PP |
717 | ds_file_group = g_ptr_array_index( |
718 | ctf_fs_trace->ds_file_groups, i); | |
94cf822e | 719 | |
547eacf1 PP |
720 | if (ds_file_group->stream_class == stream_class && |
721 | ds_file_group->stream_id == | |
722 | stream_instance_id) { | |
94cf822e PP |
723 | break; |
724 | } | |
725 | ||
726 | ds_file_group = NULL; | |
727 | } | |
728 | ||
729 | if (!ds_file_group) { | |
730 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, | |
731 | stream_class, stream_instance_id); | |
732 | if (!ds_file_group) { | |
733 | goto error; | |
734 | } | |
735 | ||
736 | add_group = true; | |
737 | } | |
738 | ||
547eacf1 PP |
739 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path, |
740 | begin_ns, index); | |
97ade20b | 741 | index = NULL; |
94cf822e PP |
742 | if (ret) { |
743 | goto error; | |
744 | } | |
745 | ||
746 | goto end; | |
747 | ||
748 | error: | |
749 | ctf_fs_ds_file_group_destroy(ds_file_group); | |
750 | ret = -1; | |
751 | ||
752 | end: | |
753 | if (add_group && ds_file_group) { | |
754 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); | |
755 | } | |
547eacf1 | 756 | |
97ade20b | 757 | ctf_fs_ds_file_destroy(ds_file); |
6de92955 PP |
758 | |
759 | if (notif_iter) { | |
50842bdc | 760 | bt_notif_iter_destroy(notif_iter); |
6de92955 PP |
761 | } |
762 | ||
97ade20b | 763 | ctf_fs_ds_index_destroy(index); |
94cf822e PP |
764 | return ret; |
765 | } | |
766 | ||
767 | static | |
e5be10ef | 768 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) |
e7a4393b JG |
769 | { |
770 | int ret = 0; | |
4f1f88a6 | 771 | const char *basename; |
e7a4393b | 772 | GError *error = NULL; |
4f1f88a6 | 773 | GDir *dir = NULL; |
547eacf1 | 774 | size_t i; |
e7a4393b | 775 | |
94cf822e | 776 | /* Check each file in the path directory, except specific ones */ |
1a9f7075 | 777 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
e7a4393b | 778 | if (!dir) { |
55314f2a | 779 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 | 780 | ctf_fs_trace->path->str, error->message, |
4f1f88a6 | 781 | error->code); |
e7a4393b JG |
782 | goto error; |
783 | } | |
784 | ||
4f1f88a6 | 785 | while ((basename = g_dir_read_name(dir))) { |
94cf822e PP |
786 | struct ctf_fs_file *file; |
787 | ||
4f1f88a6 | 788 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
e7a4393b | 789 | /* Ignore the metadata stream. */ |
3743a302 | 790 | BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 791 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
792 | continue; |
793 | } | |
794 | ||
4f1f88a6 | 795 | if (basename[0] == '.') { |
3743a302 | 796 | BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 797 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
798 | continue; |
799 | } | |
800 | ||
801 | /* Create the file. */ | |
55314f2a | 802 | file = ctf_fs_file_create(); |
e7a4393b | 803 | if (!file) { |
3743a302 | 804 | BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`", |
1a9f7075 | 805 | ctf_fs_trace->path->str, basename); |
e7a4393b JG |
806 | goto error; |
807 | } | |
808 | ||
809 | /* Create full path string. */ | |
3743a302 | 810 | g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s", |
1a9f7075 | 811 | ctf_fs_trace->path->str, basename); |
e7a4393b | 812 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
55314f2a | 813 | BT_LOGD("Ignoring non-regular file `%s`", |
1a9f7075 | 814 | file->path->str); |
e7a4393b | 815 | ctf_fs_file_destroy(file); |
4f1f88a6 | 816 | file = NULL; |
e7a4393b JG |
817 | continue; |
818 | } | |
819 | ||
55314f2a | 820 | ret = ctf_fs_file_open(file, "rb"); |
4f1f88a6 | 821 | if (ret) { |
55314f2a | 822 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
e7a4393b JG |
823 | goto error; |
824 | } | |
825 | ||
9fa0891a JG |
826 | if (file->size == 0) { |
827 | /* Skip empty stream. */ | |
55314f2a | 828 | BT_LOGD("Ignoring empty file `%s`", file->path->str); |
9fa0891a JG |
829 | ctf_fs_file_destroy(file); |
830 | continue; | |
831 | } | |
832 | ||
e5be10ef | 833 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
94cf822e | 834 | file->path->str); |
4f1f88a6 | 835 | if (ret) { |
89b08333 | 836 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
1a9f7075 | 837 | file->path->str); |
94cf822e | 838 | ctf_fs_file_destroy(file); |
e7a4393b JG |
839 | goto error; |
840 | } | |
841 | ||
4f1f88a6 | 842 | ctf_fs_file_destroy(file); |
e7a4393b JG |
843 | } |
844 | ||
547eacf1 PP |
845 | /* |
846 | * At this point, DS file groupes are created, but their | |
847 | * associated stream objects do not exist yet. This is because | |
848 | * we need to name the created stream object with the data | |
849 | * stream file's path. We have everything we need here to do | |
850 | * this. | |
851 | */ | |
852 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { | |
853 | struct ctf_fs_ds_file_group *ds_file_group = | |
854 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); | |
855 | GString *name = get_stream_instance_unique_name(ds_file_group); | |
856 | ||
857 | if (!name) { | |
858 | goto error; | |
859 | } | |
860 | ||
44c440bc | 861 | if (ds_file_group->stream_id == UINT64_C(-1)) { |
3dca2276 | 862 | /* No stream ID: use 0 */ |
e5be10ef | 863 | ds_file_group->stream = bt_private_stream_create_with_id( |
44c440bc | 864 | ds_file_group->stream_class, |
3dca2276 PP |
865 | ctf_fs_trace->next_stream_id); |
866 | ctf_fs_trace->next_stream_id++; | |
547eacf1 PP |
867 | } else { |
868 | /* Specific stream ID */ | |
e5be10ef | 869 | ds_file_group->stream = bt_private_stream_create_with_id( |
44c440bc PP |
870 | ds_file_group->stream_class, |
871 | (uint64_t) ds_file_group->stream_id); | |
547eacf1 PP |
872 | } |
873 | ||
7742909c | 874 | if (!ds_file_group->stream) { |
547eacf1 PP |
875 | BT_LOGE("Cannot create stream for DS file group: " |
876 | "addr=%p, stream-name=\"%s\"", | |
877 | ds_file_group, name->str); | |
44c440bc PP |
878 | g_string_free(name, TRUE); |
879 | goto error; | |
880 | } | |
881 | ||
e5be10ef PP |
882 | ret = bt_private_stream_set_name(ds_file_group->stream, |
883 | name->str); | |
44c440bc PP |
884 | if (ret) { |
885 | BT_LOGE("Cannot set stream's name: " | |
886 | "addr=%p, stream-name=\"%s\"", | |
887 | ds_file_group->stream, name->str); | |
888 | g_string_free(name, TRUE); | |
547eacf1 PP |
889 | goto error; |
890 | } | |
44c440bc PP |
891 | |
892 | g_string_free(name, TRUE); | |
547eacf1 PP |
893 | } |
894 | ||
e7a4393b | 895 | goto end; |
4f1f88a6 | 896 | |
e7a4393b JG |
897 | error: |
898 | ret = -1; | |
4f1f88a6 | 899 | |
e7a4393b JG |
900 | end: |
901 | if (dir) { | |
902 | g_dir_close(dir); | |
903 | dir = NULL; | |
904 | } | |
4f1f88a6 | 905 | |
e7a4393b JG |
906 | if (error) { |
907 | g_error_free(error); | |
908 | } | |
5b29e799 | 909 | |
91457551 | 910 | return ret; |
5b29e799 JG |
911 | } |
912 | ||
97ade20b | 913 | BT_HIDDEN |
55314f2a | 914 | struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, |
e5be10ef | 915 | struct ctf_fs_metadata_config *metadata_config) |
1a9f7075 PP |
916 | { |
917 | struct ctf_fs_trace *ctf_fs_trace; | |
918 | int ret; | |
919 | ||
920 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); | |
921 | if (!ctf_fs_trace) { | |
922 | goto end; | |
923 | } | |
924 | ||
1a9f7075 PP |
925 | ctf_fs_trace->path = g_string_new(path); |
926 | if (!ctf_fs_trace->path) { | |
927 | goto error; | |
928 | } | |
929 | ||
930 | ctf_fs_trace->name = g_string_new(name); | |
931 | if (!ctf_fs_trace->name) { | |
932 | goto error; | |
933 | } | |
934 | ||
935 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); | |
936 | if (!ctf_fs_trace->metadata) { | |
937 | goto error; | |
938 | } | |
939 | ||
44c440bc | 940 | ctf_fs_metadata_init(ctf_fs_trace->metadata); |
94cf822e PP |
941 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
942 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); | |
943 | if (!ctf_fs_trace->ds_file_groups) { | |
944 | goto error; | |
945 | } | |
946 | ||
a2a54545 | 947 | ret = ctf_fs_metadata_set_trace(ctf_fs_trace, metadata_config); |
1a9f7075 PP |
948 | if (ret) { |
949 | goto error; | |
950 | } | |
951 | ||
e5be10ef | 952 | ret = create_ds_file_groups(ctf_fs_trace); |
94cf822e PP |
953 | if (ret) { |
954 | goto error; | |
955 | } | |
956 | ||
27d1a78b PP |
957 | /* |
958 | * create_ds_file_groups() created all the streams that this | |
959 | * trace needs. There won't be any more. Therefore it is safe to | |
960 | * make this trace static. | |
961 | */ | |
e5be10ef | 962 | (void) bt_private_trace_make_static(ctf_fs_trace->metadata->trace); |
27d1a78b | 963 | |
1a9f7075 PP |
964 | goto end; |
965 | ||
966 | error: | |
967 | ctf_fs_trace_destroy(ctf_fs_trace); | |
968 | ctf_fs_trace = NULL; | |
44c440bc | 969 | |
1a9f7075 PP |
970 | end: |
971 | return ctf_fs_trace; | |
972 | } | |
973 | ||
974 | static | |
975 | int path_is_ctf_trace(const char *path) | |
976 | { | |
977 | GString *metadata_path = g_string_new(NULL); | |
978 | int ret = 0; | |
979 | ||
980 | if (!metadata_path) { | |
981 | ret = -1; | |
982 | goto end; | |
983 | } | |
984 | ||
3743a302 | 985 | g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME); |
1a9f7075 PP |
986 | |
987 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { | |
988 | ret = 1; | |
989 | goto end; | |
990 | } | |
991 | ||
992 | end: | |
993 | g_string_free(metadata_path, TRUE); | |
994 | return ret; | |
995 | } | |
996 | ||
997 | static | |
55314f2a | 998 | int add_trace_path(GList **trace_paths, const char *path) |
1a9f7075 | 999 | { |
4bd72b60 | 1000 | GString *norm_path = NULL; |
1a9f7075 | 1001 | int ret = 0; |
1a9f7075 | 1002 | |
4bd72b60 PP |
1003 | norm_path = bt_common_normalize_path(path, NULL); |
1004 | if (!norm_path) { | |
55314f2a | 1005 | BT_LOGE("Failed to normalize path `%s`.", path); |
1a9f7075 PP |
1006 | ret = -1; |
1007 | goto end; | |
1008 | } | |
1009 | ||
3743a302 | 1010 | // FIXME: Remove or ifdef for __MINGW32__ |
4bd72b60 | 1011 | if (strcmp(norm_path->str, "/") == 0) { |
55314f2a | 1012 | BT_LOGE("Opening a trace in `/` is not supported."); |
1a9f7075 PP |
1013 | ret = -1; |
1014 | goto end; | |
1015 | } | |
1016 | ||
4bd72b60 | 1017 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
f6ccaed9 | 1018 | BT_ASSERT(*trace_paths); |
4bd72b60 | 1019 | norm_path = NULL; |
1a9f7075 PP |
1020 | |
1021 | end: | |
4bd72b60 PP |
1022 | if (norm_path) { |
1023 | g_string_free(norm_path, TRUE); | |
1024 | } | |
1025 | ||
1a9f7075 PP |
1026 | return ret; |
1027 | } | |
1028 | ||
55314f2a JG |
1029 | BT_HIDDEN |
1030 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) | |
1a9f7075 PP |
1031 | { |
1032 | int ret; | |
1033 | GError *error = NULL; | |
1034 | GDir *dir = NULL; | |
1035 | const char *basename = NULL; | |
1036 | ||
1037 | /* Check if the starting path is a CTF trace itself */ | |
1038 | ret = path_is_ctf_trace(start_path); | |
1039 | if (ret < 0) { | |
1040 | goto end; | |
1041 | } | |
1042 | ||
1043 | if (ret) { | |
1044 | /* | |
4bd72b60 PP |
1045 | * Stop recursion: a CTF trace cannot contain another |
1046 | * CTF trace. | |
1a9f7075 | 1047 | */ |
55314f2a | 1048 | ret = add_trace_path(trace_paths, start_path); |
1a9f7075 PP |
1049 | goto end; |
1050 | } | |
1051 | ||
1052 | /* Look for subdirectories */ | |
1053 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { | |
1054 | /* Starting path is not a directory: end of recursion */ | |
1055 | goto end; | |
1056 | } | |
1057 | ||
1058 | dir = g_dir_open(start_path, 0, &error); | |
1059 | if (!dir) { | |
1060 | if (error->code == G_FILE_ERROR_ACCES) { | |
55314f2a | 1061 | BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing", |
1a9f7075 PP |
1062 | start_path, error->message, error->code); |
1063 | goto end; | |
1064 | } | |
1065 | ||
55314f2a | 1066 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
1a9f7075 PP |
1067 | start_path, error->message, error->code); |
1068 | ret = -1; | |
1069 | goto end; | |
1070 | } | |
1071 | ||
1072 | while ((basename = g_dir_read_name(dir))) { | |
1073 | GString *sub_path = g_string_new(NULL); | |
1074 | ||
1075 | if (!sub_path) { | |
1076 | ret = -1; | |
1077 | goto end; | |
1078 | } | |
1079 | ||
3743a302 | 1080 | g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename); |
55314f2a | 1081 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
1a9f7075 PP |
1082 | g_string_free(sub_path, TRUE); |
1083 | if (ret) { | |
1084 | goto end; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | end: | |
1089 | if (dir) { | |
1090 | g_dir_close(dir); | |
1091 | } | |
1092 | ||
1093 | if (error) { | |
1094 | g_error_free(error); | |
1095 | } | |
1096 | ||
1097 | return ret; | |
1098 | } | |
1099 | ||
55314f2a JG |
1100 | BT_HIDDEN |
1101 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { | |
1a9f7075 | 1102 | GList *trace_names = NULL; |
1a9f7075 | 1103 | GList *node; |
4bd72b60 PP |
1104 | const char *last_sep; |
1105 | size_t base_dist; | |
1a9f7075 PP |
1106 | |
1107 | /* | |
4bd72b60 PP |
1108 | * At this point we know that all the trace paths are |
1109 | * normalized, and so is the base path. This means that | |
1110 | * they are absolute and they don't end with a separator. | |
1111 | * We can simply find the location of the last separator | |
1112 | * in the base path, which gives us the name of the actual | |
1113 | * directory to look into, and use this location as the | |
1114 | * start of each trace name within each trace path. | |
1115 | * | |
1116 | * For example: | |
1117 | * | |
1118 | * Base path: /home/user/my-traces/some-trace | |
1119 | * Trace paths: | |
1120 | * - /home/user/my-traces/some-trace/host1/trace1 | |
1121 | * - /home/user/my-traces/some-trace/host1/trace2 | |
1122 | * - /home/user/my-traces/some-trace/host2/trace | |
1123 | * - /home/user/my-traces/some-trace/other-trace | |
1124 | * | |
1125 | * In this case the trace names are: | |
1126 | * | |
1127 | * - some-trace/host1/trace1 | |
1128 | * - some-trace/host1/trace2 | |
1129 | * - some-trace/host2/trace | |
1130 | * - some-trace/other-trace | |
1a9f7075 | 1131 | */ |
4bd72b60 | 1132 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
1a9f7075 | 1133 | |
4bd72b60 | 1134 | /* We know there's at least one separator */ |
f6ccaed9 | 1135 | BT_ASSERT(last_sep); |
1a9f7075 | 1136 | |
4bd72b60 PP |
1137 | /* Distance to base */ |
1138 | base_dist = last_sep - base_path + 1; | |
1a9f7075 PP |
1139 | |
1140 | /* Create the trace names */ | |
1141 | for (node = trace_paths; node; node = g_list_next(node)) { | |
1142 | GString *trace_name = g_string_new(NULL); | |
1143 | GString *trace_path = node->data; | |
1144 | ||
f6ccaed9 | 1145 | BT_ASSERT(trace_name); |
4bd72b60 | 1146 | g_string_assign(trace_name, &trace_path->str[base_dist]); |
1a9f7075 PP |
1147 | trace_names = g_list_append(trace_names, trace_name); |
1148 | } | |
1149 | ||
1150 | return trace_names; | |
1151 | } | |
1152 | ||
1153 | static | |
1154 | int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs, | |
1155 | const char *path_param) | |
1156 | { | |
1157 | struct ctf_fs_trace *ctf_fs_trace = NULL; | |
1158 | int ret = 0; | |
4bd72b60 | 1159 | GString *norm_path = NULL; |
1a9f7075 PP |
1160 | GList *trace_paths = NULL; |
1161 | GList *trace_names = NULL; | |
1162 | GList *tp_node; | |
1163 | GList *tn_node; | |
1164 | ||
4bd72b60 PP |
1165 | norm_path = bt_common_normalize_path(path_param, NULL); |
1166 | if (!norm_path) { | |
55314f2a | 1167 | BT_LOGE("Failed to normalize path: `%s`.", |
4bd72b60 PP |
1168 | path_param); |
1169 | goto error; | |
1170 | } | |
1171 | ||
55314f2a | 1172 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
1a9f7075 PP |
1173 | if (ret) { |
1174 | goto error; | |
1175 | } | |
1176 | ||
1177 | if (!trace_paths) { | |
55314f2a | 1178 | BT_LOGE("No CTF traces recursively found in `%s`.", |
1a9f7075 PP |
1179 | path_param); |
1180 | goto error; | |
1181 | } | |
1182 | ||
55314f2a | 1183 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
1a9f7075 | 1184 | if (!trace_names) { |
55314f2a | 1185 | BT_LOGE("Cannot create trace names from trace paths."); |
1a9f7075 PP |
1186 | goto error; |
1187 | } | |
1188 | ||
1189 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; | |
1190 | tp_node = g_list_next(tp_node), | |
1191 | tn_node = g_list_next(tn_node)) { | |
1192 | GString *trace_path = tp_node->data; | |
1193 | GString *trace_name = tn_node->data; | |
1194 | ||
55314f2a | 1195 | ctf_fs_trace = ctf_fs_trace_create(trace_path->str, |
e5be10ef | 1196 | trace_name->str, &ctf_fs->metadata_config); |
1a9f7075 | 1197 | if (!ctf_fs_trace) { |
55314f2a | 1198 | BT_LOGE("Cannot create trace for `%s`.", |
1a9f7075 PP |
1199 | trace_path->str); |
1200 | goto error; | |
1201 | } | |
52c5fe74 | 1202 | |
55314f2a JG |
1203 | ret = create_ports_for_trace(ctf_fs, ctf_fs_trace); |
1204 | if (ret) { | |
1205 | goto error; | |
1206 | } | |
1207 | ||
52c5fe74 PP |
1208 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); |
1209 | ctf_fs_trace = NULL; | |
1a9f7075 PP |
1210 | } |
1211 | ||
1a9f7075 PP |
1212 | goto end; |
1213 | ||
1214 | error: | |
1215 | ret = -1; | |
1216 | ctf_fs_trace_destroy(ctf_fs_trace); | |
1217 | ||
1218 | end: | |
1219 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { | |
1220 | if (tp_node->data) { | |
1221 | g_string_free(tp_node->data, TRUE); | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { | |
1226 | if (tn_node->data) { | |
1227 | g_string_free(tn_node->data, TRUE); | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | if (trace_paths) { | |
1232 | g_list_free(trace_paths); | |
1233 | } | |
1234 | ||
1235 | if (trace_names) { | |
1236 | g_list_free(trace_names); | |
1237 | } | |
1238 | ||
4bd72b60 PP |
1239 | if (norm_path) { |
1240 | g_string_free(norm_path, TRUE); | |
1241 | } | |
1242 | ||
1a9f7075 PP |
1243 | return ret; |
1244 | } | |
1245 | ||
5b29e799 | 1246 | static |
d94d92ac PP |
1247 | struct ctf_fs_component *ctf_fs_create( |
1248 | struct bt_self_component_source *self_comp, | |
05e21286 | 1249 | const struct bt_value *params) |
56a1cced JG |
1250 | { |
1251 | struct ctf_fs_component *ctf_fs; | |
05e21286 | 1252 | const struct bt_value *value = NULL; |
1a9f7075 | 1253 | const char *path_param; |
56a1cced JG |
1254 | |
1255 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
1256 | if (!ctf_fs) { | |
1257 | goto end; | |
1258 | } | |
1259 | ||
d94d92ac | 1260 | bt_self_component_set_data( |
707b7d35 | 1261 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1262 | ctf_fs); |
1a9f7075 | 1263 | |
4f1f88a6 PP |
1264 | /* |
1265 | * We don't need to get a new reference here because as long as | |
1266 | * our private ctf_fs_component object exists, the containing | |
1267 | * private component should also exist. | |
1268 | */ | |
d94d92ac | 1269 | ctf_fs->self_comp = self_comp; |
05e21286 | 1270 | value = bt_value_map_borrow_entry_value_const(params, "path"); |
f6ccaed9 | 1271 | if (value && !bt_value_is_string(value)) { |
56a1cced JG |
1272 | goto error; |
1273 | } | |
1274 | ||
601b0d3c | 1275 | path_param = bt_value_string_get(value); |
05e21286 PP |
1276 | value = bt_value_map_borrow_entry_value_const(params, |
1277 | "clock-class-offset-s"); | |
92540773 | 1278 | if (value) { |
a2a54545 PP |
1279 | if (!bt_value_is_integer(value)) { |
1280 | BT_LOGE("clock-class-offset-s should be an integer"); | |
1281 | goto error; | |
1282 | } | |
601b0d3c | 1283 | ctf_fs->metadata_config.clock_class_offset_s = bt_value_integer_get(value); |
a2a54545 | 1284 | } |
92540773 | 1285 | |
05e21286 PP |
1286 | value = bt_value_map_borrow_entry_value_const(params, |
1287 | "clock-class-offset-ns"); | |
a2a54545 | 1288 | if (value) { |
92540773 | 1289 | if (!bt_value_is_integer(value)) { |
a2a54545 | 1290 | BT_LOGE("clock-class-offset-ns should be an integer"); |
92540773 JD |
1291 | goto error; |
1292 | } | |
601b0d3c | 1293 | ctf_fs->metadata_config.clock_class_offset_ns = bt_value_integer_get(value); |
92540773 JD |
1294 | } |
1295 | ||
1a9f7075 PP |
1296 | ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); |
1297 | if (!ctf_fs->port_data) { | |
4f1f88a6 PP |
1298 | goto error; |
1299 | } | |
1300 | ||
97ade20b JG |
1301 | ctf_fs->traces = g_ptr_array_new_with_free_func( |
1302 | ctf_fs_trace_destroy_notifier); | |
1a9f7075 | 1303 | if (!ctf_fs->traces) { |
599faa1c PP |
1304 | goto error; |
1305 | } | |
1306 | ||
3faba6d4 | 1307 | if (create_ctf_fs_traces(ctf_fs, path_param)) { |
4f1f88a6 PP |
1308 | goto error; |
1309 | } | |
1310 | ||
1ef09eb5 JG |
1311 | goto end; |
1312 | ||
56a1cced | 1313 | error: |
1a9f7075 | 1314 | ctf_fs_destroy(ctf_fs); |
e7a4393b | 1315 | ctf_fs = NULL; |
d94d92ac | 1316 | bt_self_component_set_data( |
707b7d35 | 1317 | bt_self_component_source_as_self_component(self_comp), |
d94d92ac | 1318 | NULL); |
1a9f7075 | 1319 | |
1ef09eb5 | 1320 | end: |
56a1cced JG |
1321 | return ctf_fs; |
1322 | } | |
1323 | ||
ea0b4b9e | 1324 | BT_HIDDEN |
d94d92ac PP |
1325 | enum bt_self_component_status ctf_fs_init( |
1326 | struct bt_self_component_source *self_comp, | |
05e21286 | 1327 | const struct bt_value *params, UNUSED_VAR void *init_method_data) |
ea0b4b9e JG |
1328 | { |
1329 | struct ctf_fs_component *ctf_fs; | |
d94d92ac | 1330 | enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
ea0b4b9e | 1331 | |
d94d92ac | 1332 | ctf_fs = ctf_fs_create(self_comp, params); |
ea0b4b9e | 1333 | if (!ctf_fs) { |
d94d92ac | 1334 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
ea0b4b9e | 1335 | } |
4c1456f0 | 1336 | |
ea0b4b9e JG |
1337 | return ret; |
1338 | } | |
33f93973 PP |
1339 | |
1340 | BT_HIDDEN | |
d94d92ac PP |
1341 | enum bt_query_status ctf_fs_query( |
1342 | struct bt_self_component_class_source *comp_class, | |
c7eee084 | 1343 | struct bt_query_executor *query_exec, |
05e21286 PP |
1344 | const char *object, const struct bt_value *params, |
1345 | const struct bt_value **result) | |
33f93973 | 1346 | { |
d94d92ac | 1347 | enum bt_query_status status = BT_QUERY_STATUS_OK; |
33f93973 | 1348 | |
04c0ba87 | 1349 | if (!strcmp(object, "metadata-info")) { |
d94d92ac | 1350 | status = metadata_info_query(comp_class, params, result); |
97ade20b | 1351 | } else if (!strcmp(object, "trace-info")) { |
d94d92ac | 1352 | status = trace_info_query(comp_class, params, result); |
33f93973 | 1353 | } else { |
55314f2a | 1354 | BT_LOGE("Unknown query object `%s`", object); |
d94d92ac | 1355 | status = BT_QUERY_STATUS_INVALID_OBJECT; |
04c0ba87 | 1356 | goto end; |
33f93973 | 1357 | } |
33f93973 | 1358 | end: |
d94d92ac | 1359 | return status; |
33f93973 | 1360 | } |