]> git.ipfire.org Git - pakfire.git/commitdiff
build: mkimage: Take a fd for the output
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Jun 2023 15:52:36 +0000 (15:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Jun 2023 15:52:36 +0000 (15:52 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/build.c
src/libpakfire/include/pakfire/build.h
src/scripts/pakfire-builder.in

index 8b1ae63f82e29c3f3f38e0d0d85ed5109e0b7387..99035637a28bc9ca86ffdfc6b9f2a8e329268f96 100644 (file)
@@ -1472,10 +1472,16 @@ static PyObject* Pakfire_mkimage(PakfireObject* self, PyObject* args, PyObject*
        };
        struct pakfire_build* build = NULL;
        const char* type = NULL;
-       const char* path = NULL;
+       PyObject* file = NULL;
+       FILE* f = NULL;
        int r;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist, &type, &path))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO", kwlist, &type, &file))
+               return NULL;
+
+       // Make a file handle out of file
+       f = PyObject_AsFileHandle(file, "w");
+       if (!f)
                return NULL;
 
        // Create a new build environment
@@ -1488,7 +1494,7 @@ static PyObject* Pakfire_mkimage(PakfireObject* self, PyObject* args, PyObject*
        Py_BEGIN_ALLOW_THREADS
 
        // Run mkimage
-       r = pakfire_build_mkimage(build, type, path);
+       r = pakfire_build_mkimage(build, type, f);
        if (r) {
                Py_BLOCK_THREADS;
 
@@ -1503,6 +1509,8 @@ static PyObject* Pakfire_mkimage(PakfireObject* self, PyObject* args, PyObject*
 ERROR:
        if (build)
                pakfire_build_unref(build);
+       if (f)
+               fclose(f);
        if (r)
                return NULL;
 
index 9dcdd90bf71da817fbddbf00b6db7acc1ec73dd9..1e6ff126f5c0dac325d5ea6fdb0ffd18b9203454 100644 (file)
@@ -2211,25 +2211,38 @@ ERROR:
 }
 
 PAKFIRE_EXPORT int pakfire_build_mkimage(struct pakfire_build* build,
-               const char* type, const char* filename) {
+               const char* type, FILE* f) {
+       FILE* t = NULL;
+       char path[PATH_MAX];
        int r;
 
        // Check inputs
-       if (!type || !filename) {
+       if (!type || !f) {
                errno = EINVAL;
                return 1;
        }
 
+       // Create a path inside the build environment
+       r = pakfire_path(build->pakfire, path, "%s", PAKFIRE_TMP_DIR "/pakfire-image.XXXXXX");
+       if (r)
+               goto ERROR;
+
+       // Allocate a temporary file
+       t = pakfire_mktemp(path, 0600);
+       if (!t) {
+               ERROR(build->pakfire, "Could not allocate a temporary file: %m\n");
+               r = 1;
+               goto ERROR;
+       }
+
        // Initialize the build environment
        r = pakfire_build_init(build);
        if (r)
                goto ERROR;
 
-       // XXX Allocate a temporary file
-
        const char* args[] = {
                type,
-               // XXX tempfile
+               pakfire_relpath(build->pakfire, path),
                NULL,
        };
 
@@ -2238,8 +2251,15 @@ PAKFIRE_EXPORT int pakfire_build_mkimage(struct pakfire_build* build,
        if (r)
                goto ERROR;
 
+       // Copy data to its destination
+       r = pakfire_copy(build->pakfire, t, f);
+       if (r)
+               goto ERROR;
+
 ERROR:
-       // XXX remove the tempfile
+       // Unlink the temporary file
+       if (*path)
+               unlink(path);
 
        return r;
 }
index 143a2b2fd94de55294dad398209891aaafaa4524..59999425b44b0ead8ac9e97b09a1615890130b0e 100644 (file)
@@ -44,7 +44,7 @@ int pakfire_build_set_target(struct pakfire_build* build, const char* target);
 int pakfire_build_exec(struct pakfire_build* build, const char* path);
 
 int pakfire_build_mkimage(struct pakfire_build* build,
-       const char* type, const char* filename);
+       const char* type, FILE* f);
 
 int pakfire_build_clean(struct pakfire* pakfire, int flags);
 
index da9c3fcdb774d5308fa302a6650f601d33062e13..758f2bcd54a38ae6b9f7d2ebfc47bbba16e30f4b 100644 (file)
@@ -98,7 +98,7 @@ class Cli(object):
                image_create = image_subparsers.add_parser("create", help=_("Create a new image"))
                image_create.set_defaults(func=self._image_create)
                image_create.add_argument("type", help=_("Image Type"))
-               image_create.add_argument("path", help=_("Image Path"))
+               image_create.add_argument("path", help=_("Image Path"), type=argparse.FileType("w"))
 
                # info
                info = subparsers.add_parser("info",