Browse Source

Move non-specific code to the gpgutil module.

The write_decrypted_output function has nothing specific to the
decrypt module and can be moved to the gpgutil module.
master
parent
commit
ef7a7ac3bc
  1. 34
      src/decrypt.c
  2. 31
      src/gpgutil.c
  3. 3
      src/gpgutil.h

34
src/decrypt.c

@ -22,8 +22,6 @@
#include "decrypt.h"
#include <errno.h>
#include <glib.h>
#include <gtk/gtk.h>
@ -213,36 +211,6 @@ is_file_encrypted(gpgme_data_t buffer, GError **error)
return ret;
}
static int
write_decrypted_output(gpgutil_file_t *src, const char *filename, GError **error)
{
int ret;
gpgutil_file_t dest = { 0 };
ret = gpgme_data_seek(src->buffer, 0, SEEK_SET) != -1;
if ( ret )
ret = gpgutil_open_file(filename, "w", &dest, error);
if ( ret ) {
char buffer[512];
size_t len;
while ( ret && (len = gpgme_data_read(src->buffer, buffer, sizeof(buffer))) > 0 )
ret = gpgme_data_write(dest.buffer, buffer, len) != -1;
if ( ret )
ret = len != - 1;
}
if ( ! ret )
yki_error(error, YKI_ERROR_IO, "Cannot write to output file: %s", strerror(errno));
gpgutil_close_file(&dest, NULL);
return ret;
}
/* Public interface. */
@ -297,7 +265,7 @@ yki_decrypt(gpgme_ctx_t gpgme, const char *file, GError **error)
ret = yki_confirm_overwrite(yki.output_filename);
if ( ret )
ret = write_decrypted_output(&out, yki.output_filename, error);
ret = gpgutil_copy_buffer(&out, yki.output_filename, error);
gpgutil_close_file(&in, NULL);
gpgutil_close_file(&out, NULL);

31
src/gpgutil.c

@ -111,3 +111,34 @@ gpgutil_close_file(gpgutil_file_t *file, GError **error)
return gpgme_err_code(gerr) == 0;
}
int
gpgutil_copy_buffer(gpgutil_file_t *src, const char *filename, GError **error)
{
int ret;
gpgme_error_t gerr;
gpgutil_file_t dest = { 0 };
ret = gpgme_data_seek(src->buffer, 0, SEEK_SET) != -1;
if ( ret )
ret = gpgutil_open_file(filename, "w", &dest, error);
if ( ret ) {
char buffer[512];
size_t len;
while ( ret && (len = gpgme_data_read(src->buffer, buffer, sizeof(buffer))) > 0 )
ret = gpgme_data_write(dest.buffer, buffer, len);
if ( ret )
ret = len != -1;
}
if ( ! ret )
yki_error(error, YKI_ERROR_IO, "Cannot copy data to file: %s", strerror(errno));
gpgutil_close_file(&dest, NULL);
return ret;
}

3
src/gpgutil.h

@ -40,6 +40,9 @@ gpgutil_open_file(const char *, const char *, gpgutil_file_t *, GError **);
int
gpgutil_close_file(gpgutil_file_t *, GError **);
int
gpgutil_copy_buffer(gpgutil_file_t *, const char *, GError **);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save