compat send no SIGPIPE: multithread-safe
[babeltrace.git] / include / babeltrace / compat / memstream.h
CommitLineData
f8370579
MD
1#ifndef _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
2#define _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
3
4/*
5 * format/ctf/memstream.h
6 *
7 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * memstream compatibility layer.
10 *
eb31c5e6
MD
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
f8370579 17 *
eb31c5e6
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
c462e188
MD
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
f8370579
MD
28 */
29
f8370579
MD
30#ifdef BABELTRACE_HAVE_FMEMOPEN
31#include <stdio.h>
32
33static inline
34FILE *babeltrace_fmemopen(void *buf, size_t size, const char *mode)
35{
36 return fmemopen(buf, size, mode);
37}
38
39#else /* BABELTRACE_HAVE_FMEMOPEN */
40
41#include <stdlib.h>
42#include <stdio.h>
43
44/*
45 * Fallback for systems which don't have fmemopen. Copy buffer to a
46 * temporary file, and use that file as FILE * input.
47 */
48static inline
49FILE *babeltrace_fmemopen(void *buf, size_t size, const char *mode)
50{
51 char tmpname[PATH_MAX];
52 size_t len;
53 FILE *fp;
54 int ret;
55
56 /*
57 * Support reading only.
58 */
59 if (strcmp(mode, "rb") != 0) {
60 return NULL;
61 }
62 strncpy(tmpname, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX);
63 ret = mkstemp(tmpname);
64 if (ret < 0) {
65 return NULL;
66 }
67 /*
68 * We need to write to the file.
69 */
70 fp = fdopen(ret, "w+");
71 if (!fp) {
72 goto error_unlink;
73 }
74 /* Copy the entire buffer to the file */
75 len = fwrite(buf, sizeof(char), size, fp);
76 if (len != size) {
77 goto error_close;
78 }
79 ret = fseek(fp, 0L, SEEK_SET);
80 if (ret < 0) {
81 perror("fseek");
82 goto error_close;
83 }
84 /* We keep the handle open, but can unlink the file on the VFS. */
85 ret = unlink(tmpname);
86 if (ret < 0) {
87 perror("unlink");
88 }
89 return fp;
90
91error_close:
92 ret = fclose(fp);
93 if (ret < 0) {
94 perror("close");
95 }
96error_unlink:
97 ret = unlink(tmpname);
98 if (ret < 0) {
99 perror("unlink");
100 }
101 return NULL;
102}
103
104#endif /* BABELTRACE_HAVE_FMEMOPEN */
105
106#ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
107
108#include <stdio.h>
109
110static inline
111FILE *babeltrace_open_memstream(char **ptr, size_t *sizeloc)
112{
113 return open_memstream(ptr, sizeloc);
114}
115
116static inline
117int babeltrace_close_memstream(char **buf, size_t *size, FILE *fp)
118{
119 return fclose(fp);
120}
121
122#else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
123
124#include <stdlib.h>
125#include <stdio.h>
126
127/*
128 * Fallback for systems which don't have open_memstream. Create FILE *
129 * with babeltrace_open_memstream, but require call to
130 * babeltrace_close_memstream to flush all data written to the FILE *
131 * into the buffer (which we allocate).
132 */
133static inline
134FILE *babeltrace_open_memstream(char **ptr, size_t *sizeloc)
135{
136 char tmpname[PATH_MAX];
137 int ret;
138 FILE *fp;
139
140 strncpy(tmpname, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX);
141 ret = mkstemp(tmpname);
142 if (ret < 0) {
143 return NULL;
144 }
145 fp = fdopen(ret, "w+");
146 if (!fp) {
147 goto error_unlink;
148 }
149 /*
150 * babeltrace_flush_memstream will update the buffer content
151 * with read from fp. No need to keep the file around, just the
152 * handle.
153 */
154 ret = unlink(tmpname);
155 if (ret < 0) {
156 perror("unlink");
157 }
158 return fp;
159
160error_unlink:
161 ret = unlink(tmpname);
162 if (ret < 0) {
163 perror("unlink");
164 }
165 return NULL;
166}
167
168/* Get file size, allocate buffer, copy. */
169static inline
170int babeltrace_close_memstream(char **buf, size_t *size, FILE *fp)
171{
172 size_t len, n;
173 long pos;
174 int ret;
175
176 ret = fflush(fp);
177 if (ret < 0) {
178 perror("fflush");
179 return ret;
180 }
181 ret = fseek(fp, 0L, SEEK_END);
182 if (ret < 0) {
183 perror("fseek");
184 return ret;
185 }
186 pos = ftell(fp);
187 if (ret < 0) {
188 perror("ftell");
189 return ret;
190 }
191 *size = pos;
192 /* add final \0 */
193 *buf = calloc(pos + 1, sizeof(char));
194 if (!*buf) {
195 return -ENOMEM;
196 }
197 ret = fseek(fp, 0L, SEEK_SET);
198 if (ret < 0) {
199 perror("fseek");
200 goto error_free;
201 }
202 /* Copy the entire file into the buffer */
203 n = 0;
204 clearerr(fp);
205 while (!feof(fp) && !ferror(fp) && (*size - n > 0)) {
206 len = fread(*buf, sizeof(char), *size - n, fp);
207 n += len;
208 }
209 if (n != *size) {
210 ret = -1;
211 goto error_close;
212 }
213 ret = fclose(fp);
214 if (ret < 0) {
215 perror("fclose");
216 return ret;
217 }
218 return 0;
219
220error_close:
221 ret = fclose(fp);
222 if (ret < 0) {
223 perror("fclose");
224 }
225error_free:
226 free(*buf);
227 *buf = NULL;
228 return ret;
229}
230
231#endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
232
233#endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.043628 seconds and 4 git commands to generate.