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> |
760051fa | 30 | #include <babeltrace/plugin/notification/iterator.h> |
ea0b4b9e JG |
31 | #include <glib.h> |
32 | #include <assert.h> | |
56a1cced JG |
33 | #include <unistd.h> |
34 | #include "fs.h" | |
413bc2c4 JG |
35 | #include "metadata.h" |
36 | #include "data-stream.h" | |
e7a4393b JG |
37 | #include "file.h" |
38 | ||
39 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
40 | #define PRINT_PREFIX "ctf-fs" | |
41 | #include "print.h" | |
ea0b4b9e JG |
42 | |
43 | static bool ctf_fs_debug; | |
44 | ||
45 | static | |
760051fa JG |
46 | struct bt_notification *ctf_fs_iterator_get( |
47 | struct bt_notification_iterator *iterator) | |
ea0b4b9e | 48 | { |
d01e0f33 JG |
49 | struct bt_notification *notification = NULL; |
50 | struct ctf_fs_component *ctf_fs; | |
51 | struct bt_component *component = bt_notification_iterator_get_component( | |
52 | iterator); | |
53 | ||
54 | if (!component) { | |
55 | goto end; | |
56 | } | |
57 | ||
58 | ctf_fs = bt_component_get_private_data(component); | |
59 | if (!ctf_fs) { | |
60 | goto end; | |
61 | } | |
62 | ||
63 | notification = bt_get(ctf_fs->current_notification); | |
64 | end: | |
8cc9065f | 65 | BT_PUT(component); |
d01e0f33 | 66 | return notification; |
ea0b4b9e JG |
67 | } |
68 | ||
69 | static | |
760051fa JG |
70 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
71 | struct bt_notification_iterator *iterator) | |
ea0b4b9e | 72 | { |
d01e0f33 | 73 | enum bt_notification_iterator_status ret; |
78586d8a | 74 | struct bt_notification *notification = NULL; |
d01e0f33 JG |
75 | struct ctf_fs_component *ctf_fs; |
76 | struct bt_component *component = bt_notification_iterator_get_component( | |
77 | iterator); | |
78 | ||
79 | if (!component) { | |
80 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
81 | goto end; | |
82 | } | |
83 | ||
7aeb43bf JG |
84 | ctf_fs = bt_component_get_private_data(component); |
85 | assert(ctf_fs); | |
86 | ||
d01e0f33 JG |
87 | ret = ctf_fs_data_stream_get_next_notification(ctf_fs, ¬ification); |
88 | if (ret || !notification) { | |
89 | goto end; | |
90 | } | |
91 | ||
78586d8a JG |
92 | bt_put(ctf_fs->current_notification); |
93 | ctf_fs->current_notification = notification; | |
d01e0f33 | 94 | end: |
8cc9065f | 95 | BT_PUT(component); |
043e2020 | 96 | return ret; |
ea0b4b9e | 97 | } |
bfd20a42 | 98 | |
760051fa JG |
99 | static |
100 | void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it) | |
101 | { | |
102 | g_free(ctf_it); | |
103 | } | |
104 | ||
105 | static | |
106 | void ctf_fs_iterator_destroy(struct bt_notification_iterator *it) | |
107 | { | |
108 | void *data = bt_notification_iterator_get_private_data(it); | |
109 | ||
110 | ctf_fs_iterator_destroy_data(data); | |
111 | } | |
112 | ||
113 | static | |
ea0b4b9e JG |
114 | enum bt_component_status ctf_fs_iterator_init(struct bt_component *source, |
115 | struct bt_notification_iterator *it) | |
4c1456f0 | 116 | { |
760051fa | 117 | struct ctf_fs_iterator *ctf_it; |
413bc2c4 | 118 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
ea0b4b9e JG |
119 | |
120 | assert(source && it); | |
760051fa JG |
121 | ctf_it = g_new0(struct ctf_fs_iterator, 1); |
122 | if (!ctf_it) { | |
123 | ret = BT_COMPONENT_STATUS_NOMEM; | |
124 | goto end; | |
125 | } | |
126 | ||
127 | ret = bt_notification_iterator_set_get_cb(it, ctf_fs_iterator_get); | |
128 | if (ret) { | |
129 | goto error; | |
130 | } | |
131 | ||
132 | ret = bt_notification_iterator_set_next_cb(it, ctf_fs_iterator_next); | |
133 | if (ret) { | |
134 | goto error; | |
135 | } | |
136 | ||
137 | ret = bt_notification_iterator_set_destroy_cb(it, | |
138 | ctf_fs_iterator_destroy); | |
139 | if (ret) { | |
140 | goto error; | |
141 | } | |
142 | ||
143 | ret = bt_notification_iterator_set_private_data(it, ctf_it); | |
144 | if (ret) { | |
145 | goto error; | |
146 | } | |
147 | end: | |
ea0b4b9e | 148 | return ret; |
760051fa JG |
149 | error: |
150 | (void) bt_notification_iterator_set_private_data(it, NULL); | |
151 | ctf_fs_iterator_destroy_data(ctf_it); | |
152 | return ret; | |
153 | } | |
154 | ||
760051fa JG |
155 | static |
156 | void ctf_fs_destroy_data(struct ctf_fs_component *component) | |
157 | { | |
56a1cced JG |
158 | if (component->trace_path) { |
159 | g_string_free(component->trace_path, TRUE); | |
160 | } | |
161 | ||
413bc2c4 | 162 | ctf_fs_metadata_fini(&component->metadata); |
d01e0f33 | 163 | BT_PUT(component->current_notification); |
e7a4393b | 164 | g_ptr_array_free(component->streams, TRUE); |
760051fa JG |
165 | g_free(component); |
166 | } | |
167 | ||
168 | static | |
169 | void ctf_fs_destroy(struct bt_component *component) | |
170 | { | |
171 | void *data = bt_component_get_private_data(component); | |
172 | ||
173 | ctf_fs_destroy_data(data); | |
4c1456f0 JG |
174 | } |
175 | ||
e7a4393b JG |
176 | static |
177 | int open_trace_streams(struct ctf_fs_component *ctf_fs) | |
178 | { | |
179 | int ret = 0; | |
180 | const char *name; | |
181 | GError *error = NULL; | |
182 | GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error); | |
183 | ||
184 | if (!dir) { | |
185 | PERR("Cannot open directory \"%s\": %s (code %d)\n", | |
186 | ctf_fs->trace_path->str, error->message, | |
187 | error->code); | |
188 | goto error; | |
189 | } | |
190 | ||
191 | while ((name = g_dir_read_name(dir))) { | |
192 | struct ctf_fs_file *file = NULL; | |
193 | struct ctf_fs_stream *stream = NULL; | |
194 | ||
195 | if (!strcmp(name, CTF_FS_METADATA_FILENAME)) { | |
196 | /* Ignore the metadata stream. */ | |
197 | PDBG("Ignoring metadata file \"%s\"\n", | |
198 | name); | |
199 | continue; | |
200 | } | |
201 | ||
202 | if (name[0] == '.') { | |
203 | PDBG("Ignoring hidden file \"%s\"\n", | |
204 | name); | |
205 | continue; | |
206 | } | |
207 | ||
208 | /* Create the file. */ | |
209 | file = ctf_fs_file_create(ctf_fs); | |
210 | if (!file) { | |
211 | PERR("Cannot create stream file object\n"); | |
212 | goto error; | |
213 | } | |
214 | ||
215 | /* Create full path string. */ | |
216 | g_string_append_printf(file->path, "%s/%s", | |
217 | ctf_fs->trace_path->str, name); | |
218 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { | |
219 | PDBG("Ignoring non-regular file \"%s\"\n", name); | |
220 | ctf_fs_file_destroy(file); | |
221 | continue; | |
222 | } | |
223 | ||
224 | /* Open the file. */ | |
225 | if (ctf_fs_file_open(ctf_fs, file, "rb")) { | |
226 | ctf_fs_file_destroy(file); | |
227 | goto error; | |
228 | } | |
229 | ||
230 | /* Create a private stream; file ownership is passed to it. */ | |
231 | stream = ctf_fs_stream_create(ctf_fs, file); | |
232 | if (!stream) { | |
233 | ctf_fs_file_destroy(file); | |
234 | goto error; | |
235 | } | |
236 | ||
237 | g_ptr_array_add(ctf_fs->streams, stream); | |
238 | } | |
239 | ||
240 | goto end; | |
241 | error: | |
242 | ret = -1; | |
243 | end: | |
244 | if (dir) { | |
245 | g_dir_close(dir); | |
246 | dir = NULL; | |
247 | } | |
248 | if (error) { | |
249 | g_error_free(error); | |
250 | } | |
251 | return ret; | |
252 | } | |
253 | ||
254 | static | |
255 | void stream_destroy(void *stream) | |
256 | { | |
257 | ctf_fs_stream_destroy((struct ctf_fs_stream *) stream); | |
258 | } | |
259 | ||
56a1cced JG |
260 | static |
261 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) | |
262 | { | |
263 | struct ctf_fs_component *ctf_fs; | |
1ef09eb5 | 264 | struct bt_value *value = NULL; |
56a1cced JG |
265 | const char *path; |
266 | enum bt_value_status ret; | |
267 | ||
268 | ctf_fs = g_new0(struct ctf_fs_component, 1); | |
269 | if (!ctf_fs) { | |
270 | goto end; | |
271 | } | |
272 | ||
273 | /* FIXME: should probably look for a source URI */ | |
274 | value = bt_value_map_get(params, "path"); | |
275 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
276 | goto error; | |
277 | } | |
278 | ||
279 | ret = bt_value_string_get(value, &path); | |
280 | if (ret != BT_VALUE_STATUS_OK) { | |
281 | goto error; | |
282 | } | |
283 | ||
284 | ctf_fs->trace_path = g_string_new(path); | |
285 | if (!ctf_fs->trace_path) { | |
286 | goto error; | |
287 | } | |
288 | ||
e7a4393b | 289 | ctf_fs->streams = g_ptr_array_new_with_free_func(stream_destroy); |
56a1cced JG |
290 | ctf_fs->error_fp = stderr; |
291 | ctf_fs->page_size = (size_t) getpagesize(); | |
e7a4393b JG |
292 | |
293 | // FIXME: check error. | |
413bc2c4 | 294 | ctf_fs_metadata_set_trace(ctf_fs); |
e7a4393b JG |
295 | |
296 | ret = open_trace_streams(ctf_fs); | |
297 | if (ret) { | |
298 | goto error; | |
299 | } | |
1ef09eb5 JG |
300 | goto end; |
301 | ||
56a1cced JG |
302 | error: |
303 | ctf_fs_destroy_data(ctf_fs); | |
e7a4393b | 304 | ctf_fs = NULL; |
1ef09eb5 JG |
305 | end: |
306 | BT_PUT(value); | |
56a1cced JG |
307 | return ctf_fs; |
308 | } | |
309 | ||
ea0b4b9e JG |
310 | BT_HIDDEN |
311 | enum bt_component_status ctf_fs_init(struct bt_component *source, | |
5c80adeb | 312 | struct bt_value *params) |
ea0b4b9e JG |
313 | { |
314 | struct ctf_fs_component *ctf_fs; | |
315 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
316 | ||
317 | assert(source); | |
318 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; | |
319 | ctf_fs = ctf_fs_create(params); | |
320 | if (!ctf_fs) { | |
321 | ret = BT_COMPONENT_STATUS_NOMEM; | |
322 | goto end; | |
323 | } | |
4c1456f0 | 324 | |
ea0b4b9e JG |
325 | ret = bt_component_set_destroy_cb(source, ctf_fs_destroy); |
326 | if (ret != BT_COMPONENT_STATUS_OK) { | |
327 | goto error; | |
328 | } | |
329 | ||
330 | ret = bt_component_set_private_data(source, ctf_fs); | |
331 | if (ret != BT_COMPONENT_STATUS_OK) { | |
332 | goto error; | |
333 | } | |
334 | ||
335 | ret = bt_component_source_set_iterator_init_cb(source, | |
336 | ctf_fs_iterator_init); | |
337 | if (ret != BT_COMPONENT_STATUS_OK) { | |
338 | goto error; | |
339 | } | |
340 | end: | |
341 | return ret; | |
342 | error: | |
343 | (void) bt_component_set_private_data(source, NULL); | |
760051fa | 344 | ctf_fs_destroy_data(ctf_fs); |
ea0b4b9e JG |
345 | return ret; |
346 | } |