Coverage Report

Created: 2025-07-04 07:08

/src/fluent-bit/lib/chunkio/src/cio_memfs.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Chunk I/O
4
 *  =========
5
 *  Copyright 2018 Eduardo Silva <eduardo@monkey.io>
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <chunkio/chunkio.h>
21
#include <chunkio/cio_utils.h>
22
#include <chunkio/chunkio_compat.h>
23
#include <chunkio/cio_memfs.h>
24
#include <chunkio/cio_log.h>
25
26
#include <stdio.h>
27
#include <string.h>
28
#include <limits.h>
29
30
struct cio_memfs *cio_memfs_open(struct cio_ctx *ctx, struct cio_stream *st,
31
                                 struct cio_chunk *ch, int flags,
32
                                 size_t size)
33
0
{
34
0
    struct cio_memfs *mf;
35
36
0
    (void) flags;
37
0
    (void) ctx;
38
0
    (void) ch;
39
0
    (void) st;
40
41
0
    mf = calloc(1, sizeof(struct cio_memfs));
42
0
    if (!mf) {
43
0
        cio_errno();
44
0
        return NULL;
45
0
    }
46
0
    mf->crc_cur = cio_crc32_init();
47
48
0
    mf->buf_data = malloc(size);
49
0
    if (!mf->buf_data) {
50
0
        cio_errno();
51
0
        free(mf->name);
52
0
        free(mf);
53
0
        return NULL;
54
0
    }
55
0
    mf->buf_size = size;
56
0
    mf->buf_len = 0;
57
0
    if (ctx->realloc_size_hint > 0) {
58
0
        mf->realloc_size = ctx->realloc_size_hint;
59
0
    } else {
60
0
        mf->realloc_size = cio_getpagesize() * 8;
61
0
    }
62
63
0
    return mf;
64
0
}
65
66
void cio_memfs_close(struct cio_chunk *ch)
67
0
{
68
0
    struct cio_memfs *mf = ch->backend;
69
70
0
    if (!mf) {
71
0
        return;
72
0
    }
73
74
0
    free(mf->name);
75
0
    free(mf->buf_data);
76
0
    free(mf->meta_data);
77
0
    free(mf);
78
0
}
79
80
int cio_memfs_write(struct cio_chunk *ch, const void *buf, size_t count)
81
0
{
82
0
    size_t av_size;
83
0
    size_t new_size;
84
0
    char *tmp;
85
0
    struct cio_memfs *mf = ch->backend;
86
87
0
    if (count == 0) {
88
0
        return 0;
89
0
    }
90
91
    /* Calculate available size */
92
0
    av_size = (mf->buf_size - mf->buf_len);
93
0
    if (av_size < count) {
94
95
        /* Suggest initial new size */
96
0
        new_size = mf->buf_size + mf->realloc_size;
97
0
        while (new_size < (mf->buf_len + count)) {
98
0
            new_size += mf->realloc_size;
99
0
        }
100
101
0
        tmp = realloc(mf->buf_data, new_size);
102
0
        if (!tmp) {
103
0
            cio_errno();
104
0
            return -1;
105
0
        }
106
107
0
        mf->buf_data = tmp;
108
0
        mf->buf_size = new_size;
109
0
    }
110
111
0
    memcpy(mf->buf_data + mf->buf_len, buf, count);
112
0
    mf->buf_len += count;
113
114
0
    return 0;
115
0
}
116
117
int cio_memfs_content_copy(struct cio_chunk *ch,
118
                           void **out_buf, size_t *out_size)
119
0
{
120
0
    char *buf;
121
0
    struct cio_memfs *mf = ch->backend;
122
123
0
    buf = malloc(mf->buf_len + 1);
124
0
    if (!buf) {
125
0
        cio_errno();
126
0
        return -1;
127
0
    }
128
129
    /* Copy the data and append an extra NULL byte */
130
0
    memcpy(buf, mf->buf_data, mf->buf_len);
131
0
    buf[mf->buf_len] = '\0';
132
133
0
    *out_buf = buf;
134
0
    *out_size = mf->buf_len;
135
136
0
    return 0;
137
0
}
138
139
void cio_memfs_scan_dump(struct cio_ctx *ctx, struct cio_stream *st)
140
0
{
141
0
    char tmp[PATH_MAX];
142
0
    struct mk_list *head;
143
0
    struct cio_memfs *mf;
144
0
    struct cio_chunk *ch;
145
146
0
    (void) ctx;
147
148
0
    mk_list_foreach(head, &st->chunks) {
149
0
        ch = mk_list_entry(head, struct cio_chunk, _head);
150
0
        mf = ch->backend;
151
152
0
        snprintf(tmp, sizeof(tmp) -1, "%s/%s", ch->st->name, ch->name);
153
0
        printf("        %-60s", tmp);
154
0
        printf("meta_len=%i, data_size=%zu\n", mf->meta_len, mf->buf_len);
155
0
    }
156
0
}