X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=select.c;h=7d1212c93cd63fa42d7f54bece7986b3cbf45e7c;hp=d555f1c056d738f4ac8ae8518cc143f462224213;hb=5008f98a8208d96c3c777476d6df7534c6d2ca49;hpb=e155bb7cba1be31490cb42228f5ae3598a728b0d diff --git a/select.c b/select.c index d555f1c..7d1212c 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) \ @@ -100,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 { @@ -114,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; }; @@ -122,6 +132,8 @@ 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; }; @@ -135,6 +147,8 @@ struct user_summary_info { struct user_info *ui; int ret; int osl_errno; + regex_t *preg; + int inverse_matching; }; struct user_summary_line_info { @@ -262,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) @@ -293,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; @@ -324,8 +404,12 @@ 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; @@ -347,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; @@ -366,9 +460,14 @@ 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) @@ -481,13 +580,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; @@ -498,13 +606,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) @@ -512,6 +616,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; @@ -542,8 +647,14 @@ static int print_user_list(struct user_info *ui, void *data) sort_column = UT_FILES; else sort_column = UT_BYTES; - 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) @@ -569,7 +680,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(); @@ -577,6 +688,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; @@ -587,13 +707,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) @@ -602,6 +718,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; @@ -617,6 +734,7 @@ static int print_global_list(struct format_info *fi) }; char *header = select_conf.header_given? select_conf.header_arg : "Global list\n"; + ret = output("%s", header); if (ret < 0) return ret; @@ -624,8 +742,13 @@ static int print_global_list(struct format_info *fi) sort_column = DT_FILES; else sort_column = DT_BYTES; - return adu_loop_reverse(dir_table, sort_column, &gli, + 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) @@ -734,6 +857,21 @@ open_file: 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(); @@ -757,9 +895,13 @@ out: 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) @@ -797,7 +939,33 @@ static int setup_format_string(char *fmt, struct format_info **fi) 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) { @@ -843,6 +1011,11 @@ help: return 0; } +/** + * Main function for select mode. + * + * \return Standard. + */ int com_select(void) { struct uid_range *admissible_uids = NULL;