From 2a2387eba51486426a7aba0ae7ec79dfe1f81f7f Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Sat, 3 Aug 2013 13:15:48 +0200 Subject: [PATCH] Small refactoring --- src/cmd_parser.c | 39 ++++++++++++++++++++++++++------------- src/cmd_parser.h | 11 ++++++++--- src/createrepo_c.c | 22 ++++++++++++++++------ src/error.c | 10 ++++++++++ src/error.h | 4 ++++ 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/cmd_parser.c b/src/cmd_parser.c index f286347..090b2ce 100644 --- a/src/cmd_parser.c +++ b/src/cmd_parser.c @@ -18,7 +18,9 @@ */ #include +#include #include "cmd_parser.h" +#include "error.h" #include "compression_wrapper.h" #include "misc.h" @@ -125,23 +127,23 @@ static GOptionEntry cmd_entries[] = -struct CmdOptions *parse_arguments(int *argc, char ***argv) +struct CmdOptions *parse_arguments(int *argc, char ***argv, GError **err) { - GError *error = NULL; + gboolean ret; GOptionContext *context; + assert(!err || *err = NULL); + context = g_option_context_new("- program that creates a repomd (xml-based" " rpm metadata) repository from a set of" " rpms."); g_option_context_add_main_entries(context, cmd_entries, NULL); - gboolean ret = g_option_context_parse(context, argc, argv, &error); + ret = g_option_context_parse(context, argc, argv, err); g_option_context_free(context); - if (!ret) { - g_print("Option parsing failed: %s\n", error->message); - g_error_free(error); + + if (!ret) return NULL; - } return &(_cmd_options); } @@ -149,11 +151,17 @@ struct CmdOptions *parse_arguments(int *argc, char ***argv) gboolean -check_arguments(struct CmdOptions *options, const char *input_dir) +check_arguments(struct CmdOptions *options, + const char *input_dir, + GError **err) { + assert(!err || *err == NULL); + // Check outputdir if (options->outputdir && !g_file_test(options->outputdir, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_DIR)) { - g_warning("Specified outputdir \"%s\" doesn't exists", options->outputdir); + g_set_error(err, CR_CMD_ERROR, CRE_BADARG, + "Specified outputdir \"%s\" doesn't exists", + options->outputdir); return FALSE; } @@ -179,7 +187,9 @@ check_arguments(struct CmdOptions *options, const char *input_dir) cr_ChecksumType type; type = cr_checksum_type(options->checksum); if (type == CR_CHECKSUM_UNKNOWN) { - g_critical("Unknown/Unsupported checksum type \"%s\"", options->checksum); + g_set_error(err, CR_CMD_ERROR, CRE_BADARG, + "Unknown/Unsupported checksum type \"%s\"", + options->checksum); return FALSE; } options->checksum_type = type; @@ -196,8 +206,9 @@ check_arguments(struct CmdOptions *options, const char *input_dir) options->compression_type = CR_CW_XZ_COMPRESSION; } else { g_string_free(compress_str, TRUE); - g_critical("Unknown/Unsupported compression type \"%s\"", - options->compress_type); + g_set_error(err, CR_CMD_ERROR, CRE_BADARG, + "Unknown/Unsupported compression type \"%s\"", + options->compress_type); return FALSE; } g_string_free(compress_str, TRUE); @@ -242,7 +253,9 @@ check_arguments(struct CmdOptions *options, const char *input_dir) } if (!remote && !g_file_test(options->groupfile_fullpath, G_FILE_TEST_IS_REGULAR)) { - g_warning("groupfile %s doesn't exists", options->groupfile_fullpath); + g_set_error(err, CR_CMD_ERROR, CRE_BADARG, + "groupfile %s doesn't exists", + options->groupfile_fullpath); return FALSE; } } diff --git a/src/cmd_parser.h b/src/cmd_parser.h index 9d82c7f..e03f742 100644 --- a/src/cmd_parser.h +++ b/src/cmd_parser.h @@ -91,18 +91,23 @@ struct CmdOptions { * @param argv pointer to argv * @return CmdOptions filled by command line arguments */ -struct CmdOptions *parse_arguments(int *argc, char ***argv); +struct CmdOptions * +parse_arguments(int *argc, char ***argv, GError **err); /** * Performs some checks of arguments and fill some other items. * in the CmdOptions structure. */ -gboolean check_arguments(struct CmdOptions *options, const char *inputdir); +gboolean +check_arguments(struct CmdOptions *options, + const char *inputdir, + GError **err); /** * Frees CmdOptions. * @param options pointer to struct with command line options */ -void free_options(struct CmdOptions *options); +void +free_options(struct CmdOptions *options); #endif /* __C_CREATEREPOLIB_CMD_PARSER_H__ */ diff --git a/src/createrepo_c.c b/src/createrepo_c.c index fe49cb0..199ed2a 100644 --- a/src/createrepo_c.c +++ b/src/createrepo_c.c @@ -543,12 +543,18 @@ fill_pool(GThreadPool *pool, int main(int argc, char **argv) { + struct CmdOptions *cmd_options; + GError *tmp_err = NULL; + + // Arguments parsing - struct CmdOptions *cmd_options; - cmd_options = parse_arguments(&argc, &argv); - if (!cmd_options) + cmd_options = parse_arguments(&argc, &argv, &tmp_err); + if (!cmd_options) { + fprintf(stderr, "Argument parsing failed: %s\n", tmp_err->message); + g_error_free(tmp_err); exit(EXIT_FAILURE); + } // Arguments pre-check @@ -560,7 +566,9 @@ main(int argc, char **argv) CR_VERSION_PATCH); free_options(cmd_options); exit(EXIT_SUCCESS); - } else if (argc != 2) { + } + + if (argc != 2) { // No mandatory arguments fprintf(stderr, "Must specify exactly one directory to index.\n"); fprintf(stderr, "Usage: %s [options] \n\n", @@ -589,7 +597,7 @@ main(int argc, char **argv) // Check if inputdir exists if (!g_file_test(in_dir, G_FILE_TEST_IS_DIR)) { - g_warning("Directory %s must exist", in_dir); + fprintf(stderr, "Directory %s must exist\n", in_dir); g_free(in_dir); free_options(cmd_options); exit(EXIT_FAILURE); @@ -598,7 +606,9 @@ main(int argc, char **argv) // Check parsed arguments - if (!check_arguments(cmd_options, in_dir)) { + if (!check_arguments(cmd_options, in_dir, &tmp_err)) { + fprintf(stderr, "%s\n", tmp_err->message); + g_error_free(tmp_err); g_free(in_dir); free_options(cmd_options); exit(EXIT_FAILURE); diff --git a/src/error.c b/src/error.c index f470157..f681b77 100644 --- a/src/error.c +++ b/src/error.c @@ -71,12 +71,22 @@ cr_strerror(cr_Error rc) return "OpenSSL library related error"; case CRE_CURL: return "Curl library related error"; + case CRE_ASSERT: + return "Assert error"; + case CRE_BADCMDARG: + return "Bad command line argument(s)"; default: return "Unknown error"; } } GQuark +cr_cmd_error_quark(void) +{ + return g_quark_from_static_string("cr_cmd_error"); +} + +GQuark cr_checksum_error_quark(void) { return g_quark_from_static_string("cr_checksum_error"); diff --git a/src/error.h b/src/error.h index 439b343..7417016 100644 --- a/src/error.h +++ b/src/error.h @@ -78,6 +78,8 @@ typedef enum { (25) Ideally this error should never happend. Nevertheless if it happend, probable reason is that some values of createrepo_c object was changed (by you - a programmer) in a bad way */ + CRE_BADCMDARG, /*!< + Bad command line argument(s) */ } cr_Error; /** Converts cr_Error return code to error string. @@ -87,6 +89,7 @@ typedef enum { const char *cr_strerror(cr_Error rc); /* Error domains */ +#define CR_CMD_ERROR cr_cmd_error_quark() #define CR_CHECKSUM_ERROR cr_checksum_error_quark() #define CR_COMPRESSION_WRAPPER_ERROR cr_compression_wrapper_error_quark() #define CR_DB_ERROR cr_db_error_quark() @@ -108,6 +111,7 @@ const char *cr_strerror(cr_Error rc); #define CR_XML_PARSER_PRI_ERROR cr_xml_parser_pri_error_quark() #define CR_XML_PARSER_REPOMD_ERROR cr_xml_parser_repomd_error_quark() +GQuark cr_cmd_error_quark(void); GQuark cr_checksum_error_quark(void); GQuark cr_compression_wrapper_error_quark(void); GQuark cr_db_error_quark(void); -- 2.7.4