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_TRAILER_ATOMS \
76 ATOM(pw_name, STRING) \
79 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
80 struct atom user_list_header_trailer_atoms[] = {
81 USER_LIST_HEADER_TRAILER_ATOMS
85 #define ATOM(x, y) ulha_ ## x,
86 enum user_list_header_trailer_atoms {USER_LIST_HEADER_TRAILER_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;
144 struct format_info *trailer_fi;
147 struct user_summary_info {
148 struct user_info *ui;
149 /** Total number of files owned by this user. */
151 /** Total number of bytes owned by this user. */
153 /** Total number of directories that contain at least one file */
158 int inverse_matching;
161 struct user_summary_loop_data {
162 unsigned num_admissible_users;
163 struct user_summary_info *usis;
164 struct user_summary_info *current;
165 struct format_info *fi;
168 static FILE *output_file;
170 __printf_1_2 static int output(const char const *fmt, ...)
176 ret = vfprintf(output_file, fmt, argp);
178 return ret < 0? -E_OUTPUT : 1;
181 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
183 char *result = NULL, *tmp;
185 uint64_t val = *dirnum;
186 struct osl_object obj;
192 obj.size = sizeof(val);
193 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
196 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
199 val = *(uint64_t *)obj.data;
200 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
203 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
204 tmp = make_message("%s/%s", pfx, result? result : "");
220 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
222 struct osl_object obj;
226 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
229 return get_dir_name_by_number((uint64_t *)obj.data, name);
232 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
233 struct user_info *ui, char **dirname)
235 struct osl_object obj;
236 int ret = osl(osl_get_object(ui->table, user_table_row,
241 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
244 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
246 struct osl_object obj;
247 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
250 *num_files = *(uint64_t *)obj.data;
254 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
257 struct osl_object obj;
258 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
262 *num_files = *(uint64_t *)obj.data;
266 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
268 struct osl_object obj;
269 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
272 *num_bytes = *(uint64_t *)obj.data;
276 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
279 struct osl_object obj;
280 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
284 *num_bytes = *(uint64_t *)obj.data;
288 static void free_regex(regex_t *preg)
296 static int compile_regex(regex_t **preg, int *invert)
300 char *buf, *p = select_conf.pattern_arg;
302 if (!select_conf.pattern_given || !p[0]) {
315 *preg = adu_malloc(sizeof(regex_t));
316 ret = regcomp(*preg, p, 0);
319 size = regerror(ret, *preg, NULL, 0);
320 buf = adu_malloc(size);
321 regerror(ret, *preg, buf, size);
322 ERROR_LOG("%s\n", buf);
329 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
335 ret = regexec(preg, dirname, 0, NULL, 0);
336 if (ret == REG_NOMATCH && !inverse_matching)
338 if (ret != REG_NOMATCH && inverse_matching)
343 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
347 assert(ret == -E_OSL);
348 if (osl_errno != E_OSL_LOOP)
349 /* error not caused by loop function returning negative. */
351 assert(loop_ret < 0);
352 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
354 if (loop_ret == -E_OSL) { /* osl error in loop function */
355 assert(loop_osl_errno);
356 osl_errno = loop_osl_errno;
361 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
362 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
364 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
365 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
368 static int global_summary_loop_function(struct osl_row *row, void *data)
370 struct global_summary_info *gsi = data;
376 ret = get_dir_name_of_row(row, &dirname);
379 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
385 ret = get_num_files_of_row(row, &num);
388 gsi->num_files += num;
390 ret = get_num_bytes_of_row(row, &num);
393 gsi->num_bytes += num;
398 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
402 static int print_global_summary(struct format_info *fi)
406 struct global_summary_info gsi = {.num_dirs = 0};
407 char *header = select_conf.header_given? select_conf.header_arg :
410 union atom_value values[] = {
411 [gsa_dirs] = {.num_value = 0ULL},
412 [gsa_files] = {.num_value = 0ULL},
413 [gsa_size] = {.num_value = 0ULL}
416 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
419 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
420 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
421 free_regex(gsi.preg);
424 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
425 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
426 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
428 ret = output("%s", header);
431 buf = format_items(fi, values);
432 ret = output("%s", buf);
436 return output("%s", select_conf.trailer_arg);
439 /* row: a pointer to a row of the *user* table */
440 static int user_summary_loop_function(struct osl_row *row, void *data)
442 struct user_summary_info *usi = data;
448 ret = get_dir_name_of_user_row(row, usi->ui, &dirname);
451 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
456 ret = get_num_user_files(row, usi->ui, &num);
460 ret = get_num_user_bytes(row, usi->ui, &num);
468 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
472 static int compute_user_summary(struct user_info *ui, void *data)
474 struct user_summary_loop_data *usld = data;
475 struct user_summary_info *usi = usld->current++;
476 int ret = compile_regex(&usi->preg, &usi->inverse_matching);
481 ret = adu_loop_reverse(ui->table, UT_BYTES, usi, user_summary_loop_function,
482 &usi->ret, &usi->osl_errno);
483 free_regex(usi->preg);
487 static int print_user_summary_line(struct user_summary_info *usi,
488 struct format_info *fi)
490 struct user_info *ui = usi->ui;
491 union atom_value values[] = {
492 [usa_pw_name] = {.string_value = ui->pw_name?
494 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
495 [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
496 [usa_files] = {.num_value = (long long unsigned)usi->files},
497 [usa_size] = {.num_value = (long long unsigned)usi->bytes}
499 char *buf = format_items(fi, values);
500 int ret = output("%s", buf);
506 static int name_comp(const void *a, const void *b)
508 const struct user_summary_info *x = a, *y = b;
509 char *n1 = x->ui->pw_name;
510 char *n2 = y->ui->pw_name;
516 return strcmp(n1, n2);
519 static int uid_comp(const void *a, const void *b)
521 const struct user_summary_info *x = a, *y = b;
522 return -NUM_COMPARE(x->ui->uid, y->ui->uid);
525 static int dir_count_comp(const void *a, const void *b)
527 const struct user_summary_info *x = a, *y = b;
528 return NUM_COMPARE(x->dirs, y->dirs);
531 static int file_count_comp(const void *a, const void *b)
533 const struct user_summary_info *x = a, *y = b;
534 return NUM_COMPARE(x->files, y->files);
537 static int size_comp(const void *a, const void *b)
539 const struct user_summary_info *x = a, *y = b;
540 return NUM_COMPARE(x->bytes, y->bytes);
543 static int count_admissible_users(__a_unused struct user_info *ui, void *data)
545 struct user_summary_loop_data *usld = data;
546 usld->num_admissible_users++;
550 static int print_user_summary(struct format_info *fi)
553 int (*comp)(const void *a, const void *b);
554 struct user_summary_loop_data usld = { .fi = fi};
555 char *header = select_conf.header_given? select_conf.header_arg :
558 ret = output("%s", header);
561 ret = for_each_admissible_user(count_admissible_users, &usld);
564 if (usld.num_admissible_users == 0)
566 usld.usis = adu_calloc(usld.num_admissible_users
567 * sizeof(struct user_summary_info));
568 usld.current = usld.usis;
569 ret = for_each_admissible_user(compute_user_summary, &usld);
572 switch (select_conf.user_summary_sort_arg) {
573 case user_summary_sort_arg_name:
576 case user_summary_sort_arg_uid:
579 case user_summary_sort_arg_dir_count:
580 comp = dir_count_comp;
582 case user_summary_sort_arg_file_count:
583 comp = file_count_comp;
585 case user_summary_sort_arg_size:
588 default: /* this should never happen, but anyway */
592 qsort(usld.usis, usld.num_admissible_users,
593 sizeof(struct user_summary_info), comp);
594 for (i = 0; i < usld.num_admissible_users; i++) {
595 if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
597 ret = print_user_summary_line(usld.usis + i, usld.fi);
601 ret = output("%s", select_conf.trailer_arg);
607 static int user_list_loop_function(struct osl_row *row, void *data)
609 struct user_list_info *uli = data;
610 union atom_value values[] = {
611 [ula_pw_name] = {.string_value = uli->ui->pw_name?
612 uli->ui->pw_name : "?"},
613 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
614 [ula_files] = {.num_value = 0ULL},
615 [ula_size] = {.num_value = 0ULL},
616 [ula_dirname] = {.string_value = NULL}
620 char *dirname = NULL, *buf;
623 ret = -E_LOOP_COMPLETE;
627 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
630 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
634 values[ula_dirname].string_value = dirname;
636 ret = get_num_user_files(row, uli->ui, &num);
639 values[ula_files].num_value = num;
641 ret = get_num_user_bytes(row, uli->ui, &num);
644 values[ula_size].num_value = num;
646 buf = format_items(uli->fi, values);
649 ret = output("%s", buf);
658 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
662 static int print_user_list(struct user_info *ui, void *data)
664 struct user_list_format_info *ulfi = data;
666 enum user_table_columns sort_column;
667 struct user_list_info uli = {
670 .count = select_conf.limit_arg
672 union atom_value header_trailer_values[] = {
673 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
674 [ulha_pw_name] = {.string_value = ui->pw_name?
677 char *buf = format_items(ulfi->header_fi, header_trailer_values);
679 ret = output("%s", buf);
683 if (select_conf.list_sort_arg == list_sort_arg_file_count)
684 sort_column = UT_FILES;
686 sort_column = UT_BYTES;
688 ret = compile_regex(&uli.preg, &uli.inverse_matching);
691 ret = adu_loop_reverse(ui->table, sort_column, &uli,
692 user_list_loop_function, &uli.ret, &uli.osl_errno);
693 free_regex(uli.preg);
696 buf = format_items(ulfi->trailer_fi, header_trailer_values);
697 ret = output("%s", buf);
702 static int print_user_lists(struct format_info *fi)
704 struct user_list_format_info ulfi = {.fi = fi};
705 char *header_fmt = select_conf.header_given?
706 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
707 char *trailer_fmt = select_conf.trailer_arg;
708 int ret = parse_format_string(header_fmt,
709 user_list_header_trailer_atoms, &ulfi.header_fi);
712 ret = parse_format_string(trailer_fmt,
713 user_list_header_trailer_atoms, &ulfi.trailer_fi);
716 ret = for_each_admissible_user(print_user_list, &ulfi);
717 free_format_info(ulfi.header_fi);
718 free_format_info(ulfi.trailer_fi);
722 static int global_list_loop_function(struct osl_row *row, void *data)
724 struct global_list_info *gli = data;
725 union atom_value values[] = {
726 [gla_size] = {.num_value = 0ULL},
727 [gla_files] = {.num_value = 0ULL},
728 [gla_dirname] = {.string_value = NULL}
730 uint64_t num_files, num_bytes;
731 char *dirname = NULL, *buf;
735 ret = -E_LOOP_COMPLETE;
739 ret = get_dir_name_of_row(row, &dirname);
742 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
746 values[gla_dirname].string_value = dirname;
748 ret = get_num_files_of_row(row, &num_files);
751 values[gla_files].num_value = (long long unsigned)num_files;
753 ret = get_num_bytes_of_row(row, &num_bytes);
756 values[gla_size].num_value = (long long unsigned)num_bytes;
758 buf = format_items(gli->fi, values);
761 ret = output("%s", buf);
771 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
775 static int print_global_list(struct format_info *fi)
778 enum dir_table_columns sort_column;
779 struct global_list_info gli = {
781 .count = select_conf.limit_arg
783 char *header = select_conf.header_given?
784 select_conf.header_arg : "Global list\n";
786 ret = output("%s", header);
789 if (select_conf.list_sort_arg == list_sort_arg_file_count)
790 sort_column = DT_FILES;
792 sort_column = DT_BYTES;
793 ret = compile_regex(&gli.preg, &gli.inverse_matching);
796 ret = adu_loop_reverse(dir_table, sort_column, &gli,
797 global_list_loop_function, &gli.ret, &gli.osl_errno);
798 free_regex(gli.preg);
801 return output("%s", select_conf.trailer_arg);
804 static int print_statistics(struct format_info *fi)
806 switch (select_conf.select_mode_arg) {
807 case select_mode_arg_global_list:
808 return print_global_list(fi);
809 case select_mode_arg_global_summary:
810 return print_global_summary(fi);
811 case select_mode_arg_user_list:
812 return print_user_lists(fi);
813 case select_mode_arg_user_summary:
814 return print_user_summary(fi);
816 ERROR_LOG("bad select mode\n");
817 return -ERRNO_TO_ERROR(EINVAL);
821 static int open_pipe(char *path)
828 return ERRNO_TO_ERROR(errno);
831 return ERRNO_TO_ERROR(errno);
832 if (ret) { /* parent */
833 DEBUG_LOG("created process %d\n", ret);
835 output_file = fdopen(p[1], "w");
837 return ERRNO_TO_ERROR(errno);
841 if (p[0] != STDIN_FILENO)
842 dup2(p[0], STDIN_FILENO);
843 DEBUG_LOG("executing %s\n", path);
844 split_args(path, &argv, " \t");
845 execvp(argv[0], argv);
846 ERROR_LOG("error executing %s: %s\n", path,
847 adu_strerror(ERRNO_TO_ERROR(errno)));
851 static int open_output_stream(void)
854 int ret, flags = O_WRONLY | O_CREAT;
856 if (!select_conf.output_given)
858 p = select_conf.output_arg;
860 case '\0': /* empty string */
863 if (!p[1]) /* "-" means stdout */
865 /* string starting with a dash */
869 if (!p[1]) /* ">" is invalid */
876 /* string starting with ">>" */
877 if (!p[2]) /* ">>" is invalid */
883 if (!p[1]) /* "|" is invalid */
887 default: /* args starts with no magic character */
892 output_file = stdout;
896 return -E_BAD_OUTPUT_ARG;
899 * glibc's 'x' mode to fopen is not portable, so use open() and
902 ret = open(p, flags, 0644);
904 return -ERRNO_TO_ERROR(errno);
905 output_file = fdopen(ret, "w");
907 return -ERRNO_TO_ERROR(errno);
912 * Execute a select query.
914 * \param admissible_uids User IDs to take into account.
915 * \param fi Format information.
917 * Called once in select mode or for each \a run command in interactive mode.
919 * Open the output stream and the dir table if not already open. For each
920 * admissible uid, the user table is opened if necessary. After these
921 * preparations, the output according to \a select_mode and \a fi is written to
926 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
928 int ret = open_output_stream();
932 ret = open_dir_table(0);
936 ret = open_admissible_user_tables(admissible_uids);
940 ret = print_statistics(fi);
942 if (output_file && output_file != stdout) {
949 /** Default format string for global_list mode. */
950 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
951 /** Default format string for global_summary mode. */
952 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n"
953 /** Default format string for user_list mode. */
954 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
955 /** Default format string for user_summary mode. */
956 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:6) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
958 static int setup_format_string(char *fmt, struct format_info **fi)
963 INFO_LOG("using default format string\n");
964 switch (select_conf.select_mode_arg) {
965 case select_mode_arg_global_list:
967 fmt = GLOBAL_LIST_DFLT_FMT;
968 atoms = global_list_atoms;
970 case select_mode_arg_global_summary:
972 fmt = GLOBAL_SUMMARY_DFLT_FMT;
973 atoms = global_summary_atoms;
975 case select_mode_arg_user_list:
977 fmt = USER_LIST_DFLT_FMT;
978 atoms = user_list_atoms;
980 case select_mode_arg_user_summary:
982 fmt = USER_SUMMARY_DFLT_FMT;
983 atoms = user_summary_atoms;
986 ERROR_LOG("bad select mode\n");
987 return -ERRNO_TO_ERROR(EINVAL);
989 INFO_LOG("format string: %s\n", fmt);
990 return parse_format_string(fmt, atoms, fi);
994 * Parse a given format string.
996 * \param string The format string to parse.
997 * \param params gengetopt parameters.
998 * \param admissible_uids The array of admissible uid ranges.
999 * \param fi The format info to be used with format_items().
1001 * If \a string is not \p NULL, it is broken down into its components using
1002 * \ref create_argv() and the resulting argument vector is passed together with
1003 * \a params to gengetopt's command line parser. If --help or --detailed-help
1004 * was specified in \a string, the corresponding help text is printed and the
1005 * function returns zero.
1007 * Otherwise, any --uid or --user options are parsed and transformed into an
1008 * array of admissible uids which is returned via \a admissible_uids.
1010 * Finally, the format string given by --format (or the default format string
1011 * for the given select mode if no --format option was given in \a string) is
1012 * parsed as well resulting in a format_info structure which is returned via
1013 * \a fi. The caller uses the \a fi pointer later to format each output line.
1015 * \return Negative on errors, zero if --help or --detailed-help was given,
1016 * positive otherwise.
1018 * \sa format_items().
1020 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
1021 struct uid_range **admissible_uids, struct format_info **fi)
1023 int ret, num_uid_ranges;
1031 ret = create_argv(string, &argv);
1035 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
1039 if (select_conf.help_given || select_conf.detailed_help_given)
1041 fmt = select_conf.format_arg;
1043 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
1046 num_uid_ranges = ret;
1047 ret = append_users(select_conf.user_arg, select_conf.user_given,
1048 admissible_uids, num_uid_ranges);
1051 return setup_format_string(fmt, fi);
1053 line = select_conf.detailed_help_given?
1054 select_args_info_detailed_help : select_args_info_help;
1056 output_file = stdout;
1057 for (; *line; line++) {
1058 ret = output("%s\n", *line);
1066 * Main function for select mode.
1070 int com_select(void)
1072 struct uid_range *admissible_uids = NULL;
1074 struct format_info *fi;
1075 struct select_cmdline_parser_params params = {
1078 .check_required = 1,
1079 .check_ambiguity = 1,
1083 ret = parse_select_options(conf.select_options_arg, ¶ms,
1084 &admissible_uids, &fi);
1086 ret = read_uid_file();
1089 ret = run_select_query(admissible_uids, fi);
1090 free_format_info(fi);
1093 select_cmdline_parser_free(&select_conf);