summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-18 14:06:22 +0100
committerGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-18 14:06:22 +0100
commit2c55710a9d04323fc698f49047432b3e231d2ca7 (patch)
tree61710dbb13b44834c4eb71d18a7c0a284709d2d5 /src
parent7a94df818b590d0918b978aae5a60b911fa79ce0 (diff)
Add librules and fix other libs.
Diffstat (limited to 'src')
-rw-r--r--src/include/Rules.h20
-rw-r--r--src/include/Spy.h7
-rw-r--r--src/lib/Makefile.mk19
-rw-r--r--src/lib/conf/conf_main.c1
-rw-r--r--src/lib/rules/rules_load.c60
-rw-r--r--src/lib/rules/rules_main.c111
-rw-r--r--src/lib/rules/rules_private.h59
-rw-r--r--src/lib/spy/spy_file.c26
-rw-r--r--src/lib/spy/spy_main.c16
9 files changed, 295 insertions, 24 deletions
diff --git a/src/include/Rules.h b/src/include/Rules.h
new file mode 100644
index 0000000..6bd964d
--- /dev/null
+++ b/src/include/Rules.h
@@ -0,0 +1,20 @@
1#ifndef RULES_H
2#define RULES_H
3#include <Eina.h>
4#include <Ecore.h>
5#include <Eio.h>
6#include <Conf.h>
7
8typedef struct _Rules Rules;
9typedef struct _Rule Rule;
10
11typedef void (*Rules_Progress_Cb)(void *data, Rules *rules, Rule *rule);
12typedef void (*Rules_Done_Cb)(void *data, Rules *rules);
13typedef void (*Rules_Error_Cb)(void *data, Rules *rules, const char *errstr);
14
15int rules_init(void);
16int rules_shutdown(void);
17
18Rules * rules_new(const char *directory);
19
20#endif
diff --git a/src/include/Spy.h b/src/include/Spy.h
index cc13da7..d1b182d 100644
--- a/src/include/Spy.h
+++ b/src/include/Spy.h
@@ -16,10 +16,11 @@ int spy_shutdown(void);
16Spy * spy_new(void); 16Spy * spy_new(void);
17void spy_free(Spy *spy); 17void spy_free(Spy *spy);
18 18
19Spy_File *spy_file_new(Spy *spy, const char *file); 19Spy_File * spy_file_new(Spy *spy, const char *file);
20Spy_File * spy_file_get(Spy *spy, const char *file);
20const char * spy_line_get(Spy_Line *sl); 21const char * spy_line_get(Spy_Line *sl);
21const char *spy_file_name_get(Spy_File *sf); 22const char * spy_file_name_get(Spy_File *sf);
22void spy_file_data_set(Spy_File *sf, const void *data); 23void spy_file_data_set(Spy_File *sf, const void *data);
23const void *spy_file_data_get(Spy_File *sf); 24const void * spy_file_data_get(Spy_File *sf);
24 25
25#endif 26#endif
diff --git a/src/lib/Makefile.mk b/src/lib/Makefile.mk
index 67784be..860a1b4 100644
--- a/src/lib/Makefile.mk
+++ b/src/lib/Makefile.mk
@@ -2,7 +2,8 @@ MAINTAINERCLEANFILES += src/lib/*.gc{no,da}
2 2
3noinst_LTLIBRARIES = \ 3noinst_LTLIBRARIES = \
4src/lib/libconf.la \ 4src/lib/libconf.la \
5src/lib/libspy.la 5src/lib/libspy.la \
6src/lib/librules.la
6 7
7src_lib_libconf_la_SOURCES = \ 8src_lib_libconf_la_SOURCES = \
8src/lib/conf/conf_main.c \ 9src/lib/conf/conf_main.c \
@@ -10,7 +11,7 @@ src/lib/conf/conf_load.c \
10src/lib/conf/conf_private.h \ 11src/lib/conf/conf_private.h \
11src/include/Conf.h 12src/include/Conf.h
12src_lib_libconf_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS) 13src_lib_libconf_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS)
13src_lib_libconf_la_LDFLAGS = $(LIBS_CFLAGS) 14src_lib_libconf_la_LDFLAGS = $(LIBS_LIBS)
14 15
15src_lib_libspy_la_SOURCES = \ 16src_lib_libspy_la_SOURCES = \
16src/lib/spy/spy_file.c \ 17src/lib/spy/spy_file.c \
@@ -18,5 +19,17 @@ src/lib/spy/spy_main.c \
18src/lib/spy/spy_private.h \ 19src/lib/spy/spy_private.h \
19src/include/Spy.h 20src/include/Spy.h
20src_lib_libspy_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS) 21src_lib_libspy_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS)
21src_lib_libspy_la_LDFLAGS = $(LIBS_CFLAGS) 22src_lib_libspy_la_LDFLAGS = $(LIBS_LIBS)
23
24src_lib_librules_la_SOURCES = \
25src/lib/rules/rules_main.c \
26src/lib/rules/rules_load.c \
27src/lib/rules/rules_private.h \
28src/include/Rules.h
29src_lib_librules_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS)
30src_lib_librules_la_LDFLAGS = $(LIBS_LIBS)
31src_lib_librules_la_DEPENDENCIES = \
32src/lib/libconf.la
33src_lib_librules_la_LIBADD = \
34src/lib/libconf.la
22 35
diff --git a/src/lib/conf/conf_main.c b/src/lib/conf/conf_main.c
index 154995f..5de7438 100644
--- a/src/lib/conf/conf_main.c
+++ b/src/lib/conf/conf_main.c
@@ -147,6 +147,7 @@ conf_shutdown(void)
147 if (--_conf_init_count != 0) 147 if (--_conf_init_count != 0)
148 return _conf_init_count; 148 return _conf_init_count;
149 149
150 eio_shutdown();
150 ecore_shutdown(); 151 ecore_shutdown();
151 eina_log_domain_unregister(_conf_log_dom_global); 152 eina_log_domain_unregister(_conf_log_dom_global);
152 _conf_log_dom_global = -1; 153 _conf_log_dom_global = -1;
diff --git a/src/lib/rules/rules_load.c b/src/lib/rules/rules_load.c
new file mode 100644
index 0000000..58daf1f
--- /dev/null
+++ b/src/lib/rules/rules_load.c
@@ -0,0 +1,60 @@
1#include "rules_private.h"
2
3Eina_Bool
4rules_load_ls_filter(void *data EINA_UNUSED,
5 Eio_File *handler EINA_UNUSED,
6 const Eina_File_Direct_Info *info)
7{
8 if (info->type == EINA_FILE_DIR)
9 return EINA_FALSE;
10 return EINA_TRUE;
11}
12
13void
14rules_load_ls(void *data,
15 Eio_File *handler EINA_UNUSED,
16 const Eina_File_Direct_Info *info)
17{
18 Rules_Load *rl;
19 Rule *rule;
20
21 DBG("Rule file : %s", info->path + info->name_start);
22
23 rl = data;
24
25 /* We load file */
26
27 /* If file is OK, we register it */
28 rule = calloc(1, sizeof(Rule));
29 rule->name = strdup(info->path + info->name_start);
30
31 rl->cb.progress((void *)rl->cb.data, rl->rules, rule);
32}
33
34void
35rules_load_ls_done(void *data,
36 Eio_File *handler EINA_UNUSED)
37{
38 Rules_Load *rl;
39
40 DBG("End of rules listing.");
41 rl = data;
42 rl->cb.done((void *)rl->cb.data, rl->rules);
43 free(rl);
44}
45
46
47void
48rules_load_ls_error(void *data,
49 Eio_File *handler EINA_UNUSED,
50 int error)
51{
52 Rules_Load *rl;
53
54 rl = data;
55
56 ERR("Failed to list files in %s : %s",
57 rl->rules->directory, strerror(error));
58 rl->cb.error((void *)rl->cb.data, rl->rules, "Failed to list rules directory");
59 free(rl);
60}
diff --git a/src/lib/rules/rules_main.c b/src/lib/rules/rules_main.c
new file mode 100644
index 0000000..eb19723
--- /dev/null
+++ b/src/lib/rules/rules_main.c
@@ -0,0 +1,111 @@
1#include "rules_private.h"
2
3static int _rules_init_count = 0;
4int _rules_log_dom_global = -1;
5
6Rules *
7rules_new(const char *directory)
8{
9 Rules *rules;
10
11 rules = calloc(1, sizeof(Rules));
12 rules->directory = strdup(directory);
13}
14
15Eina_Bool
16rules_load(Rules *rules,
17 Rules_Progress_Cb progress_cb,
18 Rules_Done_Cb done_cb,
19 Rules_Error_Cb error_cb,
20 void *data)
21{
22 Rules_Load *rl;
23
24 rl = calloc(1, sizeof(Rules_Load));
25 rl->rules = rules;
26
27 rl->cb.progress = progress_cb;
28 rl->cb.done = done_cb;
29 rl->cb.error = error_cb;
30 rl->cb.data = data;
31
32 eio_file_direct_ls(rl->rules->directory,
33 rules_load_ls_filter,
34 rules_load_ls,
35 rules_load_ls_done,
36 rules_load_ls_error,
37 rl);
38 return EINA_TRUE;
39}
40
41int
42rules_init(void)
43{
44 if (++_rules_init_count != 1)
45 return _rules_init_count;
46
47 if (!eina_init())
48 {
49 fprintf(stderr, "Rules can not initialize Eina\n");
50 return --_rules_init_count;
51 }
52
53 _rules_log_dom_global = eina_log_domain_register("rules", EINA_COLOR_RED);
54 if (_rules_log_dom_global < 0)
55 {
56 EINA_LOG_ERR("Rules can not create a general log domain");
57 goto shutdown_eina;
58 }
59
60 if (!ecore_init())
61 {
62 ERR("Can not initialize Ecore");
63 goto unregister_log_domain;
64 }
65
66 if (!eio_init())
67 {
68 ERR("Can not initialize Eio");
69 goto shutdown_ecore;
70 }
71
72 if (!conf_init())
73 {
74 ERR("Can not initialize Conf");
75 goto shutdown_eio;
76 }
77
78 return _rules_init_count;
79
80shutdown_eio:
81 eio_shutdown();
82shutdown_ecore:
83 ecore_shutdown();
84unregister_log_domain:
85 eina_log_domain_unregister(_rules_log_dom_global);
86 _rules_log_dom_global = -1;
87shutdown_eina:
88 eina_shutdown();
89 return --_rules_init_count;
90}
91
92int
93rules_shutdown(void)
94{
95 if (_rules_init_count <= 0)
96 {
97 fprintf(stderr, "Rules init count not greater than 0 in shutdown.");
98 return 0;
99 }
100
101 if (--_rules_init_count != 0)
102 return _rules_init_count;
103
104 conf_shutdown();
105 eio_shutdown();
106 ecore_shutdown();
107 eina_log_domain_unregister(_rules_log_dom_global);
108 _rules_log_dom_global = -1;
109 eina_shutdown();
110 return _rules_init_count;
111}
diff --git a/src/lib/rules/rules_private.h b/src/lib/rules/rules_private.h
new file mode 100644
index 0000000..c4a370f
--- /dev/null
+++ b/src/lib/rules/rules_private.h
@@ -0,0 +1,59 @@
1#include <Rules.h>
2#include <sys/types.h>
3#include <regex.h>
4
5extern int _rules_log_dom_global;
6
7#define ERR(...) EINA_LOG_DOM_ERR(_rules_log_dom_global, __VA_ARGS__)
8#define DBG(...) EINA_LOG_DOM_DBG(_rules_log_dom_global, __VA_ARGS__)
9#define NFO(...) EINA_LOG_DOM_INFO(_rules_log_dom_global, __VA_ARGS__)
10#define WRN(...) EINA_LOG_DOM_WARN(_rules_log_dom_global, __VA_ARGS__)
11#define CRI(...) EINA_LOG_DOM_CRIT(_rules_log_dom_global, __VA_ARGS__)
12
13struct _Rules
14{
15 const char *directory;
16 Eina_Inlist *rules;
17};
18
19struct _Rule
20{
21 EINA_INLIST;
22 const char *name;
23
24 struct
25 {
26 const char *filename,
27 *type,
28 *source_host,
29 *source_path,
30 *tags;
31 Eina_Bool todel;
32 Eina_Inlist *regex;
33 } spec;
34};
35
36typedef struct _Rule_Regex
37{
38 const char *regex;
39 Eina_Bool must_match;
40 regex_t preg;
41} Rule_Regex;
42
43typedef struct _Rules_Load
44{
45 Rules *rules;
46
47 struct
48 {
49 Rules_Progress_Cb progress;
50 Rules_Done_Cb done;
51 Rules_Error_Cb error;
52 const void *data;
53 } cb;
54} Rules_Load;
55
56Eina_Bool rules_load_ls_filter(void *data, Eio_File *handler, const Eina_File_Direct_Info *info);
57void rules_load_ls(void *data, Eio_File *handler, const Eina_File_Direct_Info *info);
58void rules_load_ls_done(void *data, Eio_File *handler);
59void rules_load_ls_error(void *data, Eio_File *handler, int error);
diff --git a/src/lib/spy/spy_file.c b/src/lib/spy/spy_file.c
index 282c313..49efbbf 100644
--- a/src/lib/spy/spy_file.c
+++ b/src/lib/spy/spy_file.c
@@ -1,5 +1,15 @@
1#include "spy_private.h" 1#include "spy_private.h"
2 2
3void
4_spy_file_line_free(void *d1,
5 void *d2 EINA_UNUSED)
6{
7 Spy_Line *sl = d1;
8
9 free((char *)sl->line);
10 free(sl);
11}
12
3static void 13static void
4_spy_file_event(void *data) 14_spy_file_event(void *data)
5{ 15{
@@ -16,16 +26,6 @@ _spy_file_job(void *data)
16} 26}
17 27
18void 28void
19_spy_file_line_free(void *d1,
20 void *d2 EINA_UNUSED)
21{
22 Spy_Line *sl = d1;
23
24 free((char *)sl->line);
25 free(sl);
26}
27
28void
29_spy_file_line_extract(Spy_File *sf) 29_spy_file_line_extract(Spy_File *sf)
30{ 30{
31 const char *s, 31 const char *s,
@@ -149,7 +149,6 @@ spy_file_poll(void *data)
149 } 149 }
150 150
151 size = st.st_size; 151 size = st.st_size;
152DBG("size=%zu", size);
153 if (sf->poll.size == size) 152 if (sf->poll.size == size)
154 return EINA_TRUE; 153 return EINA_TRUE;
155 154
@@ -164,13 +163,8 @@ DBG("size=%zu", size);
164 } 163 }
165 164
166 /* We have data to read! */ 165 /* We have data to read! */
167 DBG("spy_file[%p] File activity!", sf);
168
169 toread = size - sf->poll.size; 166 toread = size - sf->poll.size;
170 167
171 DBG("spy_file[%p] len_old=%zu len_new=%zu toread=%zu",
172 sf, sf->poll.size, size, toread);
173
174 sf->read.offset = sf->poll.size; 168 sf->read.offset = sf->poll.size;
175 sf->read.length = toread; 169 sf->read.length = toread;
176 170
diff --git a/src/lib/spy/spy_main.c b/src/lib/spy/spy_main.c
index 3bb4282..139afed 100644
--- a/src/lib/spy/spy_main.c
+++ b/src/lib/spy/spy_main.c
@@ -35,6 +35,20 @@ spy_free(Spy *spy)
35} 35}
36 36
37Spy_File * 37Spy_File *
38spy_file_get(Spy *spy, const char *file)
39{
40 Spy_File *sf;
41
42 EINA_INLIST_FOREACH(spy->files, sf)
43 {
44 if (!strcmp(sf->name, file))
45 return sf;
46 }
47
48 return NULL;
49}
50
51Spy_File *
38spy_file_new(Spy *spy, const char *file) 52spy_file_new(Spy *spy, const char *file)
39{ 53{
40 Spy_File *sf; 54 Spy_File *sf;
@@ -119,8 +133,6 @@ spy_init(void)
119 133
120 return _spy_init_count; 134 return _spy_init_count;
121 135
122shutdown_ecore:
123 ecore_shutdown();
124unregister_log_domain: 136unregister_log_domain:
125 eina_log_domain_unregister(_spy_log_dom_global); 137 eina_log_domain_unregister(_spy_log_dom_global);
126 _spy_log_dom_global = -1; 138 _spy_log_dom_global = -1;