1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
34 void log_write(const gchar
*fmt
, ...);
36 static gboolean
valid_key(const guchar
*key
, gsize len
)
40 for (b
= 0; b
< len
; b
++) {
48 static file_cache_t
*get_entry(const guchar
*md5file
)
50 gint t
= g_slist_length(key_cache
);
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
)))
66 gboolean
cache_get_mutex(const guchar
*md5file
, struct file_mutex_s
**result
)
68 file_cache_t
*p
= get_entry(md5file
);
73 return p
? TRUE
: FALSE
;
76 gint
cache_file_count()
78 gint t
= g_slist_length(key_cache
);
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
)
92 void cache_adjust_timer()
94 gint t
= g_slist_length(key_cache
);
97 for (i
= 0; i
< t
; i
++) {
98 file_cache_t
*p
= g_slist_nth_data(key_cache
, i
);
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
);
116 gboolean
cache_add_file(const guchar
*md5file
, const guchar
*shakey
)
118 file_cache_t
*p
= g_malloc0(sizeof(file_cache_t
));
122 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
126 p
->fmutex
= g_malloc(sizeof(struct file_mutex_s
));
129 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
134 pthread_mutex_init(&p
->fmutex
->mutex
, NULL
);
135 memcpy(p
->filename
, md5file
, sizeof(p
->filename
));
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
);
148 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
149 pthread_mutex_destroy(&p
->fmutex
->mutex
);
151 memset(p
, 0, sizeof(file_cache_t
));
160 static void free_entry(file_cache_t
*p
)
166 pthread_mutex_destroy(&p
->fmutex
->mutex
);
170 memset(p
, 0, sizeof(file_cache_t
));
171 munlock(p
, sysconf(_SC_PAGESIZE
));
172 key_cache
= g_slist_remove(key_cache
, p
);
179 file_cache_t
*p
= g_slist_nth_data(key_cache
, 0);
188 static void reset_entry(file_cache_t
*p
)
190 if (p
&& !p
->refcount
)
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
;
205 send_status_all(STATUS_CACHE
);
210 file_cache_t
*p
= g_slist_nth_data(key_cache
, 0);
216 } while (cache_file_count());
218 send_status_all(STATUS_CACHE
);
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
);
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
);
250 p
->timeout
= p
->reset
;
252 p
->reset
= p
->timeout
= timeout
;
257 gboolean
cache_update_key(const guchar
*md5file
, const guchar
*shakey
)
259 file_cache_t
*p
= get_entry(md5file
);
262 return cache_add_file(md5file
, shakey
);
265 memcpy(p
->key
, shakey
, sizeof(p
->key
));
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
)
277 memcpy(shakey
, p
->key
, sizeof(p
->key
));
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
);
303 gboolean
cache_incr_refcount(const guchar
*md5file
)
305 file_cache_t
*p
= get_entry(md5file
);