Merge commit 'fml/master'
[adu.git] / user.c
diff --git a/user.c b/user.c
index 51510256b9994e9291989177b83edae695775045..0ae4a6c9af04fe3eab521f38f9c14b74e4f69a8c 100644 (file)
--- a/user.c
+++ b/user.c
@@ -4,13 +4,12 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file user.c uid User and user ID handling. */
+/** \file user.c \brief User and user ID handling. */
 
 #include "adu.h"
 #include <dirent.h> /* readdir() */
 #include <sys/types.h>
 #include <pwd.h>
-#include "cmdline.h" /* TODO: This file should be independent of command line options */
 #include "user.h"
 #include "fd.h"
 #include "string.h"
@@ -29,6 +28,7 @@ struct uid_range {
        uint32_t high;
 };
 
+/** Iterate over all uid ranges. */
 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
 
 /** Flags for the user hash table. */
@@ -38,6 +38,7 @@ enum uid_info_flags {
        /** Whether this uid should be taken into account. */
        UI_FL_ADMISSIBLE = 2,
 };
+
 /*
  * Contains info for each user that owns at least one regular file.
  *
@@ -50,9 +51,6 @@ 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;
 
@@ -144,6 +142,15 @@ out:
        return ret;
 }
 
+/**
+ * Convert the --uid argument to an array of uid ranges.
+ *
+ * \param orig_arg The argument to the --uid option.
+ * \param ur Result pointer.
+ *
+ * Returns Negative on errors. On success, the number of uid ranges
+ * is returned.
+ */
 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
 {
        char *arg, **argv;
@@ -192,6 +199,22 @@ out:
        return ret;
 }
 
+/**
+ * Add each given user to the array of admissible users.
+ *
+ * \param users Array of user names to add.
+ * \param num_users Length of \a users.
+ * \param admissible_uids The users which are already admissible.
+ * \param num_uid_ranges The number of intervals of \a admissible_uids.
+ *
+ * For each given user, the function checks whether that user is already
+ * admissible, i.e. its uid is contained in one of the ranges given by \a
+ * admissible_uids. If it is, the function ignores that user. Otherwise, a new
+ * length-one range consisting of that uid only is appended to \a
+ * admissible_uids.
+ *
+ * \return Negative on errors, the new number of uid ranges on success.
+ */
 int append_users(char **users, int num_users,
                struct uid_range **admissible_uids, int num_uid_ranges)
 {
@@ -243,7 +266,7 @@ static int open_user_table(struct user_info *ui, int create)
        ui->desc->num_columns = NUM_UT_COLUMNS;
        ui->desc->flags = 0;
        ui->desc->column_descriptions = user_table_cols;
-       ui->desc->dir = adu_strdup(conf.database_dir_arg);
+       ui->desc->dir = adu_strdup(database_dir);
        ui->desc->name = make_message("%u", (unsigned)ui->uid);
        pw = getpwuid(ui->uid);
        if (pw && pw->pw_name)
@@ -273,9 +296,23 @@ err:
        return ret;
 }
 
+/** Iterate over each user in the uid hash table. */
 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
        uid_hash_table + uid_hash_table_size; ui++)
 
+
+/**
+ * Execute the given function for each admissible user.
+ *
+ * \param func The function to execute.
+ * \param data Arbitrary pointer.
+ *
+ * This function calls \a func for each admissible user in the uid hash table.
+ * The \a data pointer is passed as the second argument to \a func.
+ *
+ * \return As soon as \a func returns a negative value, the loop is terminated
+ * and that negative value is returned. Otherwise, the function returns 1.
+ */
 int for_each_admissible_user(int (*func)(struct user_info *, void *),
                void *data)
 {
@@ -284,8 +321,7 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
        assert(uid_hash_table);
        for (i = 0; i < uid_hash_table_size; i++) {
                int ret;
-               struct user_info *ui = uid_hash_table +
-                       uid_hash_table_sort_idx[i];
+               struct user_info *ui = uid_hash_table + i;
 
                if (!ui_used(ui) || !ui_admissible(ui))
                        continue;
@@ -296,21 +332,29 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
        return 1;
 }
 
+/** Prime number used for calculating the slot for an uid. */
 #define PRIME1 0xb11924e1
+/** Prime number used for probe all slots. */
 #define PRIME2 0x01000193
 
+/**
+ * Create a hash table large enough of given size.
+ *
+ * \param bits Sets the maximal number of hash table entries to ^bits.
+ */
 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;
 }
 
+/**
+ * Close all open user tables and destroy the uid hash table.
+ *
+ * For each used slot in the uid hash table, close the osl user table if it is
+ * open.  Finally, free the uid hash table.
+ */
 void close_user_tables(void)
 {
        struct user_info *ui;
@@ -340,8 +384,6 @@ 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;
 }
 
 /*
@@ -380,6 +422,19 @@ static struct user_info *lookup_uid(uint32_t uid)
        return NULL;
 }
 
+/**
+ * Create and open a osl table for the given uid.
+ *
+ * \param uid The user ID.
+ * \param ui_ptr Result pointer
+ *
+ * Find out whether \a uid already exists in the uid hash table.  If yes, just
+ * return the user info struct via \a ui_ptr. Otherwise, insert \a uid into the
+ * uid hash table, create and open the osl user table and also return the user
+ * info struct via \a ui_ptr.
+ *
+ * \return Standard.
+ */
 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
 {
        struct user_info *ui = lookup_uid(uid);
@@ -396,25 +451,23 @@ int create_user_table(uint32_t uid, struct user_info **ui_ptr)
 
 static char *get_uid_list_name(void)
 {
-       return make_message("%s/uid_list", conf.database_dir_arg);
-}
-
-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);
+       return make_message("%s/uid_list", database_dir);
 }
-
-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);
-}
-
+/**
+ * Open the osl tables for all admissible uids.
+ *
+ * \param admissible_uids Determines which uids are considered admissible.
+ *
+ * Each slot in the hash table contains, among other information, a bit which
+ * specifies whether the uid of the slot is admissible in the current context.
+ *
+ * This function iterates over all entries in the hash table and checks for
+ * each used slot whether the corresponding uid is admissible with respect to
+ * \a admissible_uids. If so, it sets the admissible bit for this slot and
+ * opens the osl table of the uid.
+ *
+ * \return Stamdard.
+ */
 int open_admissible_user_tables(struct uid_range *admissible_uids)
 {
        struct user_info *ui;
@@ -441,6 +494,18 @@ int open_admissible_user_tables(struct uid_range *admissible_uids)
        return 1;
 }
 
+/**
+ * Read the file of all possible uids.
+ *
+ * This is called from select/interactive mode. First a large hash table, large
+ * enough to store all uids contained in the uid file is created. Next, the
+ * uids are read from the uid file which was created during the creation of the
+ * database and each uid is inserted into the hash table.
+ *
+ * \sa write_uid_file().
+ *
+ * \return Standard.
+ */
 int read_uid_file(void)
 {
        size_t size;
@@ -483,6 +548,18 @@ out:
        return ret;
 }
 
+/**
+ * Write the list of uids to permanent storage.
+ *
+ * This is called from create mode after the dir table and all uer tables have
+ * been created. The file simply contains the list of all uids that own at
+ * least one regular file in the base directory and hence an osl table for this
+ * uid exists.
+ *
+ * \sa read_uid_file().
+ *
+ * \return Standard.
+ */
 int write_uid_file(void)
 {
        char *buf, *p, *filename;