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 static int user_summary_loop_function(struct osl_row *row, void *data)
441 struct user_summary_info *usi = data;
447 ret = get_dir_name_of_row(row, &dirname);
450 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
455 ret = get_num_user_files(row, usi->ui, &num);
459 ret = get_num_user_bytes(row, usi->ui, &num);
467 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
471 static int compute_user_summary(struct user_info *ui, void *data)
473 struct user_summary_loop_data *usld = data;
474 struct user_summary_info *usi = usld->current++;
475 int ret = compile_regex(&usi->preg, &usi->inverse_matching);
480 ret = adu_loop_reverse(ui->table, UT_BYTES, usi, user_summary_loop_function,
481 &usi->ret, &usi->osl_errno);
482 free_regex(usi->preg);
486 static int print_user_summary_line(struct user_summary_info *usi,
487 struct format_info *fi)
489 struct user_info *ui = usi->ui;
490 union atom_value values[] = {
491 [usa_pw_name] = {.string_value = ui->pw_name?
493 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
494 [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
495 [usa_files] = {.num_value = (long long unsigned)usi->files},
496 [usa_size] = {.num_value = (long long unsigned)usi->bytes}
498 char *buf = format_items(fi, values);
499 int ret = output("%s", buf);
505 static int name_comp(const void *a, const void *b)
507 const struct user_summary_info *x = a, *y = b;
508 char *n1 = x->ui->pw_name;
509 char *n2 = y->ui->pw_name;
515 return strcmp(n1, n2);
518 static int uid_comp(const void *a, const void *b)
520 const struct user_summary_info *x = a, *y = b;
521 return -NUM_COMPARE(x->ui->uid, y->ui->uid);
524 static int dir_count_comp(const void *a, const void *b)
526 const struct user_summary_info *x = a, *y = b;
527 return NUM_COMPARE(x->dirs, y->dirs);
530 static int file_count_comp(const void *a, const void *b)
532 const struct user_summary_info *x = a, *y = b;
533 return NUM_COMPARE(x->files, y->files);
536 static int size_comp(const void *a, const void *b)
538 const struct user_summary_info *x = a, *y = b;
539 return NUM_COMPARE(x->bytes, y->bytes);
542 static int count_admissible_users(__a_unused struct user_info *ui, void *data)
544 struct user_summary_loop_data *usld = data;
545 usld->num_admissible_users++;
549 static int print_user_summary(struct format_info *fi)
552 int (*comp)(const void *a, const void *b);
553 struct user_summary_loop_data usld = { .fi = fi};
554 char *header = select_conf.header_given? select_conf.header_arg :
557 ret = output("%s", header);
560 ret = for_each_admissible_user(count_admissible_users, &usld);
563 if (usld.num_admissible_users == 0)
565 usld.usis = adu_calloc(usld.num_admissible_users
566 * sizeof(struct user_summary_info));
567 usld.current = usld.usis;
568 ret = for_each_admissible_user(compute_user_summary, &usld);
571 switch (select_conf.user_summary_sort_arg) {
572 case user_summary_sort_arg_name:
575 case user_summary_sort_arg_uid:
578 case user_summary_sort_arg_dir_count:
579 comp = dir_count_comp;
581 case user_summary_sort_arg_file_count:
582 comp = file_count_comp;
584 case user_summary_sort_arg_size:
587 default: /* this should never happen, but anyway */
591 qsort(usld.usis, usld.num_admissible_users,
592 sizeof(struct user_summary_info), comp);
593 for (i = 0; i < usld.num_admissible_users; i++) {
594 if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
596 ret = print_user_summary_line(usld.usis + i, usld.fi);
600 ret = output("%s", select_conf.trailer_arg);
606 static int user_list_loop_function(struct osl_row *row, void *data)
608 struct user_list_info *uli = data;
609 union atom_value values[] = {
610 [ula_pw_name] = {.string_value = uli->ui->pw_name?
611 uli->ui->pw_name : "?"},
612 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
613 [ula_files] = {.num_value = 0ULL},
614 [ula_size] = {.num_value = 0ULL},
615 [ula_dirname] = {.string_value = NULL}
619 char *dirname = NULL, *buf;
622 ret = -E_LOOP_COMPLETE;
626 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
629 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
633 values[ula_dirname].string_value = dirname;
635 ret = get_num_user_files(row, uli->ui, &num);
638 values[ula_files].num_value = num;
640 ret = get_num_user_bytes(row, uli->ui, &num);
643 values[ula_size].num_value = num;
645 buf = format_items(uli->fi, values);
648 ret = output("%s", buf);
657 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
661 static int print_user_list(struct user_info *ui, void *data)
663 struct user_list_format_info *ulfi = data;
665 enum user_table_columns sort_column;
666 struct user_list_info uli = {
669 .count = select_conf.limit_arg
671 union atom_value header_trailer_values[] = {
672 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
673 [ulha_pw_name] = {.string_value = ui->pw_name?
676 char *buf = format_items(ulfi->header_fi, header_trailer_values);
678 ret = output("%s", buf);
682 if (select_conf.list_sort_arg == list_sort_arg_file_count)
683 sort_column = UT_FILES;
685 sort_column = UT_BYTES;
687 ret = compile_regex(&uli.preg, &uli.inverse_matching);
690 ret = adu_loop_reverse(ui->table, sort_column, &uli,
691 user_list_loop_function, &uli.ret, &uli.osl_errno);
692 free_regex(uli.preg);
695 buf = format_items(ulfi->trailer_fi, header_trailer_values);
696 ret = output("%s", buf);
701 static int print_user_lists(struct format_info *fi)
703 struct user_list_format_info ulfi = {.fi = fi};
704 char *header_fmt = select_conf.header_given?
705 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
706 char *trailer_fmt = select_conf.trailer_arg;
707 int ret = parse_format_string(header_fmt,
708 user_list_header_trailer_atoms, &ulfi.header_fi);
711 ret = parse_format_string(trailer_fmt,
712 user_list_header_trailer_atoms, &ulfi.trailer_fi);
715 ret = for_each_admissible_user(print_user_list, &ulfi);
716 free_format_info(ulfi.header_fi);
717 free_format_info(ulfi.trailer_fi);
721 static int global_list_loop_function(struct osl_row *row, void *data)
723 struct global_list_info *gli = data;
724 union atom_value values[] = {
725 [gla_size] = {.num_value = 0ULL},
726 [gla_files] = {.num_value = 0ULL},
727 [gla_dirname] = {.string_value = NULL}
729 uint64_t num_files, num_bytes;
730 char *dirname = NULL, *buf;
734 ret = -E_LOOP_COMPLETE;
738 ret = get_dir_name_of_row(row, &dirname);
741 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
745 values[gla_dirname].string_value = dirname;
747 ret = get_num_files_of_row(row, &num_files);
750 values[gla_files].num_value = (long long unsigned)num_files;
752 ret = get_num_bytes_of_row(row, &num_bytes);
755 values[gla_size].num_value = (long long unsigned)num_bytes;
757 buf = format_items(gli->fi, values);
760 ret = output("%s", buf);
770 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
774 static int print_global_list(struct format_info *fi)
777 enum dir_table_columns sort_column;
778 struct global_list_info gli = {
780 .count = select_conf.limit_arg
782 char *header = select_conf.header_given?
783 select_conf.header_arg : "Global list\n";
785 ret = output("%s", header);
788 if (select_conf.list_sort_arg == list_sort_arg_file_count)
789 sort_column = DT_FILES;
791 sort_column = DT_BYTES;
792 ret = compile_regex(&gli.preg, &gli.inverse_matching);
795 ret = adu_loop_reverse(dir_table, sort_column, &gli,
796 global_list_loop_function, &gli.ret, &gli.osl_errno);
797 free_regex(gli.preg);
800 return output("%s", select_conf.trailer_arg);
803 static int print_statistics(struct format_info *fi)
805 switch (select_conf.select_mode_arg) {
806 case select_mode_arg_global_list:
807 return print_global_list(fi);
808 case select_mode_arg_global_summary:
809 return print_global_summary(fi);
810 case select_mode_arg_user_list:
811 return print_user_lists(fi);
812 case select_mode_arg_user_summary:
813 return print_user_summary(fi);
815 ERROR_LOG("bad select mode\n");
816 return -ERRNO_TO_ERROR(EINVAL);
819 static int open_pipe(char *path)
826 return ERRNO_TO_ERROR(errno);
829 return ERRNO_TO_ERROR(errno);
830 if (ret) { /* parent */
831 DEBUG_LOG("created process %d\n", ret);
833 output_file = fdopen(p[1], "w");
835 return ERRNO_TO_ERROR(errno);
839 if (p[0] != STDIN_FILENO)
840 dup2(p[0], STDIN_FILENO);
841 DEBUG_LOG("executing %s\n", path);
842 argc = split_args(path, &argv, " \t");
843 execvp(argv[0], argv);
844 ERROR_LOG("error executing %s: %s\n", path,
845 adu_strerror(ERRNO_TO_ERROR(errno)));
849 static int open_output_stream(void)
852 int ret, flags = O_WRONLY | O_CREAT;
854 if (!select_conf.output_given)
856 p = select_conf.output_arg;
858 case '\0': /* empty string */
861 if (!p[1]) /* "-" means stdout */
863 /* string starting with a dash */
867 if (!p[1]) /* ">" is invalid */
874 /* string starting with ">>" */
875 if (!p[2]) /* ">>" is invalid */
881 if (!p[1]) /* "|" is invalid */
885 default: /* args starts with no magic character */
890 output_file = stdout;
894 return -E_BAD_OUTPUT_ARG;
897 * glibc's 'x' mode to fopen is not portable, so use open() and
900 ret = open(p, flags, 0644);
902 return -ERRNO_TO_ERROR(errno);
903 output_file = fdopen(ret, "w");
905 return -ERRNO_TO_ERROR(errno);
910 * Execute a select query.
912 * \param admissible_uids User IDs to take into account.
913 * \param fi Format information.
915 * Called once in select mode or for each \a run command in interactive mode.
917 * Open the output stream and the dir table if not already open. For each
918 * admissible uid, the user table is opened if necessary. After these
919 * preparations, the output according to \a select_mode and \a fi is written to
924 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
926 int ret = open_output_stream();
930 ret = open_dir_table(0);
934 ret = open_admissible_user_tables(admissible_uids);
938 ret = print_statistics(fi);
940 if (output_file && output_file != stdout) {
947 /** Default format string for global_list mode. */
948 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
949 /** Default format string for global_summary mode. */
950 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n"
951 /** Default format string for user_list mode. */
952 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
953 /** Default format string for user_summary mode. */
954 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
956 static int setup_format_string(char *fmt, struct format_info **fi)
961 INFO_LOG("using default format string\n");
962 switch (select_conf.select_mode_arg) {
963 case select_mode_arg_global_list:
965 fmt = GLOBAL_LIST_DFLT_FMT;
966 atoms = global_list_atoms;
968 case select_mode_arg_global_summary:
970 fmt = GLOBAL_SUMMARY_DFLT_FMT;
971 atoms = global_summary_atoms;
973 case select_mode_arg_user_list:
975 fmt = USER_LIST_DFLT_FMT;
976 atoms = user_list_atoms;
978 case select_mode_arg_user_summary:
980 fmt = USER_SUMMARY_DFLT_FMT;
981 atoms = user_summary_atoms;
984 ERROR_LOG("bad select mode\n");
985 return -ERRNO_TO_ERROR(EINVAL);
987 INFO_LOG("format string: %s\n", fmt);
988 return parse_format_string(fmt, atoms, fi);
992 * Parse a given format string.
994 * \param string The format string to parse.
995 * \param params gengetopt parameters.
996 * \param admissible_uids The array of admissible uid ranges.
997 * \param fi The format info to be used with format_items().
999 * If \a string is not \p NULL, it is broken down into its components using
1000 * \ref create_argv() and the resulting argument vector is passed together with
1001 * \a params to gengetopt's command line parser. If --help or --detailed-help
1002 * was specified in \a string, the corresponding help text is printed and the
1003 * function returns zero.
1005 * Otherwise, any --uid or --user options are parsed and transformed into an
1006 * array of admissible uids which is returned via \a admissible_uids.
1008 * Finally, the format string given by --format (or the default format string
1009 * for the given select mode if no --format option was given in \a string) is
1010 * parsed as well resulting in a format_info structure which is returned via
1011 * \a fi. The caller uses the \a fi pointer later to format each output line.
1013 * \return Negative on errors, zero if --help or --detailed-help was given,
1014 * positive otherwise.
1016 * \sa format_items().
1018 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
1019 struct uid_range **admissible_uids, struct format_info **fi)
1021 int ret, num_uid_ranges;
1029 ret = create_argv(string, &argv);
1033 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
1037 if (select_conf.help_given || select_conf.detailed_help_given)
1039 fmt = select_conf.format_arg;
1041 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
1044 num_uid_ranges = ret;
1045 ret = append_users(select_conf.user_arg, select_conf.user_given,
1046 admissible_uids, num_uid_ranges);
1049 return setup_format_string(fmt, fi);
1051 line = select_conf.detailed_help_given?
1052 select_args_info_detailed_help : select_args_info_help;
1054 output_file = stdout;
1055 for (; *line; line++) {
1056 ret = output("%s\n", *line);
1064 * Main function for select mode.
1068 int com_select(void)
1070 struct uid_range *admissible_uids = NULL;
1072 struct format_info *fi;
1073 struct select_cmdline_parser_params params = {
1076 .check_required = 1,
1077 .check_ambiguity = 1,
1081 ret = parse_select_options(conf.select_options_arg, ¶ms,
1082 &admissible_uids, &fi);
1084 ret = read_uid_file();
1087 ret = run_select_query(admissible_uids, fi);
1088 free_format_info(fi);
1091 select_cmdline_parser_free(&select_conf);