/*
- $Id: cache.c,v 1.6 2010-04-23 11:37:05 franklahm Exp $
Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
This program is free software; you can redistribute it and/or modify
typedef struct cacheduser {
unsigned long uid; /* for future use */
uuidtype_t type;
- uuidp_t uuid;
+ unsigned char *uuid;
char *name;
time_t creationtime;
struct cacheduser *prev;
int i;
int ret = 0;
cacheduser_t *entry;
- char *uuidstring = NULL;
char timestr[200];
struct tm *tmp = NULL;
for ( i=0 ; i<256; i++) {
if ((entry = namecache[i]) != NULL) {
do {
- uuid_bin2string(entry->uuid, &uuidstring);
tmp = localtime(&entry->creationtime);
if (tmp == NULL)
continue;
if (strftime(timestr, 200, "%c", tmp) == 0)
continue;
LOG(log_debug9, logtype_default, "namecache{%d}: name:%s, uuid:%s, type: %s, cached: %s",
- i, entry->name, uuidstring, uuidtype[entry->type], timestr);
- free(uuidstring);
+ i, entry->name, uuid_bin2string(entry->uuid), uuidtype[entry->type], timestr);
} while ((entry = entry->next) != NULL);
}
}
for ( i=0; i<256; i++) {
if ((entry = uuidcache[i]) != NULL) {
do {
- uuid_bin2string(entry->uuid, &uuidstring);
+
tmp = localtime(&entry->creationtime);
if (tmp == NULL)
continue;
if (strftime(timestr, 200, "%c", tmp) == 0)
continue;
LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type: %s, cached: %s",
- i, uuidstring, entry->name, uuidtype[entry->type], timestr);
- free(uuidstring);
+ i, uuid_bin2string(entry->uuid), entry->name, uuidtype[entry->type], timestr);
} while ((entry = entry->next) != NULL);
}
}
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;
+ unsigned char *uuid = NULL;
cacheduser_t *cacheduser = NULL;
- cacheduser_t *entry;
unsigned char hash;
#ifdef DEBUG
/* get hash */
hash = hashstring((unsigned char *)name);
- /* insert cache entry into cache array */
- if (namecache[hash] == NULL) { /* this queue is empty */
+ /* 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;
- } else { /* queue is not empty, search end of queue*/
- entry = namecache[hash];
- while( entry->next != NULL)
- entry = entry->next;
- cacheduser->prev = entry;
- entry->next = cacheduser;
}
cleanup:
return ret;
}
-int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
+/*
+ * Caller provides buffer uuid for result
+ */
+int search_cachebyname( const char *name, uuidtype_t type, unsigned char *uuid) {
int ret;
unsigned char hash;
cacheduser_t *entry;
hash = hashstring((unsigned char *)name);
- if (! namecache[hash])
+ if (namecache[hash] == NULL)
return -1;
entry = namecache[hash];
if ((tim - entry->creationtime) > CACHESECONDS) {
LOG(log_debug, logtype_default, "search_cachebyname: expired: name:\'%s\' in queue {%d}", entry->name, hash);
/* remove item */
- if (entry->prev) /* 2nd to last in queue */
+ if (entry->prev) {
+ /* 2nd to last in queue */
entry->prev->next = entry->next;
- else { /* queue head */
+ 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;
}
return -1;
}
+/*
+ * Caller must free allocated name
+ */
int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
int ret;
unsigned char hash;
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 */
+ if (entry->prev) {
+ /* 2nd to last in queue */
entry->prev->next = entry->next;
- else { /* queue head */
+ 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;
}
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;
+ unsigned char *uuid = NULL;
cacheduser_t *cacheduser = NULL;
cacheduser_t *entry;
unsigned char hash;
/* get hash */
hash = hashuuid(uuid);
- /* insert cache entry into cache array */
- if (uuidcache[hash] == NULL) { /* this queue is empty */
+ /* 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;
- } else { /* queue is not empty, search end of queue*/
- entry = uuidcache[hash];
- while( entry->next != NULL)
- entry = entry->next;
- cacheduser->prev = entry;
- entry->next = cacheduser;
}
cleanup: