summaryrefslogtreecommitdiff
path: root/src/bin/filter.c
blob: 274f0cb1161eb7646a16a96499f91bd0fb69544c (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
#include "smman.h"
#include <glob.h>

static void
_filter_rules_free(void *data)
{
   rules_rule_free((Rule *)data);
}

Filter *
_filter_find(Smman *smman,
             const char *filename)
{
   Filter *filter;

   EINA_INLIST_FOREACH(smman->filters, filter)
     {
        if (!strcmp(filter->filename, filename))
          return filter;
     }
   return NULL;
}

void
filter_load(void *data,
            Rules *rules,
            Rule *rule)
{
   Smman *smman;
   Filter *filter;
   int r;
   glob_t files;
   char **s;
   size_t i;

   smman = data;
   if (smman->rules != rules)
     return;

   DBG("smman[%p] rules[%p] rule[%p][%s]", smman, rules, rule, rule->name);

   DBG("rule : %s\n\tfilename[%s]\n\tsource_host[%s]\n\t"
       "source_path[%s]\n\ttags[%s]\n\ttodel[%s]",
       rule->name, rule->spec.filename, rule->spec.source_host,
       rule->spec.source_path, rule->spec.tags,
       (rule->spec.todel) ? "EINA_TRUE" : "EINA_FALSE");

   /* Loop with globbing to write here! */
   r = glob(rule->spec.filename, GLOB_MARK, 0, &files);
   if (r)
     {
        ERR("Unable to find files matching \"%s\"", rule->spec.filename);
        return;
     }

   for (s = files.gl_pathv, i = files.gl_pathc; i; s++, i--)
     {
        DBG("Corresponding file %s", *s);

        /* We have to check the files affected by this rule */
        filter = _filter_find(smman,*s);
        if (filter)
          goto add_rule;

        DBG("No filter found for %s, creating new one", *s);
        filter = calloc(1, sizeof(Filter));
        filter->sf = spy_file_new(smman->spy, *s);
        spy_file_data_set(filter->sf, filter);
        filter->filename = strdup(*s);
        filter->rules = eina_hash_string_superfast_new(_filter_rules_free);
        smman->filters = eina_inlist_append(smman->filters,
                                            EINA_INLIST_GET(filter));

        add_rule:
        DBG("Adding rule[%p][%s] to filter[%p][%s]",
            rule, rule->name, filter, filter->filename);
        eina_hash_add(filter->rules, rule->name, rule);
     }
}

void
filter_load_done(void *data,
                 Rules *rules)
{
   Smman *smman;

   smman = data;
   if (smman->rules != rules)
     return;

   DBG("smman[%p] rules[%p]", smman, rules);
}

void
filter_load_error(void *data,
                  Rules *rules,
                  const char *errstr)
{
   Smman *smman;

   smman = data;

   if (smman->rules != rules)
     return;

   ERR("Failed to load rules : %s", errstr);
   ecore_main_loop_quit();
}