]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/acl/cache.c
Merge from 2-1
[netatalk.git] / libatalk / acl / cache.c
index 9f84106d267339b6d61287131c12394347274228..538ff33d8ddd85d09acd1864e82182b96ba2bd55 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: cache.c,v 1.2 2009-11-28 12:30:12 franklahm Exp $
+  $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
@@ -62,7 +62,8 @@ static int dumpcache() {
                     continue;
                 if (strftime(timestr, 200, "%c", tmp) == 0)
                     continue;
-                LOG(log_debug9, logtype_default, "namecache{%d}]: name:%s, uuid:%s, cached: %s", i, entry->name, uuidstring, timestr);
+                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);
             } while ((entry = entry->next) != NULL);
         }
@@ -77,7 +78,8 @@ static int dumpcache() {
                     continue;
                 if (strftime(timestr, 200, "%c", tmp) == 0)
                     continue;
-                LOG(log_debug9, logtype_default, "uuidcache{%d}: uuid:%s, name:%s, type:%d, cached: %s", i, uuidstring, entry->name, entry->type,timestr);
+                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);
             } while ((entry = entry->next) != NULL);
         }
@@ -101,7 +103,7 @@ static unsigned char hashstring(unsigned char *str) {
     return index;
 }
 
-/* hash uuid_t into unsigned char */
+/* hash atalk_uuid_t into unsigned char */
 static unsigned char hashuuid(uuidp_t uuid) {
     unsigned char index = 83;
     int i;
@@ -125,6 +127,10 @@ int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t
     cacheduser_t *entry;
     unsigned char hash;
 
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     /* allocate mem and copy values */
     name = malloc(strlen(inname)+1);
     if (!name) {
@@ -181,6 +187,11 @@ cleanup:
         if (cacheduser)
             free(cacheduser);
     }
+
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     return ret;
 }
 
@@ -190,6 +201,10 @@ int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
     cacheduser_t *entry;
     time_t tim;
 
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     hash = hashstring((unsigned char *)name);
 
     if (! namecache[hash])
@@ -202,22 +217,34 @@ int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
             /* found, now check if expired */
             tim = time(NULL);
             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 */
                     entry->prev->next = entry->next;
-                else        /* queue head */
-                    namecache[hash] = entry->next;
+                else  { /* queue head */
+                    if ((namecache[hash] = entry->next) != NULL)
+                        namecache[hash]->prev = NULL;
+                }
                 free(entry->name);
                 free(entry->uuid);
                 free(entry);
+#ifdef DEBUG
+                dumpcache();
+#endif
                 return -1;
             } else {
                 memcpy(uuid, entry->uuid, UUID_BINSIZE);
+#ifdef DEBUG
+                dumpcache();
+#endif
                 return 0;
             }
         }
         entry = entry->next;
     }
+#ifdef DEBUG
+    dumpcache();
+#endif
     return -1;
 }
 
@@ -227,6 +254,10 @@ int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
     cacheduser_t *entry;
     time_t tim;
 
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     hash = hashuuid(uuidp);
 
     if (! uuidcache[hash])
@@ -238,25 +269,37 @@ int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
         if (ret == 0) {
             tim = time(NULL);
             if ((tim - entry->creationtime) > CACHESECONDS) {
-                LOG(log_info, logtype_default, "search_cachebyuuid: expired: name:\'%s\' in queue {%d}", entry->name, hash);
-                if (entry->prev)
+                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;
-                else
-                    uuidcache[hash] = entry->next;
+                else { /* queue head  */
+                    if ((uuidcache[hash] = entry->next) != NULL)
+                        uuidcache[hash]->prev = NULL;
+                }
                 free(entry->name);
                 free(entry->uuid);
                 free(entry);
+#ifdef DEBUG
+                dumpcache();
+#endif
                 return -1;
             } else {
                 *name = malloc(strlen(entry->name)+1);
                 strcpy(*name, entry->name);
                 *type = entry->type;
+#ifdef DEBUG
+                dumpcache();
+#endif
                 return 0;
             }
         }
         entry = entry->next;
     }
 
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     return -1;
 }
 
@@ -268,6 +311,10 @@ int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const
     cacheduser_t *entry;
     unsigned char hash;
 
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     /* allocate mem and copy values */
     name = malloc(strlen(inname)+1);
     if (!name) {
@@ -324,5 +371,10 @@ cleanup:
         if (cacheduser)
             free(cacheduser);
     }
+
+#ifdef DEBUG
+    dumpcache();
+#endif
+
     return ret;
 }