lib/gpg: Use nicer helper for gpg error messages
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
This commit is contained in:
parent
1f6fc009f7
commit
6578c362fe
|
|
@ -187,14 +187,14 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
|
||||||
gpg_error = gpgme_data_new_from_fd (&kdata, fd);
|
gpg_error = gpgme_data_new_from_fd (&kdata, fd);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
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;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpg_error = gpgme_op_import (result->context, kdata);
|
gpg_error = gpgme_op_import (result->context, kdata);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
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;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -212,8 +212,7 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
|
||||||
0 /* do not copy */);
|
0 /* do not copy */);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to read signed data");
|
||||||
g_prefix_error (error, "Unable to read signed data: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -223,16 +222,14 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
|
||||||
0 /* do not copy */);
|
0 /* do not copy */);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to read signature");
|
||||||
g_prefix_error (error, "Unable to read signature: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpg_error = gpgme_op_verify (result->context, signature_buffer, data_buffer, NULL);
|
gpg_error = gpgme_op_verify (result->context, signature_buffer, data_buffer, NULL);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to complete signature verification");
|
||||||
g_prefix_error (error, "Unable to complete signature verification: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -133,8 +133,7 @@ ostree_gpg_verify_result_initable_init (GInitable *initable,
|
||||||
gpg_error = gpgme_new (&result->context);
|
gpg_error = gpgme_new (&result->context);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to create context");
|
||||||
g_prefix_error (error, "Unable to create context: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1465,8 +1465,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
gpg_error = gpgme_op_import (source_context, data_buffer);
|
gpg_error = gpgme_op_import (source_context, data_buffer);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to import keys");
|
||||||
g_prefix_error (error, "Unable to import keys: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1491,8 +1490,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
gpg_error = gpgme_get_key (source_context, key_ids[ii], &key, 0);
|
gpg_error = gpgme_get_key (source_context, key_ids[ii], &key, 0);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to find key \"%s\"", key_ids[ii]);
|
||||||
g_prefix_error (error, "Unable to find key \"%s\": ", key_ids[ii]);
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1519,8 +1517,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
|
|
||||||
if (gpgme_err_code (gpg_error) != GPG_ERR_EOF)
|
if (gpgme_err_code (gpg_error) != GPG_ERR_EOF)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to list keys");
|
||||||
g_prefix_error (error, "Unable to list keys: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1592,8 +1589,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
gpg_error = gpgme_data_new (&data_buffer);
|
gpg_error = gpgme_data_new (&data_buffer);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to create data buffer");
|
||||||
g_prefix_error (error, "Unable to create data buffer: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1602,8 +1598,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
data_buffer);
|
data_buffer);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to export keys");
|
||||||
g_prefix_error (error, "Unable to export keys: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1612,8 +1607,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
gpg_error = gpgme_op_import (target_context, data_buffer);
|
gpg_error = gpgme_op_import (target_context, data_buffer);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to import keys");
|
||||||
g_prefix_error (error, "Unable to import keys: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1628,8 +1622,7 @@ ostree_repo_remote_gpg_import (OstreeRepo *self,
|
||||||
{
|
{
|
||||||
if (import_status->result != GPG_ERR_NO_ERROR)
|
if (import_status->result != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, error);
|
ot_gpgme_throw (gpg_error, error, "Unable to import key \"%s\"",
|
||||||
g_prefix_error (error, "Unable to import key \"%s\": ",
|
|
||||||
import_status->fpr);
|
import_status->fpr);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
@ -4179,26 +4172,23 @@ sign_data (OstreeRepo *self,
|
||||||
}
|
}
|
||||||
else if (err != GPG_ERR_NO_ERROR)
|
else if (err != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
ot_gpgme_throw (err, error, "Unable to lookup key ID %s", key_id);
|
||||||
g_prefix_error (error, "Unable to lookup key ID %s: ", key_id);
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add the key to the context as a signer */
|
/* Add the key to the context as a signer */
|
||||||
if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR)
|
if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
ot_gpgme_throw (err, error, "Error signing commit");
|
||||||
g_prefix_error (error, "Error signing commit: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
gsize len;
|
gsize len;
|
||||||
const char *buf = g_bytes_get_data (input_data, &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)
|
if ((err = gpgme_data_new_from_mem (&commit_buffer, buf, len, FALSE)) != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
ot_gpgme_throw (err, error, "Failed to create buffer from commit file");
|
||||||
g_prefix_error (error, "Failed to create buffer from commit file: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4208,8 +4198,7 @@ sign_data (OstreeRepo *self,
|
||||||
if ((err = gpgme_op_sign (context, commit_buffer, signature_buffer, GPGME_SIG_MODE_DETACH))
|
if ((err = gpgme_op_sign (context, commit_buffer, signature_buffer, GPGME_SIG_MODE_DETACH))
|
||||||
!= GPG_ERR_NO_ERROR)
|
!= GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
ot_gpgme_throw (err, error, "Failure signing commit file");
|
||||||
g_prefix_error (error, "Failure signing commit file: ");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,20 +26,23 @@
|
||||||
|
|
||||||
#include "libglnx.h"
|
#include "libglnx.h"
|
||||||
|
|
||||||
void
|
/* Like glnx_throw_errno_prefix, but takes @gpg_error */
|
||||||
ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error,
|
gboolean
|
||||||
GError **error)
|
ot_gpgme_throw (gpgme_error_t gpg_error, GError **error,
|
||||||
|
const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
if (error == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
GIOErrorEnum errcode;
|
GIOErrorEnum errcode;
|
||||||
char errbuf[1024];
|
char errbuf[1024];
|
||||||
|
|
||||||
/* XXX This list is incomplete. Add cases as needed. */
|
/* XXX This list is incomplete. Add cases as needed. */
|
||||||
|
|
||||||
switch (gpgme_err_code (gpg_error))
|
switch (gpgme_err_code (gpg_error))
|
||||||
{
|
{
|
||||||
/* special case - shouldn't be here */
|
/* special case - shouldn't be here */
|
||||||
case GPG_ERR_NO_ERROR:
|
case GPG_ERR_NO_ERROR:
|
||||||
g_return_if_reached ();
|
g_assert_not_reached ();
|
||||||
|
|
||||||
/* special case - abort on out-of-memory */
|
/* special case - abort on out-of-memory */
|
||||||
case GPG_ERR_ENOMEM:
|
case GPG_ERR_ENOMEM:
|
||||||
|
|
@ -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",
|
g_set_error (error, G_IO_ERROR, errcode, "%s: %s",
|
||||||
gpgme_strsource (gpg_error),
|
gpgme_strsource (gpg_error),
|
||||||
errbuf);
|
errbuf);
|
||||||
|
va_list args;
|
||||||
|
va_start (args, fmt);
|
||||||
|
glnx_real_set_prefix_error_va (*error, fmt, args);
|
||||||
|
va_end (args);
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
|
|
@ -99,7 +108,7 @@ ot_gpgme_ctx_tmp_home_dir (gpgme_ctx_t gpgme_ctx,
|
||||||
NULL, tmp_home_dir);
|
NULL, tmp_home_dir);
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
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;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -376,7 +385,6 @@ ot_gpgme_data_input (GInputStream *input_stream)
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
|
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, NULL);
|
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -399,7 +407,6 @@ ot_gpgme_data_output (GOutputStream *output_stream)
|
||||||
if (gpg_error != GPG_ERR_NO_ERROR)
|
if (gpg_error != GPG_ERR_NO_ERROR)
|
||||||
{
|
{
|
||||||
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
|
g_assert (gpgme_err_code (gpg_error) == GPG_ERR_ENOMEM);
|
||||||
ot_gpgme_error_to_gio_error (gpg_error, NULL);
|
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -416,11 +423,7 @@ ot_gpgme_new_ctx (const char *homedir,
|
||||||
g_auto(gpgme_ctx_t) context = NULL;
|
g_auto(gpgme_ctx_t) context = NULL;
|
||||||
|
|
||||||
if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
|
if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
|
||||||
{
|
return ot_gpgme_throw (err, error, "Unable to create gpg context"), NULL;
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
|
||||||
g_prefix_error (error, "Unable to create gpg context: ");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (homedir != NULL)
|
if (homedir != NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -430,12 +433,7 @@ ot_gpgme_new_ctx (const char *homedir,
|
||||||
|
|
||||||
if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
|
if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
|
||||||
!= GPG_ERR_NO_ERROR)
|
!= GPG_ERR_NO_ERROR)
|
||||||
{
|
return ot_gpgme_throw (err, error, "Unable to set gpg homedir to '%s'", homedir), NULL;
|
||||||
ot_gpgme_error_to_gio_error (err, error);
|
|
||||||
g_prefix_error (error, "Unable to set gpg homedir to '%s': ",
|
|
||||||
homedir);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return g_steal_pointer (&context);
|
return g_steal_pointer (&context);
|
||||||
|
|
|
||||||
|
|
@ -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_ctx_t, gpgme_release, NULL)
|
||||||
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(gpgme_key_t, gpgme_key_unref, 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,
|
gboolean ot_gpgme_ctx_tmp_home_dir (gpgme_ctx_t gpgme_ctx,
|
||||||
char **out_tmp_home_dir,
|
char **out_tmp_home_dir,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue