]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/acl/cache.c
Merge remote-tracking branch 'remotes/origin/branch-netatalk-2-1'
[netatalk.git] / libatalk / acl / cache.c
index 4c640ac25ff425a0e27724e804749fd93ceed916..585bb4801e8d4a727531f6ef3a4bff19f9d7c001 100644 (file)
@@ -117,9 +117,8 @@ static unsigned char hashuuid(uuidp_t uuid) {
 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;
+    uuidp_t uuid = NULL;
     cacheduser_t *cacheduser = NULL;
-    cacheduser_t *entry;
     unsigned char hash;
 
 #ifdef DEBUG
@@ -162,15 +161,14 @@ int add_cachebyname( const char *inname, const uuidp_t inuuid, const uuidtype_t
     /* 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:
@@ -205,7 +203,7 @@ int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
 
     hash = hashstring((unsigned char *)name);
 
-    if (! namecache[hash])
+    if (namecache[hash] == NULL)
         return -1;
 
     entry = namecache[hash];
@@ -217,9 +215,14 @@ int search_cachebyname( const char *name, uuidtype_t type, uuidp_t uuid) {
             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;
                 }
@@ -271,9 +274,14 @@ int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
             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;
                 }
@@ -307,7 +315,7 @@ int search_cachebyuuid( uuidp_t uuidp, char **name, uuidtype_t *type) {
 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;
+    uuidp_t uuid = NULL;
     cacheduser_t *cacheduser = NULL;
     cacheduser_t *entry;
     unsigned char hash;
@@ -352,15 +360,14 @@ int add_cachebyuuid( uuidp_t inuuid, const char *inname, uuidtype_t type, const
     /* 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: