Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
ea0b4b9e | 2 | * fs.c |
7a278c8e | 3 | * |
ea0b4b9e | 4 | * Babeltrace CTF file system Reader Component |
7a278c8e | 5 | * |
f3bc2010 | 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7a278c8e JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
ea0b4b9e | 29 | #include <babeltrace/plugin/plugin-system.h> |
5b29e799 | 30 | #include <babeltrace/ctf-ir/packet.h> |
a4792757 | 31 | #include <babeltrace/ctf-ir/clock.h> |
760051fa | 32 | #include <babeltrace/plugin/notification/iterator.h> |
5b29e799 JG |
33 | #include <babeltrace/plugin/notification/stream.h> |
34 | #include <babeltrace/plugin/notification/event.h> | |
35 | #include <babeltrace/plugin/notification/packet.h> | |
36 | #include <babeltrace/plugin/notification/heap.h> | |
ea0b4b9e JG |
37 | #include <glib.h> |
38 | #include <assert.h> | |
56a1cced JG |
39 | #include <unistd.h> |
40 | #include "fs.h" | |
413bc2c4 JG |
41 | #include "metadata.h" |
42 | #include "data-stream.h" | |
e7a4393b JG |
43 | #include "file.h" |
44 | ||
45 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
46 | #define PRINT_PREFIX "ctf-fs" | |
47 | #include "print.h" | |
ea0b4b9e | 48 | |
78bb6992 MD |
49 | BT_HIDDEN |
50 | bool ctf_fs_debug; | |
ea0b4b9e JG |
51 | |
52 | static | |
760051fa JG |
53 | struct bt_notification *ctf_fs_iterator_get( |
54 | struct bt_notification_iterator *iterator) | |
ea0b4b9e | 55 | { |
5b29e799 JG |
56 | struct ctf_fs_iterator *ctf_it = |
57 | bt_notification_iterator_get_private_data(iterator); | |
d01e0f33 | 58 | |
5b29e799 | 59 | return bt_get(ctf_it->current_notification); |
ea0b4b9e JG |
60 | } |
61 | ||
62 | static | |
5b29e799 JG |
63 | enum bt_notification_iterator_status ctf_fs_iterator_get_next_notification( |
64 | struct ctf_fs_iterator *it, | |
65 | struct ctf_fs_stream *stream, | |
66 | struct bt_notification **notification) | |
ea0b4b9e | 67 | { |
5b29e799 | 68 | enum bt_ctf_notif_iter_status status; |
d01e0f33 | 69 | enum bt_notification_iterator_status ret; |
d01e0f33 | 70 | |
5b29e799 JG |
71 | if (stream->end_reached) { |
72 | status = BT_CTF_NOTIF_ITER_STATUS_EOF; | |
d01e0f33 JG |
73 | goto end; |
74 | } | |
75 | ||
5b29e799 JG |
76 | status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter, |
77 | notification); | |
78 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK && | |
79 | status != BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
d01e0f33 JG |
80 | goto end; |
81 | } | |
82 | ||
5b29e799 JG |
83 | /* Should be handled in bt_ctf_notif_iter_get_next_notification. */ |
84 | if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
85 | *notification = bt_notification_stream_end_create( | |
86 | stream->stream); | |
87 | if (!*notification) { | |
88 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
89 | } | |
90 | status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
91 | stream->end_reached = true; | |
92 | } | |
d01e0f33 | 93 | end: |
5b29e799 JG |
94 | switch (status) { |
95 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
96 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
97 | break; | |
98 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
99 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
100 | break; | |
101 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
102 | /* | |
103 | * Should not make it this far as this is medium-specific; | |
104 | * there is nothing for the user to do and it should have been | |
105 | * handled upstream. | |
106 | */ | |
107 | assert(0); | |
108 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: | |
109 | /* No argument provided by the user, so don't return INVAL. */ | |
110 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
111 | default: | |
112 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
113 | break; | |
114 | } | |
043e2020 | 115 | return ret; |
ea0b4b9e | 116 | } |
bfd20a42 | 117 | |
5b29e799 JG |
118 | /* |
119 | * Remove me. This is a temporary work-around due to our inhability to use | |
120 | * libbabeltrace-ctf from libbabeltrace-plugin. | |
121 | */ | |
760051fa | 122 | static |
5b29e799 JG |
123 | struct bt_ctf_stream *internal_bt_notification_get_stream( |
124 | struct bt_notification *notification) | |
760051fa | 125 | { |
5b29e799 JG |
126 | struct bt_ctf_stream *stream = NULL; |
127 | ||
128 | assert(notification); | |
129 | switch (bt_notification_get_type(notification)) { | |
130 | case BT_NOTIFICATION_TYPE_EVENT: | |
131 | { | |
132 | struct bt_ctf_event *event; | |
133 | ||
134 | event = bt_notification_event_get_event(notification); | |
135 | stream = bt_ctf_event_get_stream(event); | |
136 | bt_put(event); | |
137 | break; | |
138 | } | |
139 | case BT_NOTIFICATION_TYPE_PACKET_START: | |
140 | { | |
141 | struct bt_ctf_packet *packet; | |
142 | ||
143 | packet = bt_notification_packet_start_get_packet(notification); | |
144 | stream = bt_ctf_packet_get_stream(packet); | |
145 | bt_put(packet); | |
146 | break; | |
147 | } | |
148 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
149 | { | |
150 | struct bt_ctf_packet *packet; | |
151 | ||
152 | packet = bt_notification_packet_end_get_packet(notification); | |
153 | stream = bt_ctf_packet_get_stream(packet); | |
154 | bt_put(packet); | |
155 | break; | |
156 | } | |
157 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
158 | stream = bt_notification_stream_end_get_stream(notification); | |
159 | break; | |
160 | default: | |
161 | goto end; | |
162 | } | |
163 | end: | |
164 | return stream; | |
760051fa JG |
165 | } |
166 | ||
167 | static | |
5b29e799 | 168 | enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it) |
760051fa | 169 | { |
5b29e799 JG |
170 | size_t i, pending_streams_count = it->pending_streams->len; |
171 | enum bt_notification_iterator_status ret = | |
172 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
173 | ||
174 | /* Insert one stream-associated notification for each stream. */ | |
175 | for (i = 0; i < pending_streams_count; i++) { | |
176 | struct bt_notification *notification; | |
177 | struct ctf_fs_stream *fs_stream; | |
178 | struct bt_ctf_stream *stream; | |
179 | size_t pending_stream_index = pending_streams_count - 1 - i; | |
180 | ||
181 | fs_stream = g_ptr_array_index(it->pending_streams, | |
182 | pending_stream_index); | |
183 | ||
184 | do { | |
185 | int heap_ret; | |
186 | ||
187 | ret = ctf_fs_iterator_get_next_notification( | |
188 | it, fs_stream, ¬ification); | |
189 | if (ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
190 | printf_debug("Failed to populate heap at stream %zu\n", | |
191 | pending_stream_index); | |
192 | goto end; | |
193 | } | |
194 | ||
195 | stream = internal_bt_notification_get_stream( | |
196 | notification); | |
197 | if (stream) { | |
198 | gboolean inserted; | |
199 | ||
200 | /* | |
201 | * Associate pending ctf_fs_stream to | |
202 | * bt_ctf_stream. Ownership of stream | |
203 | * is passed to the stream ht. | |
204 | */ | |
205 | inserted = g_hash_table_insert(it->stream_ht, | |
206 | stream, fs_stream); | |
207 | if (!inserted) { | |
208 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
209 | printf_debug("Failed to associate fs stream to ctf stream\n"); | |
210 | goto end; | |
211 | } | |
212 | } | |
213 | ||
214 | heap_ret = bt_notification_heap_insert( | |
215 | it->pending_notifications, | |
216 | notification); | |
217 | bt_put(notification); | |
218 | if (heap_ret) { | |
219 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
220 | printf_debug("Failed to insert notification in heap\n"); | |
221 | goto end; | |
222 | } | |
223 | } while (!stream && ret != BT_NOTIFICATION_ITERATOR_STATUS_END); | |
224 | /* | |
225 | * Set NULL so the destruction callback registered with the | |
226 | * array is not invoked on the stream (its ownership was | |
227 | * transferred to the streams hashtable). | |
228 | */ | |
229 | g_ptr_array_index(it->pending_streams, | |
230 | pending_stream_index) = NULL; | |
231 | g_ptr_array_remove_index(it->pending_streams, | |
232 | pending_stream_index); | |
233 | } | |
760051fa | 234 | |
5b29e799 JG |
235 | g_ptr_array_free(it->pending_streams, TRUE); |
236 | it->pending_streams = NULL; | |
237 | end: | |
238 | return ret; | |
760051fa JG |
239 | } |
240 | ||
241 | static | |
5b29e799 JG |
242 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
243 | struct bt_notification_iterator *iterator) | |
4c1456f0 | 244 | { |
5b29e799 | 245 | int heap_ret; |
a11bd504 | 246 | struct bt_ctf_stream *stream = NULL; |
5b29e799 JG |
247 | struct ctf_fs_stream *fs_stream; |
248 | struct bt_notification *notification; | |
249 | struct bt_notification *next_stream_notification; | |
250 | enum bt_notification_iterator_status ret = | |
251 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
252 | struct ctf_fs_iterator *ctf_it = | |
253 | bt_notification_iterator_get_private_data(iterator); | |
254 | ||
255 | notification = bt_notification_heap_pop(ctf_it->pending_notifications); | |
256 | if (!notification && !ctf_it->pending_streams) { | |
257 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
760051fa JG |
258 | goto end; |
259 | } | |
260 | ||
5b29e799 JG |
261 | if (!notification && ctf_it->pending_streams) { |
262 | /* | |
263 | * Insert at one notification per stream in the heap and pop | |
264 | * one. | |
265 | */ | |
266 | ret = populate_heap(ctf_it); | |
267 | if (ret) { | |
268 | goto end; | |
269 | } | |
270 | ||
271 | notification = bt_notification_heap_pop( | |
272 | ctf_it->pending_notifications); | |
273 | if (!notification) { | |
274 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
275 | goto end; | |
276 | } | |
760051fa JG |
277 | } |
278 | ||
5b29e799 JG |
279 | /* notification is set from here. */ |
280 | ||
281 | stream = internal_bt_notification_get_stream(notification); | |
282 | if (!stream) { | |
283 | /* | |
284 | * The current notification is not associated to a particular | |
285 | * stream, there is no need to insert a new notification from | |
286 | * a stream in the heap. | |
287 | */ | |
288 | goto end; | |
760051fa JG |
289 | } |
290 | ||
5b29e799 JG |
291 | fs_stream = g_hash_table_lookup(ctf_it->stream_ht, stream); |
292 | if (!fs_stream) { | |
293 | /* We have reached this stream's end. */ | |
294 | goto end; | |
760051fa JG |
295 | } |
296 | ||
5b29e799 JG |
297 | ret = ctf_fs_iterator_get_next_notification(ctf_it, fs_stream, |
298 | &next_stream_notification); | |
299 | if ((ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END)) { | |
300 | heap_ret = bt_notification_heap_insert( | |
301 | ctf_it->pending_notifications, notification); | |
302 | ||
303 | assert(!next_stream_notification); | |
304 | if (heap_ret) { | |
305 | /* | |
306 | * We're dropping the most recent notification, but at | |
307 | * this point, something is seriously wrong... | |
308 | */ | |
309 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
310 | } | |
311 | BT_PUT(notification); | |
312 | goto end; | |
313 | } | |
314 | ||
315 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
316 | gboolean success; | |
317 | ||
318 | /* Remove stream. */ | |
319 | success = g_hash_table_remove(ctf_it->stream_ht, stream); | |
320 | assert(success); | |
321 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
322 | } else { | |
323 | heap_ret = bt_notification_heap_insert(ctf_it->pending_notifications, | |
324 | next_stream_notification); | |
325 | BT_PUT(next_stream_notification); | |
326 | if (heap_ret) { | |
327 | /* | |
328 | * We're dropping the most recent notification... | |
329 | */ | |
330 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
331 | } | |
760051fa | 332 | } |
5b29e799 JG |
333 | |
334 | /* | |
335 | * Ensure that the stream is removed from both pending_streams and | |
336 | * the streams hashtable on reception of the "end of stream" | |
337 | * notification. | |
338 | */ | |
760051fa | 339 | end: |
5b29e799 | 340 | BT_MOVE(ctf_it->current_notification, notification); |
33bf10b7 | 341 | bt_put(stream); |
760051fa JG |
342 | return ret; |
343 | } | |
344 | ||
760051fa | 345 | static |
5b29e799 | 346 | void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it) |
760051fa | 347 | { |
5b29e799 JG |
348 | bt_put(ctf_it->current_notification); |
349 | bt_put(ctf_it->pending_notifications); | |
350 | if (ctf_it->pending_streams) { | |
351 | g_ptr_array_free(ctf_it->pending_streams, TRUE); | |
56a1cced | 352 | } |
5b29e799 JG |
353 | if (ctf_it->stream_ht) { |
354 | g_hash_table_destroy(ctf_it->stream_ht); | |
c14d7e26 | 355 | } |
5b29e799 | 356 | g_free(ctf_it); |
760051fa JG |
357 | } |
358 | ||
359 | static | |
5b29e799 | 360 | void ctf_fs_iterator_destroy(struct bt_notification_iterator *it) |
760051fa | 361 | { |
5b29e799 | 362 | void *data = bt_notification_iterator_get_private_data(it); |
760051fa | 363 | |
5b29e799 | 364 | ctf_fs_iterator_destroy_data(data); |
4c1456f0 JG |
365 | } |
366 | ||
a4792757 JG |
367 | static |
368 | bool compare_event_notifications(struct bt_notification *a, | |
369 | struct bt_notification *b) | |
370 | { | |
371 | int ret; | |
372 | struct bt_ctf_clock *clock; | |
373 | struct bt_ctf_clock_value *a_clock_value, *b_clock_value; | |
374 | struct bt_ctf_stream_class *a_stream_class; | |
375 | struct bt_ctf_stream *a_stream; | |
376 | struct bt_ctf_event *a_event, *b_event; | |
377 | struct bt_ctf_trace *trace; | |
378 | int64_t a_ts, b_ts; | |
379 | ||
380 | // FIXME - assumes only one clock | |
381 | a_event = bt_notification_event_get_event(a); | |
382 | b_event = bt_notification_event_get_event(b); | |
383 | assert(a_event); | |
384 | assert(b_event); | |
385 | ||
386 | a_stream = bt_ctf_event_get_stream(a_event); | |
387 | assert(a_stream); | |
388 | a_stream_class = bt_ctf_stream_get_class(a_stream); | |
389 | assert(a_stream_class); | |
390 | trace = bt_ctf_stream_class_get_trace(a_stream_class); | |
391 | assert(trace); | |
392 | ||
393 | clock = bt_ctf_trace_get_clock(trace, 0); | |
394 | a_clock_value = bt_ctf_event_get_clock_value(a_event, clock); | |
395 | b_clock_value = bt_ctf_event_get_clock_value(b_event, clock); | |
396 | assert(a_clock_value); | |
397 | assert(b_clock_value); | |
398 | ||
399 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value, &a_ts); | |
400 | assert(!ret); | |
401 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value, &b_ts); | |
402 | assert(!ret); | |
403 | ||
404 | bt_put(a_event); | |
405 | bt_put(b_event); | |
406 | bt_put(a_clock_value); | |
407 | bt_put(b_clock_value); | |
408 | bt_put(a_stream); | |
409 | bt_put(a_stream_class); | |
410 | bt_put(clock); | |
411 | bt_put(trace); | |
412 | return a_ts < b_ts; | |
413 | } | |
414 | ||
e7a4393b | 415 | static |
5b29e799 JG |
416 | bool compare_notifications(struct bt_notification *a, struct bt_notification *b, |
417 | void *unused) | |
418 | { | |
a4792757 JG |
419 | static int notification_priorities[] = { |
420 | [BT_NOTIFICATION_TYPE_NEW_TRACE] = 0, | |
421 | [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS] = 1, | |
422 | [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS] = 2, | |
423 | [BT_NOTIFICATION_TYPE_PACKET_START] = 3, | |
424 | [BT_NOTIFICATION_TYPE_PACKET_END] = 4, | |
425 | [BT_NOTIFICATION_TYPE_EVENT] = 5, | |
426 | [BT_NOTIFICATION_TYPE_END_OF_TRACE] = 6, | |
427 | }; | |
428 | int a_prio, b_prio; | |
429 | enum bt_notification_type a_type, b_type; | |
430 | ||
431 | assert(a && b); | |
432 | a_type = bt_notification_get_type(a); | |
433 | b_type = bt_notification_get_type(b); | |
434 | assert(a_type > BT_NOTIFICATION_TYPE_ALL); | |
435 | assert(a_type < BT_NOTIFICATION_TYPE_NR); | |
436 | assert(b_type > BT_NOTIFICATION_TYPE_ALL); | |
437 | assert(b_type < BT_NOTIFICATION_TYPE_NR); | |
438 | ||
439 | a_prio = notification_priorities[a_type]; | |
440 | b_prio = notification_priorities[b_type]; | |
441 | ||
442 | if (likely((a_type == b_type) && a_type == BT_NOTIFICATION_TYPE_EVENT)) { | |
443 | return compare_event_notifications(a, b); | |
444 | } | |
445 | ||
446 | if (unlikely(a_prio != b_prio)) { | |
447 | return a_prio < b_prio; | |
448 | } | |
449 | ||
450 | /* Notification types are equal, but not of type "event". */ | |
451 | switch (a_type) { | |
452 | case BT_NOTIFICATION_TYPE_PACKET_START: | |
453 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
454 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
455 | { | |
456 | int64_t a_sc_id, b_sc_id; | |
457 | struct bt_ctf_stream *a_stream, *b_stream; | |
458 | struct bt_ctf_stream_class *a_sc, *b_sc; | |
459 | ||
460 | a_stream = internal_bt_notification_get_stream(a); | |
461 | b_stream = internal_bt_notification_get_stream(b); | |
462 | assert(a_stream && b_stream); | |
463 | ||
464 | a_sc = bt_ctf_stream_get_class(a_stream); | |
465 | b_sc = bt_ctf_stream_get_class(b_stream); | |
466 | assert(a_sc && b_sc); | |
467 | ||
468 | a_sc_id = bt_ctf_stream_class_get_id(a_sc); | |
469 | b_sc_id = bt_ctf_stream_class_get_id(b_sc); | |
470 | assert(a_sc_id >= 0 && b_sc_id >= 0); | |
471 | bt_put(a_sc); | |
472 | bt_put(a_stream); | |
473 | bt_put(b_sc); | |
474 | bt_put(b_stream); | |
475 | return a_sc_id < b_sc_id; | |
476 | } | |
477 | case BT_NOTIFICATION_TYPE_NEW_TRACE: | |
478 | case BT_NOTIFICATION_TYPE_END_OF_TRACE: | |
479 | /* Impossible to have two separate traces. */ | |
480 | default: | |
481 | assert(0); | |
482 | } | |
483 | ||
484 | assert(0); | |
5b29e799 JG |
485 | return a < b; |
486 | } | |
487 | ||
488 | static | |
489 | void stream_destroy(void *stream) | |
490 | { | |
491 | ctf_fs_stream_destroy((struct ctf_fs_stream *) stream); | |
492 | } | |
493 | ||
494 | static | |
495 | int open_trace_streams(struct ctf_fs_component *ctf_fs, | |
496 | struct ctf_fs_iterator *ctf_it) | |
e7a4393b JG |
497 | { |
498 | int ret = 0; | |
499 | const char *name; | |
500 | GError *error = NULL; | |
501 | GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error); | |
502 | ||
503 | if (!dir) { | |
504 | PERR("Cannot open directory \"%s\": %s (code %d)\n", | |
505 | ctf_fs->trace_path->str, error->message, | |
506 | error->code); | |
507 | goto error; | |
508 | } | |
509 | ||
510 | while ((name = g_dir_read_name(dir))) { | |
511 | struct ctf_fs_file *file = NULL; | |
512 | struct ctf_fs_stream *stream = NULL; | |
513 | ||
514 | if (!strcmp(name, CTF_FS_METADATA_FILENAME)) { | |
515 | /* Ignore the metadata stream. */ | |
516 | PDBG("Ignoring metadata file \"%s\"\n", | |
517 | name); | |
518 | continue; | |
519 | } | |
520 | ||
521 | if (name[0] == '.') { | |
522 | PDBG("Ignoring hidden file \"%s\"\n", | |
523 | name); | |
524 | continue; | |
525 | } | |
526 | ||
527 | /* Create the file. */ | |
528 | file = ctf_fs_file_create(ctf_fs); | |
529 | if (!file) { | |
530 | PERR("Cannot create stream file object\n"); | |
531 | goto error; | |
532 | } | |
533 | ||
534 | /* Create full path string. */ | |
535 | g_string_append_printf(file->path, "%s/%s", | |
536 | ctf_fs->trace_path->str, name); | |
537 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { | |
538 | PDBG("Ignoring non-regular file \"%s\"\n", name); | |
539 | ctf_fs_file_destroy(file); | |
540 | continue; | |
541 | } | |
542 | ||
543 | /* Open the file. */ | |
544 | if (ctf_fs_file_open(ctf_fs, file, "rb")) { | |
545 | ctf_fs_file_destroy(file); | |
546 | goto error; | |
547 | } | |
548 | ||
549 | /* Create a private stream; file ownership is passed to it. */ | |
550 | stream = ctf_fs_stream_create(ctf_fs, file); | |
551 | if (!stream) { | |
552 | ctf_fs_file_destroy(file); | |
553 | goto error; | |
554 | } | |
555 | ||
5b29e799 | 556 | g_ptr_array_add(ctf_it->pending_streams, stream); |
e7a4393b JG |
557 | } |
558 | ||
559 | goto end; | |
560 | error: | |
561 | ret = -1; | |
562 | end: | |
563 | if (dir) { | |
564 | g_dir_close(dir); | |
565 | dir = NULL; | |
566 | } | |
567 | if (error) { | |
568 | g_error_free(error); | |
569 | } | |
570 | return ret; | |
571 | } | |
572 | ||
573 | static | |
5b29e799 JG |
574 | enum bt_component_status ctf_fs_iterator_init(struct bt_component *source, |
575 | struct bt_notification_iterator *it) | |
e7a4393b | 576 | { |
5b29e799 JG |
577 | struct ctf_fs_iterator *ctf_it; |
578 | struct ctf_fs_component *ctf_fs; | |
579 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
580 | ||
581 | assert(source && it); | |
582 | ||
583 | ctf_fs = bt_component_get_private_data(source); | |
584 | if (!ctf_fs) { | |
585 | ret = BT_COMPONENT_STATUS_INVALID; | |
586 | goto end; | |
587 | } | |
588 | ||
589 | ctf_it = g_new0(struct ctf_fs_iterator, 1); | |
590 | if (!ctf_it) { | |
591 | ret = BT_COMPONENT_STATUS_NOMEM; | |
592 | goto end; | |
593 | } | |
594 | ||
595 | ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash, | |
596 | g_direct_equal, bt_put, stream_destroy); | |
597 | if (!ctf_it->stream_ht) { | |
598 | goto error; | |
599 | } | |
600 | ctf_it->pending_streams = g_ptr_array_new_with_free_func( | |
601 | stream_destroy); | |
602 | if (!ctf_it->pending_streams) { | |
603 | goto error; | |
604 | } | |
605 | ctf_it->pending_notifications = bt_notification_heap_create( | |
606 | compare_notifications, NULL); | |
607 | if (!ctf_it->pending_notifications) { | |
608 | goto error; | |
609 | } | |
610 | ||
611 | ret = open_trace_streams(ctf_fs, ctf_it); | |
612 | if (ret) { | |
613 | goto error; | |
614 | } | |
615 | ||
616 | ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get); | |
617 | if (ret) { | |
618 | goto error; | |
619 | } | |
620 | ||
621 | ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next); | |
622 | if (ret) { | |
623 | goto error; | |
624 | } | |
625 | ||
626 | ret = bt_notification_iterator_set_destroy_cb(it, | |
627 | ctf_fs_iterator_destroy); | |
628 | if (ret) { | |
629 | goto error; | |
630 | } | |
631 | ||
632 | ret = bt_notification_iterator_set_private_data(it, ctf_it); | |
633 | if (ret) { | |
634 | goto error; | |
635 | } | |
636 | end: | |
637 | return ret; | |
638 | error: | |
639 | (void) bt_notification_iterator_set_private_data(it, NULL); | |
640 | ctf_fs_iterator_destroy_data(ctf_it); | |
641 | goto end; | |
642 | } | |
643 | ||
644 | static | |
645 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) | |
646 | { | |
647 | if (ctf_fs->trace_path) { | |
648 | g_string_free(ctf_fs->trace_path, TRUE); | |
649 | } | |
650 | if (ctf_fs->metadata) { | |
651 | ctf_fs_metadata_fini(ctf_fs->metadata); | |
652 | g_free(ctf_fs->metadata); | |
653 | } | |
654 | g_free(ctf_fs); | |
655 | } | |
656 | ||
657 | static | |
658 | void ctf_fs_destroy(struct bt_component *component) | |
659 | { | |
660 | void *data = bt_component_get_private_data(component); | |
661 | ||
662 | ctf_fs_destroy_data(data); | |
e7a4393b JG |
663 | } |
664 | ||
56a1cced JG |
665 | static |
666 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) | |
667 | { | |
668 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 669 | struct bt_value *value = NULL; |
56a1cced JG |
670 | const char *path; |
671 | enum bt_value_status ret; | |
672 | ||
673 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
674 | if (!ctf_fs) { | |
675 | goto end; | |
676 | } | |
677 | ||
678 | /* FIXME: should probably look for a source URI */ | |
679 | value = bt_value_map_get(params, "path"); | |
680 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
681 | goto error; | |
682 | } | |
683 | ||
684 | ret = bt_value_string_get(value, &path); | |
685 | if (ret != BT_VALUE_STATUS_OK) { | |
686 | goto error; | |
687 | } | |
688 | ||
689 | ctf_fs->trace_path = g_string_new(path); | |
690 | if (!ctf_fs->trace_path) { | |
691 | goto error; | |
692 | } | |
56a1cced JG |
693 | ctf_fs->error_fp = stderr; |
694 | ctf_fs->page_size = (size_t) getpagesize(); | |
e7a4393b JG |
695 | |
696 | // FIXME: check error. | |
5b29e799 JG |
697 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
698 | if (!ctf_fs->metadata) { | |
e7a4393b JG |
699 | goto error; |
700 | } | |
5b29e799 | 701 | ctf_fs_metadata_set_trace(ctf_fs); |
1ef09eb5 JG |
702 | goto end; |
703 | ||
56a1cced JG |
704 | error: |
705 | ctf_fs_destroy_data(ctf_fs); | |
e7a4393b | 706 | ctf_fs = NULL; |
1ef09eb5 JG |
707 | end: |
708 | BT_PUT(value); | |
56a1cced JG |
709 | return ctf_fs; |
710 | } | |
711 | ||
ea0b4b9e JG |
712 | BT_HIDDEN |
713 | enum bt_component_status ctf_fs_init(struct bt_component *source, | |
5c80adeb | 714 | struct bt_value *params) |
ea0b4b9e JG |
715 | { |
716 | struct ctf_fs_component *ctf_fs; | |
717 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
718 | ||
719 | assert(source); | |
720 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; | |
721 | ctf_fs = ctf_fs_create(params); | |
722 | if (!ctf_fs) { | |
723 | ret = BT_COMPONENT_STATUS_NOMEM; | |
724 | goto end; | |
725 | } | |
4c1456f0 | 726 | |
ea0b4b9e JG |
727 | ret = bt_component_set_destroy_cb(source, ctf_fs_destroy); |
728 | if (ret != BT_COMPONENT_STATUS_OK) { | |
729 | goto error; | |
730 | } | |
731 | ||
732 | ret = bt_component_set_private_data(source, ctf_fs); | |
733 | if (ret != BT_COMPONENT_STATUS_OK) { | |
734 | goto error; | |
735 | } | |
736 | ||
737 | ret = bt_component_source_set_iterator_init_cb(source, | |
738 | ctf_fs_iterator_init); | |
739 | if (ret != BT_COMPONENT_STATUS_OK) { | |
740 | goto error; | |
741 | } | |
742 | end: | |
743 | return ret; | |
744 | error: | |
745 | (void) bt_component_set_private_data(source, NULL); | |
760051fa | 746 | ctf_fs_destroy_data(ctf_fs); |
ea0b4b9e JG |
747 | return ret; |
748 | } |