Add trace packet header accessors and support custom headers
[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>
273b65be
JG
36#include <babeltrace/compiler.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <sys/stat.h>
40#include <errno.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <inttypes.h>
44
273b65be
JG
45static
46void bt_ctf_writer_destroy(struct bt_ctf_ref *ref);
47static
273b65be
JG
48int create_stream_file(struct bt_ctf_writer *writer,
49 struct bt_ctf_stream *stream);
273b65be 50
273b65be
JG
51struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
52{
53 struct bt_ctf_writer *writer = NULL;
54
55 if (!path) {
56 goto error;
57 }
58
59 writer = g_new0(struct bt_ctf_writer, 1);
60 if (!writer) {
61 goto error;
62 }
63
273b65be
JG
64 bt_ctf_ref_init(&writer->ref_count);
65 writer->path = g_string_new(path);
66 if (!writer->path) {
67 goto error_destroy;
68 }
69
bc37ae52
JG
70 writer->trace = bt_ctf_trace_create();
71 if (!writer->trace) {
72 goto error_destroy;
73 }
74
273b65be
JG
75 /* Create trace directory if necessary and open a metadata file */
76 if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) {
77 perror("g_mkdir_with_parents");
78 goto error_destroy;
79 }
80
1b8180b9 81 writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
273b65be
JG
82 if (writer->trace_dir_fd < 0) {
83 perror("open");
84 goto error_destroy;
85 }
86
87 writer->metadata_fd = openat(writer->trace_dir_fd, "metadata",
88 O_WRONLY | O_CREAT | O_TRUNC,
89 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
273b65be
JG
90
91 return writer;
bc37ae52 92
273b65be
JG
93error_destroy:
94 unlinkat(writer->trace_dir_fd, "metadata", 0);
95 bt_ctf_writer_destroy(&writer->ref_count);
96 writer = NULL;
97error:
98 return writer;
99}
100
101void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
102{
103 struct bt_ctf_writer *writer;
104
105 if (!ref) {
106 return;
107 }
108
109 writer = container_of(ref, struct bt_ctf_writer, ref_count);
110 bt_ctf_writer_flush_metadata(writer);
111 if (writer->path) {
112 g_string_free(writer->path, TRUE);
113 }
114
115 if (writer->trace_dir_fd > 0) {
9bb7e58b
MD
116 if (close(writer->trace_dir_fd)) {
117 perror("close");
9bb7e58b 118 }
273b65be
JG
119 }
120
121 if (writer->metadata_fd > 0) {
9bb7e58b
MD
122 if (close(writer->metadata_fd)) {
123 perror("close");
9bb7e58b 124 }
273b65be
JG
125 }
126
bc37ae52 127 bt_ctf_trace_put(writer->trace);
273b65be
JG
128 g_free(writer);
129}
130
a2540e85
JG
131struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
132{
133 struct bt_ctf_trace *trace = NULL;
134
135 if (!writer) {
136 goto end;
137 }
138
139 trace = writer->trace;
140 bt_ctf_trace_get(trace);
141end:
142 return trace;
143}
144
273b65be
JG
145struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
146 struct bt_ctf_stream_class *stream_class)
147{
273b65be
JG
148 int stream_fd;
149 struct bt_ctf_stream *stream = NULL;
150
151 if (!writer || !stream_class) {
152 goto error;
153 }
154
bc37ae52 155 stream = bt_ctf_trace_create_stream(writer->trace, stream_class);
273b65be
JG
156 if (!stream) {
157 goto error;
158 }
159
160 stream_fd = create_stream_file(writer, stream);
161 if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) {
162 goto error;
163 }
164
273b65be
JG
165 writer->frozen = 1;
166 return stream;
bc37ae52 167
273b65be
JG
168error:
169 bt_ctf_stream_put(stream);
170 return NULL;
171}
172
173int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
174 const char *name,
175 const char *value)
176{
bc37ae52 177 int ret = -1;
273b65be 178
bc37ae52
JG
179 if (!writer || !name || !value) {
180 goto end;
273b65be
JG
181 }
182
bc37ae52
JG
183 ret = bt_ctf_trace_add_environment_field(writer->trace,
184 name, value);
185end:
273b65be
JG
186 return ret;
187}
188
189int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
190 struct bt_ctf_clock *clock)
191{
bc37ae52 192 int ret = -1;
273b65be
JG
193
194 if (!writer || !clock) {
12af6048
JG
195 goto end;
196 }
273b65be 197
bc37ae52 198 ret = bt_ctf_trace_add_clock(writer->trace, clock);
12af6048
JG
199end:
200 return ret;
273b65be
JG
201}
202
273b65be
JG
203char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
204{
bc37ae52 205 char *metadata_string = NULL;
273b65be
JG
206
207 if (!writer) {
208 goto end;
209 }
210
bc37ae52
JG
211 metadata_string = bt_ctf_trace_get_metadata_string(
212 writer->trace);
273b65be 213end:
bc37ae52 214 return metadata_string;
273b65be
JG
215}
216
217void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
218{
219 int ret;
220 char *metadata_string = NULL;
221
222 if (!writer) {
223 goto end;
224 }
225
bc37ae52
JG
226 metadata_string = bt_ctf_trace_get_metadata_string(
227 writer->trace);
273b65be
JG
228 if (!metadata_string) {
229 goto end;
230 }
231
232 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
233 perror("lseek");
234 goto end;
235 }
236
237 if (ftruncate(writer->metadata_fd, 0)) {
238 perror("ftruncate");
239 goto end;
240 }
241
242 ret = write(writer->metadata_fd, metadata_string,
243 strlen(metadata_string));
244 if (ret < 0) {
245 perror("write");
246 goto end;
247 }
248end:
249 g_free(metadata_string);
250}
251
252int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
253 enum bt_ctf_byte_order byte_order)
254{
255 int ret = 0;
273b65be
JG
256
257 if (!writer || writer->frozen) {
258 ret = -1;
259 goto end;
260 }
261
bc37ae52
JG
262 ret = bt_ctf_trace_set_byte_order(writer->trace,
263 byte_order);
273b65be
JG
264end:
265 return ret;
266}
267
268void bt_ctf_writer_get(struct bt_ctf_writer *writer)
269{
270 if (!writer) {
271 return;
272 }
273
274 bt_ctf_ref_get(&writer->ref_count);
275}
276
277void bt_ctf_writer_put(struct bt_ctf_writer *writer)
278{
279 if (!writer) {
280 return;
281 }
282
283 bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy);
284}
285
273b65be
JG
286static
287int create_stream_file(struct bt_ctf_writer *writer,
288 struct bt_ctf_stream *stream)
289{
290 int fd;
291 GString *filename = g_string_new(stream->stream_class->name->str);
292
293 g_string_append_printf(filename, "_%" PRIu32, stream->id);
294 fd = openat(writer->trace_dir_fd, filename->str,
295 O_RDWR | O_CREAT | O_TRUNC,
296 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
297 g_string_free(filename, TRUE);
298 return fd;
299}
This page took 0.038446 seconds and 4 git commands to generate.