summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/chunkio/src/chunkio.c
blob: a69325cfe9e5c9391f71cf6a0c2bcb23040d9794 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Chunk I/O
 *  =========
 *  Copyright 2018-2019 Eduardo Silva <eduardo@monkey.io>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <chunkio/chunkio.h>
#include <chunkio/chunkio_compat.h>
#include <chunkio/cio_os.h>
#include <chunkio/cio_log.h>
#include <chunkio/cio_file.h>
#include <chunkio/cio_stream.h>
#include <chunkio/cio_scan.h>
#include <chunkio/cio_utils.h>

#include <monkey/mk_core/mk_list.h>

/*
 * Validate if root_path exists, if don't, create it, otherwise
 * check if we have write access to it.
 */
static int check_root_path(struct cio_ctx *ctx, const char *root_path)
{
    int ret;
    int len;

    if (!root_path) {
        return -1;
    }

    len = strlen(root_path);
    if (len <= 0) {
        return -1;
    }

    ret = cio_os_isdir(root_path);
    if (ret == -1) {
        /* Try to create the path */
        ret = cio_os_mkpath(root_path, 0755);
        if (ret == -1) {
            return -1;
        }
        cio_log_info(ctx, "created root path %s", root_path);
        return 0;
    }

    /* Directory already exists, check write access */
    return access(root_path, W_OK);
}

void cio_options_init(struct cio_options *options)
{
    memset(options, 0, sizeof(struct cio_options));

    options->initialized = CIO_INITIALIZED;

    options->root_path = NULL;
    options->user = NULL;
    options->group = NULL;
    options->chmod = NULL;
    options->log_cb = NULL;
    options->log_level = CIO_LOG_INFO;
    options->flags = CIO_OPEN_RW;
    options->realloc_size_hint = CIO_DISABLE_REALLOC_HINT;
}

struct cio_ctx *cio_create(struct cio_options *options)
{
    int ret;
    struct cio_ctx *ctx;
    struct cio_options default_options;

    if (options == NULL) {
        cio_options_init(&default_options);
        options = &default_options;
    }
    else {
        if (options->initialized != CIO_INITIALIZED) {
            /* the caller 'must' call cio_options_init() or pass NULL before creating a context */
            fprintf(stderr, "[cio] 'options' has not been initialized properly\n");
            return NULL;
        }
    }
    
    /* sanitize chunk open flags */
    if (!(options->flags & CIO_OPEN_RW) && !(options->flags & CIO_OPEN_RD)) {
        options->flags |= CIO_OPEN_RW;
    }

    if (options->log_level < CIO_LOG_ERROR ||
        options->log_level > CIO_LOG_TRACE) {
        fprintf(stderr, "[cio] invalid log level, aborting\n");
        return NULL;
    }
#ifndef CIO_HAVE_BACKEND_FILESYSTEM
    if (root_path) {
        fprintf(stderr, "[cio] file system backend not supported\n");
        return NULL;
    }
#endif

    /* Create context */
    ctx = calloc(1, sizeof(struct cio_ctx));
    if (!ctx) {
        perror("calloc");
        return NULL;
    }
    mk_list_init(&ctx->streams);
    ctx->page_size = cio_getpagesize();
    ctx->max_chunks_up = CIO_MAX_CHUNKS_UP;
    ctx->options.flags = options->flags;
    ctx->realloc_size_hint = CIO_DISABLE_REALLOC_HINT;

    if (options->user != NULL) {
        ctx->options.user = strdup(options->user);
    }

    if (options->group != NULL) {
        ctx->options.group = strdup(options->group);
    }

    if (options->chmod != NULL) {
        ctx->options.chmod = strdup(options->chmod);
    }

    /* Counters */
    ctx->total_chunks = 0;
    ctx->total_chunks_up = 0;

    /* Logging */
    cio_set_log_callback(ctx, options->log_cb);
    cio_set_log_level(ctx, options->log_level);

    /* Check or initialize file system root path */
    if (options->root_path) {
        ret = check_root_path(ctx, options->root_path);
        if (ret == -1) {
            cio_log_error(ctx,
                          "[chunkio] cannot initialize root path %s\n",
                          options->root_path);
            free(ctx);
            return NULL;
        }

        ctx->options.root_path = strdup(options->root_path);
    }
    else {
        ctx->options.root_path = NULL;
    }

    if (ctx->options.user != NULL) {
        ret = cio_file_lookup_user(ctx->options.user, &ctx->processed_user);

        if (ret != CIO_OK) {
            cio_destroy(ctx);

            return NULL;
        }
    }
    else {
        ctx->processed_user = NULL;
    }

    if (ctx->options.group != NULL) {
        ret = cio_file_lookup_group(ctx->options.group, &ctx->processed_group);

        if (ret != CIO_OK) {
            cio_destroy(ctx);

            return NULL;
        }
    }
    else {
        ctx->processed_group = NULL;
    }

    if (options->realloc_size_hint > 0) {
        ret = cio_set_realloc_size_hint(ctx, options->realloc_size_hint);
        if (ret == -1) {
            cio_log_error(ctx,
                          "[chunkio] cannot initialize with realloc size hint %d\n",
                          options->realloc_size_hint);
            cio_destroy(ctx);

            return NULL;
        }
    }

    return ctx;
}

int cio_load(struct cio_ctx *ctx, char *chunk_extension)
{
    int ret;

    if (ctx->options.root_path) {
        ret = cio_scan_streams(ctx, chunk_extension);
        return ret;
    }

    return 0;
}

static int qsort_stream(struct cio_stream *stream,
                        int (*compar)(const void *, const void *))
{
    int i = 0;
    int items;
    struct mk_list *tmp;
    struct mk_list *head;
    struct cio_chunk **arr;
    struct cio_chunk *chunk;

    items = mk_list_size(&stream->chunks);
    if (items == 0) {
        return 0;
    }

    arr = malloc(sizeof(struct cio_chunk *) * items);
    if (!arr) {
        perror("malloc");
        return -1;
    }

    /* map chunks to the array and and unlink them */
    mk_list_foreach_safe(head, tmp, &stream->chunks) {
        chunk = mk_list_entry(head, struct cio_chunk, _head);
        arr[i++] = chunk;
        mk_list_del(&chunk->_head);
    }

    /* sort the chunks, just trust in 'compar' external function  */
    qsort(arr, items, sizeof(struct cio_chunk *), compar);

    /* link the chunks in the proper order back to the list head */
    for (i = 0; i < items; i++) {
        chunk = arr[i];
        mk_list_add(&chunk->_head, &stream->chunks);
    }

    free(arr);
    return 0;
}

/*
 * Sort chunks using the 'compar' callback function. This is pretty much a
 * wrapper over qsort(3). The sort is done inside every stream content.
 *
 * Use this function after cio_load() only.
 */
int cio_qsort(struct cio_ctx *ctx, int (*compar)(const void *, const void *))
{
    struct mk_list *head;
    struct cio_stream *stream;

    mk_list_foreach(head, &ctx->streams) {
        stream = mk_list_entry(head, struct cio_stream, _head);
        qsort_stream(stream, compar);
    }

    return 0;
}

void cio_destroy(struct cio_ctx *ctx)
{
    if (!ctx) {
        return;
    }

    cio_stream_destroy_all(ctx);

    if (ctx->options.user != NULL) {
        free(ctx->options.user);
    }

    if (ctx->options.group != NULL) {
        free(ctx->options.group);
    }

    if (ctx->options.chmod != NULL) {
        free(ctx->options.chmod);
    }

    if (ctx->processed_user != NULL) {
        free(ctx->processed_user);
    }

    if (ctx->processed_group != NULL) {
        free(ctx->processed_group);
    }

    if (ctx->options.root_path != NULL) {
        free(ctx->options.root_path);
    }

    free(ctx);
}

void cio_set_log_callback(struct cio_ctx *ctx, void (*log_cb))
{
    ctx->options.log_cb = log_cb;
}

int cio_set_log_level(struct cio_ctx *ctx, int level)
{
    if (level < CIO_LOG_ERROR || level > CIO_LOG_TRACE) {
        return -1;
    }

    ctx->options.log_level = level;
    return 0;
}

int cio_set_max_chunks_up(struct cio_ctx *ctx, int n)
{
    if (n < 1) {
        return -1;
    }

    ctx->max_chunks_up = n;
    return 0;
}

int cio_set_realloc_size_hint(struct cio_ctx *ctx, size_t realloc_size_hint)
{
    if (realloc_size_hint < CIO_REALLOC_HINT_MIN) {
        cio_log_error(ctx,
                      "[chunkio] cannot specify less than %zu bytes\n",
                      CIO_REALLOC_HINT_MIN);
        return -1;
    }
    else if (realloc_size_hint > CIO_REALLOC_HINT_MAX) {
        cio_log_error(ctx,
                      "[chunkio] cannot specify more than %zu bytes\n",
                      CIO_REALLOC_HINT_MAX);
        return -1;
    }

    ctx->realloc_size_hint = realloc_size_hint;

    return 0;
}

void cio_enable_file_trimming(struct cio_ctx *ctx)
{
    ctx->options.flags |= CIO_TRIM_FILES;
}

void cio_disable_file_trimming(struct cio_ctx *ctx)
{
    ctx->options.flags &= ~CIO_TRIM_FILES;
}