]> git.tuebingen.mpg.de Git - adu.git/blobdiff - adu.c
Honor the --database_dir option.
[adu.git] / adu.c
diff --git a/adu.c b/adu.c
index 1aaea35900d67a89163d51625dee4425fc36354e..26039ce7a35a2a7fc96bfd897eec08d2ddc3febd 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -3,17 +3,13 @@
 
 #include "gcc-compat.h"
 #include "cmdline.h"
-#include "osl.h"
 #include "fd.h"
-#include "hash.h"
 #include "string.h"
 #include "error.h"
+#include "portable_io.h"
 
 DEFINE_ERRLIST;
 
-#define DATABASE_DIR "/tmp/adu"
-#define UID_LIST DATABASE_DIR "/" "uid_list"
-
 /** Command line and config file options. */
 static struct gengetopt_args_info conf;
 
@@ -48,7 +44,7 @@ __printf_2_3 void __log(int ll, const char* fmt,...)
        time_t t1;
        char str[255] = "";
 
-       if (ll < 4)
+       if (ll < conf.loglevel_arg)
                return;
        outfd = stderr;
        time(&t1);
@@ -177,7 +173,6 @@ static struct osl_table_description dir_table_desc = {
        .num_columns = NUM_DT_COLUMNS,
        .flags = 0,
        .column_descriptions = dir_table_cols,
-       .dir = DATABASE_DIR
 };
 
 /** The columns of the id table. */
@@ -246,7 +241,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 = para_strdup(DATABASE_DIR);
+       ui->desc->dir = para_strdup(conf.database_dir_arg);
        ui->desc->name = make_message("%u", (unsigned)ui->uid);
        num_uids++;
        INFO_LOG(".............................uid #%u: %u\n",
@@ -292,6 +287,7 @@ static int create_tables(void)
 {
        int ret;
 
+       dir_table_desc.dir = para_strdup(conf.database_dir_arg);
        ret = osl_create_table(&dir_table_desc);
        if (ret < 0)
                return ret;
@@ -299,7 +295,22 @@ static int create_tables(void)
        return 1;
 }
 
-
+/*
+ * We use a hash table of size s=2^uid_hash_bits to map the uids into the
+ * interval [0..s]. Hash collisions are treated by open addressing, i.e.
+ * unused slots in the table are used to store different uids that hash to the
+ * same slot.
+ *
+ * If a hash collision occurs, different slots are successively probed in order
+ * to find an unused slot for the new uid. Probing is implemented via a second
+ * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
+ * odd number.
+ *
+ * An odd number is sufficient to make sure each entry of the hash table gets
+ * probed for probe_num between 0 and s-1 because s is a power of two, hence
+ * the second hash value never hash a common divisor with the hash table size.
+ * IOW: h is invertible in the ring [0..s].
+ */
 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
 {
        return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
@@ -678,9 +689,14 @@ static int print_statistics(void)
        return 1;
 }
 
+static char *get_uid_list_name(void)
+{
+       return make_message("%s/uid_list", conf.database_dir_arg);
+}
+
 static int write_uid_list(void)
 {
-       char *buf;
+       char *buf, *filename;
        uint32_t count = 0;
        struct user_info *ui;
        size_t size = num_uids * sizeof(uint32_t);
@@ -694,13 +710,17 @@ static int write_uid_list(void)
                        continue;
                write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
        }
-       ret = para_write_file(UID_LIST, buf, size);
+       filename = get_uid_list_name();
+       ret = para_write_file(filename, buf, size);
+       free(filename);
        free(buf);
        return ret;
 }
 
 static int open_dir_table(void)
 {
+       if (!dir_table_desc.dir) /* we did not create the table */
+               dir_table_desc.dir = para_strdup(conf.database_dir_arg);
        return osl_open_table(&dir_table_desc, &dir_table);
 }
 
@@ -713,6 +733,7 @@ static void close_dir_table(void)
        ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
        if (ret < 0)
                ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
+       free((char *)dir_table_desc.dir);
        dir_table = NULL;
 }
 
@@ -750,7 +771,7 @@ static void close_all_tables(void)
        free_hash_table();
 }
 
-static int com_create(char *dirname)
+static int com_create()
 {
        int ret = create_tables();
        if (ret < 0)
@@ -758,7 +779,7 @@ static int com_create(char *dirname)
        ret = open_dir_table();
        if (ret < 0)
                return ret;
-       ret = scan_dir(dirname);
+       ret = scan_dir(conf.base_dir_arg);
        if (ret < 0)
                goto out;
        ret = write_uid_list();
@@ -769,11 +790,12 @@ out:
 
 static int read_uid_file(void)
 {
-       char *map;
        size_t size;
-       int ret = mmap_full_file(UID_LIST, O_RDONLY, (void **)&map, &size, NULL);
        uint32_t n;
+       char *filename = get_uid_list_name(), *map;
+       int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
 
+       free(filename);
        if (ret < 0)
                return ret;
        num_uids = size / 4;
@@ -810,7 +832,7 @@ static int com_select(void)
 
 int main(int argc, char **argv)
 {
-       int ret = -E_SYNTAX;
+       int ret;
        struct cmdline_parser_params params = {
                .override = 0,
                .initialize = 1,
@@ -820,12 +842,11 @@ int main(int argc, char **argv)
        };
 
        cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
-       if (argc > 2)
-               goto out;
-       if (argc == 1)
+       ret = -E_SYNTAX;
+       if (conf.select_given)
                ret = com_select();
        else
-               ret = com_create(argv[1]);
+               ret = com_create();
        if (ret < 0)
                goto out;
 out: