2 $Id: cache.c,v 1.2 2009-11-28 12:30:12 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, cached: %s", i, entry->name, uuidstring, timestr);
67 } while ((entry = entry->next) != NULL);
71 for ( i=0; i<256; i++) {
72 if ((entry = uuidcache[i]) != NULL) {
74 uuid_bin2string(entry->uuid, &uuidstring);
75 tmp = localtime(&entry->creationtime);
78 if (strftime(timestr, 200, "%c", tmp) == 0)
80 LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type:%d, cached: %s", i, uuidstring, entry->name, entry->type,timestr);
82 } while ((entry = entry->next) != NULL);
89 /* hash string it into unsigned char */
90 static unsigned char hashstring(unsigned char *str) {
91 unsigned long hash = 5381;
94 while ((c = *str++) != 0)
95 hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
97 index = 85 ^ (hash & 0xff);
98 while ((hash = hash >> 8) != 0)
99 index ^= (hash & 0xff);
104 /* hash uuid_t into unsigned char */
105 static unsigned char hashuuid(uuidp_t uuid) {
106 unsigned char index = 83;
109 for (i=0; i<16; i++) {
116 /********************************************************
118 ********************************************************/
120 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
124 cacheduser_t *cacheduser = NULL;
128 /* allocate mem and copy values */
129 name = malloc(strlen(inname)+1);
131 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
136 uuid = malloc(UUID_BINSIZE);
138 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
143 cacheduser = malloc(sizeof(cacheduser_t));
145 LOG(log_error, logtype_default, "add_cachebyname: mallor error");
150 strcpy(name, inname);
151 memcpy(uuid, inuuid, UUID_BINSIZE);
153 /* fill in the cacheduser */
154 cacheduser->name = name;
155 cacheduser->uuid = uuid;
156 cacheduser->type = type;
157 cacheduser->creationtime = time(NULL);
158 cacheduser->prev = NULL;
159 cacheduser->next = NULL;
162 hash = hashstring((unsigned char *)name);
164 /* insert cache entry into cache array */
165 if (namecache[hash] == NULL) { /* this queue is empty */
166 namecache[hash] = cacheduser;
167 } else { /* queue is not empty, search end of queue*/
168 entry = namecache[hash];
169 while( entry->next != NULL)
171 cacheduser->prev = entry;
172 entry->next = cacheduser;
187 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
193 hash = hashstring((unsigned char *)name);
195 if (! namecache[hash])
198 entry = namecache[hash];
200 ret = strcmp(entry->name, name);
201 if (ret == 0 && type == entry->type) {
202 /* found, now check if expired */
204 if ((tim - entry->creationtime) > CACHESECONDS) {
206 if (entry->prev) /* 2nd to last in queue */
207 entry->prev->next = entry->next;
208 else /* queue head */
209 namecache[hash] = entry->next;
215 memcpy(uuid, entry->uuid, UUID_BINSIZE);
224 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
230 hash = hashuuid(uuidp);
232 if (! uuidcache[hash])
235 entry = uuidcache[hash];
237 ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
240 if ((tim - entry->creationtime) > CACHESECONDS) {
241 LOG(log_info, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
243 entry->prev->next = entry->next;
245 uuidcache[hash] = entry->next;
251 *name = malloc(strlen(entry->name)+1);
252 strcpy(*name, entry->name);
263 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
267 cacheduser_t *cacheduser = NULL;
271 /* allocate mem and copy values */
272 name = malloc(strlen(inname)+1);
274 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
279 uuid = malloc(UUID_BINSIZE);
281 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
286 cacheduser = malloc(sizeof(cacheduser_t));
288 LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
293 strcpy(name, inname);
294 memcpy(uuid, inuuid, UUID_BINSIZE);
296 /* fill in the cacheduser */
297 cacheduser->name = name;
298 cacheduser->type = type;
299 cacheduser->uuid = uuid;
300 cacheduser->creationtime = time(NULL);
301 cacheduser->prev = NULL;
302 cacheduser->next = NULL;
305 hash = hashuuid(uuid);
307 /* insert cache entry into cache array */
308 if (uuidcache[hash] == NULL) { /* this queue is empty */
309 uuidcache[hash] = cacheduser;
310 } else { /* queue is not empty, search end of queue*/
311 entry = uuidcache[hash];
312 while( entry->next != NULL)
314 cacheduser->prev = entry;
315 entry->next = cacheduser;