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;
151 int inverse_matching;
154 struct user_summary_line_info {
155 struct format_info *fi;
159 static FILE *output_file;
161 __printf_1_2 static int output(const char const *fmt, ...)
167 ret = vfprintf(output_file, fmt, argp);
169 return ret < 0? -E_OUTPUT : 1;
172 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
174 char *result = NULL, *tmp;
176 uint64_t val = *dirnum;
177 struct osl_object obj;
183 obj.size = sizeof(val);
184 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
187 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
190 val = *(uint64_t *)obj.data;
191 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
194 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
195 tmp = make_message("%s/%s", pfx, result? result : "");
211 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
213 struct osl_object obj;
217 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
220 return get_dir_name_by_number((uint64_t *)obj.data, name);
223 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
224 struct user_info *ui, char **dirname)
226 struct osl_object obj;
227 int ret = osl(osl_get_object(ui->table, user_table_row,
232 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
235 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
237 struct osl_object obj;
238 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
241 *num_files = *(uint64_t *)obj.data;
245 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
248 struct osl_object obj;
249 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
253 *num_files = *(uint64_t *)obj.data;
257 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
259 struct osl_object obj;
260 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
263 *num_bytes = *(uint64_t *)obj.data;
267 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
270 struct osl_object obj;
271 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
275 *num_bytes = *(uint64_t *)obj.data;
279 static void free_regex(regex_t *preg)
287 static int compile_regex(regex_t **preg, int *invert)
291 char *buf, *p = select_conf.pattern_arg;
293 if (!select_conf.pattern_given || !p[0]) {
306 *preg = adu_malloc(sizeof(regex_t));
307 ret = regcomp(*preg, p, 0);
310 size = regerror(ret, *preg, NULL, 0);
311 buf = adu_malloc(size);
312 regerror(ret, *preg, buf, size);
313 ERROR_LOG("%s\n", buf);
320 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
326 ret = regexec(preg, dirname, 0, NULL, 0);
327 if (ret == REG_NOMATCH && !inverse_matching)
329 if (ret != REG_NOMATCH && inverse_matching)
334 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
338 assert(ret == -E_OSL);
339 if (osl_errno != E_OSL_LOOP)
340 /* error not caused by loop function returning negative. */
342 assert(loop_ret < 0);
343 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
345 if (loop_ret == -E_OSL) { /* osl error in loop function */
346 assert(loop_osl_errno);
347 osl_errno = loop_osl_errno;
352 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
353 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
355 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
356 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
359 static int global_summary_loop_function(struct osl_row *row, void *data)
361 struct global_summary_info *gsi = data;
367 ret = get_dir_name_of_row(row, &dirname);
370 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
376 ret = get_num_files_of_row(row, &num);
379 gsi->num_files += num;
381 ret = get_num_bytes_of_row(row, &num);
384 gsi->num_bytes += num;
389 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
393 static int print_global_summary(struct format_info *fi)
397 struct global_summary_info gsi = {.num_dirs = 0};
398 char *header = select_conf.header_given? select_conf.header_arg :
401 union atom_value values[] = {
402 [gsa_dirs] = {.num_value = 0ULL},
403 [gsa_files] = {.num_value = 0ULL},
404 [gsa_size] = {.num_value = 0ULL}
407 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
410 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
411 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
412 free_regex(gsi.preg);
415 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
416 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
417 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
419 ret = output("%s", header);
422 buf = format_items(fi, values);
423 ret = output("%s", buf);
428 static int user_summary_loop_function(struct osl_row *row, void *data)
430 struct user_summary_info *usi = data;
436 ret = get_dir_name_of_row(row, &dirname);
439 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
444 ret = get_num_user_files(row, usi->ui, &num);
447 usi->ui->files += num;
448 ret = get_num_user_bytes(row, usi->ui, &num);
451 usi->ui->bytes += num;
456 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
460 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
462 struct user_summary_info usi = {.ui = ui};
463 int ret = compile_regex(&usi.preg, &usi.inverse_matching);
467 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
468 &usi.ret, &usi.osl_errno);
469 free_regex(usi.preg);
473 static int print_user_summary_line(struct user_info *ui, void *data)
475 struct user_summary_line_info *usli = data;
476 union atom_value values[] = {
477 [usa_pw_name] = {.string_value = ui->pw_name?
479 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
480 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
481 [usa_files] = {.num_value = (long long unsigned)ui->files},
482 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
485 int ret = -E_LOOP_COMPLETE;
490 buf = format_items(usli->fi, values);
491 ret = output("%s", buf);
497 static int name_comp(struct user_info *a, struct user_info *b)
499 char *x = a->pw_name;
500 char *y = b->pw_name;
509 static int uid_comp(struct user_info *a, struct user_info *b)
511 return -NUM_COMPARE(a->uid, b->uid);
514 static int dir_count_comp(struct user_info *a, struct user_info *b)
516 return NUM_COMPARE(a->dirs, b->dirs);
519 static int file_count_comp(struct user_info *a, struct user_info *b)
521 return NUM_COMPARE(a->files, b->files);
524 static int size_comp(struct user_info *a, struct user_info *b)
526 return NUM_COMPARE(a->bytes, b->bytes);
529 static int print_user_summary(struct format_info *fi)
532 int (*comp)(struct user_info *a, struct user_info *b);
533 struct user_summary_line_info usli = {
535 .count = select_conf.limit_arg
537 char *header = select_conf.header_given? select_conf.header_arg :
540 ret = output("%s", header);
543 ret = for_each_admissible_user(compute_user_summary, NULL);
546 switch (select_conf.user_summary_sort_arg) {
547 case user_summary_sort_arg_name:
550 case user_summary_sort_arg_uid:
553 case user_summary_sort_arg_dir_count:
554 comp = dir_count_comp;
556 case user_summary_sort_arg_file_count:
557 comp = file_count_comp;
559 case user_summary_sort_arg_size:
562 default: /* this should never happen, but anyway */
566 sort_hash_table(comp);
567 ret = for_each_admissible_user(print_user_summary_line, &usli);
568 if (ret == -E_LOOP_COMPLETE)
573 static int user_list_loop_function(struct osl_row *row, void *data)
575 struct user_list_info *uli = data;
576 union atom_value values[] = {
577 [ula_pw_name] = {.string_value = uli->ui->pw_name?
578 uli->ui->pw_name : "?"},
579 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
580 [ula_files] = {.num_value = 0ULL},
581 [ula_size] = {.num_value = 0ULL},
582 [ula_dirname] = {.string_value = NULL}
586 char *dirname = NULL, *buf;
589 ret = -E_LOOP_COMPLETE;
593 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
596 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
600 values[ula_dirname].string_value = dirname;
602 ret = get_num_user_files(row, uli->ui, &num);
605 values[ula_files].num_value = num;
607 ret = get_num_user_bytes(row, uli->ui, &num);
610 values[ula_size].num_value = num;
612 buf = format_items(uli->fi, values);
615 ret = output("%s", buf);
624 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
628 static int print_user_list(struct user_info *ui, void *data)
630 struct user_list_format_info *ulfi = data;
632 enum user_table_columns sort_column;
633 struct user_list_info uli = {
636 .count = select_conf.limit_arg
638 union atom_value header_values[] = {
639 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
640 [ulha_pw_name] = {.string_value = ui->pw_name?
643 char *buf = format_items(ulfi->header_fi, header_values);
645 ret = output("%s", buf);
649 if (select_conf.list_sort_arg == list_sort_arg_file_count)
650 sort_column = UT_FILES;
652 sort_column = UT_BYTES;
654 ret = compile_regex(&uli.preg, &uli.inverse_matching);
657 ret = adu_loop_reverse(ui->table, sort_column, &uli,
658 user_list_loop_function, &uli.ret, &uli.osl_errno);
659 free_regex(uli.preg);
663 static int print_user_lists(struct format_info *fi)
665 struct user_list_format_info ulfi = {.fi = fi};
666 char *header_fmt = select_conf.header_given?
667 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
668 int ret = parse_format_string(header_fmt,
669 user_list_header_atoms, &ulfi.header_fi);
672 ret = for_each_admissible_user(print_user_list, &ulfi);
673 free_format_info(ulfi.header_fi);
677 static int global_list_loop_function(struct osl_row *row, void *data)
679 struct global_list_info *gli = data;
680 union atom_value values[] = {
681 [gla_size] = {.num_value = 0ULL},
682 [gla_files] = {.num_value = 0ULL},
683 [gla_dirname] = {.string_value = NULL}
685 uint64_t num_files, num_bytes;
686 char *dirname = NULL, *buf;
690 ret = -E_LOOP_COMPLETE;
694 ret = get_dir_name_of_row(row, &dirname);
697 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
701 values[gla_dirname].string_value = dirname;
703 ret = get_num_files_of_row(row, &num_files);
706 values[gla_files].num_value = (long long unsigned)num_files;
708 ret = get_num_bytes_of_row(row, &num_bytes);
711 values[gla_size].num_value = (long long unsigned)num_bytes;
713 buf = format_items(gli->fi, values);
716 ret = output("%s", buf);
726 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
730 static int print_global_list(struct format_info *fi)
733 enum dir_table_columns sort_column;
734 struct global_list_info gli = {
736 .count = select_conf.limit_arg
738 char *header = select_conf.header_given?
739 select_conf.header_arg : "Global list\n";
741 ret = output("%s", header);
744 if (select_conf.list_sort_arg == list_sort_arg_file_count)
745 sort_column = DT_FILES;
747 sort_column = DT_BYTES;
748 ret = compile_regex(&gli.preg, &gli.inverse_matching);
751 ret = adu_loop_reverse(dir_table, sort_column, &gli,
752 global_list_loop_function, &gli.ret, &gli.osl_errno);
753 free_regex(gli.preg);
757 static int print_statistics(struct format_info *fi)
759 switch (select_conf.select_mode_arg) {
760 case select_mode_arg_global_list:
761 return print_global_list(fi);
762 case select_mode_arg_global_summary:
763 return print_global_summary(fi);
764 case select_mode_arg_user_list:
765 return print_user_lists(fi);
766 case select_mode_arg_user_summary:
767 return print_user_summary(fi);
769 ERROR_LOG("bad select mode\n");
770 return -ERRNO_TO_ERROR(EINVAL);
773 static int open_pipe(char *path)
780 return ERRNO_TO_ERROR(errno);
783 return ERRNO_TO_ERROR(errno);
784 if (ret) { /* parent */
785 DEBUG_LOG("created process %d\n", ret);
787 output_file = fdopen(p[1], "w");
789 return ERRNO_TO_ERROR(errno);
793 if (p[0] != STDIN_FILENO)
794 dup2(p[0], STDIN_FILENO);
795 DEBUG_LOG("executing %s\n", path);
796 argc = split_args(path, &argv, " \t");
797 execvp(argv[0], argv);
798 ERROR_LOG("error executing %s: %s\n", path,
799 adu_strerror(ERRNO_TO_ERROR(errno)));
803 static int open_output_stream(void)
806 int ret, flags = O_WRONLY | O_CREAT;
808 if (!select_conf.output_given)
810 p = select_conf.output_arg;
812 case '\0': /* empty string */
815 if (!p[1]) /* "-" means stdout */
817 /* string starting with a dash */
821 if (!p[1]) /* ">" is invalid */
828 /* string starting with ">>" */
829 if (!p[2]) /* ">>" is invalid */
835 if (!p[1]) /* "|" is invalid */
839 default: /* args starts with no magic character */
844 output_file = stdout;
848 return -E_BAD_OUTPUT_ARG;
851 * glibc's 'x' mode to fopen is not portable, so use open() and
854 ret = open(p, flags, 0644);
856 return -ERRNO_TO_ERROR(errno);
857 output_file = fdopen(ret, "w");
859 return -ERRNO_TO_ERROR(errno);
864 * Execute a select query.
866 * \param admissible_uids User IDs to take into account.
867 * \param fi Format information.
869 * Called once in select mode or for each \a run command in interactive mode.
871 * Open the output stream and the dir table if not already open. For each
872 * admissible uid, the user table is opened if necessary. After these
873 * preparations, the output according to \a select_mode and \a fi is written to
878 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
880 int ret = open_output_stream();
884 ret = open_dir_table(0);
888 ret = open_admissible_user_tables(admissible_uids);
892 ret = print_statistics(fi);
894 if (output_file && output_file != stdout) {
901 /** Default format string for global_list mode. */
902 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
903 /** Default format string for global_summary mode. */
904 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
905 /** Default format string for user_list mode. */
906 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
907 /** Default format string for user_summary mode. */
908 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
910 static int setup_format_string(char *fmt, struct format_info **fi)
915 INFO_LOG("using default format string\n");
916 switch (select_conf.select_mode_arg) {
917 case select_mode_arg_global_list:
919 fmt = GLOBAL_LIST_DFLT_FMT;
920 atoms = global_list_atoms;
922 case select_mode_arg_global_summary:
924 fmt = GLOBAL_SUMMARY_DFLT_FMT;
925 atoms = global_summary_atoms;
927 case select_mode_arg_user_list:
929 fmt = USER_LIST_DFLT_FMT;
930 atoms = user_list_atoms;
932 case select_mode_arg_user_summary:
934 fmt = USER_SUMMARY_DFLT_FMT;
935 atoms = user_summary_atoms;
938 ERROR_LOG("bad select mode\n");
939 return -ERRNO_TO_ERROR(EINVAL);
941 INFO_LOG("format string: %s\n", fmt);
942 return parse_format_string(fmt, atoms, fi);
946 * Parse a given format string.
948 * \param string The format string to parse.
949 * \param params gengetopt parameters.
950 * \param admissible_uids The array of admissible uid ranges.
951 * \param fi The format info to be used with format_items().
953 * If \a string is not \p NULL, it is broken down into its components using
954 * \ref create_argv() and the resulting argument vector is passed together with
955 * \a params to gengetopt's command line parser. If --help or --detailed-help
956 * was specified in \a string, the corresponding help text is printed and the
957 * function returns zero.
959 * Otherwise, any --uid or --user options are parsed and transformed into an
960 * array of admissible uids which is returned via \a admissible_uids.
962 * Finally, the format string given by --format (or the default format string
963 * for the given select mode if no --format option was given in \a string) is
964 * parsed as well resulting in a format_info structure which is returned via
965 * \a fi. The caller uses the \a fi pointer later to format each output line.
967 * \return Negative on errors, zero if --help or --detailed-help was given,
968 * positive otherwise.
970 * \sa format_items().
972 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
973 struct uid_range **admissible_uids, struct format_info **fi)
975 int ret, num_uid_ranges;
983 ret = create_argv(string, &argv);
987 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
991 if (select_conf.help_given || select_conf.detailed_help_given)
993 fmt = select_conf.format_arg;
995 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
998 num_uid_ranges = ret;
999 ret = append_users(select_conf.user_arg, select_conf.user_given,
1000 admissible_uids, num_uid_ranges);
1003 return setup_format_string(fmt, fi);
1005 line = select_conf.detailed_help_given?
1006 select_args_info_detailed_help : select_args_info_help;
1008 output_file = stdout;
1009 for (; *line; line++) {
1010 ret = output("%s\n", *line);
1018 * Main function for select mode.
1022 int com_select(void)
1024 struct uid_range *admissible_uids = NULL;
1026 struct format_info *fi;
1027 struct select_cmdline_parser_params params = {
1030 .check_required = 1,
1031 .check_ambiguity = 1,
1035 ret = parse_select_options(conf.select_options_arg, ¶ms,
1036 &admissible_uids, &fi);
1038 ret = read_uid_file();
1041 ret = run_select_query(admissible_uids, fi);
1042 free_format_info(fi);
1045 select_cmdline_parser_free(&select_conf);