]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/cache.c
Merge from 2-1
[netatalk.git] / libatalk / acl / cache.c
1 /*
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>
4
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.
9
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.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <errno.h>
25
26 #include <atalk/logger.h>
27 #include <atalk/afp.h>
28 #include <atalk/uuid.h>
29 #include "cache.h"
30
31 typedef struct cacheduser {
32     unsigned long uid;      /* for future use */
33     uuidtype_t type;
34     uuidp_t uuid;
35     char *name;
36     time_t creationtime;
37     struct cacheduser *prev;
38     struct cacheduser *next;
39 } cacheduser_t;
40
41 cacheduser_t *namecache[256];   /* indexed by hash of name */
42 cacheduser_t *uuidcache[256];   /* indexed by hash of uuid */
43
44 /********************************************************
45  * helper function
46  ********************************************************/
47
48 static int dumpcache() {
49     int i;
50     int ret = 0;
51     cacheduser_t *entry;
52     char *uuidstring = NULL;
53     char timestr[200];
54     struct tm *tmp = NULL;
55
56     for ( i=0 ; i<256; i++) {
57         if ((entry = namecache[i]) != NULL) {
58             do {
59                 uuid_bin2string(entry->uuid, &uuidstring);
60                 tmp = localtime(&entry->creationtime);
61                 if (tmp == NULL)
62                     continue;
63                 if (strftime(timestr, 200, "%c", tmp) == 0)
64                     continue;
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);
67                 free(uuidstring);
68             } while ((entry = entry->next) != NULL);
69         }
70     }
71
72     for ( i=0; i<256; i++) {
73         if ((entry = uuidcache[i]) != NULL) {
74             do {
75                 uuid_bin2string(entry->uuid, &uuidstring);
76                 tmp = localtime(&entry->creationtime);
77                 if (tmp == NULL)
78                     continue;
79                 if (strftime(timestr, 200, "%c", tmp) == 0)
80                     continue;
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);
83                 free(uuidstring);
84             } while ((entry = entry->next) != NULL);
85         }
86     }
87
88     return ret;
89 }
90
91 /* hash string it into unsigned char */
92 static unsigned char hashstring(unsigned char *str) {
93     unsigned long hash = 5381;
94     unsigned char index;
95     int c;
96     while ((c = *str++) != 0)
97         hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
98
99     index = 85 ^ (hash & 0xff);
100     while ((hash = hash >> 8) != 0)
101         index ^= (hash & 0xff);
102
103     return index;
104 }
105
106 /* hash atalk_uuid_t into unsigned char */
107 static unsigned char hashuuid(uuidp_t uuid) {
108     unsigned char index = 83;
109     int i;
110
111     for (i=0; i<16; i++) {
112         index ^= uuid[i];
113         index += uuid[i];
114     }
115     return index;
116 }
117
118 /********************************************************
119  * Interface
120  ********************************************************/
121
122 int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
123     int ret = 0;
124     char *name = NULL;
125     uuidp_t uuid;
126     cacheduser_t *cacheduser = NULL;
127     cacheduser_t *entry;
128     unsigned char hash;
129
130 #ifdef DEBUG
131     dumpcache();
132 #endif
133
134     /* allocate mem and copy values */
135     name = malloc(strlen(inname)+1);
136     if (!name) {
137         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
138         ret = -1;
139         goto cleanup;
140     }
141
142     uuid = malloc(UUID_BINSIZE);
143     if (!uuid) {
144         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
145         ret = -1;
146         goto cleanup;
147     }
148
149     cacheduser = malloc(sizeof(cacheduser_t));
150     if (!cacheduser) {
151         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
152         ret = -1;
153         goto cleanup;
154     }
155
156     strcpy(name, inname);
157     memcpy(uuid, inuuid, UUID_BINSIZE);
158
159     /* fill in the cacheduser */
160     cacheduser->name = name;
161     cacheduser->uuid = uuid;
162     cacheduser->type = type;
163     cacheduser->creationtime = time(NULL);
164     cacheduser->prev = NULL;
165     cacheduser->next = NULL;
166
167     /* get hash */
168     hash = hashstring((unsigned char *)name);
169
170     /* insert cache entry into cache array */
171     if (namecache[hash] == NULL) { /* this queue is empty */
172         namecache[hash] = cacheduser;
173     } else {            /* queue is not empty, search end of queue*/
174         entry = namecache[hash];
175         while( entry->next != NULL)
176             entry = entry->next;
177         cacheduser->prev = entry;
178         entry->next = cacheduser;
179     }
180
181 cleanup:
182     if (ret != 0) {
183         if (name)
184             free(name);
185         if (uuid)
186             free(uuid);
187         if (cacheduser)
188             free(cacheduser);
189     }
190
191 #ifdef DEBUG
192     dumpcache();
193 #endif
194
195     return ret;
196 }
197
198 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
199     int ret;
200     unsigned char hash;
201     cacheduser_t *entry;
202     time_t tim;
203
204 #ifdef DEBUG
205     dumpcache();
206 #endif
207
208     hash = hashstring((unsigned char *)name);
209
210     if (! namecache[hash])
211         return -1;
212
213     entry = namecache[hash];
214     while (entry) {
215         ret = strcmp(entry->name, name);
216         if (ret == 0 && type == entry->type) {
217             /* found, now check if expired */
218             tim = time(NULL);
219             if ((tim - entry->creationtime) > CACHESECONDS) {
220                 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
221                 /* remove item */
222                 if (entry->prev) /* 2nd to last in queue */
223                     entry->prev->next = entry->next;
224                 else  { /* queue head */
225                     if ((namecache[hash] = entry->next) != NULL)
226                         namecache[hash]->prev = NULL;
227                 }
228                 free(entry->name);
229                 free(entry->uuid);
230                 free(entry);
231 #ifdef DEBUG
232                 dumpcache();
233 #endif
234                 return -1;
235             } else {
236                 memcpy(uuid, entry->uuid, UUID_BINSIZE);
237 #ifdef DEBUG
238                 dumpcache();
239 #endif
240                 return 0;
241             }
242         }
243         entry = entry->next;
244     }
245 #ifdef DEBUG
246     dumpcache();
247 #endif
248     return -1;
249 }
250
251 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
252     int ret;
253     unsigned char hash;
254     cacheduser_t *entry;
255     time_t tim;
256
257 #ifdef DEBUG
258     dumpcache();
259 #endif
260
261     hash = hashuuid(uuidp);
262
263     if (! uuidcache[hash])
264         return -1;
265
266     entry = uuidcache[hash];
267     while (entry) {
268         ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
269         if (ret == 0) {
270             tim = time(NULL);
271             if ((tim - entry->creationtime) > CACHESECONDS) {
272                 LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
273                 if (entry->prev) /* 2nd to last in queue */
274                     entry->prev->next = entry->next;
275                 else { /* queue head  */
276                     if ((uuidcache[hash] = entry->next) != NULL)
277                         uuidcache[hash]->prev = NULL;
278                 }
279                 free(entry->name);
280                 free(entry->uuid);
281                 free(entry);
282 #ifdef DEBUG
283                 dumpcache();
284 #endif
285                 return -1;
286             } else {
287                 *name = malloc(strlen(entry->name)+1);
288                 strcpy(*name, entry->name);
289                 *type = entry->type;
290 #ifdef DEBUG
291                 dumpcache();
292 #endif
293                 return 0;
294             }
295         }
296         entry = entry->next;
297     }
298
299 #ifdef DEBUG
300     dumpcache();
301 #endif
302
303     return -1;
304 }
305
306 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
307     int ret = 0;
308     char *name = NULL;
309     uuidp_t uuid;
310     cacheduser_t *cacheduser = NULL;
311     cacheduser_t *entry;
312     unsigned char hash;
313
314 #ifdef DEBUG
315     dumpcache();
316 #endif
317
318     /* allocate mem and copy values */
319     name = malloc(strlen(inname)+1);
320     if (!name) {
321         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
322         ret = -1;
323         goto cleanup;
324     }
325
326     uuid = malloc(UUID_BINSIZE);
327     if (!uuid) {
328         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
329         ret = -1;
330         goto cleanup;
331     }
332
333     cacheduser = malloc(sizeof(cacheduser_t));
334     if (!cacheduser) {
335         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
336         ret = -1;
337         goto cleanup;
338     }
339
340     strcpy(name, inname);
341     memcpy(uuid, inuuid, UUID_BINSIZE);
342
343     /* fill in the cacheduser */
344     cacheduser->name = name;
345     cacheduser->type = type;
346     cacheduser->uuid = uuid;
347     cacheduser->creationtime = time(NULL);
348     cacheduser->prev = NULL;
349     cacheduser->next = NULL;
350
351     /* get hash */
352     hash = hashuuid(uuid);
353
354     /* insert cache entry into cache array */
355     if (uuidcache[hash] == NULL) { /* this queue is empty */
356         uuidcache[hash] = cacheduser;
357     } else {            /* queue is not empty, search end of queue*/
358         entry = uuidcache[hash];
359         while( entry->next != NULL)
360             entry = entry->next;
361         cacheduser->prev = entry;
362         entry->next = cacheduser;
363     }
364
365 cleanup:
366     if (ret != 0) {
367         if (name)
368             free(name);
369         if (uuid)
370             free(uuid);
371         if (cacheduser)
372             free(cacheduser);
373     }
374
375 #ifdef DEBUG
376     dumpcache();
377 #endif
378
379     return ret;
380 }