Fix: standardize man pages building/installing
[lttng-tools.git] / src / lib / lttng-ctl / filter / memstream.h
CommitLineData
53a80697
MD
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
53a80697
MD
22#ifdef LTTNG_HAVE_FMEMOPEN
23#include <stdio.h>
24
25static inline
26FILE *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 */
40static inline
41FILE *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) {
6f04ed72 73 PERROR("fseek");
53a80697
MD
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) {
6f04ed72 79 PERROR("unlink");
53a80697
MD
80 }
81 return fp;
82
83error_close:
84 ret = fclose(fp);
85 if (ret < 0) {
6f04ed72 86 PERROR("close");
53a80697
MD
87 }
88error_unlink:
89 ret = unlink(tmpname);
90 if (ret < 0) {
6f04ed72 91 PERROR("unlink");
53a80697
MD
92 }
93 return NULL;
94}
95
96#endif /* LTTNG_HAVE_FMEMOPEN */
97
98#ifdef LTTNG_HAVE_OPEN_MEMSTREAM
99
100#include <stdio.h>
101
102static inline
103FILE *lttng_open_memstream(char **ptr, size_t *sizeloc)
104{
105 return open_memstream(ptr, sizeloc);
106}
107
108static inline
109int 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 */
125static inline
126FILE *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) {
6f04ed72 148 PERROR("unlink");
53a80697
MD
149 }
150 return fp;
151
152error_unlink:
153 ret = unlink(tmpname);
154 if (ret < 0) {
6f04ed72 155 PERROR("unlink");
53a80697
MD
156 }
157 return NULL;
158}
159
160/* Get file size, allocate buffer, copy. */
161static inline
162int 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) {
6f04ed72 170 PERROR("fflush");
53a80697
MD
171 return ret;
172 }
173 ret = fseek(fp, 0L, SEEK_END);
174 if (ret < 0) {
6f04ed72 175 PERROR("fseek");
53a80697
MD
176 return ret;
177 }
178 pos = ftell(fp);
179 if (ret < 0) {
6f04ed72 180 PERROR("ftell");
53a80697
MD
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) {
6f04ed72 191 PERROR("fseek");
53a80697
MD
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) {
6f04ed72 207 PERROR("fclose");
53a80697
MD
208 return ret;
209 }
210 return 0;
211
212error_close:
213 ret = fclose(fp);
214 if (ret < 0) {
6f04ed72 215 PERROR("fclose");
53a80697
MD
216 }
217error_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.052581 seconds and 5 git commands to generate.