X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=select.c;h=4c2f12cbc7ab7e6f7f69bfbd7809daf866f4959a;hp=e09bce1052abc8f51c1e8cf58948d8440110ead5;hb=a8c239250e33966020dd175e34257b4c2d4b4f2e;hpb=0356ff402eec5e75a2c9e24c2fcb5b2c3a1cb63d diff --git a/select.c b/select.c index e09bce1..4c2f12c 100644 --- a/select.c +++ b/select.c @@ -15,6 +15,13 @@ #include "error.h" #include "portable_io.h" +/** Global dir count. */ +static uint64_t num_dirs; +/** Global files count. */ +static uint64_t num_files; +/** Global bytes count. */ +static uint64_t num_bytes; + /** The decimal representation of an uint64_t never exceeds that size. */ #define FORMATED_VALUE_SIZE 25 @@ -109,34 +116,37 @@ static int get_dir_name_by_number(uint64_t *dirnum, char **name) char *result = NULL, *tmp; struct osl_row *row; uint64_t val = *dirnum; - struct osl_object obj = {.data = &val, .size = sizeof(val)}; + struct osl_object obj; int ret; + char *pfx; again: + obj.data = &val; + obj.size = sizeof(val); ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row)); if (ret < 0) goto out; - ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj)); - if (ret < 0) - goto out; - if (result) { - tmp = make_message("%s/%s", (char *)obj.data, result); - free(result); - result = tmp; - } else - result = adu_strdup((char *)obj.data); ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj)); if (ret < 0) goto out; val = *(uint64_t *)obj.data; + ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj)); + if (ret < 0) + goto out; + pfx = (conf.print_base_dir_given || val)? (char *)obj.data : "."; + tmp = make_message("%s/%s", pfx, result? result : ""); + free(result); + result = tmp; if (val) goto again; out: if (ret < 0) { free(result); *name = NULL; - } else + } else { + assert(result); *name = result; + } return ret; } @@ -144,30 +154,14 @@ static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name) { struct osl_object obj; int ret; - char *this_dir, *prefix = NULL; *name = NULL; - ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj)); + ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj)); if (ret < 0) return ret; - this_dir = adu_strdup((char *)obj.data); - ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj)); - if (ret < 0) - goto out; - if (!*(uint64_t *)obj.data) { - *name = this_dir; - return 1; - } - ret = get_dir_name_by_number((uint64_t *)obj.data, &prefix); - if (ret < 0) - goto out; - *name = make_message("%s/%s", prefix, this_dir); - free(prefix); - ret = 1; -out: - free(this_dir); - return ret; + return get_dir_name_by_number((uint64_t *)obj.data, name); } + static int user_stats_loop_function(struct osl_row *row, void *data) { struct user_stats_info *usi = data; @@ -342,77 +336,138 @@ static void print_global_summary(void) enum enum_count_unit ud, uf; enum enum_size_unit us; + if (conf.no_global_summary_given) + return; ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d); uf = format_count_value(conf.count_unit_arg, num_files, 0, f); us = format_size_value(conf.size_unit_arg, num_bytes, 0, s); - printf("Global summary " - "(dirs(%c)/files(%c)/size(%c))\n" - "\t%s\t%s\t%s\n\n", - count_unit_abbrevs[ud], - count_unit_abbrevs[uf], - size_unit_abbrevs[us], - d, f, s + if (!conf.no_headers_given) + printf("Global summary " + "(dirs(%c)/files(%c)/size(%c))\n", + count_unit_abbrevs[ud], + count_unit_abbrevs[uf], + size_unit_abbrevs[us] + ); + printf("\t%s\t%s\t%s\n\n", d, f, s); +} + +static int print_user_summary_line(struct user_info *ui, __a_unused void *data) +{ + char formated_dir_count[FORMATED_VALUE_SIZE], + formated_file_count[FORMATED_VALUE_SIZE], + formated_bytes[FORMATED_VALUE_SIZE ]; + + format_count_value(conf.count_unit_arg, ui->dirs, + conf.count_unit_arg == count_unit_arg_h, + formated_dir_count); + format_count_value(conf.count_unit_arg, ui->files, + conf.count_unit_arg == count_unit_arg_h, + formated_file_count); + format_size_value(conf.size_unit_arg, ui->bytes, + conf.size_unit_arg == size_unit_arg_h, + formated_bytes); + printf("\t%s\t%u\t%s\t%s\t%s\n", + ui->pw_name? ui->pw_name : "?", + (unsigned)ui->uid, + formated_dir_count, + formated_file_count, + formated_bytes ); + return 1; +} +static int name_comp(const void *a, const void *b) +{ + char *x = ((struct user_info *)a)->pw_name; + char *y = ((struct user_info *)b)->pw_name; + + if (!x) + return 1; + if (!y) + return -1; + return strcmp(x, y); } -static void print_id_stats(void) +static int uid_comp(const void *a, const void *b) { - struct user_info *ui; + return -NUM_COMPARE(((struct user_info *)a)->uid, + ((struct user_info *)b)->uid); +} - printf("User summary " - "(pw_name/uid/dirs%s/files%s/size%s):\n", - count_unit_buf, count_unit_buf, size_unit_buf); - FOR_EACH_USER(ui) { - char formated_dir_count[FORMATED_VALUE_SIZE], - formated_file_count[FORMATED_VALUE_SIZE], - formated_bytes[FORMATED_VALUE_SIZE ]; - if (!ui_used(ui) || !ui_admissible(ui)) - continue; - format_count_value(conf.count_unit_arg, ui->dirs, - conf.count_unit_arg == count_unit_arg_h, - formated_dir_count); - format_count_value(conf.count_unit_arg, ui->files, - conf.count_unit_arg == count_unit_arg_h, - formated_file_count); - format_size_value(conf.size_unit_arg, ui->bytes, - conf.size_unit_arg == size_unit_arg_h, - formated_bytes); - printf("\t%s\t%u\t%s\t%s\t%s\n", - ui->pw_name? ui->pw_name : "?", - (unsigned)ui->uid, - formated_dir_count, - formated_file_count, - formated_bytes - ); - } +static int dir_count_comp(const void *a, const void *b) +{ + return NUM_COMPARE(((struct user_info *)a)->dirs, + ((struct user_info *)b)->dirs); } -static int print_user_stats(void) +static int file_count_comp(const void *a, const void *b) { - struct user_info *ui; - int ret; + return NUM_COMPARE(((struct user_info *)a)->files, + ((struct user_info *)b)->files); +} + +static int size_comp(const void *a, const void *b) +{ + return NUM_COMPARE(((struct user_info *)a)->bytes, + ((struct user_info *)b)->bytes); +} - FOR_EACH_USER(ui) { - struct user_stats_info usi = { - .count = conf.limit_arg, - .ui = ui - }; - if (!ui_used(ui) || !ui_admissible(ui)) - continue; +/* + * The comparators for sorting the user summary. + * + * This is an array of pointers to functions taking two constant void * + * pointers and returning an int. + */ +static int (*summary_comparators[])(const void *, const void *) = { + [user_summary_sort_arg_name] = name_comp, + [user_summary_sort_arg_uid] = uid_comp, + [user_summary_sort_arg_dir_count] = dir_count_comp, + [user_summary_sort_arg_file_count] = file_count_comp, + [user_summary_sort_arg_size] = size_comp, +}; + +static void print_user_summary(void) +{ + if (conf.no_user_summary_given) + return; + if (!conf.no_headers_given) + printf("User summary " + "(pw_name/uid/dirs%s/files%s/size%s):\n", + count_unit_buf, count_unit_buf, size_unit_buf); + sort_hash_table(summary_comparators[conf.user_summary_sort_arg]); + for_each_admissible_user(print_user_summary_line, NULL); +} + +static int print_user_list(struct user_info *ui, __a_unused void *data) +{ + int ret; + struct user_stats_info usi; + enum enum_user_list ula = conf.user_list_arg; + int print_size_list = (ula == user_list_arg_size + || ula == user_list_arg_both); + + if (print_size_list) { + usi.count = conf.limit_arg; + usi.ui = ui; usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY; - printf("%s (uid %u), by size%s:\n", - ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid, - size_unit_buf); + if (!conf.no_headers_given) + printf("%s (uid %u), by size%s:\n", + ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid, + size_unit_buf); ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function, &usi.ret, &usi.osl_errno); if (ret < 0) return ret; - printf("\n%s (uid %u), by file count%s:\n", - ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid, - count_unit_buf); + printf("\n"); + } + if (ula == user_list_arg_file_count || ula == user_list_arg_both) { + if (!conf.no_headers_given) + printf("%s (uid %u), by file count%s:\n", + ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid, + count_unit_buf); usi.count = conf.limit_arg, + usi.ui = ui; usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES; ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function, &usi.ret, &usi.osl_errno); @@ -420,37 +475,77 @@ static int print_user_stats(void) return ret; printf("\n"); } + if (ula == user_list_arg_none && !conf.no_user_summary_given) { + usi.count = conf.limit_arg; + usi.ui = ui; + usi.flags = USF_COMPUTE_SUMMARY; + ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function, + &usi.ret, &usi.osl_errno); + if (ret < 0) + return ret; + } + return 1; +} + +static int print_user_lists(void) +{ + return for_each_admissible_user(print_user_list, NULL); +} + +static int print_global_lists(void) +{ + struct global_stats_info gsi; + int ret; + enum enum_global_list gla = conf.global_list_arg; + int print_size_list = (gla == global_list_arg_size + || gla == global_list_arg_both); + + if (print_size_list) { + gsi.count = conf.limit_arg; + gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY; + if (!conf.no_headers_given) + printf("By size%s:\n", size_unit_buf); + ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi, + global_stats_loop_function, &gsi.ret, &gsi.osl_errno); + if (ret < 0) + return ret; + printf("\n"); + } + if (gla == global_list_arg_file_count || gla == global_list_arg_both) { + gsi.count = conf.limit_arg; + gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES; + if (!print_size_list) + gsi.flags |= GSF_COMPUTE_SUMMARY; + if (!conf.no_headers_given) + printf("By file count%s:\n", count_unit_buf); + ret = adu_loop_reverse(dir_table, DT_FILES, &gsi, + global_stats_loop_function, &gsi.ret, &gsi.osl_errno); + if (ret < 0) + return ret; + printf("\n"); + } + if (gla == global_list_arg_none && !conf.no_global_summary_given) { + /* must compute summary */ + gsi.count = conf.limit_arg; + gsi.flags = GSF_COMPUTE_SUMMARY; + ret = adu_loop_reverse(dir_table, DT_FILES, &gsi, + global_stats_loop_function, &gsi.ret, &gsi.osl_errno); + if (ret < 0) + return ret; + } return 1; } static int print_statistics(void) { int ret; - struct global_stats_info gsi = { - .count = conf.limit_arg, - .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY - }; - - printf("By size%s:\n", - size_unit_buf); - ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi, - global_stats_loop_function, &gsi.ret, &gsi.osl_errno); - if (ret < 0) - return ret; - printf("\n"); - - gsi.count = conf.limit_arg; - gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES; - printf("By file count%s:\n", - count_unit_buf); - ret = adu_loop_reverse(dir_table, DT_FILES, &gsi, - global_stats_loop_function, &gsi.ret, &gsi.osl_errno); + + ret = print_global_lists(); if (ret < 0) return ret; - printf("\n"); print_global_summary(); - print_user_stats(); - print_id_stats(); + print_user_lists(); + print_user_summary(); return 1; } @@ -460,6 +555,7 @@ static int read_uid_file(void) uint32_t n; char *filename = get_uid_list_name(), *map; int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL); + unsigned bits; if (ret < 0) { INFO_LOG("failed to map %s\n", filename); @@ -469,11 +565,14 @@ static int read_uid_file(void) num_uids = size / 4; INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename); free(filename); - /* hash table size should be a power of two and larger than the number of uids */ - uid_hash_table_size = 4; - while (uid_hash_table_size < num_uids) - uid_hash_table_size *= 2; - create_hash_table(); + /* + * Compute number of hash table bits. The hash table size must be a + * power of two and larger than the number of uids. + */ + bits = 2; + while (1 << bits < num_uids) + bits++; + create_hash_table(bits); for (n = 0; n < num_uids; n++) { uint32_t uid = read_u32(map + n * sizeof(uid)); ret = search_uid(uid, OPEN_USER_TABLE, NULL);