ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / writer / writer.c
1 /*
2 * writer.c
3 *
4 * Babeltrace CTF Writer
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #include <babeltrace/ctf-writer/clock-internal.h>
30 #include <babeltrace/ctf-writer/writer-internal.h>
31 #include <babeltrace/ctf-ir/field-types-internal.h>
32 #include <babeltrace/ctf-ir/fields-internal.h>
33 #include <babeltrace/ctf-writer/functor-internal.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/ctf-ir/trace-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
46
47 static
48 void bt_ctf_writer_destroy(struct bt_object *obj);
49
50 struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
51 {
52 struct bt_ctf_writer *writer = NULL;
53
54 if (!path) {
55 goto error;
56 }
57
58 writer = g_new0(struct bt_ctf_writer, 1);
59 if (!writer) {
60 goto error;
61 }
62
63 bt_object_init(writer, bt_ctf_writer_destroy);
64 writer->path = g_string_new(path);
65 if (!writer->path) {
66 goto error_destroy;
67 }
68
69 writer->trace = bt_ctf_trace_create();
70 if (!writer->trace) {
71 goto error_destroy;
72 }
73
74 writer->trace->is_created_by_writer = 1;
75 bt_object_set_parent(writer->trace, writer);
76 bt_put(writer->trace);
77 /* Create trace directory if necessary and open a metadata file */
78 if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) {
79 perror("g_mkdir_with_parents");
80 goto error_destroy;
81 }
82
83 writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
84 if (writer->trace_dir_fd < 0) {
85 perror("open");
86 goto error_destroy;
87 }
88
89 writer->metadata_fd = openat(writer->trace_dir_fd, "metadata",
90 O_WRONLY | O_CREAT | O_TRUNC,
91 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
92
93 return writer;
94
95 error_destroy:
96 unlinkat(writer->trace_dir_fd, "metadata", 0);
97 BT_PUT(writer);
98 error:
99 return writer;
100 }
101
102 void bt_ctf_writer_destroy(struct bt_object *obj)
103 {
104 struct bt_ctf_writer *writer;
105
106 writer = container_of(obj, struct bt_ctf_writer, base);
107 bt_ctf_writer_flush_metadata(writer);
108 if (writer->path) {
109 g_string_free(writer->path, TRUE);
110 }
111
112 if (writer->trace_dir_fd > 0) {
113 if (close(writer->trace_dir_fd)) {
114 perror("close");
115 }
116 }
117
118 if (writer->metadata_fd > 0) {
119 if (close(writer->metadata_fd)) {
120 perror("close");
121 }
122 }
123
124 bt_object_release(writer->trace);
125 g_free(writer);
126 }
127
128 struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
129 {
130 struct bt_ctf_trace *trace = NULL;
131
132 if (!writer) {
133 goto end;
134 }
135
136 trace = writer->trace;
137 bt_get(trace);
138 end:
139 return trace;
140 }
141
142 struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
143 struct bt_ctf_stream_class *stream_class)
144 {
145 struct bt_ctf_stream *stream = NULL;
146 int stream_class_count;
147 bool stream_class_found = false;
148 int i;
149
150 if (!writer || !stream_class) {
151 goto error;
152 }
153
154 /* Make sure the stream class is part of the writer's trace */
155 stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace);
156 if (stream_class_count < 0) {
157 goto error;
158 }
159
160 for (i = 0; i < stream_class_count; i++) {
161 struct bt_ctf_stream_class *existing_stream_class =
162 bt_ctf_trace_get_stream_class(writer->trace, i);
163
164 if (existing_stream_class == stream_class) {
165 stream_class_found = true;
166 }
167
168 BT_PUT(existing_stream_class);
169
170 if (stream_class_found) {
171 break;
172 }
173 }
174
175 if (!stream_class_found) {
176 int ret = bt_ctf_trace_add_stream_class(writer->trace,
177 stream_class);
178
179 if (ret) {
180 goto error;
181 }
182 }
183
184 stream = bt_ctf_stream_create(stream_class, NULL);
185 if (!stream) {
186 goto error;
187 }
188
189 return stream;
190
191 error:
192 BT_PUT(stream);
193 return stream;
194 }
195
196 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
197 const char *name,
198 const char *value)
199 {
200 int ret = -1;
201
202 if (!writer || !name || !value) {
203 goto end;
204 }
205
206 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
207 name, value);
208 end:
209 return ret;
210 }
211
212 int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer,
213 const char *name,
214 int64_t value)
215 {
216 int ret = -1;
217
218 if (!writer || !name) {
219 goto end;
220 }
221
222 ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name,
223 value);
224 end:
225 return ret;
226 }
227
228 int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
229 struct bt_ctf_clock *clock)
230 {
231 int ret = -1;
232
233 if (!writer || !clock) {
234 goto end;
235 }
236
237 ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class);
238 end:
239 return ret;
240 }
241
242 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
243 {
244 char *metadata_string = NULL;
245
246 if (!writer) {
247 goto end;
248 }
249
250 metadata_string = bt_ctf_trace_get_metadata_string(
251 writer->trace);
252 end:
253 return metadata_string;
254 }
255
256 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
257 {
258 int ret;
259 char *metadata_string = NULL;
260
261 if (!writer) {
262 goto end;
263 }
264
265 metadata_string = bt_ctf_trace_get_metadata_string(
266 writer->trace);
267 if (!metadata_string) {
268 goto end;
269 }
270
271 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
272 perror("lseek");
273 goto end;
274 }
275
276 if (ftruncate(writer->metadata_fd, 0)) {
277 perror("ftruncate");
278 goto end;
279 }
280
281 ret = write(writer->metadata_fd, metadata_string,
282 strlen(metadata_string));
283 if (ret < 0) {
284 perror("write");
285 goto end;
286 }
287 end:
288 g_free(metadata_string);
289 }
290
291 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
292 enum bt_ctf_byte_order byte_order)
293 {
294 int ret = 0;
295
296 if (!writer || writer->frozen) {
297 ret = -1;
298 goto end;
299 }
300
301 ret = bt_ctf_trace_set_byte_order(writer->trace,
302 byte_order);
303 end:
304 return ret;
305 }
306
307 void bt_ctf_writer_get(struct bt_ctf_writer *writer)
308 {
309 bt_get(writer);
310 }
311
312 void bt_ctf_writer_put(struct bt_ctf_writer *writer)
313 {
314 bt_put(writer);
315 }
316
317 BT_HIDDEN
318 void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
319 {
320 writer->frozen = 1;
321 }
This page took 0.03503 seconds and 4 git commands to generate.