- if (!num_uids)
- return 0;
- buf = para_malloc(size);
- FOR_EACH_USER(ui) {
- if (!ui->table)
- continue;
- write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
- }
- 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);
-}
-
-static void close_dir_table(void)
-{
- int ret;
-
- if (!dir_table)
- return;
- 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;
-}
-
-static void close_user_table(struct user_info *ui)
-{
- int ret;
-
- if (!ui || !ui->table)
- return;
- ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
- if (ret < 0)
- ERROR_LOG("failed to close user table %u: %s\n",
- (unsigned) ui->uid, error_txt(-ret));
- free((char *)ui->desc->name);
- ui->desc->name = NULL;
- free((char *)ui->desc->dir);
- ui->desc->dir = NULL;
- free(ui->desc);
- ui->desc = NULL;
- ui->table = NULL;
-}
-
-static void close_user_tables(void)
-{
- struct user_info *ui;
-
- FOR_EACH_USER(ui)
- close_user_table(ui);
-}
-
-static void close_all_tables(void)
-{
- close_dir_table();
- close_user_tables();
- free_hash_table();
-}
-
-static int com_create()
-{
- int ret = create_tables();
- if (ret < 0)
- return ret;
- ret = open_dir_table();
- if (ret < 0)
- return ret;
- ret = scan_dir(conf.base_dir_arg);
- if (ret < 0)
- goto out;
- ret = write_uid_list();
-out:
- close_all_tables();
- return ret;
-}
-
-static int read_uid_file(void)
-{
- size_t size;
- 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;
- /* hash table size should be a power of two and larger than the number of uids */
- uid_hash_table_size = 4;
- while (uid_hash_table_size < num_uids)
- uid_hash_table_size *= 2;
- create_hash_table();
- for (n = 0; n < num_uids; n++) {
- uint32_t uid = read_u32(map + n * sizeof(uid));
- ret = search_uid(uid, OPEN_USER_TABLE, NULL);
- if (ret < 0)
- goto out;
- }
-out:
- para_munmap(map, size);
- return ret;
-}