/** \file select.c The select mode of adu. */
#include <dirent.h> /* readdir() */
+#include <sys/types.h>
+#include <regex.h>
+
#include "format.h"
#include "adu.h"
#include "gcc-compat.h"
int ret;
int osl_errno;
struct format_info *fi;
+ regex_t *preg;
+ int inverse_matching;
};
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;
};
uint32_t count;
struct user_info *ui;
struct format_info *fi;
+ regex_t *preg;
+ int inverse_matching;
int ret;
int osl_errno;
};
struct user_info *ui;
int ret;
int osl_errno;
+ regex_t *preg;
+ int inverse_matching;
};
struct user_summary_line_info {
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)
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;
[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;
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;
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)
};
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;
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)
uli->count--;
return ret;
err:
+ free(dirname);
uli->ret = ret;
uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
return ret;
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)
[gla_dirname] = {.string_value = NULL}
};
uint64_t num_files, num_bytes;
- char *dirname, *buf;
+ char *dirname = NULL, *buf;
int ret;
check_signals();
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;
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)
gli->count--;
return ret;
err:
+ free(dirname);
gli->ret = ret;
gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
return -1;
};
char *header = select_conf.header_given?
select_conf.header_arg : "Global list\n";
+
ret = output("%s", header);
if (ret < 0)
return ret;
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)