2 * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
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"
26 #define GLOBAL_LIST_ATOMS \
29 ATOM(dirname, STRING) \
31 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
32 struct atom global_list_atoms[] = {
37 #define ATOM(x, y) gla_ ## x,
38 enum global_list_atoms {GLOBAL_LIST_ATOMS};
42 #define GLOBAL_SUMMARY_ATOMS \
47 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
48 struct atom global_summary_atoms[] = {
53 #define ATOM(x, y) gsa_ ## x,
54 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
58 #define USER_LIST_ATOMS \
59 ATOM(pw_name, STRING) \
63 ATOM(dirname, STRING) \
65 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
66 struct atom user_list_atoms[] = {
71 #define ATOM(x, y) ula_ ## x,
72 enum user_list_atoms {USER_LIST_ATOMS};
75 /* user list header */
76 #define USER_LIST_HEADER_TRAILER_ATOMS \
77 ATOM(pw_name, STRING) \
80 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
81 struct atom user_list_header_trailer_atoms[] = {
82 USER_LIST_HEADER_TRAILER_ATOMS
86 #define ATOM(x, y) ulha_ ## x,
87 enum user_list_header_trailer_atoms {USER_LIST_HEADER_TRAILER_ATOMS};
91 #define USER_SUMMARY_ATOMS \
92 ATOM(pw_name, STRING) \
98 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
99 struct atom user_summary_atoms[] = {
104 #define ATOM(x, y) usa_ ## x,
105 enum user_summary_atoms {USER_SUMMARY_ATOMS};
110 struct global_list_info {
114 struct format_info *fi;
116 int inverse_matching;
119 struct global_summary_info {
120 /** Global dir count. */
122 /** Global files count. */
124 /** Global bytes count. */
127 int inverse_matching;
132 struct user_list_info {
134 struct user_info *ui;
135 struct format_info *fi;
137 int inverse_matching;
142 struct user_list_format_info {
143 struct format_info *fi;
144 struct format_info *header_fi;
145 struct format_info *trailer_fi;
148 struct user_summary_info {
149 struct user_info *ui;
150 /** Total number of files owned by this user. */
152 /** Total number of bytes owned by this user. */
154 /** Total number of directories that contain at least one file */
159 int inverse_matching;
162 struct user_summary_loop_data {
163 unsigned num_admissible_users;
164 struct user_summary_info *usis;
165 struct user_summary_info *current;
166 struct format_info *fi;
169 static FILE *output_file;
171 __printf_1_2 static int output(const char const *fmt, ...)
177 ret = vfprintf(output_file, fmt, argp);
179 return ret < 0? -E_OUTPUT : 1;
182 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
184 char *result = NULL, *tmp;
186 uint64_t val = *dirnum;
187 struct osl_object obj;
193 obj.size = sizeof(val);
194 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
197 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
200 val = *(uint64_t *)obj.data;
201 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
204 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
205 tmp = make_message("%s/%s", pfx, result? result : "");
221 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
223 struct osl_object obj;
227 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
230 return get_dir_name_by_number((uint64_t *)obj.data, name);
233 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
234 struct user_info *ui, char **dirname)
236 struct osl_object obj;
237 int ret = osl(osl_get_object(ui->table, user_table_row,
242 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
245 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
247 struct osl_object obj;
248 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
251 *num_files = *(uint64_t *)obj.data;
255 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
258 struct osl_object obj;
259 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
263 *num_files = *(uint64_t *)obj.data;
267 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
269 struct osl_object obj;
270 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
273 *num_bytes = *(uint64_t *)obj.data;
277 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
280 struct osl_object obj;
281 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
285 *num_bytes = *(uint64_t *)obj.data;
289 static void free_regex(regex_t *preg)
297 static int compile_regex(regex_t **preg, int *invert)
301 char *buf, *p = select_conf.pattern_arg;
303 if (!select_conf.pattern_given || !p[0]) {
316 *preg = adu_malloc(sizeof(regex_t));
317 ret = regcomp(*preg, p, 0);
320 size = regerror(ret, *preg, NULL, 0);
321 buf = adu_malloc(size);
322 regerror(ret, *preg, buf, size);
323 ERROR_LOG("%s\n", buf);
330 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
336 ret = regexec(preg, dirname, 0, NULL, 0);
337 if (ret == REG_NOMATCH && !inverse_matching)
339 if (ret != REG_NOMATCH && inverse_matching)
344 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
348 assert(ret == -E_OSL);
349 if (osl_errno != E_OSL_LOOP)
350 /* error not caused by loop function returning negative. */
352 assert(loop_ret < 0);
353 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
355 if (loop_ret == -E_OSL) { /* osl error in loop function */
356 assert(loop_osl_errno);
357 osl_errno = loop_osl_errno;
362 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
363 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
365 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
366 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
369 static int global_summary_loop_function(struct osl_row *row, void *data)
371 struct global_summary_info *gsi = data;
377 ret = get_dir_name_of_row(row, &dirname);
380 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
386 ret = get_num_files_of_row(row, &num);
389 gsi->num_files += num;
391 ret = get_num_bytes_of_row(row, &num);
394 gsi->num_bytes += num;
399 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
403 static int print_global_summary(struct format_info *fi)
407 struct global_summary_info gsi = {.num_dirs = 0};
408 char *header = select_conf.header_given? select_conf.header_arg :
411 union atom_value values[] = {
412 [gsa_dirs] = {.num_value = 0ULL},
413 [gsa_files] = {.num_value = 0ULL},
414 [gsa_size] = {.num_value = 0ULL}
417 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
420 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
421 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
422 free_regex(gsi.preg);
425 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
426 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
427 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
429 ret = output("%s", header);
432 buf = format_items(fi, values);
433 ret = output("%s", buf);
437 return output("%s", select_conf.trailer_arg);
440 /* row: a pointer to a row of the *user* table */
441 static int user_summary_loop_function(struct osl_row *row, void *data)
443 struct user_summary_info *usi = data;
449 ret = get_dir_name_of_user_row(row, usi->ui, &dirname);
452 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
457 ret = get_num_user_files(row, usi->ui, &num);
461 ret = get_num_user_bytes(row, usi->ui, &num);
469 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
473 static int compute_user_summary(struct user_info *ui, void *data)
475 struct user_summary_loop_data *usld = data;
476 struct user_summary_info *usi = usld->current++;
477 int ret = compile_regex(&usi->preg, &usi->inverse_matching);
482 ret = adu_loop_reverse(ui->table, UT_BYTES, usi, user_summary_loop_function,
483 &usi->ret, &usi->osl_errno);
484 free_regex(usi->preg);
488 static int print_user_summary_line(struct user_summary_info *usi,
489 struct format_info *fi)
491 struct user_info *ui = usi->ui;
492 union atom_value values[] = {
493 [usa_pw_name] = {.string_value = ui->pw_name?
495 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
496 [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
497 [usa_files] = {.num_value = (long long unsigned)usi->files},
498 [usa_size] = {.num_value = (long long unsigned)usi->bytes}
500 char *buf = format_items(fi, values);
501 int ret = output("%s", buf);
507 static int name_comp(const void *a, const void *b)
509 const struct user_summary_info *x = a, *y = b;
510 char *n1 = x->ui->pw_name;
511 char *n2 = y->ui->pw_name;
517 return strcmp(n1, n2);
520 static int uid_comp(const void *a, const void *b)
522 const struct user_summary_info *x = a, *y = b;
523 return -NUM_COMPARE(x->ui->uid, y->ui->uid);
526 static int dir_count_comp(const void *a, const void *b)
528 const struct user_summary_info *x = a, *y = b;
529 return NUM_COMPARE(x->dirs, y->dirs);
532 static int file_count_comp(const void *a, const void *b)
534 const struct user_summary_info *x = a, *y = b;
535 return NUM_COMPARE(x->files, y->files);
538 static int size_comp(const void *a, const void *b)
540 const struct user_summary_info *x = a, *y = b;
541 return NUM_COMPARE(x->bytes, y->bytes);
544 static int count_admissible_users(__a_unused struct user_info *ui, void *data)
546 struct user_summary_loop_data *usld = data;
547 usld->num_admissible_users++;
551 static int print_user_summary(struct format_info *fi)
554 int (*comp)(const void *a, const void *b);
555 struct user_summary_loop_data usld = { .fi = fi};
556 char *header = select_conf.header_given? select_conf.header_arg :
559 ret = output("%s", header);
562 ret = for_each_admissible_user(count_admissible_users, &usld);
565 if (usld.num_admissible_users == 0)
567 usld.usis = adu_calloc(usld.num_admissible_users
568 * sizeof(struct user_summary_info));
569 usld.current = usld.usis;
570 ret = for_each_admissible_user(compute_user_summary, &usld);
573 switch (select_conf.user_summary_sort_arg) {
574 case user_summary_sort_arg_name:
577 case user_summary_sort_arg_uid:
580 case user_summary_sort_arg_dir_count:
581 comp = dir_count_comp;
583 case user_summary_sort_arg_file_count:
584 comp = file_count_comp;
586 case user_summary_sort_arg_size:
589 default: /* this should never happen, but anyway */
593 qsort(usld.usis, usld.num_admissible_users,
594 sizeof(struct user_summary_info), comp);
595 for (i = 0; i < usld.num_admissible_users; i++) {
596 if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
598 ret = print_user_summary_line(usld.usis + i, usld.fi);
602 ret = output("%s", select_conf.trailer_arg);
608 static int user_list_loop_function(struct osl_row *row, void *data)
610 struct user_list_info *uli = data;
611 union atom_value values[] = {
612 [ula_pw_name] = {.string_value = uli->ui->pw_name?
613 uli->ui->pw_name : "?"},
614 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
615 [ula_files] = {.num_value = 0ULL},
616 [ula_size] = {.num_value = 0ULL},
617 [ula_dirname] = {.string_value = NULL}
621 char *dirname = NULL, *buf;
624 ret = -E_LOOP_COMPLETE;
628 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
631 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
635 values[ula_dirname].string_value = dirname;
637 ret = get_num_user_files(row, uli->ui, &num);
640 values[ula_files].num_value = num;
642 ret = get_num_user_bytes(row, uli->ui, &num);
645 values[ula_size].num_value = num;
647 buf = format_items(uli->fi, values);
650 ret = output("%s", buf);
659 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
663 static int print_user_list(struct user_info *ui, void *data)
665 struct user_list_format_info *ulfi = data;
667 enum user_table_columns sort_column;
668 struct user_list_info uli = {
671 .count = select_conf.limit_arg
673 union atom_value header_trailer_values[] = {
674 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
675 [ulha_pw_name] = {.string_value = ui->pw_name?
678 char *buf = format_items(ulfi->header_fi, header_trailer_values);
680 ret = output("%s", buf);
684 if (select_conf.list_sort_arg == list_sort_arg_file_count)
685 sort_column = UT_FILES;
687 sort_column = UT_BYTES;
689 ret = compile_regex(&uli.preg, &uli.inverse_matching);
692 ret = adu_loop_reverse(ui->table, sort_column, &uli,
693 user_list_loop_function, &uli.ret, &uli.osl_errno);
694 free_regex(uli.preg);
697 buf = format_items(ulfi->trailer_fi, header_trailer_values);
698 ret = output("%s", buf);
703 static int print_user_lists(struct format_info *fi)
705 struct user_list_format_info ulfi = {.fi = fi};
706 char *header_fmt = select_conf.header_given?
707 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
708 char *trailer_fmt = select_conf.trailer_arg;
709 int ret = parse_format_string(header_fmt,
710 user_list_header_trailer_atoms, &ulfi.header_fi);
713 ret = parse_format_string(trailer_fmt,
714 user_list_header_trailer_atoms, &ulfi.trailer_fi);
717 ret = for_each_admissible_user(print_user_list, &ulfi);
718 free_format_info(ulfi.header_fi);
719 free_format_info(ulfi.trailer_fi);
723 static int global_list_loop_function(struct osl_row *row, void *data)
725 struct global_list_info *gli = data;
726 union atom_value values[] = {
727 [gla_size] = {.num_value = 0ULL},
728 [gla_files] = {.num_value = 0ULL},
729 [gla_dirname] = {.string_value = NULL}
731 uint64_t num_files, num_bytes;
732 char *dirname = NULL, *buf;
736 ret = -E_LOOP_COMPLETE;
740 ret = get_dir_name_of_row(row, &dirname);
743 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
747 values[gla_dirname].string_value = dirname;
749 ret = get_num_files_of_row(row, &num_files);
752 values[gla_files].num_value = (long long unsigned)num_files;
754 ret = get_num_bytes_of_row(row, &num_bytes);
757 values[gla_size].num_value = (long long unsigned)num_bytes;
759 buf = format_items(gli->fi, values);
762 ret = output("%s", buf);
772 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
776 static int print_global_list(struct format_info *fi)
779 enum dir_table_columns sort_column;
780 struct global_list_info gli = {
782 .count = select_conf.limit_arg
784 char *header = select_conf.header_given?
785 select_conf.header_arg : "Global list\n";
787 ret = output("%s", header);
790 if (select_conf.list_sort_arg == list_sort_arg_file_count)
791 sort_column = DT_FILES;
793 sort_column = DT_BYTES;
794 ret = compile_regex(&gli.preg, &gli.inverse_matching);
797 ret = adu_loop_reverse(dir_table, sort_column, &gli,
798 global_list_loop_function, &gli.ret, &gli.osl_errno);
799 free_regex(gli.preg);
802 return output("%s", select_conf.trailer_arg);
805 static int print_statistics(struct format_info *fi)
807 switch (select_conf.select_mode_arg) {
808 case select_mode_arg_global_list:
809 return print_global_list(fi);
810 case select_mode_arg_global_summary:
811 return print_global_summary(fi);
812 case select_mode_arg_user_list:
813 return print_user_lists(fi);
814 case select_mode_arg_user_summary:
815 return print_user_summary(fi);
817 ERROR_LOG("bad select mode\n");
818 return -ERRNO_TO_ERROR(EINVAL);
822 static int open_pipe(char *path)
829 return ERRNO_TO_ERROR(errno);
832 return ERRNO_TO_ERROR(errno);
833 if (ret) { /* parent */
834 DEBUG_LOG("created process %d\n", ret);
836 output_file = fdopen(p[1], "w");
838 return ERRNO_TO_ERROR(errno);
842 if (p[0] != STDIN_FILENO)
843 dup2(p[0], STDIN_FILENO);
844 DEBUG_LOG("executing %s\n", path);
845 split_args(path, &argv, " \t");
846 execvp(argv[0], argv);
847 ERROR_LOG("error executing %s: %s\n", path,
848 adu_strerror(ERRNO_TO_ERROR(errno)));
852 static int open_output_stream(void)
855 int ret, flags = O_WRONLY | O_CREAT;
857 if (!select_conf.output_given)
859 p = select_conf.output_arg;
861 case '\0': /* empty string */
864 if (!p[1]) /* "-" means stdout */
866 /* string starting with a dash */
870 if (!p[1]) /* ">" is invalid */
877 /* string starting with ">>" */
878 if (!p[2]) /* ">>" is invalid */
884 if (!p[1]) /* "|" is invalid */
888 default: /* args starts with no magic character */
893 output_file = stdout;
897 return -E_BAD_OUTPUT_ARG;
900 * glibc's 'x' mode to fopen is not portable, so use open() and
903 ret = open(p, flags, 0644);
905 return -ERRNO_TO_ERROR(errno);
906 output_file = fdopen(ret, "w");
908 return -ERRNO_TO_ERROR(errno);
913 * Execute a select query.
915 * \param admissible_uids User IDs to take into account.
916 * \param fi Format information.
918 * Called once in select mode or for each \a run command in interactive mode.
920 * Open the output stream and the dir table if not already open. For each
921 * admissible uid, the user table is opened if necessary. After these
922 * preparations, the output according to \a select_mode and \a fi is written to
927 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
929 int ret = open_output_stream();
933 ret = open_dir_table(0);
937 ret = open_admissible_user_tables(admissible_uids);
941 ret = print_statistics(fi);
943 if (output_file && output_file != stdout) {
950 /** Default format string for global_list mode. */
951 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
952 /** Default format string for global_summary mode. */
953 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n"
954 /** Default format string for user_list mode. */
955 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
956 /** Default format string for user_summary mode. */
957 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:6) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
959 static int setup_format_string(char *fmt, struct format_info **fi)
964 INFO_LOG("using default format string\n");
965 switch (select_conf.select_mode_arg) {
966 case select_mode_arg_global_list:
968 fmt = GLOBAL_LIST_DFLT_FMT;
969 atoms = global_list_atoms;
971 case select_mode_arg_global_summary:
973 fmt = GLOBAL_SUMMARY_DFLT_FMT;
974 atoms = global_summary_atoms;
976 case select_mode_arg_user_list:
978 fmt = USER_LIST_DFLT_FMT;
979 atoms = user_list_atoms;
981 case select_mode_arg_user_summary:
983 fmt = USER_SUMMARY_DFLT_FMT;
984 atoms = user_summary_atoms;
987 ERROR_LOG("bad select mode\n");
988 return -ERRNO_TO_ERROR(EINVAL);
990 INFO_LOG("format string: %s\n", fmt);
991 return parse_format_string(fmt, atoms, fi);
995 * Parse a given format string.
997 * \param string The format string to parse.
998 * \param params gengetopt parameters.
999 * \param admissible_uids The array of admissible uid ranges.
1000 * \param fi The format info to be used with format_items().
1002 * If \a string is not \p NULL, it is broken down into its components using
1003 * \ref create_argv() and the resulting argument vector is passed together with
1004 * \a params to gengetopt's command line parser. If --help or --detailed-help
1005 * was specified in \a string, the corresponding help text is printed and the
1006 * function returns zero.
1008 * Otherwise, any --uid or --user options are parsed and transformed into an
1009 * array of admissible uids which is returned via \a admissible_uids.
1011 * Finally, the format string given by --format (or the default format string
1012 * for the given select mode if no --format option was given in \a string) is
1013 * parsed as well resulting in a format_info structure which is returned via
1014 * \a fi. The caller uses the \a fi pointer later to format each output line.
1016 * \return Negative on errors, zero if --help or --detailed-help was given,
1017 * positive otherwise.
1019 * \sa format_items().
1021 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
1022 struct uid_range **admissible_uids, struct format_info **fi)
1024 int ret, num_uid_ranges;
1032 ret = create_argv(string, &argv);
1036 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
1040 if (select_conf.help_given || select_conf.detailed_help_given)
1042 fmt = select_conf.format_arg;
1044 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
1047 num_uid_ranges = ret;
1048 ret = append_users(select_conf.user_arg, select_conf.user_given,
1049 admissible_uids, num_uid_ranges);
1052 return setup_format_string(fmt, fi);
1054 line = select_conf.detailed_help_given?
1055 select_args_info_detailed_help : select_args_info_help;
1057 output_file = stdout;
1058 for (; *line; line++) {
1059 ret = output("%s\n", *line);
1067 * Main function for select mode.
1071 int com_select(void)
1073 struct uid_range *admissible_uids = NULL;
1075 struct format_info *fi;
1076 struct select_cmdline_parser_params params = {
1079 .check_required = 1,
1080 .check_ambiguity = 1,
1084 ret = parse_select_options(conf.select_options_arg, ¶ms,
1085 &admissible_uids, &fi);
1087 ret = read_uid_file();
1090 ret = run_select_query(admissible_uids, fi);
1091 free_format_info(fi);
1094 select_cmdline_parser_free(&select_conf);