2 $Id: cache.c,v 1.6 2010-04-23 11:37:05 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() {
53 struct tm *tmp = NULL;
55 for ( i=0 ; i<256; i++) {
56 if ((entry = namecache[i]) != NULL) {
58 tmp = localtime(&entry->creationtime);
61 if (strftime(timestr, 200, "%c", tmp) == 0)
63 LOG(log_debug9, logtype_default, "namecache{%d}: name:%s, uuid:%s, type: %s, cached: %s",
64 i, entry->name, uuid_bin2string(entry->uuid), uuidtype[entry->type], timestr);
65 } while ((entry = entry->next) != NULL);
69 for ( i=0; i<256; i++) {
70 if ((entry = uuidcache[i]) != NULL) {
73 tmp = localtime(&entry->creationtime);
76 if (strftime(timestr, 200, "%c", tmp) == 0)
78 LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type: %s, cached: %s",
79 i, uuid_bin2string(entry->uuid), entry->name, uuidtype[entry->type], timestr);
80 } while ((entry = entry->next) != NULL);
87 /* hash string it into unsigned char */
88 static unsigned char hashstring(unsigned char *str) {
89 unsigned long hash = 5381;
92 while ((c = *str++) != 0)
93 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
95 index = 85 ^ (hash & 0xff);
96 while ((hash = hash >> 8) != 0)
97 index ^= (hash & 0xff);
102 /* hash atalk_uuid_t into unsigned char */
103 static unsigned char hashuuid(uuidp_t uuid) {
104 unsigned char index = 83;
107 for (i=0; i<16; i++) {
114 /********************************************************
116 ********************************************************/
118 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
122 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;
195 * Caller provides buffer uuid for result
197 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
207 hash = hashstring((unsigned char *)name);
209 if (! namecache[hash])
212 entry = namecache[hash];
214 ret = strcmp(entry->name, name);
215 if (ret == 0 && type == entry->type) {
216 /* found, now check if expired */
218 if ((tim - entry->creationtime) > CACHESECONDS) {
219 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
221 if (entry->prev) /* 2nd to last in queue */
222 entry->prev->next = entry->next;
223 else { /* queue head */
224 if ((namecache[hash] = entry->next) != NULL)
225 namecache[hash]->prev = NULL;
235 memcpy(uuid, entry->uuid, UUID_BINSIZE);
251 * Caller must free allocated name
253 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
263 hash = hashuuid(uuidp);
265 if (! uuidcache[hash])
268 entry = uuidcache[hash];
270 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
273 if ((tim - entry->creationtime) > CACHESECONDS) {
274 LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
275 if (entry->prev) /* 2nd to last in queue */
276 entry->prev->next = entry->next;
277 else { /* queue head */
278 if ((uuidcache[hash] = entry->next) != NULL)
279 uuidcache[hash]->prev = NULL;
289 *name = malloc(strlen(entry->name)+1);
290 strcpy(*name, entry->name);
308 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
312 cacheduser_t *cacheduser = NULL;
320 /* allocate mem and copy values */
321 name = malloc(strlen(inname)+1);
323 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
328 uuid = malloc(UUID_BINSIZE);
330 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
335 cacheduser = malloc(sizeof(cacheduser_t));
337 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
342 strcpy(name, inname);
343 memcpy(uuid, inuuid, UUID_BINSIZE);
345 /* fill in the cacheduser */
346 cacheduser->name = name;
347 cacheduser->type = type;
348 cacheduser->uuid = uuid;
349 cacheduser->creationtime = time(NULL);
350 cacheduser->prev = NULL;
351 cacheduser->next = NULL;
354 hash = hashuuid(uuid);
356 /* insert cache entry into cache array */
357 if (uuidcache[hash] == NULL) { /* this queue is empty */
358 uuidcache[hash] = cacheduser;
359 } else { /* queue is not empty, search end of queue*/
360 entry = uuidcache[hash];
361 while( entry->next != NULL)
363 cacheduser->prev = entry;
364 entry->next = cacheduser;