* 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 <dirent.h> /* readdir() */
+#include <sys/types.h>
+#include <regex.h>
+
#include "format.h"
#include "adu.h"
#include "gcc-compat.h"
#include "user.h"
#include "select.cmdline.h"
+/** \cond */
/* global list */
#define GLOBAL_LIST_ATOMS \
ATOM(size, SIZE) \
#undef ATOM
/* user list header */
-#define USER_LIST_HEADER_ATOMS \
+#define USER_LIST_HEADER_TRAILER_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
+struct atom user_list_header_trailer_atoms[] = {
+ USER_LIST_HEADER_TRAILER_ATOMS
{.name = NULL}
};
#undef ATOM
#define ATOM(x, y) ulha_ ## x,
-enum user_list_header_atoms {USER_LIST_HEADER_ATOMS};
+enum user_list_header_trailer_atoms {USER_LIST_HEADER_TRAILER_ATOMS};
#undef ATOM
/* user summary */
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 {
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_list_format_info {
struct format_info *fi;
struct format_info *header_fi;
+ struct format_info *trailer_fi;
};
struct user_summary_info {
struct user_info *ui;
+ /** Total number of files owned by this user. */
+ uint64_t files;
+ /** Total number of bytes owned by this user. */
+ uint64_t bytes;
+ /** Total number of directories that contain at least one file */
+ uint64_t dirs;
int ret;
int osl_errno;
+ regex_t *preg;
+ int inverse_matching;
};
-struct user_summary_line_info {
+struct user_summary_loop_data {
+ unsigned num_admissible_users;
+ struct user_summary_info *usis;
+ struct user_summary_info *current;
struct format_info *fi;
- uint32_t count;
};
static FILE *output_file;
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;
buf = format_items(fi, values);
ret = output("%s", buf);
free(buf);
- return ret;
+ if (ret < 0)
+ return ret;
+ return output("%s", select_conf.trailer_arg);
}
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;
- usi->ui->files += num;
+ usi->files += num;
ret = get_num_user_bytes(row, usi->ui, &num);
if (ret < 0)
goto err;
- usi->ui->bytes += num;
- usi->ui->dirs++;
+ usi->bytes += num;
+ usi->dirs++;
return 1;
err:
usi->ret = ret;
return ret;
}
-static int compute_user_summary(struct user_info *ui, __a_unused void *data)
+static int compute_user_summary(struct user_info *ui, void *data)
{
- struct user_summary_info usi = {.ui = ui};
+ struct user_summary_loop_data *usld = data;
+ struct user_summary_info *usi = usld->current++;
+ int ret = compile_regex(&usi->preg, &usi->inverse_matching);
- return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
- &usi.ret, &usi.osl_errno);
+ if (ret < 0)
+ return ret;
+ usi->ui = ui;
+ 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)
+static int print_user_summary_line(struct user_summary_info *usi,
+ struct format_info *fi)
{
- struct user_summary_line_info *usli = data;
+ struct user_info *ui = usi->ui;
union atom_value values[] = {
[usa_pw_name] = {.string_value = ui->pw_name?
ui->pw_name : "?"},
[usa_uid] = {.num_value = (long long unsigned)ui->uid},
- [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
- [usa_files] = {.num_value = (long long unsigned)ui->files},
- [usa_size] = {.num_value = (long long unsigned)ui->bytes}
+ [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
+ [usa_files] = {.num_value = (long long unsigned)usi->files},
+ [usa_size] = {.num_value = (long long unsigned)usi->bytes}
};
- char *buf;
- int ret = -E_LOOP_COMPLETE;
-
- if (!usli->count)
- return ret;
+ char *buf = format_items(fi, values);
+ int ret = output("%s", buf);
- buf = format_items(usli->fi, values);
- ret = output("%s", buf);
free(buf);
- usli->count--;
return ret;
}
-static int name_comp(struct user_info *a, struct user_info *b)
+static int name_comp(const void *a, const void *b)
{
- char *x = a->pw_name;
- char *y = b->pw_name;
+ const struct user_summary_info *x = a, *y = b;
+ char *n1 = x->ui->pw_name;
+ char *n2 = y->ui->pw_name;
- if (!x)
+ if (!n1)
return 1;
- if (!y)
+ if (!n2)
return -1;
- return strcmp(x, y);
+ return strcmp(n1, n2);
+}
+
+static int uid_comp(const void *a, const void *b)
+{
+ const struct user_summary_info *x = a, *y = b;
+ return -NUM_COMPARE(x->ui->uid, y->ui->uid);
}
-static int uid_comp(struct user_info *a, struct user_info *b)
+static int dir_count_comp(const void *a, const void *b)
{
- return -NUM_COMPARE(a->uid, b->uid);
+ const struct user_summary_info *x = a, *y = b;
+ return NUM_COMPARE(x->dirs, y->dirs);
}
-static int dir_count_comp(struct user_info *a, struct user_info *b)
+static int file_count_comp(const void *a, const void *b)
{
- return NUM_COMPARE(a->dirs, b->dirs);
+ const struct user_summary_info *x = a, *y = b;
+ return NUM_COMPARE(x->files, y->files);
}
-static int file_count_comp(struct user_info *a, struct user_info *b)
+static int size_comp(const void *a, const void *b)
{
- return NUM_COMPARE(a->files, b->files);
+ const struct user_summary_info *x = a, *y = b;
+ return NUM_COMPARE(x->bytes, y->bytes);
}
-static int size_comp(struct user_info *a, struct user_info *b)
+static int count_admissible_users(__a_unused struct user_info *ui, void *data)
{
- return NUM_COMPARE(a->bytes, b->bytes);
+ struct user_summary_loop_data *usld = data;
+ usld->num_admissible_users++;
+ return 1;
}
static int print_user_summary(struct format_info *fi)
{
- 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
- };
+ int i, ret;
+ int (*comp)(const void *a, const void *b);
+ struct user_summary_loop_data usld = { .fi = fi};
char *header = select_conf.header_given? select_conf.header_arg :
"User summary\n";
ret = output("%s", header);
if (ret < 0)
return ret;
- ret = for_each_admissible_user(compute_user_summary, NULL);
+ ret = for_each_admissible_user(count_admissible_users, &usld);
if (ret < 0)
return ret;
+ if (usld.num_admissible_users == 0)
+ return 1;
+ usld.usis = adu_calloc(usld.num_admissible_users
+ * sizeof(struct user_summary_info));
+ usld.current = usld.usis;
+ ret = for_each_admissible_user(compute_user_summary, &usld);
+ if (ret < 0)
+ goto out;
switch (select_conf.user_summary_sort_arg) {
case user_summary_sort_arg_name:
comp = name_comp;
case user_summary_sort_arg_size:
comp = size_comp;
break;
+ default: /* this should never happen, but anyway */
+ comp = size_comp;
+ break;
+ }
+ qsort(usld.usis, usld.num_admissible_users,
+ sizeof(struct user_summary_info), comp);
+ for (i = 0; i < usld.num_admissible_users; i++) {
+ if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
+ break;
+ ret = print_user_summary_line(usld.usis + i, usld.fi);
+ if (ret < 0)
+ goto out;
}
- sort_hash_table(comp);
- ret = for_each_admissible_user(print_user_summary_line, &usli);
- if (ret == -E_LOOP_COMPLETE)
- ret = 1;
+ ret = output("%s", select_conf.trailer_arg);
+out:
+ free(usld.usis);
return ret;
}
};
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;
.fi = ulfi->fi,
.count = select_conf.limit_arg
};
- union atom_value header_values[] = {
+ union atom_value header_trailer_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);
+ char *buf = format_items(ulfi->header_fi, header_trailer_values);
ret = output("%s", buf);
free(buf);
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);
+ if (ret < 0)
+ return ret;
+ buf = format_items(ulfi->trailer_fi, header_trailer_values);
+ ret = output("%s", buf);
+ free(buf);
+ 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";
+ char *trailer_fmt = select_conf.trailer_arg;
int ret = parse_format_string(header_fmt,
- user_list_header_atoms, &ulfi.header_fi);
+ user_list_header_trailer_atoms, &ulfi.header_fi);
+ if (ret < 0)
+ return ret;
+ ret = parse_format_string(trailer_fmt,
+ user_list_header_trailer_atoms, &ulfi.trailer_fi);
if (ret < 0)
return ret;
ret = for_each_admissible_user(print_user_list, &ulfi);
free_format_info(ulfi.header_fi);
+ free_format_info(ulfi.trailer_fi);
return ret;
}
[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);
+ if (ret < 0)
+ return ret;
+ return output("%s", select_conf.trailer_arg);
}
static int print_statistics(struct format_info *fi)
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();
return ret;
}
+/** Default format string for global_list mode. */
#define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
-#define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
+/** Default format string for global_summary mode. */
+#define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n"
+/** Default format string for user_list mode. */
#define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
-#define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
+/** Default format string for user_summary mode. */
+#define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:6) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
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)
{
return 0;
}
+/**
+ * Main function for select mode.
+ *
+ * \return Standard.
+ */
int com_select(void)
{
struct uid_range *admissible_uids = NULL;