X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=select.c;h=44178805122bbd2afddaceb0d15de4918cb95d58;hp=e66b2dd41a28127dcada1a8b3cbe587ea5d464a6;hb=e302759338931fca58ee2497dbcc7c86ef6963bf;hpb=26ace9b8c1dc872e5da1b1122b45f01419761aab diff --git a/select.c b/select.c index e66b2dd..4417880 100644 --- a/select.c +++ b/select.c @@ -4,9 +4,12 @@ * Licensed under the GPL v2. For licencing details see COPYING. */ -/** \file select.c The select mode of adu. */ +/** \file select.c \brief The select mode of adu. */ #include /* readdir() */ +#include +#include + #include "format.h" #include "adu.h" #include "gcc-compat.h" @@ -17,6 +20,7 @@ #include "user.h" #include "select.cmdline.h" +/** \cond */ /* global list */ #define GLOBAL_LIST_ATOMS \ ATOM(size, SIZE) \ @@ -67,6 +71,21 @@ struct atom user_list_atoms[] = { enum user_list_atoms {USER_LIST_ATOMS}; #undef ATOM +/* user list header */ +#define USER_LIST_HEADER_ATOMS \ + ATOM(pw_name, STRING) \ + ATOM(uid, ID) + +#define ATOM(x, y) { .name = #x, .type = AT_ ## y}, +struct atom user_list_header_atoms[] = { + USER_LIST_HEADER_ATOMS + {.name = NULL} +}; +#undef ATOM +#define ATOM(x, y) ulha_ ## x, +enum user_list_header_atoms {USER_LIST_HEADER_ATOMS}; +#undef ATOM + /* user summary */ #define USER_SUMMARY_ATOMS \ ATOM(pw_name, STRING) \ @@ -85,11 +104,15 @@ struct atom user_summary_atoms[] = { enum user_summary_atoms {USER_SUMMARY_ATOMS}; #undef ATOM +/** \endcond */ + struct global_list_info { uint32_t count; int ret; int osl_errno; struct format_info *fi; + regex_t *preg; + int inverse_matching; }; struct global_summary_info { @@ -99,6 +122,8 @@ struct global_summary_info { uint64_t num_files; /** Global bytes count. */ uint64_t num_bytes; + regex_t *preg; + int inverse_matching; int ret; int osl_errno; }; @@ -107,16 +132,29 @@ struct user_list_info { uint32_t count; struct user_info *ui; struct format_info *fi; + regex_t *preg; + int inverse_matching; int ret; int osl_errno; }; +struct user_list_format_info { + struct format_info *fi; + struct format_info *header_fi; +}; + struct user_summary_info { struct user_info *ui; int ret; int osl_errno; + regex_t *preg; + int inverse_matching; }; +struct user_summary_line_info { + struct format_info *fi; + uint32_t count; +}; static FILE *output_file; @@ -238,6 +276,61 @@ static int get_num_user_bytes(struct osl_row *row, struct user_info *ui, return 1; } +static void free_regex(regex_t *preg) +{ + if (!preg) + return; + regfree(preg); + free(preg); +} + +static int compile_regex(regex_t **preg, int *invert) +{ + int ret; + size_t size; + char *buf, *p = select_conf.pattern_arg; + + if (!select_conf.pattern_given || !p[0]) { + *preg = NULL; + return 0; + } + if (p[0] == '!') { + if (!p[1]) { + *preg = NULL; + return -E_REGEX; + } + *invert = 1; + p++; + } else + *invert = 0; + *preg = adu_malloc(sizeof(regex_t)); + ret = regcomp(*preg, p, 0); + if (!ret) + return 1; + size = regerror(ret, *preg, NULL, 0); + buf = adu_malloc(size); + regerror(ret, *preg, buf, size); + ERROR_LOG("%s\n", buf); + free(buf); + free_regex(*preg); + *preg = NULL; + return -E_REGEX; +} + +static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching) +{ + int ret; + + if (!preg) + return 1; + ret = regexec(preg, dirname, 0, NULL, 0); + if (ret == REG_NOMATCH && !inverse_matching) + return 0; + if (ret != REG_NOMATCH && inverse_matching) + return 0; + return 1; +} + static int check_loop_return(int ret, int loop_ret, int loop_osl_errno) { if (ret >= 0) @@ -269,6 +362,17 @@ static int global_summary_loop_function(struct osl_row *row, void *data) int ret; uint64_t num; + if (gsi->preg) { + char *dirname; + ret = get_dir_name_of_row(row, &dirname); + if (ret < 0) + goto err; + ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching); + free(dirname); + if (!ret) + return 1; + } + ret = get_num_files_of_row(row, &num); if (ret < 0) goto err; @@ -291,6 +395,8 @@ static int print_global_summary(struct format_info *fi) int ret; char *buf; struct global_summary_info gsi = {.num_dirs = 0}; + char *header = select_conf.header_given? select_conf.header_arg : + "Global summary\n"; union atom_value values[] = { [gsa_dirs] = {.num_value = 0ULL}, @@ -298,18 +404,21 @@ static int print_global_summary(struct format_info *fi) [gsa_size] = {.num_value = 0ULL} }; + ret = compile_regex(&gsi.preg, &gsi.inverse_matching); + if (ret < 0) + return ret; ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi, global_summary_loop_function, &gsi.ret, &gsi.osl_errno); + free_regex(gsi.preg); if (ret < 0) return ret; values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs; values[gsa_files].num_value = (long long unsigned)gsi.num_files; values[gsa_size].num_value = (long long unsigned)gsi.num_bytes; - if (!select_conf.no_headers_given) { - ret = output("Global summary\n"); - if (ret < 0) - return ret; - } + + ret = output("%s", header); + if (ret < 0) + return ret; buf = format_items(fi, values); ret = output("%s", buf); free(buf); @@ -322,6 +431,16 @@ static int user_summary_loop_function(struct osl_row *row, void *data) uint64_t num; int ret; + if (usi->preg) { + char *dirname; + ret = get_dir_name_of_row(row, &dirname); + if (ret < 0) + goto err; + ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching); + free(dirname); + if (!ret) + return 1; + } ret = get_num_user_files(row, usi->ui, &num); if (ret < 0) goto err; @@ -341,14 +460,19 @@ err: static int compute_user_summary(struct user_info *ui, __a_unused void *data) { struct user_summary_info usi = {.ui = ui}; + int ret = compile_regex(&usi.preg, &usi.inverse_matching); - return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function, + if (ret < 0) + return ret; + ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function, &usi.ret, &usi.osl_errno); + free_regex(usi.preg); + return ret; } static int print_user_summary_line(struct user_info *ui, void *data) { - struct format_info *fi = data; + struct user_summary_line_info *usli = data; union atom_value values[] = { [usa_pw_name] = {.string_value = ui->pw_name? ui->pw_name : "?"}, @@ -358,18 +482,22 @@ static int print_user_summary_line(struct user_info *ui, void *data) [usa_size] = {.num_value = (long long unsigned)ui->bytes} }; char *buf; - int ret; + int ret = -E_LOOP_COMPLETE; - buf = format_items(fi, values); + if (!usli->count) + return ret; + + buf = format_items(usli->fi, values); ret = output("%s", buf); free(buf); + usli->count--; return ret; } -static int name_comp(const void *a, const void *b) +static int name_comp(struct user_info *a, struct user_info *b) { - char *x = ((struct user_info *)a)->pw_name; - char *y = ((struct user_info *)b)->pw_name; + char *x = a->pw_name; + char *y = b->pw_name; if (!x) return 1; @@ -378,56 +506,68 @@ static int name_comp(const void *a, const void *b) return strcmp(x, y); } -static int uid_comp(const void *a, const void *b) +static int uid_comp(struct user_info *a, struct user_info *b) { - return -NUM_COMPARE(((struct user_info *)a)->uid, - ((struct user_info *)b)->uid); + return -NUM_COMPARE(a->uid, b->uid); } -static int dir_count_comp(const void *a, const void *b) +static int dir_count_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->dirs, - ((struct user_info *)b)->dirs); + return NUM_COMPARE(a->dirs, b->dirs); } -static int file_count_comp(const void *a, const void *b) +static int file_count_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->files, - ((struct user_info *)b)->files); + return NUM_COMPARE(a->files, b->files); } -static int size_comp(const void *a, const void *b) +static int size_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->bytes, - ((struct user_info *)b)->bytes); + return NUM_COMPARE(a->bytes, b->bytes); } static int print_user_summary(struct format_info *fi) { - /* - * The comparators for sorting the user summary. - * - * This is an array of pointers to functions taking two constant void * - * pointers and returning an int. - */ - static int (*summary_comparators[])(const void *, const void *) = { - [user_summary_sort_arg_name] = name_comp, - [user_summary_sort_arg_uid] = uid_comp, - [user_summary_sort_arg_dir_count] = dir_count_comp, - [user_summary_sort_arg_file_count] = file_count_comp, - [user_summary_sort_arg_size] = size_comp, + int ret; + int (*comp)(struct user_info *a, struct user_info *b); + struct user_summary_line_info usli = { + .fi = fi, + .count = select_conf.limit_arg }; + char *header = select_conf.header_given? select_conf.header_arg : + "User summary\n"; - if (!select_conf.no_headers_given) { - int ret = output("User summary\n"); - if (ret < 0) - return ret; - } - int ret = for_each_admissible_user(compute_user_summary, fi); + ret = output("%s", header); + if (ret < 0) + return ret; + ret = for_each_admissible_user(compute_user_summary, NULL); if (ret < 0) return ret; - sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]); - return for_each_admissible_user(print_user_summary_line, fi); + switch (select_conf.user_summary_sort_arg) { + case user_summary_sort_arg_name: + comp = name_comp; + break; + case user_summary_sort_arg_uid: + comp = uid_comp; + break; + case user_summary_sort_arg_dir_count: + comp = dir_count_comp; + break; + case user_summary_sort_arg_file_count: + comp = file_count_comp; + break; + case user_summary_sort_arg_size: + comp = size_comp; + break; + default: /* this should never happen, but anyway */ + comp = size_comp; + break; + } + sort_hash_table(comp); + ret = for_each_admissible_user(print_user_summary_line, &usli); + if (ret == -E_LOOP_COMPLETE) + ret = 1; + return ret; } static int user_list_loop_function(struct osl_row *row, void *data) @@ -443,13 +583,22 @@ static int user_list_loop_function(struct osl_row *row, void *data) }; uint64_t num; int ret; - char *dirname, *buf; + char *dirname = NULL, *buf; check_signals(); ret = -E_LOOP_COMPLETE; if (!uli->count) goto err; + ret = get_dir_name_of_user_row(row, uli->ui, &dirname); + if (ret < 0) + goto err; + if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) { + free(dirname); + return 1; + } + values[ula_dirname].string_value = dirname; + ret = get_num_user_files(row, uli->ui, &num); if (ret < 0) goto err; @@ -460,13 +609,9 @@ static int user_list_loop_function(struct osl_row *row, void *data) goto err; values[ula_size].num_value = num; - ret = get_dir_name_of_user_row(row, uli->ui, &dirname); - if (ret < 0) - goto err; - values[ula_dirname].string_value = dirname; - buf = format_items(uli->fi, values); free(dirname); + dirname = NULL; ret = output("%s", buf); free(buf); if (ret < 0) @@ -474,6 +619,7 @@ static int user_list_loop_function(struct osl_row *row, void *data) uli->count--; return ret; err: + free(dirname); uli->ret = ret; uli->osl_errno = (ret == -E_OSL)? osl_errno : 0; return ret; @@ -481,26 +627,51 @@ err: static int print_user_list(struct user_info *ui, void *data) { - struct format_info *fi = data; + struct user_list_format_info *ulfi = data; int ret; - enum user_table_columns sort_column = UT_BYTES; + enum user_table_columns sort_column; struct user_list_info uli = { .ui = ui, - .fi = fi, + .fi = ulfi->fi, .count = select_conf.limit_arg }; + union atom_value header_values[] = { + [ulha_uid] = {.num_value = (long long unsigned)ui->uid}, + [ulha_pw_name] = {.string_value = ui->pw_name? + ui->pw_name : "?"} + }; + char *buf = format_items(ulfi->header_fi, header_values); + ret = output("%s", buf); + free(buf); + if (ret < 0) + return ret; if (select_conf.list_sort_arg == list_sort_arg_file_count) sort_column = UT_FILES; + else + sort_column = UT_BYTES; - if (!select_conf.no_headers_given) { - ret = output("%s (uid %u)\n", - ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid); - if (ret < 0) - return ret; - } - return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function, - &uli.ret, &uli.osl_errno); + ret = compile_regex(&uli.preg, &uli.inverse_matching); + if (ret < 0) + return ret; + ret = adu_loop_reverse(ui->table, sort_column, &uli, + user_list_loop_function, &uli.ret, &uli.osl_errno); + free_regex(uli.preg); + return ret; +} + +static int print_user_lists(struct format_info *fi) +{ + struct user_list_format_info ulfi = {.fi = fi}; + char *header_fmt = select_conf.header_given? + select_conf.header_arg : "uid %(uid)(%(pw_name)):\n"; + int ret = parse_format_string(header_fmt, + user_list_header_atoms, &ulfi.header_fi); + if (ret < 0) + return ret; + ret = for_each_admissible_user(print_user_list, &ulfi); + free_format_info(ulfi.header_fi); + return ret; } static int global_list_loop_function(struct osl_row *row, void *data) @@ -512,7 +683,7 @@ static int global_list_loop_function(struct osl_row *row, void *data) [gla_dirname] = {.string_value = NULL} }; uint64_t num_files, num_bytes; - char *dirname, *buf; + char *dirname = NULL, *buf; int ret; check_signals(); @@ -520,6 +691,15 @@ static int global_list_loop_function(struct osl_row *row, void *data) if (!gli->count) goto err; + ret = get_dir_name_of_row(row, &dirname); + if (ret < 0) + goto err; + if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) { + free(dirname); + return 1; + } + values[gla_dirname].string_value = dirname; + ret = get_num_files_of_row(row, &num_files); if (ret < 0) goto err; @@ -530,13 +710,9 @@ static int global_list_loop_function(struct osl_row *row, void *data) goto err; values[gla_size].num_value = (long long unsigned)num_bytes; - ret = get_dir_name_of_row(row, &dirname); - if (ret < 0) - goto err; - values[gla_dirname].string_value = dirname; - buf = format_items(gli->fi, values); free(dirname); + dirname = NULL; ret = output("%s", buf); free(buf); if (ret < 0) @@ -545,6 +721,7 @@ static int global_list_loop_function(struct osl_row *row, void *data) gli->count--; return ret; err: + free(dirname); gli->ret = ret; gli->osl_errno = (ret == -E_OSL)? osl_errno : 0; return -1; @@ -553,69 +730,181 @@ err: static int print_global_list(struct format_info *fi) { int ret; - enum dir_table_columns sort_column = DT_BYTES; + enum dir_table_columns sort_column; struct global_list_info gli = { .fi = fi, .count = select_conf.limit_arg }; + char *header = select_conf.header_given? + select_conf.header_arg : "Global list\n"; - if (!select_conf.no_headers_given) { - ret = output("Global list\n"); - if (ret < 0) - return ret; - } + ret = output("%s", header); + if (ret < 0) + return ret; if (select_conf.list_sort_arg == list_sort_arg_file_count) sort_column = DT_FILES; - return adu_loop_reverse(dir_table, sort_column, &gli, + else + sort_column = DT_BYTES; + ret = compile_regex(&gli.preg, &gli.inverse_matching); + if (ret < 0) + return ret; + ret = adu_loop_reverse(dir_table, sort_column, &gli, global_list_loop_function, &gli.ret, &gli.osl_errno); + free_regex(gli.preg); + return ret; } static int print_statistics(struct format_info *fi) { switch (select_conf.select_mode_arg) { - case select_mode_arg_global_list: - return print_global_list(fi); - case select_mode_arg_global_summary: - return print_global_summary(fi); - case select_mode_arg_user_list: - return for_each_admissible_user(print_user_list, fi); - case select_mode_arg_user_summary: - return print_user_summary(fi); + case select_mode_arg_global_list: + return print_global_list(fi); + case select_mode_arg_global_summary: + return print_global_summary(fi); + case select_mode_arg_user_list: + return print_user_lists(fi); + case select_mode_arg_user_summary: + return print_user_summary(fi); }; ERROR_LOG("bad select mode\n"); - return -ERRNO_TO_ERROR(-EINVAL); + return -ERRNO_TO_ERROR(EINVAL); } -int run_select_query(struct uid_range *admissible_uids, struct format_info *fi) +static int open_pipe(char *path) { - int ret; + int p[2], ret, argc; + char **argv; - if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) { - output_file = fopen(select_conf.output_arg, "w"); + ret = pipe(p); + if (ret < 0) + return ERRNO_TO_ERROR(errno); + ret = fork(); + if (ret < 0) + return ERRNO_TO_ERROR(errno); + if (ret) { /* parent */ + DEBUG_LOG("created process %d\n", ret); + close(p[0]); + output_file = fdopen(p[1], "w"); if (!output_file) - return -ERRNO_TO_ERROR(errno); - } else - output_file = stdout; + return ERRNO_TO_ERROR(errno); + return 1; + } + close(p[1]); + if (p[0] != STDIN_FILENO) + dup2(p[0], STDIN_FILENO); + DEBUG_LOG("executing %s\n", path); + argc = split_args(path, &argv, " \t"); + execvp(argv[0], argv); + ERROR_LOG("error executing %s: %s\n", path, + adu_strerror(ERRNO_TO_ERROR(errno))); + _exit(EXIT_FAILURE); +} + +static int open_output_stream(void) +{ + char *p; + int ret, flags = O_WRONLY | O_CREAT; + + if (!select_conf.output_given) + goto use_stdout; + p = select_conf.output_arg; + switch (p[0]) { + case '\0': /* empty string */ + goto bad_output_arg; + case '-': + if (!p[1]) /* "-" means stdout */ + goto use_stdout; + /* string starting with a dash */ + flags |= O_EXCL; + goto open_file; + case '>': + if (!p[1]) /* ">" is invalid */ + goto bad_output_arg; + if (p[1] != '>') { + p++; + flags |= O_TRUNC; + goto open_file; + } + /* string starting with ">>" */ + if (!p[2]) /* ">>" is invalid */ + goto bad_output_arg; + flags |= O_APPEND; + p += 2; + goto open_file; + case '|': + if (!p[1]) /* "|" is invalid */ + goto bad_output_arg; + p++; + return open_pipe(p); + default: /* args starts with no magic character */ + flags |= O_EXCL; + goto open_file; + } +use_stdout: + output_file = stdout; + return 1; +bad_output_arg: + output_file = NULL; + return -E_BAD_OUTPUT_ARG; +open_file: + /* + * glibc's 'x' mode to fopen is not portable, so use open() and + * fdopen(). + */ + ret = open(p, flags, 0644); + if (ret < 0) + return -ERRNO_TO_ERROR(errno); + output_file = fdopen(ret, "w"); + if (!output_file) + return -ERRNO_TO_ERROR(errno); + return 1; +} +/** + * Execute a select query. + * + * \param admissible_uids User IDs to take into account. + * \param fi Format information. + * + * Called once in select mode or for each \a run command in interactive mode. + * + * Open the output stream and the dir table if not already open. For each + * admissible uid, the user table is opened if necessary. After these + * preparations, the output according to \a select_mode and \a fi is written to + * the output stream. + * + * \return Standard. + */ +int run_select_query(struct uid_range *admissible_uids, struct format_info *fi) +{ + int ret = open_output_stream(); + + if (ret < 0) + goto out; ret = open_dir_table(0); if (ret < 0) goto out; check_signals(); - ret = read_uid_file(admissible_uids); + ret = open_admissible_user_tables(admissible_uids); if (ret < 0) goto out; check_signals(); ret = print_statistics(fi); out: - close_all_tables(); - if (output_file != stdout) + if (output_file && output_file != stdout) { fclose(output_file); + output_file = NULL; + } return ret; } +/** Default format string for global_list mode. */ #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n" +/** Default format string for global_summary mode. */ #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n" +/** Default format string for user_list mode. */ #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n" +/** Default format string for user_summary mode. */ #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n" static int setup_format_string(char *fmt, struct format_info **fi) @@ -647,13 +936,39 @@ static int setup_format_string(char *fmt, struct format_info **fi) break; default: ERROR_LOG("bad select mode\n"); - return -ERRNO_TO_ERROR(-EINVAL); + return -ERRNO_TO_ERROR(EINVAL); }; INFO_LOG("format string: %s\n", fmt); return parse_format_string(fmt, atoms, fi); } -/* return: < 0: error, >0: OK, == 0: help given */ +/** + * Parse a given format string. + * + * \param string The format string to parse. + * \param params gengetopt parameters. + * \param admissible_uids The array of admissible uid ranges. + * \param fi The format info to be used with format_items(). + * + * If \a string is not \p NULL, it is broken down into its components using + * \ref create_argv() and the resulting argument vector is passed together with + * \a params to gengetopt's command line parser. If --help or --detailed-help + * was specified in \a string, the corresponding help text is printed and the + * function returns zero. + * + * Otherwise, any --uid or --user options are parsed and transformed into an + * array of admissible uids which is returned via \a admissible_uids. + * + * Finally, the format string given by --format (or the default format string + * for the given select mode if no --format option was given in \a string) is + * parsed as well resulting in a format_info structure which is returned via + * \a fi. The caller uses the \a fi pointer later to format each output line. + * + * \return Negative on errors, zero if --help or --detailed-help was given, + * positive otherwise. + * + * \sa format_items(). + */ int parse_select_options(char *string, struct select_cmdline_parser_params *params, struct uid_range **admissible_uids, struct format_info **fi) { @@ -699,6 +1014,11 @@ help: return 0; } +/** + * Main function for select mode. + * + * \return Standard. + */ int com_select(void) { struct uid_range *admissible_uids = NULL; @@ -712,12 +1032,16 @@ int com_select(void) .print_errors = 1 }; - select_cmdline_parser_init(&select_conf); ret = parse_select_options(conf.select_options_arg, ¶ms, &admissible_uids, &fi); - if (ret <= 0) /* do not run query if help was given */ - return ret; - ret = run_select_query(admissible_uids, fi); - free_format_info(fi); + if (ret > 0) { + ret = read_uid_file(); + if (ret < 0) + goto out; + ret = run_select_query(admissible_uids, fi); + free_format_info(fi); + } +out: + select_cmdline_parser_free(&select_conf); return ret; }