2 $Id: cache.c,v 1.4 2010-04-23 05:54:54 franklahm Exp $
3 Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
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.
18 #endif /* HAVE_CONFIG_H */
26 #include <atalk/logger.h>
27 #include <atalk/afp.h>
28 #include <atalk/uuid.h>
31 typedef struct cacheduser {
32 unsigned long uid; /* for future use */
37 struct cacheduser *prev;
38 struct cacheduser *next;
41 cacheduser_t *namecache[256]; /* indexed by hash of name */
42 cacheduser_t *uuidcache[256]; /* indexed by hash of uuid */
44 /********************************************************
46 ********************************************************/
48 static int dumpcache() {
52 char *uuidstring = NULL;
54 struct tm *tmp = NULL;
56 for ( i=0 ; i<256; i++) {
57 if ((entry = namecache[i]) != NULL) {
59 uuid_bin2string(entry->uuid, &uuidstring);
60 tmp = localtime(&entry->creationtime);
63 if (strftime(timestr, 200, "%c", tmp) == 0)
65 LOG(log_debug9, logtype_default, "namecache{%d}: name:%s, uuid:%s, type: %s, cached: %s",
66 i, entry->name, uuidstring, uuidtype[entry->type], timestr);
68 } while ((entry = entry->next) != NULL);
72 for ( i=0; i<256; i++) {
73 if ((entry = uuidcache[i]) != NULL) {
75 uuid_bin2string(entry->uuid, &uuidstring);
76 tmp = localtime(&entry->creationtime);
79 if (strftime(timestr, 200, "%c", tmp) == 0)
81 LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type: %s, cached: %s",
82 i, uuidstring, entry->name, uuidtype[entry->type], timestr);
84 } while ((entry = entry->next) != NULL);
91 /* hash string it into unsigned char */
92 static unsigned char hashstring(unsigned char *str) {
93 unsigned long hash = 5381;
96 while ((c = *str++) != 0)
97 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
99 index = 85 ^ (hash & 0xff);
100 while ((hash = hash >> 8) != 0)
101 index ^= (hash & 0xff);
106 /* hash uuid_t into unsigned char */
107 static unsigned char hashuuid(uuidp_t uuid) {
108 unsigned char index = 83;
111 for (i=0; i<16; i++) {
118 /********************************************************
120 ********************************************************/
122 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
126 cacheduser_t *cacheduser = NULL;
130 /* allocate mem and copy values */
131 name = malloc(strlen(inname)+1);
133 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
138 uuid = malloc(UUID_BINSIZE);
140 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
145 cacheduser = malloc(sizeof(cacheduser_t));
147 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
152 strcpy(name, inname);
153 memcpy(uuid, inuuid, UUID_BINSIZE);
155 /* fill in the cacheduser */
156 cacheduser->name = name;
157 cacheduser->uuid = uuid;
158 cacheduser->type = type;
159 cacheduser->creationtime = time(NULL);
160 cacheduser->prev = NULL;
161 cacheduser->next = NULL;
164 hash = hashstring((unsigned char *)name);
166 /* insert cache entry into cache array */
167 if (namecache[hash] == NULL) { /* this queue is empty */
168 namecache[hash] = cacheduser;
169 } else { /* queue is not empty, search end of queue*/
170 entry = namecache[hash];
171 while( entry->next != NULL)
173 cacheduser->prev = entry;
174 entry->next = cacheduser;
189 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
195 hash = hashstring((unsigned char *)name);
197 if (! namecache[hash])
200 entry = namecache[hash];
202 ret = strcmp(entry->name, name);
203 if (ret == 0 && type == entry->type) {
204 /* found, now check if expired */
206 if ((tim - entry->creationtime) > CACHESECONDS) {
207 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
209 if (entry->prev) /* 2nd to last in queue */
210 entry->prev->next = entry->next;
211 else /* queue head */
212 namecache[hash] = entry->next;
218 memcpy(uuid, entry->uuid, UUID_BINSIZE);
227 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
233 hash = hashuuid(uuidp);
235 if (! uuidcache[hash])
238 entry = uuidcache[hash];
240 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
243 if ((tim - entry->creationtime) > CACHESECONDS) {
244 LOG(log_info, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
246 entry->prev->next = entry->next;
248 uuidcache[hash] = entry->next;
254 *name = malloc(strlen(entry->name)+1);
255 strcpy(*name, entry->name);
266 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
270 cacheduser_t *cacheduser = NULL;
274 /* allocate mem and copy values */
275 name = malloc(strlen(inname)+1);
277 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
282 uuid = malloc(UUID_BINSIZE);
284 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
289 cacheduser = malloc(sizeof(cacheduser_t));
291 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
296 strcpy(name, inname);
297 memcpy(uuid, inuuid, UUID_BINSIZE);
299 /* fill in the cacheduser */
300 cacheduser->name = name;
301 cacheduser->type = type;
302 cacheduser->uuid = uuid;
303 cacheduser->creationtime = time(NULL);
304 cacheduser->prev = NULL;
305 cacheduser->next = NULL;
308 hash = hashuuid(uuid);
310 /* insert cache entry into cache array */
311 if (uuidcache[hash] == NULL) { /* this queue is empty */
312 uuidcache[hash] = cacheduser;
313 } else { /* queue is not empty, search end of queue*/
314 entry = uuidcache[hash];
315 while( entry->next != NULL)
317 cacheduser->prev = entry;
318 entry->next = cacheduser;