]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
key: Make dumping a key less braindead
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 19:11:48 +0000 (19:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 19:11:48 +0000 (19:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/key.c
src/pakfire/key.h
src/python/key.c

index d6adb5f83b4b53c893e49cc32c587d73c8fd9421..dd55556038c15e4a695f155951be8707970758ac 100644 (file)
@@ -739,24 +739,15 @@ ERROR:
        return r;
 }
 
-char* pakfire_key_dump(struct pakfire_key* key) {
+char* pakfire_key_dump(struct pakfire_key* key, size_t* length) {
        char* buffer = NULL;
        FILE* f = NULL;
-       int fd = -EBADF;
        int r;
 
-       // Allocate a buffer in memory
-       fd = memfd_create("pakfire-key-dump", MFD_CLOEXEC);
-       if (fd < 0) {
-               ERROR(key->ctx, "Could not allocate a temporary file: %m\n");
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Re-open as FILE handle
-       f = fdopen(fd, "r+");
+       // Create a file handle to write to the buffer
+       f = open_memstream(&buffer, length);
        if (!f) {
-               ERROR(key->ctx, "Could not open file handle for temporary file: %m\n");
+               ERROR(key->ctx, "Could not open a new memstream: %m\n");
                r = -errno;
                goto ERROR;
        }
@@ -768,41 +759,9 @@ char* pakfire_key_dump(struct pakfire_key* key) {
                goto ERROR;
        }
 
-       // Determine the length of the buffer
-       size_t length = ftell(f);
-
-       // Go back to the beginning of the file
-       r = pakfire_rewind(f);
-       if (r < 0) {
-               ERROR(key->ctx, "Could not rewind buffer: %m\n");
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Allocate a buffer
-       buffer = calloc(1, length + 1);
-       if (!buffer) {
-               ERROR(key->ctx, "Could not allocate buffer of %zu byte(s)\n", length + 1);
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Read everything into the buffer
-       size_t bytes_read = fread(buffer, 1, length, f);
-       if (bytes_read < length) {
-               ERROR(key->ctx, "Could not read back the buffer: %m\n");
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Success!
-       r = 0;
-
 ERROR:
        if (f)
                fclose(f);
-       if (fd >= 0)
-               close(fd);
 
        // Free the buffer on error
        if (r && buffer) {
index c7eedeb3504d7ea48fb91e65362874284405689b..f97923e55ca0dc8f95d7bd6b21aebe834f853ceb 100644 (file)
@@ -52,7 +52,7 @@ int pakfire_key_generate(struct pakfire_key** key, struct pakfire_ctx* ctx,
 int pakfire_key_export(struct pakfire_key* key, FILE* f, const pakfire_key_export_mode_t mode);
 int pakfire_key_import(struct pakfire_key** key, struct pakfire_ctx* ctx, FILE* f);
 
-char* pakfire_key_dump(struct pakfire_key* key);
+char* pakfire_key_dump(struct pakfire_key* key, size_t* length);
 
 // Sign
 int pakfire_key_sign(struct pakfire_key* key,
index 052f3af8771de8cfe3ccfaec863a501001641b76..a90732e37cbee0ab54aa8e11fd5098ee3e9aa917 100644 (file)
@@ -70,12 +70,22 @@ static PyObject* Key_repr(KeyObject* self) {
 }
 
 static PyObject* Key_str(KeyObject* self) {
-       char* string = pakfire_key_dump(self->key);
+       PyObject* ret = NULL;
+       char* string = NULL;
+       size_t length = 0;
 
-       PyObject* object = PyUnicode_FromString(string);
+       // Dump the key
+       string = pakfire_key_dump(self->key, &length);
+       if (!string) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       // Parse the string
+       ret = PyUnicode_FromStringAndSize(string, length);
        free(string);
 
-       return object;
+       return ret;
 }
 
 static PyObject* Key_get_id(KeyObject* self) {