diff src/director/user-directory.c @ 13018:dab7043e8263

director: Avoid potential problems by making sure users list is always sorted by timestamp.
author Timo Sirainen <tss@iki.fi>
date Wed, 11 May 2011 15:57:47 +0300
parents 44d0474a451e
children d9d5759196ee
line wrap: on
line diff
--- a/src/director/user-directory.c	Wed May 11 15:35:20 2011 +0300
+++ b/src/director/user-directory.c	Wed May 11 15:57:47 2011 +0300
@@ -68,14 +68,31 @@
 user_directory_add(struct user_directory *dir, unsigned int username_hash,
 		   struct mail_host *host, time_t timestamp)
 {
-	struct user *user;
+	struct user *user, *pos;
 
 	user = i_new(struct user, 1);
 	user->username_hash = username_hash;
 	user->host = host;
 	user->host->user_count++;
 	user->timestamp = timestamp;
-	DLLIST2_APPEND(&dir->head, &dir->tail, user);
+
+	if (dir->tail == NULL || dir->tail->timestamp <= timestamp)
+		DLLIST2_APPEND(&dir->head, &dir->tail, user);
+	else {
+		/* need to insert to correct position */
+		for (pos = dir->tail; pos != NULL; pos = pos->prev) {
+			if (pos->timestamp <= timestamp)
+				break;
+		}
+		if (pos == NULL)
+			DLLIST2_PREPEND(&dir->head, &dir->tail, user);
+		else {
+			user->prev = pos;
+			user->next = pos->next;
+			user->prev->next = user;
+			user->next->prev = user;
+		}
+	}
 
 	hash_table_insert(dir->hash, POINTER_CAST(user->username_hash), user);
 	return user;