Coverage Report

Created: 2026-02-09 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/plugins/in_blob/blob_file.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2015-2026 The Fluent Bit Authors
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 <fluent-bit/flb_input_plugin.h>
21
#include <fluent-bit/flb_input_blob.h>
22
23
#include <fcntl.h>
24
#include <sys/stat.h>
25
26
#include "blob.h"
27
#include "blob_db.h"
28
29
int blob_file_append(struct blob_ctx *ctx, char *path, struct stat *st)
30
0
{
31
0
    int fd;
32
0
    int ret;
33
0
    uint64_t id_found;
34
0
    struct cfl_list *head;
35
0
    struct blob_file *bfile;
36
0
    struct flb_input_instance *ins = ctx->ins;
37
38
    /* check if the file already exists in the linked list in memory */
39
0
    cfl_list_foreach(head, &ctx->files) {
40
0
        bfile = cfl_list_entry(head, struct blob_file, _head);
41
0
        if (strcmp(bfile->path, path) == 0) {
42
            /* file already exists */
43
0
            return 1;
44
0
        }
45
0
    }
46
47
0
#ifdef FLB_HAVE_SQLDB
48
0
    if (ctx->database_file) {
49
        /* the file was already registered, just skipt it */
50
0
        if (blob_db_file_exists(ctx, path, &id_found) == FLB_TRUE) {
51
0
            return 1;
52
0
        }
53
0
    }
54
0
#endif
55
56
    /* try to open the file */
57
0
    fd = open(path, O_RDONLY);
58
0
    if (fd == -1) {
59
0
        flb_errno();
60
0
        flb_plg_error(ctx->ins, "cannot open %s", path);
61
0
        return -1;
62
0
    }
63
0
    close(fd);
64
65
    /* create the reference entry */
66
0
    bfile = flb_calloc(1, sizeof(struct blob_file));
67
0
    if (!bfile) {
68
0
        flb_errno();
69
0
        return -1;
70
0
    }
71
72
0
    bfile->path = cfl_sds_create(path);
73
0
    if (!bfile->path) {
74
0
        flb_free(bfile);
75
0
        return -1;
76
0
    }
77
0
    bfile->size = st->st_size;
78
79
0
#ifdef FLB_HAVE_SQLDB
80
    /* insert the entry into the database */
81
0
    bfile->db_id = blob_db_file_insert(ctx, path, st->st_size);
82
0
    if (bfile->db_id < 0) {
83
0
        cfl_sds_destroy(bfile->path);
84
0
        flb_free(bfile);
85
0
        return -1;
86
0
    }
87
0
#endif
88
89
0
    ret = flb_input_blob_file_register(ctx->ins, ctx->log_encoder,
90
0
                                       ins->tag, ins->tag_len,
91
0
                                       bfile->path, bfile->size);
92
0
    if (ret == -1) {
93
0
        cfl_sds_destroy(bfile->path);
94
0
        flb_free(bfile);
95
0
        return -1;
96
0
    }
97
98
0
    cfl_list_add(&bfile->_head, &ctx->files);
99
0
    return 0;
100
0
}
101
102
/* release resources of a blob_file */
103
void blob_file_list_remove(struct blob_file *bfile)
104
0
{
105
0
    if (bfile->path) {
106
0
        cfl_sds_destroy(bfile->path);
107
0
    }
108
0
    flb_free(bfile);
109
0
}
110
111
/* release all blob_files from the context list */
112
void blob_file_list_remove_all(struct blob_ctx *ctx)
113
0
{
114
0
    struct cfl_list *head;
115
0
    struct cfl_list *tmp;
116
0
    struct blob_file *bfile;
117
118
0
    cfl_list_foreach_safe(head, tmp, &ctx->files) {
119
        bfile = cfl_list_entry(head, struct blob_file, _head);
120
0
        cfl_list_del(&bfile->_head);
121
0
        blob_file_list_remove(bfile);
122
0
    }
123
0
}