return r;
}
-char* pakfire_key_dump(struct pakfire_key* key, size_t* length) {
- char* buffer = NULL;
+int pakfire_key_export_string(struct pakfire_key* self, char** buffer, size_t* length) {
FILE* f = NULL;
int r;
// Create a file handle to write to the buffer
- f = open_memstream(&buffer, length);
+ f = open_memstream(buffer, length);
if (!f) {
- ERROR(key->ctx, "Could not open a new memstream: %m\n");
+ ERROR(self->ctx, "Could not open a new memstream: %m\n");
r = -errno;
goto ERROR;
}
// Export the public part of the key
- r = pakfire_key_export(key, f, PAKFIRE_KEY_EXPORT_MODE_PUBLIC);
+ r = pakfire_key_export(self, f, PAKFIRE_KEY_EXPORT_MODE_PUBLIC);
if (r < 0) {
- ERROR(key->ctx, "Could not export key: %m\n");
+ ERROR(self->ctx, "Could not export key: %m\n");
goto ERROR;
}
if (f)
fclose(f);
- // Free the buffer on error
- if (r && buffer) {
- free(buffer);
- buffer = NULL;
- }
-
- return buffer;
+ return r;
}
/*
int pakfire_key_generate(struct pakfire_key** key, struct pakfire_ctx* ctx,
const pakfire_key_algo_t algo, const char* comment);
int pakfire_key_export(struct pakfire_key* key, FILE* f, const pakfire_key_export_mode_t mode);
+int pakfire_key_export_string(struct pakfire_key* self, char** buffer, size_t* length);
int pakfire_key_import(struct pakfire_key** key, struct pakfire_ctx* ctx, FILE* f);
-char* pakfire_key_dump(struct pakfire_key* key, size_t* length);
-
// Sign
int pakfire_key_sign(struct pakfire_key* key, FILE* s, FILE* f, const char* comment);
int pakfire_key_sign_string(struct pakfire_key* key,
static PyObject* Key_str(KeyObject* self) {
PyObject* ret = NULL;
- char* string = NULL;
+ char* buffer = NULL;
size_t length = 0;
+ int r;
// Dump the key
- string = pakfire_key_dump(self->key, &length);
- if (!string) {
+ r = pakfire_key_export_string(self->key, &buffer, &length);
+ if (r < 0) {
+ errno = -errno;
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
// Parse the string
- ret = PyUnicode_FromStringAndSize(string, length);
- free(string);
+ ret = PyUnicode_FromStringAndSize(buffer, length);
+ free(buffer);
return ret;
}
static PyObject* Key_export(KeyObject* self, PyObject* args) {
pakfire_key_export_mode_t mode = PAKFIRE_KEY_EXPORT_MODE_PUBLIC;
PyObject* object = NULL;
+ char* buffer = NULL;
+ size_t length = 0;
int secret = 0;
if (!PyArg_ParseTuple(args, "|p", &secret))
return NULL;
- char* data = NULL;
- size_t data_length = 0;
-
// Create buffer to write the key to
- FILE* f = open_memstream(&data, &data_length);
+ FILE* f = open_memstream(&buffer, &length);
if (!f) {
PyErr_SetFromErrno(PyExc_OSError);
goto ERROR;
// Export the key
int r = pakfire_key_export(self->key, f, mode);
- if (r) {
+ if (r < 0) {
+ errno = -r;
PyErr_SetFromErrno(PyExc_OSError);
goto ERROR;
}
- object = PyUnicode_FromStringAndSize(data, data_length);
+ object = PyUnicode_FromStringAndSize(buffer, length);
ERROR:
if (f)
fclose(f);
- if (data)
- free(data);
+ if (buffer)
+ free(buffer);
return object;
}