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"
24 #define GLOBAL_LIST_ATOMS \
27 ATOM(dirname, STRING) \
29 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
30 struct atom global_list_atoms[] = {
35 #define ATOM(x, y) gla_ ## x,
36 enum global_list_atoms {GLOBAL_LIST_ATOMS};
40 #define GLOBAL_SUMMARY_ATOMS \
45 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
46 struct atom global_summary_atoms[] = {
51 #define ATOM(x, y) gsa_ ## x,
52 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
56 #define USER_LIST_ATOMS \
57 ATOM(pw_name, STRING) \
61 ATOM(dirname, STRING) \
63 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
64 struct atom user_list_atoms[] = {
69 #define ATOM(x, y) ula_ ## x,
70 enum user_list_atoms {USER_LIST_ATOMS};
73 /* user list header */
74 #define USER_LIST_HEADER_ATOMS \
75 ATOM(pw_name, STRING) \
78 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
79 struct atom user_list_header_atoms[] = {
80 USER_LIST_HEADER_ATOMS
84 #define ATOM(x, y) ulha_ ## x,
85 enum user_list_header_atoms {USER_LIST_HEADER_ATOMS};
89 #define USER_SUMMARY_ATOMS \
90 ATOM(pw_name, STRING) \
96 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
97 struct atom user_summary_atoms[] = {
102 #define ATOM(x, y) usa_ ## x,
103 enum user_summary_atoms {USER_SUMMARY_ATOMS};
106 struct global_list_info {
110 struct format_info *fi;
112 int inverse_matching;
115 struct global_summary_info {
116 /** Global dir count. */
118 /** Global files count. */
120 /** Global bytes count. */
123 int inverse_matching;
128 struct user_list_info {
130 struct user_info *ui;
131 struct format_info *fi;
133 int inverse_matching;
138 struct user_list_format_info {
139 struct format_info *fi;
140 struct format_info *header_fi;
143 struct user_summary_info {
144 struct user_info *ui;
148 int inverse_matching;
151 struct user_summary_line_info {
152 struct format_info *fi;
156 static FILE *output_file;
158 __printf_1_2 static int output(const char const *fmt, ...)
164 ret = vfprintf(output_file, fmt, argp);
166 return ret < 0? -E_OUTPUT : 1;
169 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
171 char *result = NULL, *tmp;
173 uint64_t val = *dirnum;
174 struct osl_object obj;
180 obj.size = sizeof(val);
181 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
184 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
187 val = *(uint64_t *)obj.data;
188 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
191 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
192 tmp = make_message("%s/%s", pfx, result? result : "");
208 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
210 struct osl_object obj;
214 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
217 return get_dir_name_by_number((uint64_t *)obj.data, name);
220 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
221 struct user_info *ui, char **dirname)
223 struct osl_object obj;
224 int ret = osl(osl_get_object(ui->table, user_table_row,
229 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
232 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
234 struct osl_object obj;
235 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
238 *num_files = *(uint64_t *)obj.data;
242 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
245 struct osl_object obj;
246 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
250 *num_files = *(uint64_t *)obj.data;
254 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
256 struct osl_object obj;
257 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
260 *num_bytes = *(uint64_t *)obj.data;
264 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
267 struct osl_object obj;
268 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
272 *num_bytes = *(uint64_t *)obj.data;
276 static void free_regex(regex_t *preg)
284 static int compile_regex(regex_t **preg, int *invert)
288 char *buf, *p = select_conf.pattern_arg;
290 if (!select_conf.pattern_given || !p[0]) {
303 *preg = adu_malloc(sizeof(regex_t));
304 ret = regcomp(*preg, p, 0);
307 size = regerror(ret, *preg, NULL, 0);
308 buf = adu_malloc(size);
309 regerror(ret, *preg, buf, size);
310 ERROR_LOG("%s\n", buf);
317 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
323 ret = regexec(preg, dirname, 0, NULL, 0);
324 if (ret == REG_NOMATCH && !inverse_matching)
326 if (ret != REG_NOMATCH && inverse_matching)
331 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
335 assert(ret == -E_OSL);
336 if (osl_errno != E_OSL_LOOP)
337 /* error not caused by loop function returning negative. */
339 assert(loop_ret < 0);
340 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
342 if (loop_ret == -E_OSL) { /* osl error in loop function */
343 assert(loop_osl_errno);
344 osl_errno = loop_osl_errno;
349 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
350 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
352 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
353 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
356 static int global_summary_loop_function(struct osl_row *row, void *data)
358 struct global_summary_info *gsi = data;
364 ret = get_dir_name_of_row(row, &dirname);
367 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
373 ret = get_num_files_of_row(row, &num);
376 gsi->num_files += num;
378 ret = get_num_bytes_of_row(row, &num);
381 gsi->num_bytes += num;
386 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
390 static int print_global_summary(struct format_info *fi)
394 struct global_summary_info gsi = {.num_dirs = 0};
395 char *header = select_conf.header_given? select_conf.header_arg :
398 union atom_value values[] = {
399 [gsa_dirs] = {.num_value = 0ULL},
400 [gsa_files] = {.num_value = 0ULL},
401 [gsa_size] = {.num_value = 0ULL}
404 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
407 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
408 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
409 free_regex(gsi.preg);
412 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
413 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
414 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
416 ret = output("%s", header);
419 buf = format_items(fi, values);
420 ret = output("%s", buf);
425 static int user_summary_loop_function(struct osl_row *row, void *data)
427 struct user_summary_info *usi = data;
433 ret = get_dir_name_of_row(row, &dirname);
436 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
441 ret = get_num_user_files(row, usi->ui, &num);
444 usi->ui->files += num;
445 ret = get_num_user_bytes(row, usi->ui, &num);
448 usi->ui->bytes += num;
453 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
457 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
459 struct user_summary_info usi = {.ui = ui};
460 int ret = compile_regex(&usi.preg, &usi.inverse_matching);
464 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
465 &usi.ret, &usi.osl_errno);
466 free_regex(usi.preg);
470 static int print_user_summary_line(struct user_info *ui, void *data)
472 struct user_summary_line_info *usli = data;
473 union atom_value values[] = {
474 [usa_pw_name] = {.string_value = ui->pw_name?
476 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
477 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
478 [usa_files] = {.num_value = (long long unsigned)ui->files},
479 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
482 int ret = -E_LOOP_COMPLETE;
487 buf = format_items(usli->fi, values);
488 ret = output("%s", buf);
494 static int name_comp(struct user_info *a, struct user_info *b)
496 char *x = a->pw_name;
497 char *y = b->pw_name;
506 static int uid_comp(struct user_info *a, struct user_info *b)
508 return -NUM_COMPARE(a->uid, b->uid);
511 static int dir_count_comp(struct user_info *a, struct user_info *b)
513 return NUM_COMPARE(a->dirs, b->dirs);
516 static int file_count_comp(struct user_info *a, struct user_info *b)
518 return NUM_COMPARE(a->files, b->files);
521 static int size_comp(struct user_info *a, struct user_info *b)
523 return NUM_COMPARE(a->bytes, b->bytes);
526 static int print_user_summary(struct format_info *fi)
529 int (*comp)(struct user_info *a, struct user_info *b);
530 struct user_summary_line_info usli = {
532 .count = select_conf.limit_arg
534 char *header = select_conf.header_given? select_conf.header_arg :
537 ret = output("%s", header);
540 ret = for_each_admissible_user(compute_user_summary, NULL);
543 switch (select_conf.user_summary_sort_arg) {
544 case user_summary_sort_arg_name:
547 case user_summary_sort_arg_uid:
550 case user_summary_sort_arg_dir_count:
551 comp = dir_count_comp;
553 case user_summary_sort_arg_file_count:
554 comp = file_count_comp;
556 case user_summary_sort_arg_size:
560 sort_hash_table(comp);
561 ret = for_each_admissible_user(print_user_summary_line, &usli);
562 if (ret == -E_LOOP_COMPLETE)
567 static int user_list_loop_function(struct osl_row *row, void *data)
569 struct user_list_info *uli = data;
570 union atom_value values[] = {
571 [ula_pw_name] = {.string_value = uli->ui->pw_name?
572 uli->ui->pw_name : "?"},
573 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
574 [ula_files] = {.num_value = 0ULL},
575 [ula_size] = {.num_value = 0ULL},
576 [ula_dirname] = {.string_value = NULL}
580 char *dirname = NULL, *buf;
583 ret = -E_LOOP_COMPLETE;
587 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
590 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
594 values[ula_dirname].string_value = dirname;
596 ret = get_num_user_files(row, uli->ui, &num);
599 values[ula_files].num_value = num;
601 ret = get_num_user_bytes(row, uli->ui, &num);
604 values[ula_size].num_value = num;
606 buf = format_items(uli->fi, values);
609 ret = output("%s", buf);
618 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
622 static int print_user_list(struct user_info *ui, void *data)
624 struct user_list_format_info *ulfi = data;
626 enum user_table_columns sort_column;
627 struct user_list_info uli = {
630 .count = select_conf.limit_arg
632 union atom_value header_values[] = {
633 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
634 [ulha_pw_name] = {.string_value = ui->pw_name?
637 char *buf = format_items(ulfi->header_fi, header_values);
639 ret = output("%s", buf);
643 if (select_conf.list_sort_arg == list_sort_arg_file_count)
644 sort_column = UT_FILES;
646 sort_column = UT_BYTES;
648 ret = compile_regex(&uli.preg, &uli.inverse_matching);
651 ret = adu_loop_reverse(ui->table, sort_column, &uli,
652 user_list_loop_function, &uli.ret, &uli.osl_errno);
653 free_regex(uli.preg);
657 static int print_user_lists(struct format_info *fi)
659 struct user_list_format_info ulfi = {.fi = fi};
660 char *header_fmt = select_conf.header_given?
661 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
662 int ret = parse_format_string(header_fmt,
663 user_list_header_atoms, &ulfi.header_fi);
666 ret = for_each_admissible_user(print_user_list, &ulfi);
667 free_format_info(ulfi.header_fi);
671 static int global_list_loop_function(struct osl_row *row, void *data)
673 struct global_list_info *gli = data;
674 union atom_value values[] = {
675 [gla_size] = {.num_value = 0ULL},
676 [gla_files] = {.num_value = 0ULL},
677 [gla_dirname] = {.string_value = NULL}
679 uint64_t num_files, num_bytes;
680 char *dirname = NULL, *buf;
684 ret = -E_LOOP_COMPLETE;
688 ret = get_dir_name_of_row(row, &dirname);
691 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
695 values[gla_dirname].string_value = dirname;
697 ret = get_num_files_of_row(row, &num_files);
700 values[gla_files].num_value = (long long unsigned)num_files;
702 ret = get_num_bytes_of_row(row, &num_bytes);
705 values[gla_size].num_value = (long long unsigned)num_bytes;
707 buf = format_items(gli->fi, values);
710 ret = output("%s", buf);
720 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
724 static int print_global_list(struct format_info *fi)
727 enum dir_table_columns sort_column;
728 struct global_list_info gli = {
730 .count = select_conf.limit_arg
732 char *header = select_conf.header_given?
733 select_conf.header_arg : "Global list\n";
735 ret = output("%s", header);
738 if (select_conf.list_sort_arg == list_sort_arg_file_count)
739 sort_column = DT_FILES;
741 sort_column = DT_BYTES;
742 ret = compile_regex(&gli.preg, &gli.inverse_matching);
745 ret = adu_loop_reverse(dir_table, sort_column, &gli,
746 global_list_loop_function, &gli.ret, &gli.osl_errno);
747 free_regex(gli.preg);
751 static int print_statistics(struct format_info *fi)
753 switch (select_conf.select_mode_arg) {
754 case select_mode_arg_global_list:
755 return print_global_list(fi);
756 case select_mode_arg_global_summary:
757 return print_global_summary(fi);
758 case select_mode_arg_user_list:
759 return print_user_lists(fi);
760 case select_mode_arg_user_summary:
761 return print_user_summary(fi);
763 ERROR_LOG("bad select mode\n");
764 return -ERRNO_TO_ERROR(EINVAL);
767 static int open_pipe(char *path)
774 return ERRNO_TO_ERROR(errno);
777 return ERRNO_TO_ERROR(errno);
778 if (ret) { /* parent */
779 DEBUG_LOG("created process %d\n", ret);
781 output_file = fdopen(p[1], "w");
783 return ERRNO_TO_ERROR(errno);
787 if (p[0] != STDIN_FILENO)
788 dup2(p[0], STDIN_FILENO);
789 DEBUG_LOG("executing %s\n", path);
790 argc = split_args(path, &argv, " \t");
791 execvp(argv[0], argv);
792 ERROR_LOG("error executing %s: %s\n", path,
793 adu_strerror(ERRNO_TO_ERROR(errno)));
797 static int open_output_stream(void)
800 int ret, flags = O_WRONLY | O_CREAT;
802 if (!select_conf.output_given)
804 p = select_conf.output_arg;
806 case '\0': /* empty string */
809 if (!p[1]) /* "-" means stdout */
811 /* string starting with a dash */
815 if (!p[1]) /* ">" is invalid */
822 /* string starting with ">>" */
823 if (!p[2]) /* ">>" is invalid */
829 if (!p[1]) /* "|" is invalid */
833 default: /* args starts with no magic character */
838 output_file = stdout;
842 return -E_BAD_OUTPUT_ARG;
845 * glibc's 'x' mode to fopen is not portable, so use open() and
848 ret = open(p, flags, 0644);
850 return -ERRNO_TO_ERROR(errno);
851 output_file = fdopen(ret, "w");
853 return -ERRNO_TO_ERROR(errno);
857 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
859 int ret = open_output_stream();
863 ret = open_dir_table(0);
867 ret = open_admissible_user_tables(admissible_uids);
871 ret = print_statistics(fi);
873 if (output_file && output_file != stdout) {
880 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
881 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
882 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
883 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
885 static int setup_format_string(char *fmt, struct format_info **fi)
890 INFO_LOG("using default format string\n");
891 switch (select_conf.select_mode_arg) {
892 case select_mode_arg_global_list:
894 fmt = GLOBAL_LIST_DFLT_FMT;
895 atoms = global_list_atoms;
897 case select_mode_arg_global_summary:
899 fmt = GLOBAL_SUMMARY_DFLT_FMT;
900 atoms = global_summary_atoms;
902 case select_mode_arg_user_list:
904 fmt = USER_LIST_DFLT_FMT;
905 atoms = user_list_atoms;
907 case select_mode_arg_user_summary:
909 fmt = USER_SUMMARY_DFLT_FMT;
910 atoms = user_summary_atoms;
913 ERROR_LOG("bad select mode\n");
914 return -ERRNO_TO_ERROR(EINVAL);
916 INFO_LOG("format string: %s\n", fmt);
917 return parse_format_string(fmt, atoms, fi);
920 /* return: < 0: error, >0: OK, == 0: help given */
921 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
922 struct uid_range **admissible_uids, struct format_info **fi)
924 int ret, num_uid_ranges;
932 ret = create_argv(string, &argv);
936 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
940 if (select_conf.help_given || select_conf.detailed_help_given)
942 fmt = select_conf.format_arg;
944 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
947 num_uid_ranges = ret;
948 ret = append_users(select_conf.user_arg, select_conf.user_given,
949 admissible_uids, num_uid_ranges);
952 return setup_format_string(fmt, fi);
954 line = select_conf.detailed_help_given?
955 select_args_info_detailed_help : select_args_info_help;
957 output_file = stdout;
958 for (; *line; line++) {
959 ret = output("%s\n", *line);
968 struct uid_range *admissible_uids = NULL;
970 struct format_info *fi;
971 struct select_cmdline_parser_params params = {
975 .check_ambiguity = 1,
979 ret = parse_select_options(conf.select_options_arg, ¶ms,
980 &admissible_uids, &fi);
982 ret = read_uid_file();
985 ret = run_select_query(admissible_uids, fi);
986 free_format_info(fi);
989 select_cmdline_parser_free(&select_conf);