Remove unnecessary inclusions of version.h
[lttng-tools.git] / src / lib / lttng-ctl / filter / memstream.h
1 #ifndef _LTTNG_CTL_MEMSTREAM_H
2 #define _LTTNG_CTL_MEMSTREAM_H
3
4 /*
5 * src/lib/lttng-ctl/memstream.h
6 *
7 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * memstream compatibility layer.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22 #ifdef LTTNG_HAVE_FMEMOPEN
23 #include <stdio.h>
24
25 static inline
26 FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
27 {
28 return fmemopen(buf, size, mode);
29 }
30
31 #else /* LTTNG_HAVE_FMEMOPEN */
32
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 /*
37 * Fallback for systems which don't have fmemopen. Copy buffer to a
38 * temporary file, and use that file as FILE * input.
39 */
40 static inline
41 FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
42 {
43 char tmpname[PATH_MAX];
44 size_t len;
45 FILE *fp;
46 int ret;
47
48 /*
49 * Support reading only.
50 */
51 if (strcmp(mode, "rb") != 0) {
52 return NULL;
53 }
54 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
55 ret = mkstemp(tmpname);
56 if (ret < 0) {
57 return NULL;
58 }
59 /*
60 * We need to write to the file.
61 */
62 fp = fdopen(ret, "w+");
63 if (!fp) {
64 goto error_unlink;
65 }
66 /* Copy the entire buffer to the file */
67 len = fwrite(buf, sizeof(char), size, fp);
68 if (len != size) {
69 goto error_close;
70 }
71 ret = fseek(fp, 0L, SEEK_SET);
72 if (ret < 0) {
73 PERROR("fseek");
74 goto error_close;
75 }
76 /* We keep the handle open, but can unlink the file on the VFS. */
77 ret = unlink(tmpname);
78 if (ret < 0) {
79 PERROR("unlink");
80 }
81 return fp;
82
83 error_close:
84 ret = fclose(fp);
85 if (ret < 0) {
86 PERROR("close");
87 }
88 error_unlink:
89 ret = unlink(tmpname);
90 if (ret < 0) {
91 PERROR("unlink");
92 }
93 return NULL;
94 }
95
96 #endif /* LTTNG_HAVE_FMEMOPEN */
97
98 #ifdef LTTNG_HAVE_OPEN_MEMSTREAM
99
100 #include <stdio.h>
101
102 static inline
103 FILE *lttng_open_memstream(char **ptr, size_t *sizeloc)
104 {
105 return open_memstream(ptr, sizeloc);
106 }
107
108 static inline
109 int lttng_close_memstream(char **buf, size_t *size, FILE *fp)
110 {
111 return fclose(fp);
112 }
113
114 #else /* LTTNG_HAVE_OPEN_MEMSTREAM */
115
116 #include <stdlib.h>
117 #include <stdio.h>
118
119 /*
120 * Fallback for systems which don't have open_memstream. Create FILE *
121 * with lttng_open_memstream, but require call to
122 * lttng_close_memstream to flush all data written to the FILE *
123 * into the buffer (which we allocate).
124 */
125 static inline
126 FILE *lttng_open_memstream(char **ptr, size_t *sizeloc)
127 {
128 char tmpname[PATH_MAX];
129 int ret;
130 FILE *fp;
131
132 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
133 ret = mkstemp(tmpname);
134 if (ret < 0) {
135 return NULL;
136 }
137 fp = fdopen(ret, "w+");
138 if (!fp) {
139 goto error_unlink;
140 }
141 /*
142 * lttng_flush_memstream will update the buffer content
143 * with read from fp. No need to keep the file around, just the
144 * handle.
145 */
146 ret = unlink(tmpname);
147 if (ret < 0) {
148 PERROR("unlink");
149 }
150 return fp;
151
152 error_unlink:
153 ret = unlink(tmpname);
154 if (ret < 0) {
155 PERROR("unlink");
156 }
157 return NULL;
158 }
159
160 /* Get file size, allocate buffer, copy. */
161 static inline
162 int lttng_close_memstream(char **buf, size_t *size, FILE *fp)
163 {
164 size_t len, n;
165 long pos;
166 int ret;
167
168 ret = fflush(fp);
169 if (ret < 0) {
170 PERROR("fflush");
171 return ret;
172 }
173 ret = fseek(fp, 0L, SEEK_END);
174 if (ret < 0) {
175 PERROR("fseek");
176 return ret;
177 }
178 pos = ftell(fp);
179 if (ret < 0) {
180 PERROR("ftell");
181 return ret;
182 }
183 *size = pos;
184 /* add final \0 */
185 *buf = calloc(pos + 1, sizeof(char));
186 if (!*buf) {
187 return -ENOMEM;
188 }
189 ret = fseek(fp, 0L, SEEK_SET);
190 if (ret < 0) {
191 PERROR("fseek");
192 goto error_free;
193 }
194 /* Copy the entire file into the buffer */
195 n = 0;
196 clearerr(fp);
197 while (!feof(fp) && !ferror(fp) && (*size - n > 0)) {
198 len = fread(*buf, sizeof(char), *size - n, fp);
199 n += len;
200 }
201 if (n != *size) {
202 ret = -1;
203 goto error_close;
204 }
205 ret = fclose(fp);
206 if (ret < 0) {
207 PERROR("fclose");
208 return ret;
209 }
210 return 0;
211
212 error_close:
213 ret = fclose(fp);
214 if (ret < 0) {
215 PERROR("fclose");
216 }
217 error_free:
218 free(*buf);
219 *buf = NULL;
220 return ret;
221 }
222
223 #endif /* LTTNG_HAVE_OPEN_MEMSTREAM */
224
225 #endif /* _LTTNG_CTL_MEMSTREAM_H */
This page took 0.049644 seconds and 4 git commands to generate.