1 #ifndef _LTTNG_CTL_MEMSTREAM_H
2 #define _LTTNG_CTL_MEMSTREAM_H
5 * src/lib/lttng-ctl/memstream.h
7 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * memstream compatibility layer.
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
22 #ifdef LTTNG_HAVE_FMEMOPEN
26 FILE *lttng_fmemopen(void *buf
, size_t size
, const char *mode
)
28 return fmemopen(buf
, size
, mode
);
31 #else /* LTTNG_HAVE_FMEMOPEN */
37 * Fallback for systems which don't have fmemopen. Copy buffer to a
38 * temporary file, and use that file as FILE * input.
41 FILE *lttng_fmemopen(void *buf
, size_t size
, const char *mode
)
43 char tmpname
[PATH_MAX
];
49 * Support reading only.
51 if (strcmp(mode
, "rb") != 0) {
54 strncpy(tmpname
, "/tmp/lttng-tmp-XXXXXX", PATH_MAX
);
55 ret
= mkstemp(tmpname
);
60 * We need to write to the file.
62 fp
= fdopen(ret
, "w+");
66 /* Copy the entire buffer to the file */
67 len
= fwrite(buf
, sizeof(char), size
, fp
);
71 ret
= fseek(fp
, 0L, SEEK_SET
);
76 /* We keep the handle open, but can unlink the file on the VFS. */
77 ret
= unlink(tmpname
);
89 ret
= unlink(tmpname
);
96 #endif /* LTTNG_HAVE_FMEMOPEN */
98 #ifdef LTTNG_HAVE_OPEN_MEMSTREAM
103 FILE *lttng_open_memstream(char **ptr
, size_t *sizeloc
)
105 return open_memstream(ptr
, sizeloc
);
109 int lttng_close_memstream(char **buf
, size_t *size
, FILE *fp
)
114 #else /* LTTNG_HAVE_OPEN_MEMSTREAM */
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).
126 FILE *lttng_open_memstream(char **ptr
, size_t *sizeloc
)
128 char tmpname
[PATH_MAX
];
132 strncpy(tmpname
, "/tmp/lttng-tmp-XXXXXX", PATH_MAX
);
133 ret
= mkstemp(tmpname
);
137 fp
= fdopen(ret
, "w+");
142 * lttng_flush_memstream will update the buffer content
143 * with read from fp. No need to keep the file around, just the
146 ret
= unlink(tmpname
);
153 ret
= unlink(tmpname
);
160 /* Get file size, allocate buffer, copy. */
162 int lttng_close_memstream(char **buf
, size_t *size
, FILE *fp
)
173 ret
= fseek(fp
, 0L, SEEK_END
);
185 *buf
= calloc(pos
+ 1, sizeof(char));
189 ret
= fseek(fp
, 0L, SEEK_SET
);
194 /* Copy the entire file into the buffer */
197 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
198 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
223 #endif /* LTTNG_HAVE_OPEN_MEMSTREAM */
225 #endif /* _LTTNG_CTL_MEMSTREAM_H */
This page took 0.032786 seconds and 4 git commands to generate.