summaryrefslogtreecommitdiffstats
path: root/src/basic/memstream-util.c
blob: 4e147fd78f52780d407171ac5618e708602e3b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "memstream-util.h"

void memstream_done(MemStream *m) {
        assert(m);

        /* First, close file stream, as the buffer may be reallocated on close. */
        safe_fclose(m->f);

        /* Then, free buffer. */
        free(m->buf);
}

FILE* memstream_init(MemStream *m) {
        assert(m);
        assert(!m->f);

        m->f = open_memstream_unlocked(&m->buf, &m->sz);
        return m->f;
}

int memstream_finalize(MemStream *m, char **ret_buf, size_t *ret_size) {
        int r;

        assert(m);
        assert(m->f);
        assert(ret_buf);

        /* Add terminating NUL, so that the output buffer is a valid string. */
        fputc('\0', m->f);

        r = fflush_and_check(m->f);
        if (r < 0)
                return r;

        m->f = safe_fclose(m->f);

        /* On fclose(), the buffer may be reallocated, and may trigger OOM. */
        if (!m->buf)
                return -ENOMEM;

        assert(m->sz > 0);

        *ret_buf = TAKE_PTR(m->buf);
        if (ret_size)
                *ret_size = m->sz - 1;

        m->sz = 0; /* For safety when the MemStream object will be reused later. */
        return 0;
}

int memstream_dump_internal(
                int level,
                int error,
                const char *file,
                int line,
                const char *func,
                MemStream *m) {

        _cleanup_free_ char *buf = NULL;
        int r;

        assert(m);

        r = memstream_finalize(m, &buf, NULL);
        if (r < 0)
                return log_full_errno(level, r, "Failed to flush memstream: %m: %m");

        return log_dump_internal(level, error, file, line, func, buf);
}