Skip to content

Commit

Permalink
lib/gpg: Use nicer helper for gpg error messages
Browse files Browse the repository at this point in the history
The vast majority of invocations of `ot_gpgme_error_to_gio_error()` were paired
with `g_prefix_error()`; let's combine them for the same reason we do
`glnx_throw_errno_prefix()`. For the few cases that don't we might as well add
some prefix.

I also changed it to `return FALSE` in prep for more style porting.

Closes: #1135
Approved by: jlebon
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Sep 7, 2017
1 parent 1f6fc00 commit 6578c36
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 53 deletions.
13 changes: 5 additions & 8 deletions src/libostree/ostree-gpg-verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
gpg_error = gpgme_data_new_from_fd (&kdata, fd);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
ot_gpgme_throw (gpg_error, error, "Loading data from fd %i", fd);
goto out;
}

gpg_error = gpgme_op_import (result->context, kdata);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
ot_gpgme_throw (gpg_error, error, "Failed to import key");
goto out;
}
}
Expand All @@ -212,8 +212,7 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
0 /* do not copy */);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to read signed data: ");
ot_gpgme_throw (gpg_error, error, "Unable to read signed data");
goto out;
}

Expand All @@ -223,16 +222,14 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
0 /* do not copy */);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to read signature: ");
ot_gpgme_throw (gpg_error, error, "Unable to read signature");
goto out;
}

gpg_error = gpgme_op_verify (result->context, signature_buffer, data_buffer, NULL);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to complete signature verification: ");
ot_gpgme_throw (gpg_error, error, "Unable to complete signature verification");
goto out;
}

Expand Down
3 changes: 1 addition & 2 deletions src/libostree/ostree-gpg-verify-result.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ ostree_gpg_verify_result_initable_init (GInitable *initable,
gpg_error = gpgme_new (&result->context);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to create context: ");
ot_gpgme_throw (gpg_error, error, "Unable to create context");
goto out;
}

Expand Down
35 changes: 12 additions & 23 deletions src/libostree/ostree-repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1465,8 +1465,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
gpg_error = gpgme_op_import (source_context, data_buffer);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to import keys: ");
ot_gpgme_throw (gpg_error, error, "Unable to import keys");
goto out;
}

Expand All @@ -1491,8 +1490,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
gpg_error = gpgme_get_key (source_context, key_ids[ii], &key, 0);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to find key \"%s\": ", key_ids[ii]);
ot_gpgme_throw (gpg_error, error, "Unable to find key \"%s\"", key_ids[ii]);
goto out;
}

Expand All @@ -1519,8 +1517,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,

if (gpgme_err_code (gpg_error) != GPG_ERR_EOF)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to list keys: ");
ot_gpgme_throw (gpg_error, error, "Unable to list keys");
goto out;
}
}
Expand Down Expand Up @@ -1592,8 +1589,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
gpg_error = gpgme_data_new (&data_buffer);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to create data buffer: ");
ot_gpgme_throw (gpg_error, error, "Unable to create data buffer");
goto out;
}

Expand All @@ -1602,8 +1598,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
data_buffer);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to export keys: ");
ot_gpgme_throw (gpg_error, error, "Unable to export keys");
goto out;
}

Expand All @@ -1612,8 +1607,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
gpg_error = gpgme_op_import (target_context, data_buffer);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to import keys: ");
ot_gpgme_throw (gpg_error, error, "Unable to import keys");
goto out;
}

Expand All @@ -1628,8 +1622,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
{
if (import_status->result != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to import key \"%s\": ",
ot_gpgme_throw (gpg_error, error, "Unable to import key \"%s\"",
import_status->fpr);
goto out;
}
Expand Down Expand Up @@ -4179,26 +4172,23 @@ sign_data (OstreeRepo *self,
}
else if (err != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to lookup key ID %s: ", key_id);
ot_gpgme_throw (err, error, "Unable to lookup key ID %s", key_id);
goto out;
}

/* Add the key to the context as a signer */
if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Error signing commit: ");
ot_gpgme_throw (err, error, "Error signing commit");
goto out;
}

{
gsize len;
const char *buf = g_bytes_get_data (input_data, &len);
if ((err = gpgme_data_new_from_mem (&commit_buffer, buf, len, FALSE)) != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Failed to create buffer from commit file: ");
ot_gpgme_throw (err, error, "Failed to create buffer from commit file");
goto out;
}
}
Expand All @@ -4208,8 +4198,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_op_sign (context, commit_buffer, signature_buffer, GPGME_SIG_MODE_DETACH))
!= GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Failure signing commit file: ");
ot_gpgme_throw (err, error, "Failure signing commit file");
goto out;
}

Expand Down
36 changes: 17 additions & 19 deletions src/libotutil/ot-gpg-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@

#include "libglnx.h"

void
ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error,
GError **error)
/* Like glnx_throw_errno_prefix, but takes @gpg_error */
gboolean
ot_gpgme_throw (gpgme_error_t gpg_error, GError **error,
const char *fmt, ...)
{
if (error == NULL)
return FALSE;

GIOErrorEnum errcode;
char errbuf[1024];

/* XXX This list is incomplete. Add cases as needed. */

switch (gpgme_err_code (gpg_error))
{
/* special case - shouldn't be here */
case GPG_ERR_NO_ERROR:
g_return_if_reached ();
g_assert_not_reached ();

/* special case - abort on out-of-memory */
case GPG_ERR_ENOMEM:
Expand All @@ -63,6 +66,12 @@ ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error,
g_set_error (error, G_IO_ERROR, errcode, "%s: %s",
gpgme_strsource (gpg_error),
errbuf);
va_list args;
va_start (args, fmt);
glnx_real_set_prefix_error_va (*error, fmt, args);
va_end (args);

return FALSE;
}

gboolean
Expand Down Expand Up @@ -99,7 +108,7 @@ ot_gpgme_ctx_tmp_home_dir (gpgme_ctx_t gpgme_ctx,
NULL, tmp_home_dir);
if (gpg_error != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (gpg_error, error);
ot_gpgme_throw (gpg_error, error, "gpgme_ctx_set_engine_info");
goto out;
}

Expand Down Expand Up @@ -376,7 +385,6 @@ ot_gpgme_data_input (GInputStream *input_stream)
if (gpg_error != GPG_ERR_NO_ERROR)
{
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
ot_gpgme_error_to_gio_error (gpg_error, NULL);
g_assert_not_reached ();
}

Expand All @@ -399,7 +407,6 @@ ot_gpgme_data_output (GOutputStream *output_stream)
if (gpg_error != GPG_ERR_NO_ERROR)
{
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
ot_gpgme_error_to_gio_error (gpg_error, NULL);
g_assert_not_reached ();
}

Expand All @@ -416,11 +423,7 @@ ot_gpgme_new_ctx (const char *homedir,
g_auto(gpgme_ctx_t) context = NULL;

if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to create gpg context: ");
return NULL;
}
return ot_gpgme_throw (err, error, "Unable to create gpg context"), NULL;

if (homedir != NULL)
{
Expand All @@ -430,12 +433,7 @@ ot_gpgme_new_ctx (const char *homedir,

if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
!= GPG_ERR_NO_ERROR)
{
ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to set gpg homedir to '%s': ",
homedir);
return NULL;
}
return ot_gpgme_throw (err, error, "Unable to set gpg homedir to '%s'", homedir), NULL;
}

return g_steal_pointer (&context);
Expand Down
3 changes: 2 additions & 1 deletion src/libotutil/ot-gpg-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ G_DEFINE_AUTO_CLEANUP_FREE_FUNC(gpgme_data_t, gpgme_data_release, NULL)
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(gpgme_ctx_t, gpgme_release, NULL)
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(gpgme_key_t, gpgme_key_unref, NULL)

void ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error, GError **error);
gboolean ot_gpgme_throw (gpgme_error_t gpg_error, GError **error,
const char *fmt, ...) G_GNUC_PRINTF (3, 4);

gboolean ot_gpgme_ctx_tmp_home_dir (gpgme_ctx_t gpgme_ctx,
char **out_tmp_home_dir,
Expand Down

0 comments on commit 6578c36

Please sign in to comment.