Browse Source

Isolate the encrypt code out of the main function.

Move most of the code out of the main function; only keep the
code for option parsing, error reporting, and freeing resources.
master
Damien Goutte-Gattat 2 years ago
parent
commit
eef595f546
  1. 1
      src/errors.h
  2. 57
      src/yki.c

1
src/errors.h

@ -5,3 +5,4 @@ DEFINE_YKI_ERROR(3, GPGME_CLOSE, "Cannot close GpgME data buffer")
DEFINE_YKI_ERROR(4, GPGME_CRYPTO, "Cannot perform cryptographic operation")
DEFINE_YKI_ERROR(5, GPGME_KEYLIST, "Cannot list keys")
DEFINE_YKI_ERROR(6, INVALID_COMMAND, "Invalid command line")
DEFINE_YKI_ERROR(7, NOT_IMPLEMENTED, "Feature not implemented")

57
src/yki.c

@ -115,7 +115,7 @@ free_key_in_list(GtkTreeModel *store, GtkTreePath *path, GtkTreeIter *iter, gpoi
}
static gpgme_error_t
perform_operation(yorkie_ctx_t *yki, gpgme_data_t in, gpgme_data_t out)
encrypt_or_sign_buffer(yorkie_ctx_t *yki, gpgme_data_t in, gpgme_data_t out)
{
gpgme_error_t gerr;
@ -214,7 +214,7 @@ create_encrypt_dialog(yorkie_ctx_t *yki)
}
static int
perform_operation_on_file(yorkie_ctx_t *yki, const char *filename, GError **error)
encrypt_or_sign_file(yorkie_ctx_t *yki, const char *filename, GError **error)
{
gpgutil_file_t in, out;
gchar *outname;
@ -229,7 +229,7 @@ perform_operation_on_file(yorkie_ctx_t *yki, const char *filename, GError **erro
}
if ( ! ret ) {
gpgme_error_t gerr = perform_operation(yki, in.buffer, out.buffer);
gpgme_error_t gerr = encrypt_or_sign_buffer(yki, in.buffer, out.buffer);
if ( (ret = gpgme_err_code(gerr)) )
yki_error_gpgme(error, YKI_ERROR_GPGME_CRYPTO, gerr);
}
@ -240,6 +240,25 @@ perform_operation_on_file(yorkie_ctx_t *yki, const char *filename, GError **erro
return ret;
}
static int
do_encrypt(yorkie_ctx_t *yki, const char *file, GError **error)
{
GtkDialog *dlg;
yki->selected_keys = 0;
yki->symmetric = yki->sign = yki->detached = FALSE;
dlg = create_encrypt_dialog(yki);
populate_public_key_list(yki->gpgme, yki->public_keys, error);
if ( ! *error && gtk_dialog_run(dlg) == GTK_RESPONSE_OK && can_proceed(*yki) )
encrypt_or_sign_file(yki, file, error);
gtk_tree_model_foreach(GTK_TREE_MODEL(yki->public_keys), free_key_in_list, NULL);
gtk_widget_destroy(GTK_WIDGET(dlg));
return *error ? -1 : 0;
}
static gboolean
check_non_option_args(GOptionContext *parser, GOptionGroup *group, gpointer data, GError **error)
{
@ -263,11 +282,10 @@ int
main(int argc, char **argv)
{
yorkie_ctx_t yki;
GtkDialog *dlg;
GError *error = NULL;
gboolean encrypt;
gchar **files;
GOptionEntry options[] = {
{ "encrypt", 'e', 0, G_OPTION_ARG_NONE, &encrypt, "Encrypt the file", NULL },
{ "decrupt", 'd', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &encrypt, "Decrypt the file", NULL },
@ -279,8 +297,6 @@ main(int argc, char **argv)
encrypt = TRUE;
files = NULL;
yki.selected_keys = 0;
yki.symmetric = yki.sign = yki.detached = FALSE;
parser = g_option_context_new("- Perform cryptographic operation on specified file");
main_options = g_option_group_new(NULL, NULL, NULL, (gpointer) &files, NULL);
@ -288,31 +304,26 @@ main(int argc, char **argv)
g_option_group_set_parse_hooks(main_options, NULL, check_non_option_args);
g_option_context_set_main_group(parser, main_options);
g_option_context_add_group(parser, gtk_get_option_group(TRUE));
if ( ! g_option_context_parse(parser, &argc, &argv, &error) ) {
g_printerr("Error: Invalid command line: %s\n", error->message);
exit(EXIT_FAILURE);
}
dlg = create_encrypt_dialog(&yki);
yki.gpgme = gpgutil_initialize(&error);
g_option_context_parse(parser, &argc, &argv, &error);
if ( ! error )
populate_public_key_list(yki.gpgme, yki.public_keys, &error);
yki.gpgme = gpgutil_initialize(&error);
if ( ! error && gtk_dialog_run(dlg) == GTK_RESPONSE_OK && can_proceed(yki) )
perform_operation_on_file(&yki, files[0], &error);
if ( ! error ) {
if ( encrypt )
do_encrypt(&yki, files[0], &error);
else
g_set_error(&error, YKI_ERROR, YKI_ERROR_NOT_IMPLEMENTED, "Cannot decrypt");
}
if ( error ) {
if ( error )
g_printerr("Error: %s: %s\n", yki_error_msg(error->code), error->message);
g_error_free(error);
}
gtk_tree_model_foreach(GTK_TREE_MODEL(yki.public_keys), free_key_in_list, NULL);
gtk_widget_destroy(GTK_WIDGET(dlg));
gpgme_release(yki.gpgme);
g_option_context_free(parser);
g_strfreev(files);
if ( yki.gpgme )
gpgme_release(yki.gpgme);
return error ? EXIT_FAILURE : EXIT_SUCCESS;
}

Loading…
Cancel
Save