]> git.tuebingen.mpg.de Git - adu.git/blobdiff - select.c
Introduce select-mode and use pretty formating for the global lists.
[adu.git] / select.c
index 943b8fde59d8810a108c3a4026d17ef6c225ffac..7b3338376cd977cf316d639377279be15fa11faa 100644 (file)
--- a/select.c
+++ b/select.c
@@ -7,6 +7,7 @@
 /** \file select.c The select mode of adu. */
 
 #include <dirent.h> /* readdir() */
+#include "format.h"
 #include "adu.h"
 #include "gcc-compat.h"
 #include "cmdline.h"
@@ -22,24 +23,67 @@ 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
 
-/* these get filled in by the select command. */
-static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
+#define GLOBAL_LIST_ATOMS \
+       ATOM(size, SIZE) \
+       ATOM(files, COUNT) \
+       ATOM(dirname, STRING) \
 
-enum global_stats_flags {
-       GSF_PRINT_DIRNAME = 1,
-       GSF_PRINT_BYTES = 2,
-       GSF_PRINT_FILES = 4,
-       GSF_COMPUTE_SUMMARY = 8,
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom global_list_atoms[] = {
+       GLOBAL_LIST_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) gla_ ## x,
+enum global_list_atoms {GLOBAL_LIST_ATOMS};
+#undef ATOM
+
+#define GLOBAL_SUMMARY_ATOMS \
+       ATOM(dirs, COUNT) \
+       ATOM(files, COUNT) \
+       ATOM(size, SIZE)
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom global_summary_atoms[] = {
+       GLOBAL_SUMMARY_ATOMS
+       {.name = NULL}
+};
+#undef ATOM
+#define ATOM(x, y) gsa_ ## x,
+enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
+#undef ATOM
+
+#define USER_SUMMARY_ATOMS \
+       ATOM(pw_name, STRING) \
+       ATOM(uid, ID) \
+       ATOM(dirs, COUNT) \
+       ATOM(files, COUNT) \
+       ATOM(size, SIZE)
+
+#define ATOM(x, y) { .name = #x, .type = AT_ ## y},
+struct atom user_summary_atoms[] = {
+       USER_SUMMARY_ATOMS
+       {.name = NULL}
 };
+#undef ATOM
+#define ATOM(x, y) usa_ ## x,
+enum user_summary_atoms {USER_SUMMARY_ATOMS};
+#undef ATOM
+
+
+
+/* these get filled in by the select command. */
+static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
 
-struct global_stats_info {
+struct global_list_info {
        uint32_t count;
        int ret;
        int osl_errno;
-       enum global_stats_flags flags;
+       struct format_info *fi;
 };
 
 enum user_stats_flags {
@@ -253,80 +297,6 @@ err:
        return -1;
 }
 
-static int global_stats_loop_function(struct osl_row *row, void *data)
-{
-       struct global_stats_info *gsi = data;
-       struct osl_object obj;
-       char *dirname, formated_value[FORMATED_VALUE_SIZE];
-       int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
-
-       check_signals();
-       if (!gsi->count && !summary) {
-               ret = -E_LOOP_COMPLETE;
-               goto err;
-       }
-       if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
-               uint64_t files;
-               ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
-               if (ret < 0)
-                       goto err;
-               files = *(uint64_t *)obj.data;
-               if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
-                       format_count_value(select_conf.count_unit_arg, files,
-                               select_conf.count_unit_arg == count_unit_arg_h,
-                               formated_value);
-                       ret = output("\t%s%s", formated_value,
-                               (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
-                               "\t" : "\n");
-                       if (ret < 0)
-                               goto err;
-               }
-               if (summary)
-                       num_files += files;
-       }
-       if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
-               uint64_t bytes;
-               ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
-               if (ret < 0)
-                       goto err;
-               bytes = *(uint64_t *)obj.data;
-               if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
-                       format_size_value(select_conf.size_unit_arg, bytes,
-                               select_conf.size_unit_arg == size_unit_arg_h,
-                               formated_value);
-                       ret = output("%s%s%s",
-                               (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
-                               formated_value,
-                               (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
-                       );
-                       if (ret < 0)
-                               goto err;
-               }
-               if (summary) {
-                       num_bytes += bytes;
-                       num_dirs++;
-               }
-       }
-       if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
-               ret = get_dir_name_of_row(row, &dirname);
-               if (ret < 0)
-                       goto err;
-               ret = output("%s%s\n",
-                       (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
-                       dirname);
-               free(dirname);
-               if (ret < 0)
-                       goto err;
-       }
-       if (gsi->count > 0)
-               gsi->count--;
-       return 1;
-err:
-       gsi->ret = ret;
-       gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
-}
-
 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
 {
        if (ret >= 0)
@@ -352,55 +322,45 @@ static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private
        return check_loop_return(ret, *loop_ret, *loop_osl_errno);
 }
 
-static int print_global_summary(void)
+static int print_global_summary(struct format_info *fi)
 {
-       char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
-               s[FORMATED_VALUE_SIZE];
-       enum enum_count_unit ud, uf;
-       enum enum_size_unit us;
        int ret;
+       char *buf;
+       union atom_value values[] = {
+               [gsa_dirs] = {.num_value = (long long unsigned)num_dirs},
+               [gsa_files] = {.num_value =  (long long unsigned)num_files},
+               [gsa_size] = {.num_value =  (long long unsigned)num_bytes}
+       };
 
        if (select_conf.no_global_summary_given)
                return 1;
-       ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
-       uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
-       us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
-
        if (!select_conf.no_headers_given) {
-               ret = output("Global summary "
-                       "(dirs(%c)/files(%c)/size(%c))\n",
-                       count_unit_abbrevs[ud],
-                       count_unit_abbrevs[uf],
-                       size_unit_abbrevs[us]
-               );
+               ret = output("Global summary\n");
                if (ret < 0)
                        return ret;
        }
-       return output("\t%s\t%s\t%s\n\n", d, f, s);
+       buf = format_items(fi, values);
+       ret = output("%s", buf);
+       free(buf);
+       return ret;
 }
 
 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(select_conf.count_unit_arg, ui->dirs,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_dir_count);
-       format_count_value(select_conf.count_unit_arg, ui->files,
-               select_conf.count_unit_arg == count_unit_arg_h,
-               formated_file_count);
-       format_size_value(select_conf.size_unit_arg, ui->bytes,
-               select_conf.size_unit_arg == size_unit_arg_h,
-               formated_bytes);
-       return output("\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
-       );
+       struct format_info *fi = data;
+       union atom_value values[] = {
+               [usa_pw_name] = {.string_value = ui->pw_name?
+                       ui->pw_name : "?"},
+               [usa_uid] = {.num_value = (long long unsigned)ui->uid},
+               [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
+               [usa_files] = {.num_value =  (long long unsigned)ui->files},
+               [usa_size] = {.num_value =  (long long unsigned)ui->bytes}
+       };
+       char *buf = format_items(fi, values);
+       int ret = output("%s", buf);
+
+       free(buf);
+       return ret;
 }
 
 static int name_comp(const void *a, const void *b)
@@ -453,19 +413,17 @@ static int (*summary_comparators[])(const void *, const void *) = {
        [user_summary_sort_arg_size] = size_comp,
 };
 
-static int print_user_summary(void)
+static int print_user_summary(struct format_info *fi)
 {
        if (select_conf.no_user_summary_given)
                return 1;
        if (!select_conf.no_headers_given) {
-               int ret = output("User summary "
-                       "(pw_name/uid/dirs%s/files%s/size%s):\n",
-                       count_unit_buf, count_unit_buf, size_unit_buf);
+               int ret = output("User summary\n");
                if (ret < 0)
                        return ret;
        }
        sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
-       return for_each_admissible_user(print_user_summary_line, NULL);
+       return for_each_admissible_user(print_user_summary_line, fi);
 }
 
 static int print_user_list(struct user_info *ui, __a_unused void *data)
@@ -531,77 +489,114 @@ static int print_user_lists(void)
        return for_each_admissible_user(print_user_list, NULL);
 }
 
-static int print_global_lists(void)
+static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
 {
-       struct global_stats_info gsi;
-       int ret;
-       enum enum_global_list gla = select_conf.global_list_arg;
-       int print_size_list = (gla == global_list_arg_size
-               || gla == global_list_arg_both);
+       struct osl_object obj;
+       int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
+       if (ret < 0)
+               return ret;
+       *num_files = *(uint64_t *)obj.data;
+       return 1;
+}
 
-       if (print_size_list) {
-               gsi.count = select_conf.limit_arg;
-               gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
-               if (!select_conf.no_headers_given) {
-                       ret = output("By size%s:\n", size_unit_buf);
-                       if (ret < 0)
-                               return ret;
-               }
-               ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
-                       global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
-               if (ret < 0)
-                       return ret;
-               ret = output("\n");
-               if (ret < 0)
-                       return ret;
-       }
-       if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
-               gsi.count = select_conf.limit_arg;
-               gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
-               if (!print_size_list)
-                       gsi.flags |= GSF_COMPUTE_SUMMARY;
-               if (!select_conf.no_headers_given) {
-                       ret = output("By file count%s:\n", count_unit_buf);
-                       if (ret < 0)
-                               return ret;
-               }
-               ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
-                       global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
-               if (ret < 0)
-                       return ret;
-               ret = output("\n");
-               if (ret < 0)
-                       return ret;
-       }
-       if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
-               /* must compute summary */
-               gsi.count = select_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;
-       }
+static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
+{
+       struct osl_object obj;
+       int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
+       if (ret < 0)
+               return ret;
+       *num_bytes = *(uint64_t *)obj.data;
        return 1;
 }
 
-static int print_statistics(void)
+static int global_list_loop_function(struct osl_row *row, void *data)
 {
+       struct global_list_info *gli = data;
+       union atom_value values[] = {
+               [gla_size] = {.num_value = 0ULL},
+               [gla_files] = {.num_value =  0ULL},
+               [gla_dirname] = {.string_value = NULL}
+       };
+       uint64_t num_files, num_bytes;
+       char *dirname, *buf;
        int ret;
 
-       ret = print_global_lists();
-       if (ret < 0)
-               return ret;
-       ret = print_global_summary();
+       check_signals();
+       ret = -E_LOOP_COMPLETE;
+       if (!gli->count)
+               goto err;
+
+       ret = get_num_files_of_row(row, &num_files);
        if (ret < 0)
-               return ret;
-       ret = print_user_lists();
+               goto err;
+       values[gla_files].num_value = (long long unsigned)num_files;
+
+       ret = get_num_bytes_of_row(row, &num_bytes);
        if (ret < 0)
-               return ret;
-       ret = print_user_summary();
+               goto err;
+       values[gla_size].num_value = (long long unsigned)num_bytes;
+
+       ret = get_dir_name_of_row(row, &dirname);
        if (ret < 0)
-               return ret;
-       return 1;
+               goto err;
+       values[gla_dirname].string_value = dirname;
+
+       buf = format_items(gli->fi, values);
+       free(dirname);
+       ret = output("%s", buf);
+       free(buf);
+       if (gli->count > 0)
+               gli->count--;
+       return ret;
+err:
+       gli->ret = ret;
+       gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
+       return -1;
+}
+
+static int print_global_list(struct format_info *fi)
+{
+       int ret;
+       enum dir_table_columns sort_column = DT_BYTES;
+       struct global_list_info gli = {
+               .fi = fi,
+               .count = select_conf.limit_arg
+       };
+
+       if (!select_conf.no_headers_given) {
+               ret = output("Global list\n");
+               if (ret < 0)
+                       return ret;
+       }
+       if (select_conf.sort_arg == sort_arg_files)
+               sort_column = DT_FILES;
+       return adu_loop_reverse(dir_table, sort_column, &gli,
+               global_list_loop_function, &gli.ret, &gli.osl_errno);
+}
+
+static int print_statistics(struct select_format_info *sli)
+{
+       int ret;
+
+       switch (select_conf.select_mode_arg) {
+               case select_mode_arg_global_list:
+                       ret = print_global_list(sli->global_list_fi);
+                       free_format_info(sli->global_list_fi);
+                       return ret;
+               case select_mode_arg_global_summary:
+                       ret = print_global_summary(sli->global_summary_fi);
+                       free_format_info(sli->global_summary_fi);
+                       return ret;
+               case select_mode_arg_user_list:
+                       ret = print_user_lists();
+                       return ret;
+               case select_mode_arg_user_summary:
+                       ret = print_user_summary(sli->user_summary_fi);
+                       free_format_info(sli->user_summary_fi);
+                       return ret;
+       };
+       ERROR_LOG("bad select mode\n");
+       return ERRNO_TO_ERROR(-EINVAL);
 }
 
 static int read_uid_file(struct uid_range *admissible_uids)
@@ -639,7 +634,8 @@ out:
        return ret;
 }
 
-int run_select_query(struct uid_range *admissible_uids)
+int run_select_query(struct uid_range *admissible_uids,
+               struct select_format_info *sfi)
 {
        int ret;
 
@@ -667,7 +663,7 @@ int run_select_query(struct uid_range *admissible_uids)
        if (ret < 0)
                goto out;
        check_signals();
-       ret = print_statistics();
+       ret = print_statistics(sfi);
 out:
        close_all_tables();
        if (output_file != stdout)
@@ -677,20 +673,49 @@ out:
 
 /* return: < 0: error, >0: OK, == 0: help given */
 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
-               struct uid_range **admissible_uids)
+               struct uid_range **admissible_uids, struct select_format_info *sfi)
 {
        int ret;
        const char **line;
 
-       if (select_cmdline_parser_string_ext(string, &select_conf, "select",
-                       params))
-               return -E_SYNTAX;
+       if (conf.select_options_given) {
+               int argc;
+               char **argv;
+
+               ret = create_argv(string, &argv);
+               if (ret < 0)
+                       return ret;
+               argc = ret;
+               ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
+               free_argv(argv);
+               if (ret)
+                       return -E_SYNTAX;
+               if (select_conf.help_given || select_conf.detailed_help_given)
+                       goto help;
+
+       }
        ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
        if (ret < 0)
                return ret;
-       if (!select_conf.help_given && !select_conf.detailed_help_given)
-               return 1;
-
+       ret = parse_format_string(select_conf.user_summary_format_arg,
+               user_summary_atoms, &sfi->user_summary_fi);
+       if (ret < 0)
+               return ret;
+       ret = parse_format_string(select_conf.global_summary_format_arg,
+               global_summary_atoms, &sfi->global_summary_fi);
+       if (ret < 0)
+               goto global_summary_err;
+       ret = parse_format_string(select_conf.global_list_format_arg,
+               global_list_atoms, &sfi->global_list_fi);
+       if (ret < 0)
+               goto global_list_err;
+       return 1;
+global_list_err:
+       free_format_info(sfi->global_summary_fi);
+global_summary_err:
+       free_format_info(sfi->user_summary_fi);
+       return ret;
+help:
        line = select_conf.detailed_help_given?
                select_args_info_detailed_help : select_args_info_help;
        if (!output_file)
@@ -706,20 +731,20 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
 int com_select(void)
 {
        struct uid_range *admissible_uids = NULL;
-
-       if (conf.select_options_given) {
-               int ret;
-               struct select_cmdline_parser_params params = {
-                       .override = 1,
-                       .initialize = 1,
-                       .check_required = 1,
-                       .check_ambiguity = 1,
-                       .print_errors = 1
-               };
-               ret = parse_select_options(conf.select_options_arg, &params,
-                       &admissible_uids);
-               if (ret <= 0) /* do not run query if help was given */
-                       return ret;
-       }
-       return run_select_query(admissible_uids);
+       struct select_format_info sfi;
+       int ret;
+       struct select_cmdline_parser_params params = {
+               .override = 1,
+               .initialize = 1,
+               .check_required = 1,
+               .check_ambiguity = 1,
+               .print_errors = 1
+       };
+
+       select_cmdline_parser_init(&select_conf);
+       ret = parse_select_options(conf.select_options_arg, &params,
+               &admissible_uids, &sfi);
+       if (ret <= 0) /* do not run query if help was given */
+               return ret;
+       return run_select_query(admissible_uids, &sfi);
 }