]> git.tuebingen.mpg.de Git - adu.git/blobdiff - select.c
Make user summary sortable by user defined column.
[adu.git] / select.c
index e09bce1052abc8f51c1e8cf58948d8440110ead5..515bbc4a5ecae2a26cfa3c4ffb4ce33e6b690c33 100644 (file)
--- a/select.c
+++ b/select.c
 #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
 
@@ -357,72 +364,124 @@ static void print_global_summary(void)
 
 }
 
-static void print_id_stats(void)
+static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
 {
-       struct user_info *ui;
+       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 int uid_comp(const void *a, const void *b)
+{
+       return -NUM_COMPARE(((struct user_info *)a)->uid,
+               ((struct user_info *)b)->uid);
+}
+
+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 file_count_comp(const void *a, const void *b)
+{
+       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);
+}
 
+/*
+ * 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)
+{
        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
-               );
-       }
+       sort_hash_table(summary_comparators[conf.user_summary_sort_arg]);
+       for_each_admissible_user(print_user_summary_line, NULL);
 }
 
-static int print_user_stats(void)
+static int print_user_stat(struct user_info *ui, __a_unused void *data)
 {
-       struct user_info *ui;
        int ret;
+       struct user_stats_info usi = {
+               .count = conf.limit_arg,
+               .ui = ui
+       };
 
-       FOR_EACH_USER(ui) {
-               struct user_stats_info usi = {
-                       .count = conf.limit_arg,
-                       .ui = ui
-               };
-               if (!ui_used(ui) || !ui_admissible(ui))
-                       continue;
-               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);
-               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);
-               usi.count = conf.limit_arg,
-               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);
-               if (ret < 0)
-                       return ret;
-               printf("\n");
-       }
+       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);
+       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);
+       usi.count = conf.limit_arg,
+       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);
+       if (ret < 0)
+               return ret;
+       printf("\n");
        return 1;
 }
 
+static int print_user_stats(void)
+{
+       return for_each_admissible_user(print_user_stat, NULL);
+}
+
 static int print_statistics(void)
 {
        int ret;
@@ -450,7 +509,7 @@ static int print_statistics(void)
        printf("\n");
        print_global_summary();
        print_user_stats();
-       print_id_stats();
+       print_user_summary();
        return 1;
 }
 
@@ -460,6 +519,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 +529,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);