From c8f04af235027c2dc41b727fa33c64bfc97935ff Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 14 Jun 2023 15:52:36 +0000 Subject: [PATCH] build: mkimage: Take a fd for the output Signed-off-by: Michael Tremer --- src/_pakfire/pakfire.c | 14 ++++++++--- src/libpakfire/build.c | 32 +++++++++++++++++++++----- src/libpakfire/include/pakfire/build.h | 2 +- src/scripts/pakfire-builder.in | 2 +- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 8b1ae63f8..99035637a 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -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; diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 9dcdd90bf..1e6ff126f 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -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; } diff --git a/src/libpakfire/include/pakfire/build.h b/src/libpakfire/include/pakfire/build.h index 143a2b2fd..59999425b 100644 --- a/src/libpakfire/include/pakfire/build.h +++ b/src/libpakfire/include/pakfire/build.h @@ -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); diff --git a/src/scripts/pakfire-builder.in b/src/scripts/pakfire-builder.in index da9c3fcdb..758f2bcd5 100644 --- a/src/scripts/pakfire-builder.in +++ b/src/scripts/pakfire-builder.in @@ -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", -- 2.39.5