2 rxpd_file.c - regex policy daemon
5 2007, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 rxpd_file_new (struct rxpd_base* base, const char* filename)
28 struct rxpd_file* self = NULL;
31 strcspn(filename, RXPD_FILE_ILG_CHARS) != strlen (filename) ||
32 strlen (filename) + strlen (base->basedir) > 4095)
34 rxpd_log (base, LOG_ERR, "illegal filename: '%s'\n", filename?filename:"");
38 strcpy (buf, base->basedir);
39 strcat (buf, filename);
40 filename = rxpd_strdup (buf);
42 self = rxpd_malloc (sizeof (struct rxpd_file));
43 self->filename = filename;
45 const char* basename = strrchr (filename, '/');
50 psplay_init (&self->node, basename);
51 llist_init (&self->rules);
53 psplay_insert (&base->files, &self->node);
55 rxpd_log (base, LOG_INFO, "new file: '%s'\n", filename);
60 rxpd_file_delete (struct rxpd_file* self)
64 LLIST_WHILE_HEAD (&self->rules, n)
66 struct rxpd_rule* node = (struct rxpd_rule*)n;
67 rxpd_rule_delete (node);
69 psplay_remove (&self->base->files, &self->node);
70 free ((void*)self->filename);
76 rxpd_file_load (struct rxpd_file* self)
78 FILE* f = fopen (self->filename, "r");
79 // TODO error handling
82 /* First purge old rules */
83 LLIST_WHILE_HEAD (&self->rules, n)
85 struct rxpd_rule* node = (struct rxpd_rule*)n;
86 rxpd_rule_delete (node);
89 // TODO test excess line length = error
92 rxpd_log (self->base, LOG_NOTICE, "loading '%s'\n", self->filename);
94 while (fgets (buf, 4096, f))
96 size_t last = strlen(buf);
97 if (buf[last-1] == '\n')
100 struct rxpd_rule* rule;
101 rule = rxpd_rule_new (buf);
105 rxpd_log (self->base, LOG_DEBUG, "new rule '%s'\n", rule->string);
107 llist_insert_tail (&self->rules, &rule->node);
115 rxpd_log (self->base, LOG_ERR, "failed loading '%s'\n", self->filename);
121 rxpd_file_save (struct rxpd_file* self)
123 FILE* f = fopen (self->filename, "w");
124 // TODO error handling
127 LLIST_FOREACH (&self->rules, n)
129 struct rxpd_rule* node = (struct rxpd_rule*)n;
130 if (node->atime != (time_t)-1)
131 fprintf (f, "%ld:%s\n", node->atime, node->string);
132 else if (*node->string != '#')
133 fprintf (f, ":%s\n", node->string);
135 fprintf (f, "%s\n", node->string);
139 rxpd_log (self->base, LOG_NOTICE, "saved '%s'\n", self->filename);
144 rxpd_log (self->base, LOG_ERR, "failed saving '%s'\n", self->filename);
150 rxpd_file_cmp (const void* A, const void* B)
152 return strcmp (A, B);