From: Andre Noll Date: Sun, 9 Nov 2008 22:18:40 +0000 (+0100) Subject: Implement pattern matching for select mode. X-Git-Tag: v0.0.5~1^2~6 X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=commitdiff_plain;h=63cf7c8fbf05a53b58e1de1116ec2660b9cb7801 Implement pattern matching for select mode. --- diff --git a/adu.ggo b/adu.ggo index ec24054..bbca871 100644 --- a/adu.ggo +++ b/adu.ggo @@ -48,19 +48,6 @@ details=" goes to stdout. Lower values mean more verbose logging. " -option "paths" p -#~~~~~~~~~~~~~~~ -"files to take into account" -string typestr="pattern" -optional -details=" - Shell wildcard pattern that must match a file in order to be - included in the database in --create mode or in the output - for --select mode. Only the part of the filename below the - base directory is matched against the pattern. The default - is to take all files into account. See fnmatch(3) for details. -" - ############### section "Modes" ############### diff --git a/error.h b/error.h index 87d3fe2..4c9bcac 100644 --- a/error.h +++ b/error.h @@ -33,6 +33,7 @@ _ERROR(BAD_UNIT, "invalid unit specifier") \ _ERROR(BAD_ATOM, "invalid atom") \ _ERROR(BAD_OUTPUT_ARG, "invalid name for output") \ + _ERROR(REGEX, "regular expression error") /** diff --git a/select.c b/select.c index d555f1c..cd18bee 100644 --- a/select.c +++ b/select.c @@ -7,6 +7,9 @@ /** \file select.c The select mode of adu. */ #include /* readdir() */ +#include +#include + #include "format.h" #include "adu.h" #include "gcc-compat.h" @@ -105,6 +108,8 @@ struct global_list_info { int ret; int osl_errno; struct format_info *fi; + regex_t *preg; + int inverse_matching; }; struct global_summary_info { @@ -114,6 +119,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 +129,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 +144,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 +273,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 +359,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 +401,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 +428,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 +457,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 +577,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 +603,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 +613,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 +644,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 +677,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 +685,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 +704,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 +715,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 +731,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 +739,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) diff --git a/select.ggo b/select.ggo index 6384c39..99ed133 100644 --- a/select.ggo +++ b/select.ggo @@ -47,6 +47,28 @@ details=" except global_summary (which outputs only one single line). " +option "pattern" p +#~~~~~~~~~~~~~~~~~ +"only consider matching directories" +string typestr="regex" +optional +details=" + Regular expression that must match the directory name for + the directory to be considered for the output of the query. + See regex(7) for details. + + Depending on whether --print-base-dir is given, the absolute + directory name or only the part of the directory name below + the base directory is matched against \"regex\". + + If this option is not given (the default) all directories + are taken into account. + + If \"regex\" starts with '!', directories are matched against + the remaining part of \"regex\" and the sense of matching is + reversed. +" + option "header" H #~~~~~~~~~~~~~~~~ "use a customized header for listings/summaries"