summaryrefslogtreecommitdiffstats
path: root/common/elapi/providers/file/file_provider.c
blob: 589ed5eb770f22fea90ccc7a18ded33280889e3c (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
/*
    ELAPI

    Module implements a provider for sinks based on file.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <errno.h>      /* for errors */
#include <stdlib.h>     /* for free() */

#include "file_provider.h"
#include "ini_config.h"
#include "trace.h"
#include "config.h"
/* FIXME: temporary for debugging */
#include "collection_tools.h"


/* Function to read configuration */
int file_read_cfg(struct file_prvdr_cfg *file_cfg,
                  char *name,
                  struct collection_item *ini_config)
{
    int error = EOK;
    TRACE_FLOW_STRING("file_read_cfg", "Entry point");

    /* FIXME: read configuration items */

    TRACE_FLOW_STRING("file_read_cfg", "Exit");
    return error;
}


/* Function to create context */
int file_create_ctx(struct file_prvdr_ctx **file_ctx,
                    char *name,
                    struct collection_item *ini_config)
{
    int error = EOK;
    struct file_prvdr_ctx *ctx = NULL;

    TRACE_FLOW_STRING("file_create_ctx", "Entry point");

    ctx = (struct file_prvdr_ctx *)malloc(sizeof(struct file_prvdr_ctx));
    if (ctx == NULL) {
        TRACE_ERROR_NUMBER("Failed to allocate context", ENOMEM);
        return ENOMEM;
    }

    /* Init allocatable items */
    ctx->config.filename = NULL;

    /* Read configuration data */
    error = file_read_cfg(&(ctx->config), name, ini_config);
    if (error) {
        TRACE_ERROR_NUMBER("Error reading sink configuration", error);
        free(ctx);
        return error;
    }

    *file_ctx = ctx;

    TRACE_FLOW_STRING("file_create_ctx", "Exit");
    return error;
}


/* File init function */
int file_init(void **priv_ctx,
              char *name,
              struct collection_item *ini_config)
{
    int error = EOK;
    TRACE_FLOW_STRING("file_init", "Entry point");

    /* Start with creating context */
    error = file_create_ctx((struct file_prvdr_ctx **)priv_ctx,
                            name,
                            ini_config);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to create context", error);
        return error;
    }

	/* Open file */
    /* FIXME: ... */

    TRACE_FLOW_STRING("file_init", "Exit");
    return error;
}

/* File close function */
void file_close(void **priv_ctx)
{
    struct file_prvdr_ctx **ctx = NULL;

    TRACE_FLOW_STRING("file_close", "Entry point");

    ctx = (struct file_prvdr_ctx **)priv_ctx;

    /* Close file */
    /* FIXME: ... */

    /* If we allocated file name free it */
    if ((*ctx)->config.filename != NULL) {
        TRACE_INFO_STRING("Freeing file name", (*ctx)->config.filename);
        free((*ctx)->config.filename);
    }

    /* Free and indicate that the context is freed */
    free(*ctx);
    *ctx = NULL;

    TRACE_FLOW_STRING("file_close", "Exit");
}

/* File submit function */
int file_submit(void *priv_ctx, struct collection_item *event)
{
    int error = EOK;
    TRACE_FLOW_STRING("file_submit", "Entry point");


    /* FIXME: Placeholder for now */
    col_print_collection(event);

    TRACE_FLOW_STRING("file_sumbit", "Exit");
    return error;
}


/* This is the equivalent of the get info function */
void file_ability(struct sink_cpb *cpb_block)
{
    TRACE_FLOW_STRING("file_ability", "Entry point");

    cpb_block->init_cb = file_init;
    cpb_block->submit_cb = file_submit;
    cpb_block->close_cb = file_close;

    TRACE_FLOW_STRING("file_ability", "Exit");
}