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"
17 #include "portable_io.h"
19 /** Global dir count. */
20 static uint64_t num_dirs;
21 /** Global files count. */
22 static uint64_t num_files;
23 /** Global bytes count. */
24 static uint64_t num_bytes;
27 /** The decimal representation of an uint64_t never exceeds that size. */
28 #define FORMATED_VALUE_SIZE 25
30 #define USER_SUMMARY_ATOMS \
31 ATOM(pw_name, STRING) \
37 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
38 struct atom user_summary_atoms[] = {
43 #define ATOM(x, y) usa_ ## x,
44 enum user_summary_atoms {USER_SUMMARY_ATOMS};
47 /* these get filled in by the select command. */
48 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
50 enum global_stats_flags {
51 GSF_PRINT_DIRNAME = 1,
54 GSF_COMPUTE_SUMMARY = 8,
57 struct global_stats_info {
61 enum global_stats_flags flags;
64 enum user_stats_flags {
65 USF_PRINT_DIRNAME = 1,
68 USF_COMPUTE_SUMMARY = 8,
71 struct user_stats_info {
73 enum user_stats_flags flags;
79 static const uint64_t size_unit_divisors[] = {
80 [size_unit_arg_b] = 1ULL,
81 [size_unit_arg_k] = 1024ULL,
82 [size_unit_arg_m] = 1024ULL * 1024ULL,
83 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
84 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
87 static const uint64_t count_unit_divisors[] = {
89 [count_unit_arg_n] = 1ULL,
90 [count_unit_arg_k] = 1000ULL,
91 [count_unit_arg_m] = 1000ULL * 1000ULL,
92 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
93 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
96 static const char size_unit_abbrevs[] = " BKMGT";
97 static const char count_unit_abbrevs[] = " kmgt";
98 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
99 uint64_t value, int print_unit, char *result)
101 enum enum_size_unit u = unit;
102 char unit_buf[2] = "\0\0";
104 if (unit == size_unit_arg_h) /* human readable */
105 for (u = size_unit_arg_b; u < size_unit_arg_t &&
106 value > size_unit_divisors[u + 1]; u++)
109 unit_buf[0] = size_unit_abbrevs[u];
110 sprintf(result, "%llu%s",
111 (long long unsigned)value / size_unit_divisors[u], unit_buf);
115 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
116 uint64_t value, int print_unit, char *result)
118 enum enum_count_unit u = unit;
119 char unit_buf[2] = "\0\0";
121 if (unit == count_unit_arg_h) /* human readable */
122 for (u = count_unit_arg_n; u < count_unit_arg_t &&
123 value > count_unit_divisors[u + 1]; u++)
126 unit_buf[0] = count_unit_abbrevs[u];
127 sprintf(result, "%llu%s",
128 (long long unsigned)value / count_unit_divisors[u], unit_buf);
132 static FILE *output_file;
134 __printf_1_2 static int output(const char const *fmt, ...)
140 ret = vfprintf(output_file, fmt, argp);
142 return ret < 0? -E_OUTPUT : 1;
145 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
147 char *result = NULL, *tmp;
149 uint64_t val = *dirnum;
150 struct osl_object obj;
156 obj.size = sizeof(val);
157 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
160 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
163 val = *(uint64_t *)obj.data;
164 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
167 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
168 tmp = make_message("%s/%s", pfx, result? result : "");
184 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
186 struct osl_object obj;
190 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
193 return get_dir_name_by_number((uint64_t *)obj.data, name);
196 static int user_stats_loop_function(struct osl_row *row, void *data)
198 struct user_stats_info *usi = data;
199 struct osl_object obj;
200 int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
201 char formated_value[FORMATED_VALUE_SIZE];
204 if (!usi->count && !summary) {
205 ret = -E_LOOP_COMPLETE;
208 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
210 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
213 files = *(uint64_t *)obj.data;
214 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
215 format_count_value(select_conf.count_unit_arg, files,
216 select_conf.count_unit_arg == count_unit_arg_h,
218 ret = output("\t%s%s", formated_value,
219 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
225 usi->ui->files += files;
227 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
229 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
232 bytes = *(uint64_t *)obj.data;
233 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
234 format_size_value(select_conf.size_unit_arg, bytes,
235 select_conf.size_unit_arg == size_unit_arg_h,
237 ret = output("%s%s%s",
238 (usi->flags & USF_PRINT_FILES)? "" : "\t",
240 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
246 usi->ui->bytes += bytes;
251 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
253 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
256 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
259 ret = output("%s%s\n",
260 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
271 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
275 static int global_stats_loop_function(struct osl_row *row, void *data)
277 struct global_stats_info *gsi = data;
278 struct osl_object obj;
279 char *dirname, formated_value[FORMATED_VALUE_SIZE];
280 int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
283 if (!gsi->count && !summary) {
284 ret = -E_LOOP_COMPLETE;
287 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
289 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
292 files = *(uint64_t *)obj.data;
293 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
294 format_count_value(select_conf.count_unit_arg, files,
295 select_conf.count_unit_arg == count_unit_arg_h,
297 ret = output("\t%s%s", formated_value,
298 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
306 if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
308 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
311 bytes = *(uint64_t *)obj.data;
312 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
313 format_size_value(select_conf.size_unit_arg, bytes,
314 select_conf.size_unit_arg == size_unit_arg_h,
316 ret = output("%s%s%s",
317 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
319 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
329 if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
330 ret = get_dir_name_of_row(row, &dirname);
333 ret = output("%s%s\n",
334 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
345 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
349 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
353 assert(ret == -E_OSL);
354 if (osl_errno != E_OSL_LOOP)
355 /* error not caused by loop function returning negative. */
357 assert(loop_ret < 0);
358 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
360 if (loop_ret == -E_OSL) { /* osl error in loop function */
361 assert(loop_osl_errno);
362 osl_errno = loop_osl_errno;
367 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
368 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
370 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
371 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
374 static int print_global_summary(void)
376 char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
377 s[FORMATED_VALUE_SIZE];
378 enum enum_count_unit ud, uf;
379 enum enum_size_unit us;
382 if (select_conf.no_global_summary_given)
384 ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
385 uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
386 us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
388 if (!select_conf.no_headers_given) {
389 ret = output("Global summary "
390 "(dirs(%c)/files(%c)/size(%c))\n",
391 count_unit_abbrevs[ud],
392 count_unit_abbrevs[uf],
393 size_unit_abbrevs[us]
398 return output("\t%s\t%s\t%s\n\n", d, f, s);
401 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
403 struct format_info *fi = data;
404 union atom_value values[] = {
405 [usa_pw_name] = {.string_value = ui->pw_name?
407 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
408 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
409 [usa_files] = {.num_value = (long long unsigned)ui->files},
410 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
412 char *buf = format_items(fi, values);
413 return output("%s", buf);
416 static int name_comp(const void *a, const void *b)
418 char *x = ((struct user_info *)a)->pw_name;
419 char *y = ((struct user_info *)b)->pw_name;
428 static int uid_comp(const void *a, const void *b)
430 return -NUM_COMPARE(((struct user_info *)a)->uid,
431 ((struct user_info *)b)->uid);
434 static int dir_count_comp(const void *a, const void *b)
436 return NUM_COMPARE(((struct user_info *)a)->dirs,
437 ((struct user_info *)b)->dirs);
440 static int file_count_comp(const void *a, const void *b)
442 return NUM_COMPARE(((struct user_info *)a)->files,
443 ((struct user_info *)b)->files);
446 static int size_comp(const void *a, const void *b)
448 return NUM_COMPARE(((struct user_info *)a)->bytes,
449 ((struct user_info *)b)->bytes);
453 * The comparators for sorting the user summary.
455 * This is an array of pointers to functions taking two constant void *
456 * pointers and returning an int.
458 static int (*summary_comparators[])(const void *, const void *) = {
459 [user_summary_sort_arg_name] = name_comp,
460 [user_summary_sort_arg_uid] = uid_comp,
461 [user_summary_sort_arg_dir_count] = dir_count_comp,
462 [user_summary_sort_arg_file_count] = file_count_comp,
463 [user_summary_sort_arg_size] = size_comp,
466 static int print_user_summary(struct format_info *fi)
468 if (select_conf.no_user_summary_given)
470 if (!select_conf.no_headers_given) {
471 int ret = output("User summary\n");
475 sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
476 return for_each_admissible_user(print_user_summary_line, fi);
479 static int print_user_list(struct user_info *ui, __a_unused void *data)
482 struct user_stats_info usi;
483 enum enum_user_list ula = select_conf.user_list_arg;
484 int print_size_list = (ula == user_list_arg_size
485 || ula == user_list_arg_both);
487 if (print_size_list) {
488 usi.count = select_conf.limit_arg;
490 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
491 if (!select_conf.no_headers_given) {
492 ret = output("%s (uid %u), by size%s:\n",
493 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
498 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
499 &usi.ret, &usi.osl_errno);
506 if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
507 if (!select_conf.no_headers_given) {
508 ret = output("%s (uid %u), by file count%s:\n",
509 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
514 usi.count = select_conf.limit_arg,
516 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
517 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
518 &usi.ret, &usi.osl_errno);
525 if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
526 usi.count = select_conf.limit_arg;
528 usi.flags = USF_COMPUTE_SUMMARY;
529 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
530 &usi.ret, &usi.osl_errno);
537 static int print_user_lists(void)
539 return for_each_admissible_user(print_user_list, NULL);
542 static int print_global_lists(void)
544 struct global_stats_info gsi;
546 enum enum_global_list gla = select_conf.global_list_arg;
547 int print_size_list = (gla == global_list_arg_size
548 || gla == global_list_arg_both);
550 if (print_size_list) {
551 gsi.count = select_conf.limit_arg;
552 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
553 if (!select_conf.no_headers_given) {
554 ret = output("By size%s:\n", size_unit_buf);
558 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
559 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
566 if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
567 gsi.count = select_conf.limit_arg;
568 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
569 if (!print_size_list)
570 gsi.flags |= GSF_COMPUTE_SUMMARY;
571 if (!select_conf.no_headers_given) {
572 ret = output("By file count%s:\n", count_unit_buf);
576 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
577 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
584 if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
585 /* must compute summary */
586 gsi.count = select_conf.limit_arg;
587 gsi.flags = GSF_COMPUTE_SUMMARY;
588 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
589 global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
596 static int print_statistics(struct select_format_info *sli)
600 ret = print_global_lists();
603 ret = print_global_summary();
606 ret = print_user_lists();
609 ret = print_user_summary(sli->user_summary_fi);
615 static int read_uid_file(struct uid_range *admissible_uids)
619 char *filename = get_uid_list_name(), *map;
620 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
624 INFO_LOG("failed to map %s\n", filename);
629 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
632 * Compute number of hash table bits. The hash table size must be a
633 * power of two and larger than the number of uids.
636 while (1 << bits < num_uids)
638 create_hash_table(bits);
639 for (n = 0; n < num_uids; n++) {
640 uint32_t uid = read_u32(map + n * sizeof(uid));
641 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
646 adu_munmap(map, size);
650 int run_select_query(struct uid_range *admissible_uids,
651 struct select_format_info *sfi)
655 if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
656 output_file = fopen(select_conf.output_arg, "w");
658 return -ERRNO_TO_ERROR(errno);
660 output_file = stdout;
662 if (select_conf.count_unit_arg != count_unit_arg_h)
663 count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
665 count_unit_buf[0] = '\0';
666 if (select_conf.size_unit_arg != size_unit_arg_h)
667 size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
669 size_unit_buf[0] = '\0';
671 ret = open_dir_table(0);
675 ret = read_uid_file(admissible_uids);
679 ret = print_statistics(sfi);
682 if (output_file != stdout)
687 /* return: < 0: error, >0: OK, == 0: help given */
688 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
689 struct uid_range **admissible_uids, struct select_format_info *sfi)
694 if (conf.select_options_given) {
698 ret = create_argv(string, &argv);
702 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
706 if (select_conf.help_given || select_conf.detailed_help_given)
710 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
713 ret = parse_format_string(select_conf.user_summary_format_arg,
714 user_summary_atoms, &sfi->user_summary_fi);
719 line = select_conf.detailed_help_given?
720 select_args_info_detailed_help : select_args_info_help;
722 output_file = stdout;
723 for (; *line; line++) {
724 ret = output("%s\n", *line);
733 struct uid_range *admissible_uids = NULL;
734 struct select_format_info sfi;
736 struct select_cmdline_parser_params params = {
740 .check_ambiguity = 1,
744 select_cmdline_parser_init(&select_conf);
745 ret = parse_select_options(conf.select_options_arg, ¶ms,
746 &admissible_uids, &sfi);
747 if (ret <= 0) /* do not run query if help was given */
749 return run_select_query(admissible_uids, &sfi);