]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/cache.c
Adjust cachedump LOG message
[netatalk.git] / libatalk / acl / cache.c
1 /*
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>
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 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     /* allocate mem and copy values */
131     name = malloc(strlen(inname)+1);
132     if (!name) {
133         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
134         ret = -1;
135         goto cleanup;
136     }
137
138     uuid = malloc(UUID_BINSIZE);
139     if (!uuid) {
140         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
141         ret = -1;
142         goto cleanup;
143     }
144
145     cacheduser = malloc(sizeof(cacheduser_t));
146     if (!cacheduser) {
147         LOG(log_error, logtype_default, "add_cachebyname: mallor error");
148         ret = -1;
149         goto cleanup;
150     }
151
152     strcpy(name, inname);
153     memcpy(uuid, inuuid, UUID_BINSIZE);
154
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;
162
163     /* get hash */
164     hash = hashstring((unsigned char *)name);
165
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)
172             entry = entry->next;
173         cacheduser->prev = entry;
174         entry->next = cacheduser;
175     }
176
177 cleanup:
178     if (ret != 0) {
179         if (name)
180             free(name);
181         if (uuid)
182             free(uuid);
183         if (cacheduser)
184             free(cacheduser);
185     }
186     return ret;
187 }
188
189 int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
190     int ret;
191     unsigned char hash;
192     cacheduser_t *entry;
193     time_t tim;
194
195     hash = hashstring((unsigned char *)name);
196
197     if (! namecache[hash])
198         return -1;
199
200     entry = namecache[hash];
201     while (entry) {
202         ret = strcmp(entry->name, name);
203         if (ret == 0 && type == entry->type) {
204             /* found, now check if expired */
205             tim = time(NULL);
206             if ((tim - entry->creationtime) > CACHESECONDS) {
207                 LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
208                 /* remove item */
209                 if (entry->prev) /* 2nd to last in queue */
210                     entry->prev->next = entry->next;
211                 else        /* queue head */
212                     namecache[hash] = entry->next;
213                 free(entry->name);
214                 free(entry->uuid);
215                 free(entry);
216                 return -1;
217             } else {
218                 memcpy(uuid, entry->uuid, UUID_BINSIZE);
219                 return 0;
220             }
221         }
222         entry = entry->next;
223     }
224     return -1;
225 }
226
227 int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
228     int ret;
229     unsigned char hash;
230     cacheduser_t *entry;
231     time_t tim;
232
233     hash = hashuuid(uuidp);
234
235     if (! uuidcache[hash])
236         return -1;
237
238     entry = uuidcache[hash];
239     while (entry) {
240         ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
241         if (ret == 0) {
242             tim = time(NULL);
243             if ((tim - entry->creationtime) > CACHESECONDS) {
244                 LOG(log_info, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
245                 if (entry->prev)
246                     entry->prev->next = entry->next;
247                 else
248                     uuidcache[hash] = entry->next;
249                 free(entry->name);
250                 free(entry->uuid);
251                 free(entry);
252                 return -1;
253             } else {
254                 *name = malloc(strlen(entry->name)+1);
255                 strcpy(*name, entry->name);
256                 *type = entry->type;
257                 return 0;
258             }
259         }
260         entry = entry->next;
261     }
262
263     return -1;
264 }
265
266 int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
267     int ret = 0;
268     char *name = NULL;
269     uuidp_t uuid;
270     cacheduser_t *cacheduser = NULL;
271     cacheduser_t *entry;
272     unsigned char hash;
273
274     /* allocate mem and copy values */
275     name = malloc(strlen(inname)+1);
276     if (!name) {
277         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
278         ret = -1;
279         goto cleanup;
280     }
281
282     uuid = malloc(UUID_BINSIZE);
283     if (!uuid) {
284         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
285         ret = -1;
286         goto cleanup;
287     }
288
289     cacheduser = malloc(sizeof(cacheduser_t));
290     if (!cacheduser) {
291         LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
292         ret = -1;
293         goto cleanup;
294     }
295
296     strcpy(name, inname);
297     memcpy(uuid, inuuid, UUID_BINSIZE);
298
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;
306
307     /* get hash */
308     hash = hashuuid(uuid);
309
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)
316             entry = entry->next;
317         cacheduser->prev = entry;
318         entry->next = cacheduser;
319     }
320
321 cleanup:
322     if (ret != 0) {
323         if (name)
324             free(name);
325         if (uuid)
326             free(uuid);
327         if (cacheduser)
328             free(cacheduser);
329     }
330     return ret;
331 }