A task-oriented GnuPG frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

144 lines
3.4 KiB

/*
* Yorkie - A task-oriented GnuPG frontend
* Copyright (C) 2020 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gpgutil.h"
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include "error.h"
gpgme_ctx_t
gpgutil_initialize(GError **error)
{
gpgme_ctx_t ctx = NULL;
gpgme_error_t gerr;
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
if ( (gerr = gpgme_new(&ctx)) )
yki_error_gpgme(error, YKI_ERROR_GPGME_INTERNAL, gerr);
return ctx;
}
int
gpgutil_open_file(const char *name,
const char *mode,
gpgutil_file_t *file,
GError **error)
{
gpgme_error_t gerr;
g_assert(file);
gerr = 0;
file->buffer = NULL;
file->file = NULL;
if ( name )
file->file = fopen(name, mode);
else
file->file = tmpfile();
if ( ! file->file )
gerr = gpgme_error_from_errno(errno);
if ( ! gerr )
gerr = gpgme_data_new_from_stream(&file->buffer, file->file);
if ( ! gerr && name )
gerr = gpgme_data_set_file_name(file->buffer, name);
if ( gerr ) {
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
}
if ( file->file ) {
fclose(file->file);
file->file = NULL;
}
yki_error_gpgme(error, YKI_ERROR_GPGME_OPEN, gerr);
}
return gpgme_err_code(gerr) == 0;
}
int
gpgutil_close_file(gpgutil_file_t *file, GError **error)
{
gpgme_error_t gerr = 0;
g_assert(file);
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
if ( fclose(file->file) )
gerr = gpgme_error_from_errno(errno);
file->file = NULL;
}
if ( gerr )
yki_error_gpgme(error, YKI_ERROR_GPGME_CLOSE, gerr);
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;
}