]> git.tuebingen.mpg.de Git - adu.git/blobdiff - select.c
Move user and user ID related functions to separate files.
[adu.git] / select.c
index 9a19825ba0caf0bda41608242455b78a8fc3e564..46b5372492611d3b8ed315cf203372dae41ba580 100644 (file)
--- a/select.c
+++ b/select.c
@@ -14,7 +14,8 @@
 #include "fd.h"
 #include "string.h"
 #include "error.h"
-#include "portable_io.h"
+#include "user.h"
+#include "select.cmdline.h"
 
 /* global list */
 #define GLOBAL_LIST_ATOMS \
@@ -282,7 +283,7 @@ static int global_summary_loop_function(struct osl_row *row, void *data)
 err:
        gsi->ret = ret;
        gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
+       return ret;
 }
 
 static int print_global_summary(struct format_info *fi)
@@ -334,7 +335,7 @@ static int user_summary_loop_function(struct osl_row *row, void *data)
 err:
        usi->ret = ret;
        usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
+       return ret;
 }
 
 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
@@ -468,12 +469,14 @@ static int user_list_loop_function(struct osl_row *row, void *data)
        free(dirname);
        ret = output("%s", buf);
        free(buf);
+       if (ret < 0)
+               goto err;
        uli->count--;
        return ret;
 err:
        uli->ret = ret;
        uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
-       return -1;
+       return ret;
 }
 
 static int print_user_list(struct user_info *ui, void *data)
@@ -536,6 +539,8 @@ static int global_list_loop_function(struct osl_row *row, void *data)
        free(dirname);
        ret = output("%s", buf);
        free(buf);
+       if (ret < 0)
+               goto err;
        if (gli->count > 0)
                gli->count--;
        return ret;
@@ -581,41 +586,6 @@ static int print_statistics(struct format_info *fi)
        return -ERRNO_TO_ERROR(-EINVAL);
 }
 
-static int read_uid_file(struct uid_range *admissible_uids)
-{
-       size_t size;
-       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);
-               free(filename);
-               return ret;
-       }
-       num_uids = size / 4;
-       INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
-       free(filename);
-       /*
-        * 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, admissible_uids, OPEN_USER_TABLE, NULL);
-               if (ret < 0)
-                       goto out;
-       }
-out:
-       adu_munmap(map, size);
-       return ret;
-}
-
 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
 {
        int ret;
@@ -648,6 +618,41 @@ out:
 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
 
+static int setup_format_string(char *fmt, struct format_info **fi)
+{
+       struct atom *atoms;
+
+       if (!fmt)
+               INFO_LOG("using default format string\n");
+       switch (select_conf.select_mode_arg) {
+       case select_mode_arg_global_list:
+               if (!fmt)
+                       fmt = GLOBAL_LIST_DFLT_FMT;
+               atoms = global_list_atoms;
+               break;
+       case select_mode_arg_global_summary:
+               if (!fmt)
+                       fmt = GLOBAL_SUMMARY_DFLT_FMT;
+               atoms = global_summary_atoms;
+               break;
+       case select_mode_arg_user_list:
+               if (!fmt)
+                       fmt = USER_LIST_DFLT_FMT;
+               atoms = user_list_atoms;
+               break;
+       case select_mode_arg_user_summary:
+               if (!fmt)
+                       fmt = USER_SUMMARY_DFLT_FMT;
+               atoms = user_summary_atoms;
+               break;
+       default:
+               ERROR_LOG("bad select mode\n");
+               return -ERRNO_TO_ERROR(-EINVAL);
+       };
+       INFO_LOG("format string: %s\n", fmt);
+       return parse_format_string(fmt, atoms, fi);
+}
+
 /* 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 format_info **fi)
@@ -655,7 +660,6 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
        int ret;
        const char **line;
        char *fmt = NULL;
-       struct atom *atoms;
 
        if (string) {
                int argc;
@@ -672,41 +676,11 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para
                if (select_conf.help_given || select_conf.detailed_help_given)
                        goto help;
                fmt = select_conf.format_arg;
-
        }
        ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
        if (ret < 0)
                return ret;
-
-       if (!fmt)
-               INFO_LOG("using default format string\n");
-       switch (select_conf.select_mode_arg) {
-               case select_mode_arg_global_list:
-                       if (!fmt)
-                               fmt = GLOBAL_LIST_DFLT_FMT;
-                       atoms = global_list_atoms;
-                       break;
-               case select_mode_arg_global_summary:
-                       if (!fmt)
-                               fmt = GLOBAL_SUMMARY_DFLT_FMT;
-                       atoms = global_summary_atoms;
-                       break;
-               case select_mode_arg_user_list:
-                       if (!fmt)
-                               fmt = USER_LIST_DFLT_FMT;
-                       atoms = user_list_atoms;
-                       break;
-               case select_mode_arg_user_summary:
-                       if (!fmt)
-                               fmt = USER_SUMMARY_DFLT_FMT;
-                       atoms = user_summary_atoms;
-                       break;
-               default:
-                       ERROR_LOG("bad select mode\n");
-                       return -ERRNO_TO_ERROR(-EINVAL);
-       };
-       INFO_LOG("format string: %s\n", fmt);
-       return parse_format_string(fmt, atoms, fi);
+       return setup_format_string(fmt, fi);
 help:
        line = select_conf.detailed_help_given?
                select_args_info_detailed_help : select_args_info_help;