File: cache.c

package info (click to toggle)
netatalk 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,716 kB
  • sloc: ansic: 85,115; sh: 10,385; perl: 1,703; makefile: 1,363
file content (364 lines) | stat: -rw-r--r-- 10,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
  Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include <atalk/logger.h>
#include <atalk/afp.h>
#include <atalk/uuid.h>
#include "cache.h"

typedef struct cacheduser {
    unsigned long uid;      /* for future use */
    uuidtype_t type;
    uuidp_t uuid;
    char *name;
    time_t creationtime;
    struct cacheduser *prev;
    struct cacheduser *next;
} cacheduser_t;

cacheduser_t *namecache[256];   /* indexed by hash of name */
cacheduser_t *uuidcache[256];   /* indexed by hash of uuid */

/********************************************************
 * helper function
 ********************************************************/

void uuidcache_dump(void) {
    int i;
    int ret = 0;
    cacheduser_t *entry;
    char timestr[200];
    struct tm *tmp = NULL;

    for ( i=0 ; i<256; i++) {
        if ((entry = namecache[i]) != NULL) {
            do {
                tmp = localtime(&entry->creationtime);
                if (tmp == NULL)
                    continue;
                if (strftime(timestr, 200, "%c", tmp) == 0)
                    continue;
                LOG(log_debug, logtype_default,
                    "namecache{%d}: name:%s, uuid:%s, type%s: %s, cached: %s",
                    i,
                    entry->name,
                    uuid_bin2string(entry->uuid),
                    (entry->type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
                    uuidtype[entry->type & UUIDTYPESTR_MASK],
                    timestr);
            } while ((entry = entry->next) != NULL);
        }
    }

    for ( i=0; i<256; i++) {
        if ((entry = uuidcache[i]) != NULL) {
            do {

                tmp = localtime(&entry->creationtime);
                if (tmp == NULL)
                    continue;
                if (strftime(timestr, 200, "%c", tmp) == 0)
                    continue;
                LOG(log_debug, logtype_default,
                    "uuidcache{%d}: uuid:%s, name:%s, type%s: %s, cached: %s",
                    i,
                    uuid_bin2string(entry->uuid),
                    entry->name,
                    (entry->type & UUID_ENOENT) == UUID_ENOENT ? "[negative]" : "",
                    uuidtype[entry->type & UUIDTYPESTR_MASK],
                    timestr);
            } while ((entry = entry->next) != NULL);
        }
    }
}

/* hash string it into unsigned char */
static unsigned char hashstring(unsigned char *str) {
    unsigned long hash = 5381;
    unsigned char index;
    int c;
    while ((c = *str++) != 0)
        hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */

    index = 85 ^ (hash & 0xff);
    while ((hash = hash >> 8) != 0)
        index ^= (hash & 0xff);

    return index;
}

/* hash atalk_uuid_t into unsigned char */
static unsigned char hashuuid(uuidp_t uuid) {
    unsigned char index = 83;
    int i;

    for (i=0; i<16; i++) {
        index ^= uuid[i];
        index += uuid[i];
    }
    return index;
}

/********************************************************
 * Interface
 ********************************************************/

int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t type, const unsigned long uid _U_) {
    int ret = 0;
    char *name = NULL;
    uuidp_t uuid = NULL;
    cacheduser_t *cacheduser = NULL;
    unsigned char hash;

    /* allocate mem and copy values */
    name = malloc(strlen(inname)+1);
    if (!name) {
        LOG(log_error, logtype_default, "add_cachebyname: mallor error");
        ret = -1;
        goto cleanup;
    }

    uuid = malloc(UUID_BINSIZE);
    if (!uuid) {
        LOG(log_error, logtype_default, "add_cachebyname: mallor error");
        ret = -1;
        goto cleanup;
    }

    cacheduser = malloc(sizeof(cacheduser_t));
    if (!cacheduser) {
        LOG(log_error, logtype_default, "add_cachebyname: mallor error");
        ret = -1;
        goto cleanup;
    }

    strcpy(name, inname);
    memcpy(uuid, inuuid, UUID_BINSIZE);

    /* fill in the cacheduser */
    cacheduser->name = name;
    cacheduser->uuid = uuid;
    cacheduser->type = type;
    cacheduser->creationtime = time(NULL);
    cacheduser->prev = NULL;
    cacheduser->next = NULL;

    /* get hash */
    hash = hashstring((unsigned char *)name);

    /* insert cache entry into cache array at head of queue */
    if (namecache[hash] == NULL) {
        /* this queue is empty */
        namecache[hash] = cacheduser;
    } else {
        cacheduser->next = namecache[hash];
        namecache[hash]->prev = cacheduser;
        namecache[hash] = cacheduser;
    }

cleanup:
    if (ret != 0) {
        if (name)
            free(name);
        if (uuid)
            free(uuid);
        if (cacheduser)
            free(cacheduser);
    }

    return ret;
}

/*!
 * Search cache by name and uuid type
 *
 * @args name     (r)  name to search
 * @args type     (rw) type (user or group) of name, returns found type here which might
 *                     mark it as a negative entry
 * @args uuid     (w)  found uuid is returned here
 * @returns       0 on sucess, entry found
 *                -1 no entry found
 */
int search_cachebyname(const char *name, uuidtype_t *type, uuidp_t uuid) {
    int ret;
    unsigned char hash;
    cacheduser_t *entry;
    time_t tim;

    hash = hashstring((unsigned char *)name);

    if (namecache[hash] == NULL)
        return -1;

    entry = namecache[hash];
    while (entry) {
        ret = strcmp(entry->name, name);
        if (ret == 0 && *type == (entry->type & UUIDTYPESTR_MASK)) {
            /* found, now check if expired */
            tim = time(NULL);
            if ((tim - entry->creationtime) > CACHESECONDS) {
                LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\"%s\"", entry->name);
                /* remove item */
                if (entry->prev) {
                    /* 2nd to last in queue */
                    entry->prev->next = entry->next;
                    if (entry->next)
                        /* not the last element */
                        entry->next->prev = entry->prev;
                } else  {
                    /* queue head */
                    if ((namecache[hash] = entry->next) != NULL)
                        namecache[hash]->prev = NULL;
                }
                free(entry->name);
                free(entry->uuid);
                free(entry);
                return -1;
            } else {
                memcpy(uuid, entry->uuid, UUID_BINSIZE);
                *type = entry->type;
                return 0;
            }
        }
        entry = entry->next;
    }

    return -1;
}

/* 
 * Caller must free allocated name
 */
int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
    int ret;
    unsigned char hash;
    cacheduser_t *entry;
    time_t tim;

    hash = hashuuid(uuidp);

    if (! uuidcache[hash])
        return -1;

    entry = uuidcache[hash];
    while (entry) {
        ret = memcmp(entry->uuid, uuidp, UUID_BINSIZE);
        if (ret == 0) {
            tim = time(NULL);
            if ((tim - entry->creationtime) > CACHESECONDS) {
                LOG(log_debug, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
                if (entry->prev) {
                    /* 2nd to last in queue */
                    entry->prev->next = entry->next;
                    if (entry->next)
                        /* not the last element */
                        entry->next->prev = entry->prev;
                } else {
                    /* queue head  */
                    if ((uuidcache[hash] = entry->next) != NULL)
                        uuidcache[hash]->prev = NULL;
                }
                free(entry->name);
                free(entry->uuid);
                free(entry);
                return -1;
            } else {
                *name = malloc(strlen(entry->name)+1);
                strcpy(*name, entry->name);
                *type = entry->type;
                return 0;
            }
        }
        entry = entry->next;
    }

    return -1;
}

int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const unsigned long uid _U_) {
    int ret = 0;
    char *name = NULL;
    uuidp_t uuid = NULL;
    cacheduser_t *cacheduser = NULL;
    cacheduser_t *entry;
    unsigned char hash;

    /* allocate mem and copy values */
    name = malloc(strlen(inname)+1);
    if (!name) {
        LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
        ret = -1;
        goto cleanup;
    }

    uuid = malloc(UUID_BINSIZE);
    if (!uuid) {
        LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
        ret = -1;
        goto cleanup;
    }

    cacheduser = malloc(sizeof(cacheduser_t));
    if (!cacheduser) {
        LOG(log_error, logtype_default, "add_cachebyuuid: mallor error");
        ret = -1;
        goto cleanup;
    }

    strcpy(name, inname);
    memcpy(uuid, inuuid, UUID_BINSIZE);

    /* fill in the cacheduser */
    cacheduser->name = name;
    cacheduser->type = type;
    cacheduser->uuid = uuid;
    cacheduser->creationtime = time(NULL);
    cacheduser->prev = NULL;
    cacheduser->next = NULL;

    /* get hash */
    hash = hashuuid(uuid);

    /* insert cache entry into cache array at head of queue */
    if (uuidcache[hash] == NULL) {
        /* this queue is empty */
        uuidcache[hash] = cacheduser;
    } else {
        cacheduser->next = uuidcache[hash];
        uuidcache[hash]->prev = cacheduser;
        uuidcache[hash] = cacheduser;
    }

cleanup:
    if (ret != 0) {
        if (name)
            free(name);
        if (uuid)
            free(uuid);
        if (cacheduser)
            free(cacheduser);
    }

    return ret;
}