ir: add bt_ctf_trace_get_stream_class_by_id()
[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
adc315b8 29#include <babeltrace/ctf-ir/clock-internal.h>
273b65be 30#include <babeltrace/ctf-writer/writer-internal.h>
adc315b8
JG
31#include <babeltrace/ctf-ir/event-types-internal.h>
32#include <babeltrace/ctf-ir/event-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>
de3dd40e
PP
36#include <babeltrace/ctf-ir/ref.h>
37#include <babeltrace/ctf-ir/common-internal.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
de3dd40e 48void bt_ctf_writer_destroy(struct bt_ref *ref);
273b65be 49static
273b65be
JG
50int create_stream_file(struct bt_ctf_writer *writer,
51 struct bt_ctf_stream *stream);
273b65be 52
273b65be
JG
53struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
54{
55 struct bt_ctf_writer *writer = NULL;
56
57 if (!path) {
58 goto error;
59 }
60
61 writer = g_new0(struct bt_ctf_writer, 1);
62 if (!writer) {
63 goto error;
64 }
65
de3dd40e 66 bt_ctf_base_init(writer, bt_ctf_writer_destroy);
273b65be
JG
67 writer->path = g_string_new(path);
68 if (!writer->path) {
69 goto error_destroy;
70 }
71
bc37ae52
JG
72 writer->trace = bt_ctf_trace_create();
73 if (!writer->trace) {
74 goto error_destroy;
75 }
76
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);
de3dd40e 97 bt_ctf_writer_destroy(&writer->base.ref_count);
273b65be
JG
98 writer = NULL;
99error:
100 return writer;
101}
102
de3dd40e 103void bt_ctf_writer_destroy(struct bt_ref *ref)
273b65be
JG
104{
105 struct bt_ctf_writer *writer;
de3dd40e 106 struct bt_ctf_base *base;
273b65be
JG
107
108 if (!ref) {
109 return;
110 }
111
de3dd40e
PP
112 base = container_of(ref, struct bt_ctf_base, ref_count);
113 writer = container_of(base, struct bt_ctf_writer, base);
273b65be
JG
114 bt_ctf_writer_flush_metadata(writer);
115 if (writer->path) {
116 g_string_free(writer->path, TRUE);
117 }
118
119 if (writer->trace_dir_fd > 0) {
9bb7e58b
MD
120 if (close(writer->trace_dir_fd)) {
121 perror("close");
9bb7e58b 122 }
273b65be
JG
123 }
124
125 if (writer->metadata_fd > 0) {
9bb7e58b
MD
126 if (close(writer->metadata_fd)) {
127 perror("close");
9bb7e58b 128 }
273b65be
JG
129 }
130
bc37ae52 131 bt_ctf_trace_put(writer->trace);
273b65be
JG
132 g_free(writer);
133}
134
a2540e85
JG
135struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
136{
137 struct bt_ctf_trace *trace = NULL;
138
139 if (!writer) {
140 goto end;
141 }
142
143 trace = writer->trace;
144 bt_ctf_trace_get(trace);
145end:
146 return trace;
147}
148
273b65be
JG
149struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
150 struct bt_ctf_stream_class *stream_class)
151{
273b65be
JG
152 int stream_fd;
153 struct bt_ctf_stream *stream = NULL;
154
155 if (!writer || !stream_class) {
156 goto error;
157 }
158
bc37ae52 159 stream = bt_ctf_trace_create_stream(writer->trace, stream_class);
273b65be
JG
160 if (!stream) {
161 goto error;
162 }
163
164 stream_fd = create_stream_file(writer, stream);
165 if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) {
166 goto error;
167 }
168
273b65be
JG
169 writer->frozen = 1;
170 return stream;
bc37ae52 171
273b65be
JG
172error:
173 bt_ctf_stream_put(stream);
174 return NULL;
175}
176
177int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
178 const char *name,
179 const char *value)
180{
bc37ae52 181 int ret = -1;
273b65be 182
bc37ae52
JG
183 if (!writer || !name || !value) {
184 goto end;
273b65be
JG
185 }
186
7f800dc7 187 ret = bt_ctf_trace_set_environment_field_string(writer->trace,
bc37ae52
JG
188 name, value);
189end:
273b65be
JG
190 return ret;
191}
192
193int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
194 struct bt_ctf_clock *clock)
195{
bc37ae52 196 int ret = -1;
273b65be
JG
197
198 if (!writer || !clock) {
12af6048
JG
199 goto end;
200 }
273b65be 201
bc37ae52 202 ret = bt_ctf_trace_add_clock(writer->trace, clock);
12af6048
JG
203end:
204 return ret;
273b65be
JG
205}
206
273b65be
JG
207char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
208{
bc37ae52 209 char *metadata_string = NULL;
273b65be
JG
210
211 if (!writer) {
212 goto end;
213 }
214
bc37ae52
JG
215 metadata_string = bt_ctf_trace_get_metadata_string(
216 writer->trace);
273b65be 217end:
bc37ae52 218 return metadata_string;
273b65be
JG
219}
220
221void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
222{
223 int ret;
224 char *metadata_string = NULL;
225
226 if (!writer) {
227 goto end;
228 }
229
bc37ae52
JG
230 metadata_string = bt_ctf_trace_get_metadata_string(
231 writer->trace);
273b65be
JG
232 if (!metadata_string) {
233 goto end;
234 }
235
236 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
237 perror("lseek");
238 goto end;
239 }
240
241 if (ftruncate(writer->metadata_fd, 0)) {
242 perror("ftruncate");
243 goto end;
244 }
245
246 ret = write(writer->metadata_fd, metadata_string,
247 strlen(metadata_string));
248 if (ret < 0) {
249 perror("write");
250 goto end;
251 }
252end:
253 g_free(metadata_string);
254}
255
256int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
257 enum bt_ctf_byte_order byte_order)
258{
259 int ret = 0;
273b65be
JG
260
261 if (!writer || writer->frozen) {
262 ret = -1;
263 goto end;
264 }
265
bc37ae52
JG
266 ret = bt_ctf_trace_set_byte_order(writer->trace,
267 byte_order);
273b65be
JG
268end:
269 return ret;
270}
271
272void bt_ctf_writer_get(struct bt_ctf_writer *writer)
273{
de3dd40e 274 bt_ctf_get(writer);
273b65be
JG
275}
276
277void bt_ctf_writer_put(struct bt_ctf_writer *writer)
278{
de3dd40e 279 bt_ctf_put(writer);
273b65be
JG
280}
281
273b65be
JG
282static
283int create_stream_file(struct bt_ctf_writer *writer,
284 struct bt_ctf_stream *stream)
285{
286 int fd;
287 GString *filename = g_string_new(stream->stream_class->name->str);
288
3ea33115
JG
289 if (stream->stream_class->name->len == 0) {
290 int64_t ret;
291
292 ret = bt_ctf_stream_class_get_id(stream->stream_class);
293 if (ret < 0) {
294 fd = -1;
295 goto error;
296 }
297
298 g_string_printf(filename, "stream_%" PRId64, ret);
299 }
300
273b65be
JG
301 g_string_append_printf(filename, "_%" PRIu32, stream->id);
302 fd = openat(writer->trace_dir_fd, filename->str,
303 O_RDWR | O_CREAT | O_TRUNC,
304 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3ea33115 305error:
273b65be
JG
306 g_string_free(filename, TRUE);
307 return fd;
308}
This page took 0.040654 seconds and 4 git commands to generate.