]> git.tuebingen.mpg.de Git - adu.git/commitdiff
User handling improvments.
authorAndre Noll <maan@systemlinux.org>
Sat, 8 Nov 2008 22:05:54 +0000 (23:05 +0100)
committerAndre Noll <maan@systemlinux.org>
Sat, 8 Nov 2008 22:05:54 +0000 (23:05 +0100)
- move global variable num_uids to user.c.
- logging improvements.
- replace search_uid() by create_user_table(). search_uid() had a
  horrible interface.
- in user.c, use FOR_EACH_USER() to loop over all users rather than
  for_each_admissible_user().

adu.c
adu.h
create.c
user.c
user.h

diff --git a/adu.c b/adu.c
index a586ade942d892312a7b8b552614b2cfe3fbc88e..ed1b4fa32aee7dac5a7650f2074f8f37df2820d1 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -23,8 +23,6 @@ struct gengetopt_args_info conf;
 /** Options passed to --select-options. */
 struct select_args_info select_conf;
 
-/** The number of different uids found so far. */
-uint32_t num_uids = 0;
 
 /**
  * The table containing the directory names and statistics.
diff --git a/adu.h b/adu.h
index 509c0a2248ea392decce4e2891a6c89606d34cc3..74083ec3f3eb6e849a2b2294af99aefb99f384b0 100644 (file)
--- a/adu.h
+++ b/adu.h
@@ -134,7 +134,6 @@ enum dir_table_columns {
        NUM_DT_COLUMNS
 };
 
-extern uint32_t num_uids;
 extern struct osl_table *dir_table;
 
 /** The adu command line options. */
index d08da1c026909de78ccd4386d22d9545de139c80..a5bd29810539e79a24d45fa6a287a0f644771e93 100644 (file)
--- a/create.c
+++ b/create.c
@@ -58,9 +58,7 @@ static int update_user_row(struct osl_table *t, uint64_t dir_num,
                objects[UT_BYTES].size = sizeof(*add);
                objects[UT_FILES].data = &num_files;
                objects[UT_FILES].size = sizeof(num_files);
-               INFO_LOG("######################### ret: %d\n", ret);
                ret = osl(osl_add_row(t, objects));
-               INFO_LOG("######################### ret: %d\n", ret);
                return ret;
        } else { /* add size and increment file count */
                uint64_t num;
@@ -133,7 +131,7 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num)
                dir_size += size;
                dir_files++;
                uid = s.st_uid;
-               ret = search_uid(uid, NULL, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
+               ret = create_user_table(uid, &ui);
                if (ret < 0)
                        goto out;
                ui->bytes += size;
diff --git a/user.c b/user.c
index 2b9fb202ed9f2dbc3cb584d45af659c9370c74b0..27982091e9c2fb3dcd6bb2016a61c51b21c67657 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;
 
+/** The number of used slots in the hash table. */
+static uint32_t num_uids = 0;
+
 /*
  * The columns of the per-user tables.
  *
@@ -243,8 +246,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);
 
-       INFO_LOG(".............................uid #%u: %u\n",
-               (unsigned)num_uids, (unsigned)ui->uid);
+       DEBUG_LOG("opening table for uid %u\n", (unsigned)ui->uid);
        if (create) {
                ret = osl(osl_create_table(ui->desc));
                if (ret < 0)
@@ -268,15 +270,16 @@ err:
        return ret;
 }
 
+#define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
+       uid_hash_table + uid_hash_table_size; ui++)
+
 int for_each_admissible_user(int (*func)(struct user_info *, void *),
                void *data)
 {
-       struct user_info *ui = uid_hash_table;
-
-       if (!ui)
-               return -ERRNO_TO_ERROR(EFAULT);
+       struct user_info *ui;
 
-       for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
+       assert(uid_hash_table);
+       FOR_EACH_USER(ui) {
                int ret;
 
                if (!ui_used(ui) || !ui_admissible(ui))
@@ -298,30 +301,30 @@ void create_hash_table(unsigned bits)
                sizeof(struct user_info));
 }
 
-static int close_user_table(struct user_info *ui, __a_unused void *data)
+void close_user_tables(void)
 {
-       int ret;
+       struct user_info *ui;
 
-       ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
-       if (ret < 0)
-               ERROR_LOG("failed to close user table %u: %s\n",
-                       (unsigned) ui->uid, adu_strerror(-ret));
-       free((char *)ui->desc->name);
-       ui->desc->name = NULL;
-       free((char *)ui->desc->dir);
-       ui->desc->dir = NULL;
-       free(ui->pw_name);
-       ui->pw_name = NULL;
-       free(ui->desc);
-       ui->desc = NULL;
-       ui->table = NULL;
-       ui->flags = 0;
-       return 1;
-}
+       FOR_EACH_USER(ui) {
+               int ret;
 
-void close_user_tables(void)
-{
-       for_each_admissible_user(close_user_table, NULL);
+               if (!ui_used(ui))
+                       continue;
+               ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
+               if (ret < 0)
+                       ERROR_LOG("failed to close user table %u: %s\n",
+                               (unsigned)ui->uid, adu_strerror(-ret));
+               free((char *)ui->desc->name);
+               ui->desc->name = NULL;
+               free((char *)ui->desc->dir);
+               ui->desc->dir = NULL;
+               free(ui->pw_name);
+               ui->pw_name = NULL;
+               free(ui->desc);
+               ui->desc = NULL;
+               ui->table = NULL;
+               ui->flags = 0;
+       }
        free(uid_hash_table);
        uid_hash_table = NULL;
 }
@@ -348,38 +351,32 @@ static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
                % uid_hash_table_size;
 }
 
-int search_uid(uint32_t uid, struct uid_range *urs,
-               enum search_uid_flags flags, struct user_info **ui_ptr)
+static struct user_info *lookup_uid(uint32_t uid)
 {
        uint32_t p;
 
        for (p = 0; p < uid_hash_table_size; p++) {
                struct user_info *ui = uid_hash_table + double_hash(uid, p);
-
-               if (!ui_used(ui)) {
-                       int ret;
-                       if (!flags)
-                               return -E_BAD_UID;
-                       ui->uid = uid;
-                       ui->flags |= UI_FL_SLOT_USED;
-                       if (!uid_is_admissible(uid, urs))
-                               return 0;
-                       ui->flags |= UI_FL_ADMISSIBLE;
-                       ret = open_user_table(ui, flags & CREATE_USER_TABLE);
-                       if (ret < 0)
-                               return ret;
-
-                       if (ui_ptr)
-                               *ui_ptr = ui;
-                       return 1;
-               }
-               if (ui->uid != uid)
-                       continue;
-               if (ui_ptr)
-                       *ui_ptr = ui;
-               return 0;
+               if (!ui_used(ui))
+                       return ui;
+               if (ui->uid == uid)
+                       return ui;
        }
-       return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
+       return NULL;
+}
+
+int create_user_table(uint32_t uid, struct user_info **ui_ptr)
+{
+       struct user_info *ui = lookup_uid(uid);
+
+       if (!ui)
+               return -E_HASH_TABLE_OVERFLOW;
+       *ui_ptr = ui;
+       if (ui_used(ui))
+               return 1;
+       ui->uid = uid;
+       ui->flags |= UI_FL_SLOT_USED;
+       return open_user_table(ui, 1);
 }
 
 static char *get_uid_list_name(void)
@@ -402,7 +399,7 @@ int read_uid_file(struct uid_range *admissible_uids)
        unsigned bits;
 
        if (ret < 0) {
-               INFO_LOG("failed to map %s\n", filename);
+               ERROR_LOG("failed to map %s\n", filename);
                free(filename);
                return ret;
        }
@@ -419,40 +416,47 @@ int read_uid_file(struct uid_range *admissible_uids)
        create_hash_table(bits);
        for (n = 0; n < num_uids; n++) {
                uint32_t uid = read_u32(map + n * sizeof(uid));
-               ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
-               if (ret < 0)
+               struct user_info *ui = lookup_uid(uid);
+               assert(ui);
+               if (ui_used(ui)) { /* impossible */
+                       ERROR_LOG("duplicate user id!?\n");
+                       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:
        adu_munmap(map, size);
        return ret;
 }
 
-static int write_uid(struct user_info *ui, void *data)
-{
-       char **p = data;
-
-       write_u32(*p, ui->uid);
-       *p += sizeof(uint32_t);
-       return 1;
-}
-
 int write_uid_file(void)
 {
        char *buf, *p, *filename;
        size_t size = num_uids * sizeof(uint32_t);
        int ret;
+       struct user_info *ui;
 
        if (!num_uids)
                return 0;
        buf = p = adu_malloc(size);
-       ret = for_each_admissible_user(write_uid, &p);
-       if (ret < 0)
-               goto out;
+       FOR_EACH_USER(ui) {
+               if (!ui_used(ui))
+                       continue;
+               write_u32(p, ui->uid);
+               p += sizeof(uint32_t);
+       }
        filename = get_uid_list_name();
        ret = adu_write_file(filename, buf, size);
        free(filename);
-out:
        free(buf);
        return ret;
 }
diff --git a/user.h b/user.h
index 31f3a62155f7f6d0d1ab44ad088fed413ded3053..aa07acc5213d5f2e85790b20eb84daa804ba808e 100644 (file)
--- a/user.h
+++ b/user.h
@@ -33,13 +33,7 @@ struct user_info {
 /** An opaque struct that contains info about which users are admissible. */
 struct uid_range;
 
-enum search_uid_flags {
-       OPEN_USER_TABLE = 1,
-       CREATE_USER_TABLE = 2,
-};
-int search_uid(uint32_t uid, struct uid_range *urs,
-               enum search_uid_flags flags, struct user_info **ui_ptr);
-
+int create_user_table(uint32_t uid, struct user_info **ui_ptr);
 int read_uid_file(struct uid_range *admissible_uids);
 int write_uid_file(void);