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:
563 sort_hash_table(comp);
564 ret = for_each_admissible_user(print_user_summary_line, &usli);
565 if (ret == -E_LOOP_COMPLETE)
570 static int user_list_loop_function(struct osl_row *row, void *data)
572 struct user_list_info *uli = data;
573 union atom_value values[] = {
574 [ula_pw_name] = {.string_value = uli->ui->pw_name?
575 uli->ui->pw_name : "?"},
576 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
577 [ula_files] = {.num_value = 0ULL},
578 [ula_size] = {.num_value = 0ULL},
579 [ula_dirname] = {.string_value = NULL}
583 char *dirname = NULL, *buf;
586 ret = -E_LOOP_COMPLETE;
590 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
593 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
597 values[ula_dirname].string_value = dirname;
599 ret = get_num_user_files(row, uli->ui, &num);
602 values[ula_files].num_value = num;
604 ret = get_num_user_bytes(row, uli->ui, &num);
607 values[ula_size].num_value = num;
609 buf = format_items(uli->fi, values);
612 ret = output("%s", buf);
621 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
625 static int print_user_list(struct user_info *ui, void *data)
627 struct user_list_format_info *ulfi = data;
629 enum user_table_columns sort_column;
630 struct user_list_info uli = {
633 .count = select_conf.limit_arg
635 union atom_value header_values[] = {
636 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
637 [ulha_pw_name] = {.string_value = ui->pw_name?
640 char *buf = format_items(ulfi->header_fi, header_values);
642 ret = output("%s", buf);
646 if (select_conf.list_sort_arg == list_sort_arg_file_count)
647 sort_column = UT_FILES;
649 sort_column = UT_BYTES;
651 ret = compile_regex(&uli.preg, &uli.inverse_matching);
654 ret = adu_loop_reverse(ui->table, sort_column, &uli,
655 user_list_loop_function, &uli.ret, &uli.osl_errno);
656 free_regex(uli.preg);
660 static int print_user_lists(struct format_info *fi)
662 struct user_list_format_info ulfi = {.fi = fi};
663 char *header_fmt = select_conf.header_given?
664 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
665 int ret = parse_format_string(header_fmt,
666 user_list_header_atoms, &ulfi.header_fi);
669 ret = for_each_admissible_user(print_user_list, &ulfi);
670 free_format_info(ulfi.header_fi);
674 static int global_list_loop_function(struct osl_row *row, void *data)
676 struct global_list_info *gli = data;
677 union atom_value values[] = {
678 [gla_size] = {.num_value = 0ULL},
679 [gla_files] = {.num_value = 0ULL},
680 [gla_dirname] = {.string_value = NULL}
682 uint64_t num_files, num_bytes;
683 char *dirname = NULL, *buf;
687 ret = -E_LOOP_COMPLETE;
691 ret = get_dir_name_of_row(row, &dirname);
694 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
698 values[gla_dirname].string_value = dirname;
700 ret = get_num_files_of_row(row, &num_files);
703 values[gla_files].num_value = (long long unsigned)num_files;
705 ret = get_num_bytes_of_row(row, &num_bytes);
708 values[gla_size].num_value = (long long unsigned)num_bytes;
710 buf = format_items(gli->fi, values);
713 ret = output("%s", buf);
723 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
727 static int print_global_list(struct format_info *fi)
730 enum dir_table_columns sort_column;
731 struct global_list_info gli = {
733 .count = select_conf.limit_arg
735 char *header = select_conf.header_given?
736 select_conf.header_arg : "Global list\n";
738 ret = output("%s", header);
741 if (select_conf.list_sort_arg == list_sort_arg_file_count)
742 sort_column = DT_FILES;
744 sort_column = DT_BYTES;
745 ret = compile_regex(&gli.preg, &gli.inverse_matching);
748 ret = adu_loop_reverse(dir_table, sort_column, &gli,
749 global_list_loop_function, &gli.ret, &gli.osl_errno);
750 free_regex(gli.preg);
754 static int print_statistics(struct format_info *fi)
756 switch (select_conf.select_mode_arg) {
757 case select_mode_arg_global_list:
758 return print_global_list(fi);
759 case select_mode_arg_global_summary:
760 return print_global_summary(fi);
761 case select_mode_arg_user_list:
762 return print_user_lists(fi);
763 case select_mode_arg_user_summary:
764 return print_user_summary(fi);
766 ERROR_LOG("bad select mode\n");
767 return -ERRNO_TO_ERROR(EINVAL);
770 static int open_pipe(char *path)
777 return ERRNO_TO_ERROR(errno);
780 return ERRNO_TO_ERROR(errno);
781 if (ret) { /* parent */
782 DEBUG_LOG("created process %d\n", ret);
784 output_file = fdopen(p[1], "w");
786 return ERRNO_TO_ERROR(errno);
790 if (p[0] != STDIN_FILENO)
791 dup2(p[0], STDIN_FILENO);
792 DEBUG_LOG("executing %s\n", path);
793 argc = split_args(path, &argv, " \t");
794 execvp(argv[0], argv);
795 ERROR_LOG("error executing %s: %s\n", path,
796 adu_strerror(ERRNO_TO_ERROR(errno)));
800 static int open_output_stream(void)
803 int ret, flags = O_WRONLY | O_CREAT;
805 if (!select_conf.output_given)
807 p = select_conf.output_arg;
809 case '\0': /* empty string */
812 if (!p[1]) /* "-" means stdout */
814 /* string starting with a dash */
818 if (!p[1]) /* ">" is invalid */
825 /* string starting with ">>" */
826 if (!p[2]) /* ">>" is invalid */
832 if (!p[1]) /* "|" is invalid */
836 default: /* args starts with no magic character */
841 output_file = stdout;
845 return -E_BAD_OUTPUT_ARG;
848 * glibc's 'x' mode to fopen is not portable, so use open() and
851 ret = open(p, flags, 0644);
853 return -ERRNO_TO_ERROR(errno);
854 output_file = fdopen(ret, "w");
856 return -ERRNO_TO_ERROR(errno);
861 * Execute a select query.
863 * \param admissible_uids User IDs to take into account.
864 * \param fi Format information.
866 * Called once in select mode or for each \a run command in interactive mode.
868 * Open the output stream and the dir table if not already open. For each
869 * admissible uid, the user table is opened if necessary. After these
870 * preparations, the output according to \a select_mode and \a fi is written to
875 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
877 int ret = open_output_stream();
881 ret = open_dir_table(0);
885 ret = open_admissible_user_tables(admissible_uids);
889 ret = print_statistics(fi);
891 if (output_file && output_file != stdout) {
898 /** Default format string for global_list mode. */
899 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
900 /** Default format string for global_summary mode. */
901 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
902 /** Default format string for user_list mode. */
903 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
904 /** Default format string for user_summary mode. */
905 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
907 static int setup_format_string(char *fmt, struct format_info **fi)
912 INFO_LOG("using default format string\n");
913 switch (select_conf.select_mode_arg) {
914 case select_mode_arg_global_list:
916 fmt = GLOBAL_LIST_DFLT_FMT;
917 atoms = global_list_atoms;
919 case select_mode_arg_global_summary:
921 fmt = GLOBAL_SUMMARY_DFLT_FMT;
922 atoms = global_summary_atoms;
924 case select_mode_arg_user_list:
926 fmt = USER_LIST_DFLT_FMT;
927 atoms = user_list_atoms;
929 case select_mode_arg_user_summary:
931 fmt = USER_SUMMARY_DFLT_FMT;
932 atoms = user_summary_atoms;
935 ERROR_LOG("bad select mode\n");
936 return -ERRNO_TO_ERROR(EINVAL);
938 INFO_LOG("format string: %s\n", fmt);
939 return parse_format_string(fmt, atoms, fi);
942 /* return: < 0: error, >0: OK, == 0: help given */
943 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
944 struct uid_range **admissible_uids, struct format_info **fi)
946 int ret, num_uid_ranges;
954 ret = create_argv(string, &argv);
958 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
962 if (select_conf.help_given || select_conf.detailed_help_given)
964 fmt = select_conf.format_arg;
966 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
969 num_uid_ranges = ret;
970 ret = append_users(select_conf.user_arg, select_conf.user_given,
971 admissible_uids, num_uid_ranges);
974 return setup_format_string(fmt, fi);
976 line = select_conf.detailed_help_given?
977 select_args_info_detailed_help : select_args_info_help;
979 output_file = stdout;
980 for (; *line; line++) {
981 ret = output("%s\n", *line);
989 * Main function for select mode.
995 struct uid_range *admissible_uids = NULL;
997 struct format_info *fi;
998 struct select_cmdline_parser_params params = {
1001 .check_required = 1,
1002 .check_ambiguity = 1,
1006 ret = parse_select_options(conf.select_options_arg, ¶ms,
1007 &admissible_uids, &fi);
1009 ret = read_uid_file();
1012 ret = run_select_query(admissible_uids, fi);
1013 free_format_info(fi);
1016 select_cmdline_parser_free(&select_conf);