ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / writer / writer.c
CommitLineData
273b65be
JG
1/*
2 * writer.c
3 *
4 * Babeltrace CTF Writer
5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
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
ac0c6bdd 29#include <babeltrace/ctf-writer/clock-internal.h>
273b65be 30#include <babeltrace/ctf-writer/writer-internal.h>
2e33ac5a
PP
31#include <babeltrace/ctf-ir/field-types-internal.h>
32#include <babeltrace/ctf-ir/fields-internal.h>
273b65be 33#include <babeltrace/ctf-writer/functor-internal.h>
adc315b8 34#include <babeltrace/ctf-ir/stream-class-internal.h>
3f043b05 35#include <babeltrace/ctf-ir/stream-internal.h>
319fd969 36#include <babeltrace/ctf-ir/trace-internal.h>
83509119 37#include <babeltrace/ref.h>
273b65be
JG
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
273b65be 47static
83509119
JG
48void bt_ctf_writer_destroy(struct bt_object *obj);
49
273b65be
JG
50struct 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
83509119 63 bt_object_init(writer, bt_ctf_writer_destroy);
273b65be
JG
64 writer->path = g_string_new(path);
65 if (!writer->path) {
66 goto error_destroy;
67 }
68
bc37ae52
JG
69 writer->trace = bt_ctf_trace_create();
70 if (!writer->trace) {
71 goto error_destroy;
72 }
73
319fd969 74 writer->trace->is_created_by_writer = 1;
e6a8e8e4
JG
75 bt_object_set_parent(writer->trace, writer);
76 bt_put(writer->trace);
273b65be
JG
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
1b8180b9 83 writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
273b65be
JG
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);
273b65be
JG
92
93 return writer;
bc37ae52 94
273b65be
JG
95error_destroy:
96 unlinkat(writer->trace_dir_fd, "metadata", 0);
83509119 97 BT_PUT(writer);
273b65be
JG
98error:
99 return writer;
100}
101
83509119 102void bt_ctf_writer_destroy(struct bt_object *obj)
273b65be
JG
103{
104 struct bt_ctf_writer *writer;
273b65be 105
83509119 106 writer = container_of(obj, struct bt_ctf_writer, base);
273b65be
JG
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) {
9bb7e58b
MD
113 if (close(writer->trace_dir_fd)) {
114 perror("close");
9bb7e58b 115 }
273b65be
JG
116 }
117
118 if (writer->metadata_fd > 0) {
9bb7e58b
MD
119 if (close(writer->metadata_fd)) {
120 perror("close");
9bb7e58b 121 }
273b65be
JG
122 }
123
e6a8e8e4 124 bt_object_release(writer->trace);
273b65be
JG
125 g_free(writer);
126}
127
a2540e85
JG
128struct 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;
83509119 137 bt_get(trace);
a2540e85
JG
138end:
139 return trace;
140}
141
273b65be
JG
142struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
143 struct bt_ctf_stream_class *stream_class)
144{
273b65be 145 struct bt_ctf_stream *stream = NULL;
319fd969
PP
146 int stream_class_count;
147 bool stream_class_found = false;
148 int i;
273b65be
JG
149
150 if (!writer || !stream_class) {
151 goto error;
152 }
153
319fd969
PP
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) {
273b65be
JG
157 goto error;
158 }
159
319fd969
PP
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
b71d7298 184 stream = bt_ctf_stream_create(stream_class, NULL);
319fd969 185 if (!stream) {
273b65be
JG
186 goto error;
187 }
188
273b65be 189 return stream;
bc37ae52 190
273b65be 191error:
83509119
JG
192 BT_PUT(stream);
193 return stream;
273b65be
JG
194}
195
196int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
197 const char *name,
198 const char *value)
199{
bc37ae52 200 int ret = -1;
273b65be 201
bc37ae52
JG
202 if (!writer || !name || !value) {
203 goto end;
273b65be
JG
204 }
205
7f800dc7 206 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
bc37ae52
JG
207 name, value);
208end:
273b65be
JG
209 return ret;
210}
211
d7503815
SM
212int 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);
224end:
225 return ret;
226}
227
273b65be
JG
228int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
229 struct bt_ctf_clock *clock)
230{
bc37ae52 231 int ret = -1;
273b65be
JG
232
233 if (!writer || !clock) {
12af6048
JG
234 goto end;
235 }
273b65be 236
ac0c6bdd 237 ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class);
12af6048
JG
238end:
239 return ret;
273b65be
JG
240}
241
273b65be
JG
242char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
243{
bc37ae52 244 char *metadata_string = NULL;
273b65be
JG
245
246 if (!writer) {
247 goto end;
248 }
249
bc37ae52
JG
250 metadata_string = bt_ctf_trace_get_metadata_string(
251 writer->trace);
273b65be 252end:
bc37ae52 253 return metadata_string;
273b65be
JG
254}
255
256void 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
bc37ae52
JG
265 metadata_string = bt_ctf_trace_get_metadata_string(
266 writer->trace);
273b65be
JG
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 }
287end:
288 g_free(metadata_string);
289}
290
291int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
292 enum bt_ctf_byte_order byte_order)
293{
294 int ret = 0;
273b65be
JG
295
296 if (!writer || writer->frozen) {
297 ret = -1;
298 goto end;
299 }
300
bc37ae52
JG
301 ret = bt_ctf_trace_set_byte_order(writer->trace,
302 byte_order);
273b65be
JG
303end:
304 return ret;
305}
306
307void bt_ctf_writer_get(struct bt_ctf_writer *writer)
308{
83509119 309 bt_get(writer);
273b65be
JG
310}
311
312void bt_ctf_writer_put(struct bt_ctf_writer *writer)
313{
83509119 314 bt_put(writer);
273b65be
JG
315}
316
319fd969
PP
317BT_HIDDEN
318void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
273b65be 319{
319fd969 320 writer->frozen = 1;
273b65be 321}
This page took 0.047109 seconds and 4 git commands to generate.