Validate notification type before casting
[babeltrace.git] / plugins / ctf / fs / fs.c
CommitLineData
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
43static bool ctf_fs_debug;
44
45static
760051fa
JG
46struct 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);
64end:
8cc9065f 65 BT_PUT(component);
d01e0f33 66 return notification;
ea0b4b9e
JG
67}
68
69static
760051fa
JG
70enum 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
fc9a526c 87 ret = ctf_fs_data_stream_get_next_notification(ctf_fs, &notification, 0);
d01e0f33
JG
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 94end:
8cc9065f 95 BT_PUT(component);
043e2020 96 return ret;
ea0b4b9e 97}
bfd20a42 98
760051fa
JG
99static
100void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it)
101{
102 g_free(ctf_it);
103}
104
105static
106void 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
113static
ea0b4b9e
JG
114enum 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 }
147end:
ea0b4b9e 148 return ret;
760051fa
JG
149error:
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
155static
156void 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);
c14d7e26
JG
164 if (component->streams) {
165 g_ptr_array_free(component->streams, TRUE);
166 }
760051fa
JG
167 g_free(component);
168}
169
170static
171void ctf_fs_destroy(struct bt_component *component)
172{
173 void *data = bt_component_get_private_data(component);
174
175 ctf_fs_destroy_data(data);
4c1456f0
JG
176}
177
e7a4393b
JG
178static
179int open_trace_streams(struct ctf_fs_component *ctf_fs)
180{
181 int ret = 0;
182 const char *name;
183 GError *error = NULL;
184 GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
185
186 if (!dir) {
187 PERR("Cannot open directory \"%s\": %s (code %d)\n",
188 ctf_fs->trace_path->str, error->message,
189 error->code);
190 goto error;
191 }
192
193 while ((name = g_dir_read_name(dir))) {
194 struct ctf_fs_file *file = NULL;
195 struct ctf_fs_stream *stream = NULL;
196
197 if (!strcmp(name, CTF_FS_METADATA_FILENAME)) {
198 /* Ignore the metadata stream. */
199 PDBG("Ignoring metadata file \"%s\"\n",
200 name);
201 continue;
202 }
203
204 if (name[0] == '.') {
205 PDBG("Ignoring hidden file \"%s\"\n",
206 name);
207 continue;
208 }
209
210 /* Create the file. */
211 file = ctf_fs_file_create(ctf_fs);
212 if (!file) {
213 PERR("Cannot create stream file object\n");
214 goto error;
215 }
216
217 /* Create full path string. */
218 g_string_append_printf(file->path, "%s/%s",
219 ctf_fs->trace_path->str, name);
220 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
221 PDBG("Ignoring non-regular file \"%s\"\n", name);
222 ctf_fs_file_destroy(file);
223 continue;
224 }
225
226 /* Open the file. */
227 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
228 ctf_fs_file_destroy(file);
229 goto error;
230 }
231
232 /* Create a private stream; file ownership is passed to it. */
233 stream = ctf_fs_stream_create(ctf_fs, file);
234 if (!stream) {
235 ctf_fs_file_destroy(file);
236 goto error;
237 }
238
239 g_ptr_array_add(ctf_fs->streams, stream);
240 }
241
242 goto end;
243error:
244 ret = -1;
245end:
246 if (dir) {
247 g_dir_close(dir);
248 dir = NULL;
249 }
250 if (error) {
251 g_error_free(error);
252 }
253 return ret;
254}
255
256static
257void stream_destroy(void *stream)
258{
259 ctf_fs_stream_destroy((struct ctf_fs_stream *) stream);
260}
261
56a1cced
JG
262static
263struct ctf_fs_component *ctf_fs_create(struct bt_value *params)
264{
265 struct ctf_fs_component *ctf_fs;
1ef09eb5 266 struct bt_value *value = NULL;
56a1cced
JG
267 const char *path;
268 enum bt_value_status ret;
269
270 ctf_fs = g_new0(struct ctf_fs_component, 1);
271 if (!ctf_fs) {
272 goto end;
273 }
274
275 /* FIXME: should probably look for a source URI */
276 value = bt_value_map_get(params, "path");
277 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
278 goto error;
279 }
280
281 ret = bt_value_string_get(value, &path);
282 if (ret != BT_VALUE_STATUS_OK) {
283 goto error;
284 }
285
286 ctf_fs->trace_path = g_string_new(path);
287 if (!ctf_fs->trace_path) {
288 goto error;
289 }
290
e7a4393b 291 ctf_fs->streams = g_ptr_array_new_with_free_func(stream_destroy);
56a1cced
JG
292 ctf_fs->error_fp = stderr;
293 ctf_fs->page_size = (size_t) getpagesize();
e7a4393b
JG
294
295 // FIXME: check error.
413bc2c4 296 ctf_fs_metadata_set_trace(ctf_fs);
e7a4393b
JG
297
298 ret = open_trace_streams(ctf_fs);
299 if (ret) {
300 goto error;
301 }
1ef09eb5
JG
302 goto end;
303
56a1cced
JG
304error:
305 ctf_fs_destroy_data(ctf_fs);
e7a4393b 306 ctf_fs = NULL;
1ef09eb5
JG
307end:
308 BT_PUT(value);
56a1cced
JG
309 return ctf_fs;
310}
311
ea0b4b9e
JG
312BT_HIDDEN
313enum bt_component_status ctf_fs_init(struct bt_component *source,
5c80adeb 314 struct bt_value *params)
ea0b4b9e
JG
315{
316 struct ctf_fs_component *ctf_fs;
317 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
318
319 assert(source);
320 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
321 ctf_fs = ctf_fs_create(params);
322 if (!ctf_fs) {
323 ret = BT_COMPONENT_STATUS_NOMEM;
324 goto end;
325 }
4c1456f0 326
ea0b4b9e
JG
327 ret = bt_component_set_destroy_cb(source, ctf_fs_destroy);
328 if (ret != BT_COMPONENT_STATUS_OK) {
329 goto error;
330 }
331
332 ret = bt_component_set_private_data(source, ctf_fs);
333 if (ret != BT_COMPONENT_STATUS_OK) {
334 goto error;
335 }
336
337 ret = bt_component_source_set_iterator_init_cb(source,
338 ctf_fs_iterator_init);
339 if (ret != BT_COMPONENT_STATUS_OK) {
340 goto error;
341 }
342end:
343 return ret;
344error:
345 (void) bt_component_set_private_data(source, NULL);
760051fa 346 ctf_fs_destroy_data(ctf_fs);
ea0b4b9e
JG
347 return ret;
348}
This page took 0.037095 seconds and 4 git commands to generate.