*/
#include <string.h>
+#include <assert.h>
#include "cmd_parser.h"
+#include "error.h"
#include "compression_wrapper.h"
#include "misc.h"
-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);
}
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;
}
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;
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);
}
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;
}
}
* @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__ */
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
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] <directory_to_index>\n\n",
// 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);
// 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);
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");
(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.
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()
#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);