Created status.c.
[pwmd.git] / src / cache.c
blob921975f010a68bace05181984355a8cee5659a7e
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <glib.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <errno.h>
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #include "cache.h"
31 #include "status.h"
32 #include "mem.h"
34 void log_write(const gchar *fmt, ...);
36 static gboolean valid_key(const guchar *key, gsize len)
38 gint b;
40 for (b = 0; b < len; b++) {
41 if (key[b])
42 return TRUE;
45 return FALSE;
48 static file_cache_t *get_entry(const guchar *md5file)
50 gint t = g_slist_length(key_cache);
51 gint i;
53 if (!md5file)
54 return NULL;
56 for (i = 0; i < t; i++) {
57 file_cache_t *p = g_slist_nth_data(key_cache, i);
59 if (!memcmp(p->filename, md5file, sizeof(p->filename)))
60 return p;
63 return NULL;
66 gboolean cache_get_mutex(const guchar *md5file, struct file_mutex_s **result)
68 file_cache_t *p = get_entry(md5file);
70 if (p)
71 *result = p->fmutex;
73 return p ? TRUE : FALSE;
76 gint cache_file_count()
78 gint t = g_slist_length(key_cache);
79 gint i;
80 gint n = 0;
82 for (i = 0; i < t; i++) {
83 file_cache_t *p = g_slist_nth_data(key_cache, i);
85 if (valid_key(p->key, sizeof(p->key)) == TRUE)
86 n++;
89 return n;
92 void cache_adjust_timer()
94 gint t = g_slist_length(key_cache);
95 gint i;
97 for (i = 0; i < t; i++) {
98 file_cache_t *p = g_slist_nth_data(key_cache, i);
100 if (p->reset >= 0) {
101 if (p->timeout > 0)
102 p->timeout--;
104 if (p->timeout == 0) {
105 if (valid_key(p->key, sizeof(p->key)) == TRUE) {
106 memset(&p->key, 0, sizeof(p->key));
107 send_status_all(STATUS_CACHE);
110 p->timeout = -1;
116 gboolean cache_add_file(const guchar *md5file, const guchar *shakey)
118 file_cache_t *p = g_malloc0(sizeof(file_cache_t));
119 GSList *new;
121 if (!p) {
122 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
123 return FALSE;
126 p->fmutex = g_malloc(sizeof(struct file_mutex_s));
128 if (!p->fmutex) {
129 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
130 g_free(p);
131 return FALSE;
134 pthread_mutex_init(&p->fmutex->mutex, NULL);
135 memcpy(p->filename, md5file, sizeof(p->filename));
137 if (shakey)
138 memcpy(p->key, shakey, sizeof(p->key));
140 p->reset = p->timeout = -2;
142 if (mlock(p, sysconf(_SC_PAGESIZE)))
143 log_write("mlock(): %s", strerror(errno));
145 new = g_slist_append(key_cache, p);
147 if (!new) {
148 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
149 pthread_mutex_destroy(&p->fmutex->mutex);
150 g_free(p->fmutex);
151 memset(p, 0, sizeof(file_cache_t));
152 g_free(p);
153 return FALSE;
156 key_cache = new;
157 return TRUE;
160 static void free_entry(file_cache_t *p)
162 if (!p)
163 return;
165 if (p->fmutex) {
166 pthread_mutex_destroy(&p->fmutex->mutex);
167 g_free(p->fmutex);
170 memset(p, 0, sizeof(file_cache_t));
171 munlock(p, sysconf(_SC_PAGESIZE));
172 key_cache = g_slist_remove(key_cache, p);
173 g_free(p);
176 void cache_free()
178 for (;;) {
179 file_cache_t *p = g_slist_nth_data(key_cache, 0);
181 if (!p)
182 break;
184 free_entry(p);
188 static void reset_entry(file_cache_t *p)
190 if (p && !p->refcount)
191 free_entry(p);
192 else if (p)
193 memset(p->key, 0, sizeof(p->key));
196 gboolean cache_clear(const guchar *md5file, gint which)
198 file_cache_t *p = md5file ? get_entry(md5file) : NULL;
200 if (which == 1) {
201 if (!p)
202 return FALSE;
204 reset_entry(p);
205 send_status_all(STATUS_CACHE);
206 return TRUE;
209 do {
210 file_cache_t *p = g_slist_nth_data(key_cache, 0);
212 if (!p)
213 break;
215 reset_entry(p);
216 } while (cache_file_count());
218 send_status_all(STATUS_CACHE);
219 return TRUE;
222 gboolean cache_iscached(const guchar *md5file)
224 file_cache_t *p = get_entry(md5file);
226 return p ? valid_key(p->key, sizeof(p->key)) : FALSE;
229 gboolean cache_reset_timeout(const guchar *md5file, glong timeout)
231 file_cache_t *p = get_entry(md5file);
233 if (!p)
234 return FALSE;
236 if (p->reset == -2)
237 return cache_set_timeout(md5file, timeout);
239 return cache_set_timeout(md5file, p->reset);
242 gboolean cache_set_timeout(const guchar *md5file, glong timeout)
244 file_cache_t *p = get_entry(md5file);
246 if (!p)
247 return FALSE;
249 if (timeout == -2)
250 p->timeout = p->reset;
251 else
252 p->reset = p->timeout = timeout;
254 return TRUE;
257 gboolean cache_update_key(const guchar *md5file, const guchar *shakey)
259 file_cache_t *p = get_entry(md5file);
261 if (!p)
262 return cache_add_file(md5file, shakey);
264 if (p->reset)
265 memcpy(p->key, shakey, sizeof(p->key));
267 return TRUE;
270 gboolean cache_get_key(const guchar *md5file, guchar *shakey)
272 file_cache_t *p = get_entry(md5file);
274 if (!p || valid_key(p->key, sizeof(p->key)) == FALSE)
275 return FALSE;
277 memcpy(shakey, p->key, sizeof(p->key));
278 return TRUE;
281 gboolean cache_has_file(const guchar *md5file)
283 file_cache_t *p = get_entry(md5file);
285 return p ? TRUE : FALSE;
288 gboolean cache_decr_refcount(const guchar *md5file)
290 file_cache_t *p = get_entry(md5file);
292 if (!p)
293 return FALSE;
295 p->refcount--;
297 if (p->refcount < 0)
298 p->refcount = 0;
300 return TRUE;
303 gboolean cache_incr_refcount(const guchar *md5file)
305 file_cache_t *p = get_entry(md5file);
307 if (!p)
308 return FALSE;
310 p->refcount++;
311 return TRUE;