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.
 
 
 
 

108 lines
2.6 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(name && mode && file);
gerr = 0;
file->buffer = NULL;
file->file = NULL;
if ( ! (file->file = fopen(name, mode)) )
gerr = gpgme_error_from_errno(errno);
if ( ! gerr )
gerr = gpgme_data_new_from_stream(&file->buffer, file->file);
if ( ! gerr )
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);
}
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);
}