2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file select.c The select mode of adu. */
9 #include <dirent.h> /* readdir() */
12 #include "gcc-compat.h"
18 #include "select.cmdline.h"
21 #define GLOBAL_LIST_ATOMS \
24 ATOM(dirname, STRING) \
26 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
27 struct atom global_list_atoms[] = {
32 #define ATOM(x, y) gla_ ## x,
33 enum global_list_atoms {GLOBAL_LIST_ATOMS};
37 #define GLOBAL_SUMMARY_ATOMS \
42 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
43 struct atom global_summary_atoms[] = {
48 #define ATOM(x, y) gsa_ ## x,
49 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
53 #define USER_LIST_ATOMS \
54 ATOM(pw_name, STRING) \
58 ATOM(dirname, STRING) \
60 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
61 struct atom user_list_atoms[] = {
66 #define ATOM(x, y) ula_ ## x,
67 enum user_list_atoms {USER_LIST_ATOMS};
70 /* user list header */
71 #define USER_LIST_HEADER_ATOMS \
72 ATOM(pw_name, STRING) \
75 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
76 struct atom user_list_header_atoms[] = {
77 USER_LIST_HEADER_ATOMS
81 #define ATOM(x, y) ulha_ ## x,
82 enum user_list_header_atoms {USER_LIST_HEADER_ATOMS};
86 #define USER_SUMMARY_ATOMS \
87 ATOM(pw_name, STRING) \
93 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
94 struct atom user_summary_atoms[] = {
99 #define ATOM(x, y) usa_ ## x,
100 enum user_summary_atoms {USER_SUMMARY_ATOMS};
103 struct global_list_info {
107 struct format_info *fi;
110 struct global_summary_info {
111 /** Global dir count. */
113 /** Global files count. */
115 /** Global bytes count. */
121 struct user_list_info {
123 struct user_info *ui;
124 struct format_info *fi;
129 struct user_list_format_info {
130 struct format_info *fi;
131 struct format_info *header_fi;
134 struct user_summary_info {
135 struct user_info *ui;
140 struct user_summary_line_info {
141 struct format_info *fi;
145 static FILE *output_file;
147 __printf_1_2 static int output(const char const *fmt, ...)
153 ret = vfprintf(output_file, fmt, argp);
155 return ret < 0? -E_OUTPUT : 1;
158 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
160 char *result = NULL, *tmp;
162 uint64_t val = *dirnum;
163 struct osl_object obj;
169 obj.size = sizeof(val);
170 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
173 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
176 val = *(uint64_t *)obj.data;
177 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
180 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
181 tmp = make_message("%s/%s", pfx, result? result : "");
197 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
199 struct osl_object obj;
203 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
206 return get_dir_name_by_number((uint64_t *)obj.data, name);
209 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
210 struct user_info *ui, char **dirname)
212 struct osl_object obj;
213 int ret = osl(osl_get_object(ui->table, user_table_row,
218 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
221 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
223 struct osl_object obj;
224 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
227 *num_files = *(uint64_t *)obj.data;
231 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
234 struct osl_object obj;
235 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
239 *num_files = *(uint64_t *)obj.data;
243 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
245 struct osl_object obj;
246 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
249 *num_bytes = *(uint64_t *)obj.data;
253 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
256 struct osl_object obj;
257 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
261 *num_bytes = *(uint64_t *)obj.data;
265 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
269 assert(ret == -E_OSL);
270 if (osl_errno != E_OSL_LOOP)
271 /* error not caused by loop function returning negative. */
273 assert(loop_ret < 0);
274 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
276 if (loop_ret == -E_OSL) { /* osl error in loop function */
277 assert(loop_osl_errno);
278 osl_errno = loop_osl_errno;
283 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
284 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
286 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
287 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
290 static int global_summary_loop_function(struct osl_row *row, void *data)
292 struct global_summary_info *gsi = data;
296 ret = get_num_files_of_row(row, &num);
299 gsi->num_files += num;
301 ret = get_num_bytes_of_row(row, &num);
304 gsi->num_bytes += num;
309 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
313 static int print_global_summary(struct format_info *fi)
317 struct global_summary_info gsi = {.num_dirs = 0};
318 char *header = select_conf.header_given? select_conf.header_arg :
321 union atom_value values[] = {
322 [gsa_dirs] = {.num_value = 0ULL},
323 [gsa_files] = {.num_value = 0ULL},
324 [gsa_size] = {.num_value = 0ULL}
327 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
328 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
331 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
332 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
333 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
335 ret = output("%s", header);
338 buf = format_items(fi, values);
339 ret = output("%s", buf);
344 static int user_summary_loop_function(struct osl_row *row, void *data)
346 struct user_summary_info *usi = data;
350 ret = get_num_user_files(row, usi->ui, &num);
353 usi->ui->files += num;
354 ret = get_num_user_bytes(row, usi->ui, &num);
357 usi->ui->bytes += num;
362 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
366 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
368 struct user_summary_info usi = {.ui = ui};
370 return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
371 &usi.ret, &usi.osl_errno);
374 static int print_user_summary_line(struct user_info *ui, void *data)
376 struct user_summary_line_info *usli = data;
377 union atom_value values[] = {
378 [usa_pw_name] = {.string_value = ui->pw_name?
380 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
381 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
382 [usa_files] = {.num_value = (long long unsigned)ui->files},
383 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
386 int ret = -E_LOOP_COMPLETE;
391 buf = format_items(usli->fi, values);
392 ret = output("%s", buf);
398 static int name_comp(struct user_info *a, struct user_info *b)
400 char *x = a->pw_name;
401 char *y = b->pw_name;
410 static int uid_comp(struct user_info *a, struct user_info *b)
412 return -NUM_COMPARE(a->uid, b->uid);
415 static int dir_count_comp(struct user_info *a, struct user_info *b)
417 return NUM_COMPARE(a->dirs, b->dirs);
420 static int file_count_comp(struct user_info *a, struct user_info *b)
422 return NUM_COMPARE(a->files, b->files);
425 static int size_comp(struct user_info *a, struct user_info *b)
427 return NUM_COMPARE(a->bytes, b->bytes);
430 static int print_user_summary(struct format_info *fi)
433 int (*comp)(struct user_info *a, struct user_info *b);
434 struct user_summary_line_info usli = {
436 .count = select_conf.limit_arg
438 char *header = select_conf.header_given? select_conf.header_arg :
441 ret = output("%s", header);
444 ret = for_each_admissible_user(compute_user_summary, NULL);
447 switch (select_conf.user_summary_sort_arg) {
448 case user_summary_sort_arg_name:
451 case user_summary_sort_arg_uid:
454 case user_summary_sort_arg_dir_count:
455 comp = dir_count_comp;
457 case user_summary_sort_arg_file_count:
458 comp = file_count_comp;
460 case user_summary_sort_arg_size:
464 sort_hash_table(comp);
465 ret = for_each_admissible_user(print_user_summary_line, &usli);
466 if (ret == -E_LOOP_COMPLETE)
471 static int user_list_loop_function(struct osl_row *row, void *data)
473 struct user_list_info *uli = data;
474 union atom_value values[] = {
475 [ula_pw_name] = {.string_value = uli->ui->pw_name?
476 uli->ui->pw_name : "?"},
477 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
478 [ula_files] = {.num_value = 0ULL},
479 [ula_size] = {.num_value = 0ULL},
480 [ula_dirname] = {.string_value = NULL}
487 ret = -E_LOOP_COMPLETE;
491 ret = get_num_user_files(row, uli->ui, &num);
494 values[ula_files].num_value = num;
496 ret = get_num_user_bytes(row, uli->ui, &num);
499 values[ula_size].num_value = num;
501 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
504 values[ula_dirname].string_value = dirname;
506 buf = format_items(uli->fi, values);
508 ret = output("%s", buf);
516 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
520 static int print_user_list(struct user_info *ui, void *data)
522 struct user_list_format_info *ulfi = data;
524 enum user_table_columns sort_column;
525 struct user_list_info uli = {
528 .count = select_conf.limit_arg
530 union atom_value header_values[] = {
531 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
532 [ulha_pw_name] = {.string_value = ui->pw_name?
535 char *buf = format_items(ulfi->header_fi, header_values);
537 ret = output("%s", buf);
541 if (select_conf.list_sort_arg == list_sort_arg_file_count)
542 sort_column = UT_FILES;
544 sort_column = UT_BYTES;
545 return adu_loop_reverse(ui->table, sort_column, &uli, user_list_loop_function,
546 &uli.ret, &uli.osl_errno);
549 static int print_user_lists(struct format_info *fi)
551 struct user_list_format_info ulfi = {.fi = fi};
552 char *header_fmt = select_conf.header_given?
553 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
554 int ret = parse_format_string(header_fmt,
555 user_list_header_atoms, &ulfi.header_fi);
558 ret = for_each_admissible_user(print_user_list, &ulfi);
559 free_format_info(ulfi.header_fi);
563 static int global_list_loop_function(struct osl_row *row, void *data)
565 struct global_list_info *gli = data;
566 union atom_value values[] = {
567 [gla_size] = {.num_value = 0ULL},
568 [gla_files] = {.num_value = 0ULL},
569 [gla_dirname] = {.string_value = NULL}
571 uint64_t num_files, num_bytes;
576 ret = -E_LOOP_COMPLETE;
580 ret = get_num_files_of_row(row, &num_files);
583 values[gla_files].num_value = (long long unsigned)num_files;
585 ret = get_num_bytes_of_row(row, &num_bytes);
588 values[gla_size].num_value = (long long unsigned)num_bytes;
590 ret = get_dir_name_of_row(row, &dirname);
593 values[gla_dirname].string_value = dirname;
595 buf = format_items(gli->fi, values);
597 ret = output("%s", buf);
606 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
610 static int print_global_list(struct format_info *fi)
613 enum dir_table_columns sort_column;
614 struct global_list_info gli = {
616 .count = select_conf.limit_arg
618 char *header = select_conf.header_given?
619 select_conf.header_arg : "Global list\n";
620 ret = output("%s", header);
623 if (select_conf.list_sort_arg == list_sort_arg_file_count)
624 sort_column = DT_FILES;
626 sort_column = DT_BYTES;
627 return adu_loop_reverse(dir_table, sort_column, &gli,
628 global_list_loop_function, &gli.ret, &gli.osl_errno);
631 static int print_statistics(struct format_info *fi)
633 switch (select_conf.select_mode_arg) {
634 case select_mode_arg_global_list:
635 return print_global_list(fi);
636 case select_mode_arg_global_summary:
637 return print_global_summary(fi);
638 case select_mode_arg_user_list:
639 return print_user_lists(fi);
640 case select_mode_arg_user_summary:
641 return print_user_summary(fi);
643 ERROR_LOG("bad select mode\n");
644 return -ERRNO_TO_ERROR(EINVAL);
647 static int open_pipe(char *path)
654 return ERRNO_TO_ERROR(errno);
657 return ERRNO_TO_ERROR(errno);
658 if (ret) { /* parent */
659 DEBUG_LOG("created process %d\n", ret);
661 output_file = fdopen(p[1], "w");
663 return ERRNO_TO_ERROR(errno);
667 if (p[0] != STDIN_FILENO)
668 dup2(p[0], STDIN_FILENO);
669 DEBUG_LOG("executing %s\n", path);
670 argc = split_args(path, &argv, " \t");
671 execvp(argv[0], argv);
672 ERROR_LOG("error executing %s: %s\n", path,
673 adu_strerror(ERRNO_TO_ERROR(errno)));
677 static int open_output_stream(void)
680 int ret, flags = O_WRONLY | O_CREAT;
682 if (!select_conf.output_given)
684 p = select_conf.output_arg;
686 case '\0': /* empty string */
689 if (!p[1]) /* "-" means stdout */
691 /* string starting with a dash */
695 if (!p[1]) /* ">" is invalid */
702 /* string starting with ">>" */
703 if (!p[2]) /* ">>" is invalid */
709 if (!p[1]) /* "|" is invalid */
713 default: /* args starts with no magic character */
718 output_file = stdout;
722 return -E_BAD_OUTPUT_ARG;
725 * glibc's 'x' mode to fopen is not portable, so use open() and
728 ret = open(p, flags, 0644);
730 return -ERRNO_TO_ERROR(errno);
731 output_file = fdopen(ret, "w");
733 return -ERRNO_TO_ERROR(errno);
737 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
739 int ret = open_output_stream();
743 ret = open_dir_table(0);
747 ret = open_admissible_user_tables(admissible_uids);
751 ret = print_statistics(fi);
753 if (output_file && output_file != stdout) {
760 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
761 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
762 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
763 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
765 static int setup_format_string(char *fmt, struct format_info **fi)
770 INFO_LOG("using default format string\n");
771 switch (select_conf.select_mode_arg) {
772 case select_mode_arg_global_list:
774 fmt = GLOBAL_LIST_DFLT_FMT;
775 atoms = global_list_atoms;
777 case select_mode_arg_global_summary:
779 fmt = GLOBAL_SUMMARY_DFLT_FMT;
780 atoms = global_summary_atoms;
782 case select_mode_arg_user_list:
784 fmt = USER_LIST_DFLT_FMT;
785 atoms = user_list_atoms;
787 case select_mode_arg_user_summary:
789 fmt = USER_SUMMARY_DFLT_FMT;
790 atoms = user_summary_atoms;
793 ERROR_LOG("bad select mode\n");
794 return -ERRNO_TO_ERROR(EINVAL);
796 INFO_LOG("format string: %s\n", fmt);
797 return parse_format_string(fmt, atoms, fi);
800 /* return: < 0: error, >0: OK, == 0: help given */
801 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
802 struct uid_range **admissible_uids, struct format_info **fi)
804 int ret, num_uid_ranges;
812 ret = create_argv(string, &argv);
816 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
820 if (select_conf.help_given || select_conf.detailed_help_given)
822 fmt = select_conf.format_arg;
824 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
827 num_uid_ranges = ret;
828 ret = append_users(select_conf.user_arg, select_conf.user_given,
829 admissible_uids, num_uid_ranges);
832 return setup_format_string(fmt, fi);
834 line = select_conf.detailed_help_given?
835 select_args_info_detailed_help : select_args_info_help;
837 output_file = stdout;
838 for (; *line; line++) {
839 ret = output("%s\n", *line);
848 struct uid_range *admissible_uids = NULL;
850 struct format_info *fi;
851 struct select_cmdline_parser_params params = {
855 .check_ambiguity = 1,
859 ret = parse_select_options(conf.select_options_arg, ¶ms,
860 &admissible_uids, &fi);
862 ret = read_uid_file();
865 ret = run_select_query(admissible_uids, fi);
866 free_format_info(fi);
869 select_cmdline_parser_free(&select_conf);