From c7d5ddb6906ab8a18181fee29ac78d604db5fdb9 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 25 Dec 2008 12:19:56 +0100 Subject: [PATCH] Make user.c independent of command line options. --- create.c | 4 ++-- interactive.c | 3 ++- select.c | 5 +++-- user.c | 27 ++++++++++++++------------- user.h | 10 ++++++---- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/create.c b/create.c index 87b0b9a..2925d57 100644 --- a/create.c +++ b/create.c @@ -131,7 +131,7 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num) dir_size += size; dir_files++; uid = s.st_uid; - ret = create_user_table(uid, &ui); + ret = create_user_table(conf.database_dir_arg, uid, &ui); if (ret < 0) goto out; ret = update_user_row(ui->table, this_dir_num, &size); @@ -173,7 +173,7 @@ int com_create(void) ret = scan_dir(conf.base_dir_arg, &zero); if (ret < 0) goto out; - ret = write_uid_file(); + ret = write_uid_file(conf.database_dir_arg); out: return ret; } diff --git a/interactive.c b/interactive.c index 7327ee1..08f700f 100644 --- a/interactive.c +++ b/interactive.c @@ -12,6 +12,7 @@ #include "format.h" #include "user.h" #include "string.h" +#include "cmdline.h" #include "select.cmdline.h" #include "select.h" #include "error.h" @@ -213,7 +214,7 @@ int com_interactive(void) ret = parse_select_options(NULL, NULL, &admissible_uids, &fi); if (ret< 0) return ret; - ret = read_uid_file(); + ret = read_uid_file(conf.database_dir_arg); if (ret < 0) return ret; while (read_input_line(line, sizeof(line)) >= 0) { diff --git a/select.c b/select.c index e30697e..4ebfbbf 100644 --- a/select.c +++ b/select.c @@ -912,7 +912,8 @@ int run_select_query(struct uid_range *admissible_uids, struct format_info *fi) if (ret < 0) goto out; check_signals(); - ret = open_admissible_user_tables(admissible_uids); + ret = open_admissible_user_tables(conf.database_dir_arg, + admissible_uids); if (ret < 0) goto out; check_signals(); @@ -1062,7 +1063,7 @@ int com_select(void) ret = parse_select_options(conf.select_options_arg, ¶ms, &admissible_uids, &fi); if (ret > 0) { - ret = read_uid_file(); + ret = read_uid_file(conf.database_dir_arg); if (ret < 0) goto out; ret = run_select_query(admissible_uids, fi); diff --git a/user.c b/user.c index 026d65b..dfa1c10 100644 --- a/user.c +++ b/user.c @@ -10,7 +10,6 @@ #include /* readdir() */ #include #include -#include "cmdline.h" /* TODO: This file should be independent of command line options */ #include "user.h" #include "fd.h" #include "string.h" @@ -258,7 +257,7 @@ static inline int ui_admissible(struct user_info *ui) return ui->flags & UI_FL_ADMISSIBLE; } -static int open_user_table(struct user_info *ui, int create) +static int open_user_table(const char *dirname, struct user_info *ui, int create) { int ret; struct passwd *pw; @@ -267,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(dirname); ui->desc->name = make_message("%u", (unsigned)ui->uid); pw = getpwuid(ui->uid); if (pw && pw->pw_name) @@ -436,7 +435,8 @@ static struct user_info *lookup_uid(uint32_t uid) * * \return Standard. */ -int create_user_table(uint32_t uid, struct user_info **ui_ptr) +int create_user_table(const char *dirname, uint32_t uid, + struct user_info **ui_ptr) { struct user_info *ui = lookup_uid(uid); @@ -447,12 +447,12 @@ int create_user_table(uint32_t uid, struct user_info **ui_ptr) return 1; ui->uid = uid; ui->flags |= UI_FL_SLOT_USED; - return open_user_table(ui, 1); + return open_user_table(dirname, ui, 1); } -static char *get_uid_list_name(void) +static char *get_uid_list_name(const char *dirname) { - return make_message("%s/uid_list", conf.database_dir_arg); + return make_message("%s/uid_list", dirname); } /** * Open the osl tables for all admissible uids. @@ -469,7 +469,8 @@ static char *get_uid_list_name(void) * * \return Stamdard. */ -int open_admissible_user_tables(struct uid_range *admissible_uids) +int open_admissible_user_tables(const char *dirname, + struct uid_range *admissible_uids) { struct user_info *ui; @@ -488,7 +489,7 @@ int open_admissible_user_tables(struct uid_range *admissible_uids) ui->flags |= UI_FL_ADMISSIBLE; if (ui->table) continue; - ret = open_user_table(ui, 0); + ret = open_user_table(dirname, ui, 0); if (ret < 0) return ret; } @@ -507,11 +508,11 @@ int open_admissible_user_tables(struct uid_range *admissible_uids) * * \return Standard. */ -int read_uid_file(void) +int read_uid_file(const char *dirname) { size_t size; uint32_t n; - char *filename = get_uid_list_name(), *map; + char *filename = get_uid_list_name(dirname), *map; int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL); unsigned bits; @@ -561,7 +562,7 @@ out: * * \return Standard. */ -int write_uid_file(void) +int write_uid_file(const char *dirname) { char *buf, *p, *filename; size_t size = num_uids * sizeof(uint32_t); @@ -577,7 +578,7 @@ int write_uid_file(void) write_u32(p, ui->uid); p += sizeof(uint32_t); } - filename = get_uid_list_name(); + filename = get_uid_list_name(dirname); ret = adu_write_file(filename, buf, size); free(filename); free(buf); diff --git a/user.h b/user.h index a6fbbf6..c64983e 100644 --- a/user.h +++ b/user.h @@ -35,9 +35,10 @@ struct user_info { /** An opaque struct that contains info about which users are admissible. */ struct uid_range; -int create_user_table(uint32_t uid, struct user_info **ui_ptr); -int read_uid_file(void); -int write_uid_file(void); +int create_user_table(const char *dirname, uint32_t uid, + struct user_info **ui_ptr); +int read_uid_file(const char *dirname); +int write_uid_file(const char *dirname); void create_hash_table(unsigned bits); int for_each_admissible_user(int (*func)(struct user_info *, void *), @@ -46,4 +47,5 @@ int parse_uid_arg(const char *orig_arg, struct uid_range **ur); int append_users(char **users, int num_users, struct uid_range **admissible_uids, int num_uid_ranges); void close_user_tables(void); -int open_admissible_user_tables(struct uid_range *admissible_uids); +int open_admissible_user_tables(const char *dirname, + struct uid_range *admissible_uids); -- 2.39.5