2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file select.c \brief The select mode of adu. */
9 #include <dirent.h> /* readdir() */
10 #include <sys/types.h>
15 #include "gcc-compat.h"
21 #include "select.cmdline.h"
25 #define GLOBAL_LIST_ATOMS \
28 ATOM(dirname, STRING) \
30 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
31 struct atom global_list_atoms[] = {
36 #define ATOM(x, y) gla_ ## x,
37 enum global_list_atoms {GLOBAL_LIST_ATOMS};
41 #define GLOBAL_SUMMARY_ATOMS \
46 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
47 struct atom global_summary_atoms[] = {
52 #define ATOM(x, y) gsa_ ## x,
53 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
57 #define USER_LIST_ATOMS \
58 ATOM(pw_name, STRING) \
62 ATOM(dirname, STRING) \
64 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
65 struct atom user_list_atoms[] = {
70 #define ATOM(x, y) ula_ ## x,
71 enum user_list_atoms {USER_LIST_ATOMS};
74 /* user list header */
75 #define USER_LIST_HEADER_ATOMS \
76 ATOM(pw_name, STRING) \
79 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
80 struct atom user_list_header_atoms[] = {
81 USER_LIST_HEADER_ATOMS
85 #define ATOM(x, y) ulha_ ## x,
86 enum user_list_header_atoms {USER_LIST_HEADER_ATOMS};
90 #define USER_SUMMARY_ATOMS \
91 ATOM(pw_name, STRING) \
97 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
98 struct atom user_summary_atoms[] = {
103 #define ATOM(x, y) usa_ ## x,
104 enum user_summary_atoms {USER_SUMMARY_ATOMS};
109 struct global_list_info {
113 struct format_info *fi;
115 int inverse_matching;
118 struct global_summary_info {
119 /** Global dir count. */
121 /** Global files count. */
123 /** Global bytes count. */
126 int inverse_matching;
131 struct user_list_info {
133 struct user_info *ui;
134 struct format_info *fi;
136 int inverse_matching;
141 struct user_list_format_info {
142 struct format_info *fi;
143 struct format_info *header_fi;
146 struct user_summary_info {
147 struct user_info *ui;
148 /** Total number of files owned by this user. */
150 /** Total number of bytes owned by this user. */
152 /** Total number of directories that contain at least one file */
157 int inverse_matching;
160 struct user_summary_loop_data {
161 unsigned num_admissible_users;
162 struct user_summary_info *usis;
163 struct user_summary_info *current;
164 struct format_info *fi;
167 static FILE *output_file;
169 __printf_1_2 static int output(const char const *fmt, ...)
175 ret = vfprintf(output_file, fmt, argp);
177 return ret < 0? -E_OUTPUT : 1;
180 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
182 char *result = NULL, *tmp;
184 uint64_t val = *dirnum;
185 struct osl_object obj;
191 obj.size = sizeof(val);
192 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
195 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
198 val = *(uint64_t *)obj.data;
199 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
202 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
203 tmp = make_message("%s/%s", pfx, result? result : "");
219 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
221 struct osl_object obj;
225 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
228 return get_dir_name_by_number((uint64_t *)obj.data, name);
231 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
232 struct user_info *ui, char **dirname)
234 struct osl_object obj;
235 int ret = osl(osl_get_object(ui->table, user_table_row,
240 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
243 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
245 struct osl_object obj;
246 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
249 *num_files = *(uint64_t *)obj.data;
253 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
256 struct osl_object obj;
257 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
261 *num_files = *(uint64_t *)obj.data;
265 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
267 struct osl_object obj;
268 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
271 *num_bytes = *(uint64_t *)obj.data;
275 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
278 struct osl_object obj;
279 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
283 *num_bytes = *(uint64_t *)obj.data;
287 static void free_regex(regex_t *preg)
295 static int compile_regex(regex_t **preg, int *invert)
299 char *buf, *p = select_conf.pattern_arg;
301 if (!select_conf.pattern_given || !p[0]) {
314 *preg = adu_malloc(sizeof(regex_t));
315 ret = regcomp(*preg, p, 0);
318 size = regerror(ret, *preg, NULL, 0);
319 buf = adu_malloc(size);
320 regerror(ret, *preg, buf, size);
321 ERROR_LOG("%s\n", buf);
328 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
334 ret = regexec(preg, dirname, 0, NULL, 0);
335 if (ret == REG_NOMATCH && !inverse_matching)
337 if (ret != REG_NOMATCH && inverse_matching)
342 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
346 assert(ret == -E_OSL);
347 if (osl_errno != E_OSL_LOOP)
348 /* error not caused by loop function returning negative. */
350 assert(loop_ret < 0);
351 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
353 if (loop_ret == -E_OSL) { /* osl error in loop function */
354 assert(loop_osl_errno);
355 osl_errno = loop_osl_errno;
360 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
361 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
363 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
364 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
367 static int global_summary_loop_function(struct osl_row *row, void *data)
369 struct global_summary_info *gsi = data;
375 ret = get_dir_name_of_row(row, &dirname);
378 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
384 ret = get_num_files_of_row(row, &num);
387 gsi->num_files += num;
389 ret = get_num_bytes_of_row(row, &num);
392 gsi->num_bytes += num;
397 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
401 static int print_global_summary(struct format_info *fi)
405 struct global_summary_info gsi = {.num_dirs = 0};
406 char *header = select_conf.header_given? select_conf.header_arg :
409 union atom_value values[] = {
410 [gsa_dirs] = {.num_value = 0ULL},
411 [gsa_files] = {.num_value = 0ULL},
412 [gsa_size] = {.num_value = 0ULL}
415 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
418 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
419 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
420 free_regex(gsi.preg);
423 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
424 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
425 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
427 ret = output("%s", header);
430 buf = format_items(fi, values);
431 ret = output("%s", buf);
436 static int user_summary_loop_function(struct osl_row *row, void *data)
438 struct user_summary_info *usi = data;
444 ret = get_dir_name_of_row(row, &dirname);
447 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
452 ret = get_num_user_files(row, usi->ui, &num);
456 ret = get_num_user_bytes(row, usi->ui, &num);
464 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
468 static int compute_user_summary(struct user_info *ui, void *data)
470 struct user_summary_loop_data *usld = data;
471 struct user_summary_info *usi = usld->current++;
472 int ret = compile_regex(&usi->preg, &usi->inverse_matching);
477 ret = adu_loop_reverse(ui->table, UT_BYTES, usi, user_summary_loop_function,
478 &usi->ret, &usi->osl_errno);
479 free_regex(usi->preg);
483 static int print_user_summary_line(struct user_summary_info *usi,
484 struct format_info *fi)
486 struct user_info *ui = usi->ui;
487 union atom_value values[] = {
488 [usa_pw_name] = {.string_value = ui->pw_name?
490 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
491 [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
492 [usa_files] = {.num_value = (long long unsigned)usi->files},
493 [usa_size] = {.num_value = (long long unsigned)usi->bytes}
495 char *buf = format_items(fi, values);
496 int ret = output("%s", buf);
502 static int name_comp(const void *a, const void *b)
504 const struct user_summary_info *x = a, *y = b;
505 char *n1 = x->ui->pw_name;
506 char *n2 = y->ui->pw_name;
512 return strcmp(n1, n2);
515 static int uid_comp(const void *a, const void *b)
517 const struct user_summary_info *x = a, *y = b;
518 return -NUM_COMPARE(x->ui->uid, y->ui->uid);
521 static int dir_count_comp(const void *a, const void *b)
523 const struct user_summary_info *x = a, *y = b;
524 return NUM_COMPARE(x->dirs, y->dirs);
527 static int file_count_comp(const void *a, const void *b)
529 const struct user_summary_info *x = a, *y = b;
530 return NUM_COMPARE(x->files, y->files);
533 static int size_comp(const void *a, const void *b)
535 const struct user_summary_info *x = a, *y = b;
536 return NUM_COMPARE(x->bytes, y->bytes);
539 static int count_admissible_users(__a_unused struct user_info *ui, void *data)
541 struct user_summary_loop_data *usld = data;
542 usld->num_admissible_users++;
546 static int print_user_summary(struct format_info *fi)
549 int (*comp)(const void *a, const void *b);
550 struct user_summary_loop_data usld = { .fi = fi};
551 char *header = select_conf.header_given? select_conf.header_arg :
554 ret = output("%s", header);
557 ret = for_each_admissible_user(count_admissible_users, &usld);
560 if (usld.num_admissible_users == 0)
562 usld.usis = adu_calloc(usld.num_admissible_users
563 * sizeof(struct user_summary_info));
564 usld.current = usld.usis;
565 ret = for_each_admissible_user(compute_user_summary, &usld);
568 switch (select_conf.user_summary_sort_arg) {
569 case user_summary_sort_arg_name:
572 case user_summary_sort_arg_uid:
575 case user_summary_sort_arg_dir_count:
576 comp = dir_count_comp;
578 case user_summary_sort_arg_file_count:
579 comp = file_count_comp;
581 case user_summary_sort_arg_size:
584 default: /* this should never happen, but anyway */
588 qsort(usld.usis, usld.num_admissible_users,
589 sizeof(struct user_summary_info), comp);
590 for (i = 0; i < usld.num_admissible_users; i++) {
591 if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
593 print_user_summary_line(usld.usis + i, usld.fi);
600 static int user_list_loop_function(struct osl_row *row, void *data)
602 struct user_list_info *uli = data;
603 union atom_value values[] = {
604 [ula_pw_name] = {.string_value = uli->ui->pw_name?
605 uli->ui->pw_name : "?"},
606 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
607 [ula_files] = {.num_value = 0ULL},
608 [ula_size] = {.num_value = 0ULL},
609 [ula_dirname] = {.string_value = NULL}
613 char *dirname = NULL, *buf;
616 ret = -E_LOOP_COMPLETE;
620 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
623 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
627 values[ula_dirname].string_value = dirname;
629 ret = get_num_user_files(row, uli->ui, &num);
632 values[ula_files].num_value = num;
634 ret = get_num_user_bytes(row, uli->ui, &num);
637 values[ula_size].num_value = num;
639 buf = format_items(uli->fi, values);
642 ret = output("%s", buf);
651 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
655 static int print_user_list(struct user_info *ui, void *data)
657 struct user_list_format_info *ulfi = data;
659 enum user_table_columns sort_column;
660 struct user_list_info uli = {
663 .count = select_conf.limit_arg
665 union atom_value header_values[] = {
666 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
667 [ulha_pw_name] = {.string_value = ui->pw_name?
670 char *buf = format_items(ulfi->header_fi, header_values);
672 ret = output("%s", buf);
676 if (select_conf.list_sort_arg == list_sort_arg_file_count)
677 sort_column = UT_FILES;
679 sort_column = UT_BYTES;
681 ret = compile_regex(&uli.preg, &uli.inverse_matching);
684 ret = adu_loop_reverse(ui->table, sort_column, &uli,
685 user_list_loop_function, &uli.ret, &uli.osl_errno);
686 free_regex(uli.preg);
690 static int print_user_lists(struct format_info *fi)
692 struct user_list_format_info ulfi = {.fi = fi};
693 char *header_fmt = select_conf.header_given?
694 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
695 int ret = parse_format_string(header_fmt,
696 user_list_header_atoms, &ulfi.header_fi);
699 ret = for_each_admissible_user(print_user_list, &ulfi);
700 free_format_info(ulfi.header_fi);
704 static int global_list_loop_function(struct osl_row *row, void *data)
706 struct global_list_info *gli = data;
707 union atom_value values[] = {
708 [gla_size] = {.num_value = 0ULL},
709 [gla_files] = {.num_value = 0ULL},
710 [gla_dirname] = {.string_value = NULL}
712 uint64_t num_files, num_bytes;
713 char *dirname = NULL, *buf;
717 ret = -E_LOOP_COMPLETE;
721 ret = get_dir_name_of_row(row, &dirname);
724 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
728 values[gla_dirname].string_value = dirname;
730 ret = get_num_files_of_row(row, &num_files);
733 values[gla_files].num_value = (long long unsigned)num_files;
735 ret = get_num_bytes_of_row(row, &num_bytes);
738 values[gla_size].num_value = (long long unsigned)num_bytes;
740 buf = format_items(gli->fi, values);
743 ret = output("%s", buf);
753 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
757 static int print_global_list(struct format_info *fi)
760 enum dir_table_columns sort_column;
761 struct global_list_info gli = {
763 .count = select_conf.limit_arg
765 char *header = select_conf.header_given?
766 select_conf.header_arg : "Global list\n";
768 ret = output("%s", header);
771 if (select_conf.list_sort_arg == list_sort_arg_file_count)
772 sort_column = DT_FILES;
774 sort_column = DT_BYTES;
775 ret = compile_regex(&gli.preg, &gli.inverse_matching);
778 ret = adu_loop_reverse(dir_table, sort_column, &gli,
779 global_list_loop_function, &gli.ret, &gli.osl_errno);
780 free_regex(gli.preg);
784 static int print_statistics(struct format_info *fi)
786 switch (select_conf.select_mode_arg) {
787 case select_mode_arg_global_list:
788 return print_global_list(fi);
789 case select_mode_arg_global_summary:
790 return print_global_summary(fi);
791 case select_mode_arg_user_list:
792 return print_user_lists(fi);
793 case select_mode_arg_user_summary:
794 return print_user_summary(fi);
796 ERROR_LOG("bad select mode\n");
797 return -ERRNO_TO_ERROR(EINVAL);
800 static int open_pipe(char *path)
807 return ERRNO_TO_ERROR(errno);
810 return ERRNO_TO_ERROR(errno);
811 if (ret) { /* parent */
812 DEBUG_LOG("created process %d\n", ret);
814 output_file = fdopen(p[1], "w");
816 return ERRNO_TO_ERROR(errno);
820 if (p[0] != STDIN_FILENO)
821 dup2(p[0], STDIN_FILENO);
822 DEBUG_LOG("executing %s\n", path);
823 argc = split_args(path, &argv, " \t");
824 execvp(argv[0], argv);
825 ERROR_LOG("error executing %s: %s\n", path,
826 adu_strerror(ERRNO_TO_ERROR(errno)));
830 static int open_output_stream(void)
833 int ret, flags = O_WRONLY | O_CREAT;
835 if (!select_conf.output_given)
837 p = select_conf.output_arg;
839 case '\0': /* empty string */
842 if (!p[1]) /* "-" means stdout */
844 /* string starting with a dash */
848 if (!p[1]) /* ">" is invalid */
855 /* string starting with ">>" */
856 if (!p[2]) /* ">>" is invalid */
862 if (!p[1]) /* "|" is invalid */
866 default: /* args starts with no magic character */
871 output_file = stdout;
875 return -E_BAD_OUTPUT_ARG;
878 * glibc's 'x' mode to fopen is not portable, so use open() and
881 ret = open(p, flags, 0644);
883 return -ERRNO_TO_ERROR(errno);
884 output_file = fdopen(ret, "w");
886 return -ERRNO_TO_ERROR(errno);
891 * Execute a select query.
893 * \param admissible_uids User IDs to take into account.
894 * \param fi Format information.
896 * Called once in select mode or for each \a run command in interactive mode.
898 * Open the output stream and the dir table if not already open. For each
899 * admissible uid, the user table is opened if necessary. After these
900 * preparations, the output according to \a select_mode and \a fi is written to
905 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
907 int ret = open_output_stream();
911 ret = open_dir_table(0);
915 ret = open_admissible_user_tables(conf.database_dir_arg,
920 ret = print_statistics(fi);
922 if (output_file && output_file != stdout) {
929 /** Default format string for global_list mode. */
930 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
931 /** Default format string for global_summary mode. */
932 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
933 /** Default format string for user_list mode. */
934 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
935 /** Default format string for user_summary mode. */
936 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
938 static int setup_format_string(char *fmt, struct format_info **fi)
943 INFO_LOG("using default format string\n");
944 switch (select_conf.select_mode_arg) {
945 case select_mode_arg_global_list:
947 fmt = GLOBAL_LIST_DFLT_FMT;
948 atoms = global_list_atoms;
950 case select_mode_arg_global_summary:
952 fmt = GLOBAL_SUMMARY_DFLT_FMT;
953 atoms = global_summary_atoms;
955 case select_mode_arg_user_list:
957 fmt = USER_LIST_DFLT_FMT;
958 atoms = user_list_atoms;
960 case select_mode_arg_user_summary:
962 fmt = USER_SUMMARY_DFLT_FMT;
963 atoms = user_summary_atoms;
966 ERROR_LOG("bad select mode\n");
967 return -ERRNO_TO_ERROR(EINVAL);
969 INFO_LOG("format string: %s\n", fmt);
970 return parse_format_string(fmt, atoms, fi);
974 * Parse a given format string.
976 * \param string The format string to parse.
977 * \param params gengetopt parameters.
978 * \param admissible_uids The array of admissible uid ranges.
979 * \param fi The format info to be used with format_items().
981 * If \a string is not \p NULL, it is broken down into its components using
982 * \ref create_argv() and the resulting argument vector is passed together with
983 * \a params to gengetopt's command line parser. If --help or --detailed-help
984 * was specified in \a string, the corresponding help text is printed and the
985 * function returns zero.
987 * Otherwise, any --uid or --user options are parsed and transformed into an
988 * array of admissible uids which is returned via \a admissible_uids.
990 * Finally, the format string given by --format (or the default format string
991 * for the given select mode if no --format option was given in \a string) is
992 * parsed as well resulting in a format_info structure which is returned via
993 * \a fi. The caller uses the \a fi pointer later to format each output line.
995 * \return Negative on errors, zero if --help or --detailed-help was given,
996 * positive otherwise.
998 * \sa format_items().
1000 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
1001 struct uid_range **admissible_uids, struct format_info **fi)
1003 int ret, num_uid_ranges;
1011 ret = create_argv(string, &argv);
1015 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
1019 if (select_conf.help_given || select_conf.detailed_help_given)
1021 fmt = select_conf.format_arg;
1023 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
1026 num_uid_ranges = ret;
1027 ret = append_users(select_conf.user_arg, select_conf.user_given,
1028 admissible_uids, num_uid_ranges);
1031 return setup_format_string(fmt, fi);
1033 line = select_conf.detailed_help_given?
1034 select_args_info_detailed_help : select_args_info_help;
1036 output_file = stdout;
1037 for (; *line; line++) {
1038 ret = output("%s\n", *line);
1046 * Main function for select mode.
1050 int com_select(void)
1052 struct uid_range *admissible_uids = NULL;
1054 struct format_info *fi;
1055 struct select_cmdline_parser_params params = {
1058 .check_required = 1,
1059 .check_ambiguity = 1,
1063 ret = parse_select_options(conf.select_options_arg, ¶ms,
1064 &admissible_uids, &fi);
1066 ret = read_uid_file(conf.database_dir_arg);
1069 ret = run_select_query(admissible_uids, fi);
1070 free_format_info(fi);
1073 select_cmdline_parser_free(&select_conf);