]> git.tuebingen.mpg.de Git - adu.git/blobdiff - user.c
Make --limit work also for the user summary.
[adu.git] / user.c
diff --git a/user.c b/user.c
index 27982091e9c2fb3dcd6bb2016a61c51b21c67657..1bdd34bbc9274ebe74ffd8d0401e9ecdae3cdf9e 100644 (file)
--- a/user.c
+++ b/user.c
@@ -50,6 +50,9 @@ static struct user_info *uid_hash_table;
 /** This is always a power of two. It is set in create_hash_table(). */
 static uint32_t uid_hash_table_size;
 
+/* Array of indices to the entries of \a uid_hash_table. */
+static int *uid_hash_table_sort_idx;
+
 /** The number of used slots in the hash table. */
 static uint32_t num_uids = 0;
 
@@ -246,7 +249,7 @@ static int open_user_table(struct user_info *ui, int create)
        if (pw && pw->pw_name)
                ui->pw_name = adu_strdup(pw->pw_name);
 
-       DEBUG_LOG("opening table for uid %u\n", (unsigned)ui->uid);
+       INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
        if (create) {
                ret = osl(osl_create_table(ui->desc));
                if (ret < 0)
@@ -276,11 +279,13 @@ err:
 int for_each_admissible_user(int (*func)(struct user_info *, void *),
                void *data)
 {
-       struct user_info *ui;
+       int i;
 
        assert(uid_hash_table);
-       FOR_EACH_USER(ui) {
+       for (i = 0; i < uid_hash_table_size; i++) {
                int ret;
+               struct user_info *ui = uid_hash_table +
+                       uid_hash_table_sort_idx[i];
 
                if (!ui_used(ui) || !ui_admissible(ui))
                        continue;
@@ -296,9 +301,14 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
 
 void create_hash_table(unsigned bits)
 {
+       int i;
+
        uid_hash_table_size = 1 << bits;
        uid_hash_table = adu_calloc(uid_hash_table_size *
                sizeof(struct user_info));
+       uid_hash_table_sort_idx = adu_malloc(uid_hash_table_size * sizeof(int));
+       for (i = 0; i < uid_hash_table_size; i++)
+               uid_hash_table_sort_idx[i] = i;
 }
 
 void close_user_tables(void)
@@ -310,6 +320,9 @@ void close_user_tables(void)
 
                if (!ui_used(ui))
                        continue;
+               if (!ui->table)
+                       continue;
+               INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
                ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
                if (ret < 0)
                        ERROR_LOG("failed to close user table %u: %s\n",
@@ -327,6 +340,8 @@ void close_user_tables(void)
        }
        free(uid_hash_table);
        uid_hash_table = NULL;
+       free(uid_hash_table_sort_idx);
+       uid_hash_table_sort_idx = NULL;
 }
 
 /*
@@ -384,13 +399,50 @@ static char *get_uid_list_name(void)
        return make_message("%s/uid_list", conf.database_dir_arg);
 }
 
-void sort_hash_table(int (*comp)(const void *, const void *))
+static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
+
+static int comp_wrapper(const void *a, const void *b)
+{
+       struct user_info *x = uid_hash_table + *(unsigned *)a;
+       struct user_info *y = uid_hash_table + *(unsigned *)b;
+       return hash_table_comparator(x, y);
+}
+
+void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
+{
+       hash_table_comparator = comp;
+       qsort(uid_hash_table_sort_idx, uid_hash_table_size,
+               sizeof(*uid_hash_table_sort_idx), comp_wrapper);
+}
+
+int open_admissible_user_tables(struct uid_range *admissible_uids)
 {
-       qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
-               comp);
+       struct user_info *ui;
+
+       assert(uid_hash_table);
+       DEBUG_LOG("size: %d\n", uid_hash_table_size);
+       FOR_EACH_USER(ui) {
+               int ret;
+
+               if (!ui_used(ui)) {
+                       continue;
+               }
+               if (!uid_is_admissible(ui->uid, admissible_uids)) {
+                       DEBUG_LOG("uid %u is not admissible\n", ui->uid);
+                       ui->flags &= ~UI_FL_ADMISSIBLE;
+                       continue;
+               }
+               ui->flags |= UI_FL_ADMISSIBLE;
+               if (ui->table)
+                       continue;
+               ret = open_user_table(ui, 0);
+               if (ret < 0)
+                       return ret;
+       }
+       return 1;
 }
 
-int read_uid_file(struct uid_range *admissible_uids)
+int read_uid_file(void)
 {
        size_t size;
        uint32_t n;
@@ -420,17 +472,11 @@ int read_uid_file(struct uid_range *admissible_uids)
                assert(ui);
                if (ui_used(ui)) { /* impossible */
                        ERROR_LOG("duplicate user id!?\n");
-                       ret =-EFAULT;
+                       ret = -EFAULT;
                        goto out;
                }
                ui->uid = uid;
                ui->flags |= UI_FL_SLOT_USED;
-               if (!uid_is_admissible(uid, admissible_uids))
-                       continue;
-               ui->flags |= UI_FL_ADMISSIBLE;
-               ret = open_user_table(ui, 0);
-               if (ret < 0)
-                       return ret;
        }
        ret = 1;
 out: