utils/checksum: Port to new code style

Just happened to be reading this code, it's an easy port.

Closes: #842
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-05-09 09:26:11 -04:00 committed by Atomic Bot
parent 50f73cbac3
commit 052ba81c03
1 changed files with 18 additions and 40 deletions

View File

@ -47,7 +47,7 @@ ot_csum_from_gchecksum (GChecksum *checksum)
{ {
guchar *ret = g_malloc (32); guchar *ret = g_malloc (32);
gsize len = 32; gsize len = 32;
g_checksum_get_digest (checksum, ret, &len); g_checksum_get_digest (checksum, ret, &len);
g_assert (len == 32); g_assert (len == 32);
return ret; return ret;
@ -62,13 +62,11 @@ ot_gio_write_update_checksum (GOutputStream *out,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
if (out) if (out)
{ {
if (!g_output_stream_write_all (out, data, len, out_bytes_written, if (!g_output_stream_write_all (out, data, len, out_bytes_written,
cancellable, error)) cancellable, error))
goto out; return FALSE;
} }
else if (out_bytes_written) else if (out_bytes_written)
{ {
@ -77,10 +75,7 @@ ot_gio_write_update_checksum (GOutputStream *out,
if (checksum) if (checksum)
g_checksum_update (checksum, data, len); g_checksum_update (checksum, data, len);
return TRUE;
ret = TRUE;
out:
return ret;
} }
gboolean gboolean
@ -90,8 +85,6 @@ ot_gio_splice_update_checksum (GOutputStream *out,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
g_return_val_if_fail (out != NULL || checksum != NULL, FALSE); g_return_val_if_fail (out != NULL || checksum != NULL, FALSE);
if (checksum != NULL) if (checksum != NULL)
@ -101,24 +94,25 @@ ot_gio_splice_update_checksum (GOutputStream *out,
do do
{ {
if (!g_input_stream_read_all (in, buf, sizeof(buf), &bytes_read, cancellable, error)) if (!g_input_stream_read_all (in, buf, sizeof(buf), &bytes_read, cancellable, error))
goto out; return FALSE;
if (!ot_gio_write_update_checksum (out, buf, bytes_read, &bytes_written, checksum, if (!ot_gio_write_update_checksum (out, buf, bytes_read, &bytes_written, checksum,
cancellable, error)) cancellable, error))
goto out; return FALSE;
} }
while (bytes_read > 0); while (bytes_read > 0);
} }
else if (out != NULL) else if (out != NULL)
{ {
if (g_output_stream_splice (out, in, 0, cancellable, error) < 0) if (g_output_stream_splice (out, in, 0, cancellable, error) < 0)
goto out; return FALSE;
} }
ret = TRUE; return TRUE;
out:
return ret;
} }
/* Copy @in to @out, return in @out_csum the binary checksum for
* all data read.
*/
gboolean gboolean
ot_gio_splice_get_checksum (GOutputStream *out, ot_gio_splice_get_checksum (GOutputStream *out,
GInputStream *in, GInputStream *in,
@ -126,22 +120,14 @@ ot_gio_splice_get_checksum (GOutputStream *out,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE; g_autoptr(GChecksum) checksum = g_checksum_new (G_CHECKSUM_SHA256);
GChecksum *checksum = NULL;
g_autofree guchar *ret_csum = NULL;
checksum = g_checksum_new (G_CHECKSUM_SHA256);
if (!ot_gio_splice_update_checksum (out, in, checksum, cancellable, error)) if (!ot_gio_splice_update_checksum (out, in, checksum, cancellable, error))
goto out; return FALSE;
ret_csum = ot_csum_from_gchecksum (checksum); g_autofree guchar *ret_csum = ot_csum_from_gchecksum (checksum);
ret = TRUE;
ot_transfer_out_value (out_csum, &ret_csum); ot_transfer_out_value (out_csum, &ret_csum);
out: return TRUE;
g_clear_pointer (&checksum, (GDestroyNotify) g_checksum_free);
return ret;
} }
gboolean gboolean
@ -162,21 +148,13 @@ ot_checksum_file_at (int dfd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
GChecksum *checksum = NULL;
char *ret = NULL;
g_autoptr(GInputStream) in = NULL; g_autoptr(GInputStream) in = NULL;
if (!ot_openat_read_stream (dfd, path, TRUE, &in, cancellable, error)) if (!ot_openat_read_stream (dfd, path, TRUE, &in, cancellable, error))
goto out; return FALSE;
checksum = g_checksum_new (checksum_type);
g_autoptr(GChecksum) checksum = g_checksum_new (checksum_type);
if (!ot_gio_splice_update_checksum (NULL, in, checksum, cancellable, error)) if (!ot_gio_splice_update_checksum (NULL, in, checksum, cancellable, error))
goto out; return FALSE;
ret = g_strdup (g_checksum_get_string (checksum));
out:
g_clear_pointer (&checksum, (GDestroyNotify) g_checksum_free);
return ret;
return g_strdup (g_checksum_get_string (checksum));
} }