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.
25 #ifdef LTTNG_HAVE_FMEMOPEN
29 FILE *lttng_fmemopen(void *buf
, size_t size
, const char *mode
)
31 return fmemopen(buf
, size
, mode
);
34 #else /* LTTNG_HAVE_FMEMOPEN */
40 * Fallback for systems which don't have fmemopen. Copy buffer to a
41 * temporary file, and use that file as FILE * input.
44 FILE *lttng_fmemopen(void *buf
, size_t size
, const char *mode
)
46 char tmpname
[PATH_MAX
];
52 * Support reading only.
54 if (strcmp(mode
, "rb") != 0) {
57 strncpy(tmpname
, "/tmp/lttng-tmp-XXXXXX", PATH_MAX
);
58 ret
= mkstemp(tmpname
);
63 * We need to write to the file.
65 fp
= fdopen(ret
, "w+");
69 /* Copy the entire buffer to the file */
70 len
= fwrite(buf
, sizeof(char), size
, fp
);
74 ret
= fseek(fp
, 0L, SEEK_SET
);
79 /* We keep the handle open, but can unlink the file on the VFS. */
80 ret
= unlink(tmpname
);
92 ret
= unlink(tmpname
);
99 #endif /* LTTNG_HAVE_FMEMOPEN */
101 #ifdef LTTNG_HAVE_OPEN_MEMSTREAM
106 FILE *lttng_open_memstream(char **ptr
, size_t *sizeloc
)
108 return open_memstream(ptr
, sizeloc
);
112 int lttng_close_memstream(char **buf
, size_t *size
, FILE *fp
)
117 #else /* LTTNG_HAVE_OPEN_MEMSTREAM */
123 * Fallback for systems which don't have open_memstream. Create FILE *
124 * with lttng_open_memstream, but require call to
125 * lttng_close_memstream to flush all data written to the FILE *
126 * into the buffer (which we allocate).
129 FILE *lttng_open_memstream(char **ptr
, size_t *sizeloc
)
131 char tmpname
[PATH_MAX
];
135 strncpy(tmpname
, "/tmp/lttng-tmp-XXXXXX", PATH_MAX
);
136 ret
= mkstemp(tmpname
);
140 fp
= fdopen(ret
, "w+");
145 * lttng_flush_memstream will update the buffer content
146 * with read from fp. No need to keep the file around, just the
149 ret
= unlink(tmpname
);
156 ret
= unlink(tmpname
);
163 /* Get file size, allocate buffer, copy. */
165 int lttng_close_memstream(char **buf
, size_t *size
, FILE *fp
)
176 ret
= fseek(fp
, 0L, SEEK_END
);
188 *buf
= calloc(pos
+ 1, sizeof(char));
192 ret
= fseek(fp
, 0L, SEEK_SET
);
197 /* Copy the entire file into the buffer */
200 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
201 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
226 #endif /* LTTNG_HAVE_OPEN_MEMSTREAM */
228 #endif /* _LTTNG_CTL_MEMSTREAM_H */
This page took 0.034996 seconds and 5 git commands to generate.