]> git.ipfire.org Git - pakfire.git/commitdiff
execute: Implement passing arguments to scripts
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 May 2021 17:31:25 +0000 (17:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 May 2021 17:31:25 +0000 (17:31 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/build.c
src/libpakfire/execute.c
src/libpakfire/include/pakfire/execute.h
src/libpakfire/transaction.c

index d7d5edbbb011812cb8eff727846077a5a55e71f0..2cf36fd418c8fba92c10b70850f85d0bf7932054 100644 (file)
@@ -585,7 +585,7 @@ static PyObject* Pakfire_execute_script(PakfireObject* self, PyObject* args, PyO
        Pakfire_execute_logging_callback = logging_callback;
 
        // Execute script
-       int r = pakfire_execute_script(self->pakfire, script, strlen(script), envp, flags,
+       int r = pakfire_execute_script(self->pakfire, script, strlen(script), NULL, envp, flags,
                (logging_callback) ? __Pakfire_execute_logging_callback : NULL, NULL);
 
        // Cleanup
index 14b14f5556106c50e0468e459c9e2fc90f04fce4..7cf0c6ad3f8c1e2a35c9e37029d99aafd2255f84 100644 (file)
@@ -50,7 +50,7 @@ static const char* stages[] = {
        "\n" \
        "exit 0\n"
 
-static int pakfire_build_run_script(Pakfire pakfire, const char* filename,
+static int pakfire_build_run_script(Pakfire pakfire, const char* filename, const char* args[],
                pakfire_execute_logging_callback logging_callback, void* data) {
        char* script = NULL;
        size_t size = 0;
@@ -76,7 +76,7 @@ static int pakfire_build_run_script(Pakfire pakfire, const char* filename,
        }
 
        // Execute it
-       r = pakfire_execute_script(pakfire, script, size, NULL, 0, logging_callback, data);
+       r = pakfire_execute_script(pakfire, script, size, args, NULL, 0, logging_callback, data);
        if (r) {
                ERROR(pakfire, "Script '%s' failed with status %d\n", filename, r);
        }
@@ -307,7 +307,7 @@ static int pakfire_build_stage(Pakfire pakfire, PakfireParser makefile, const ch
 
        INFO(pakfire, "Running build stage '%s'\n", stage);
 
-       r = pakfire_execute_script(pakfire, script, strlen(script), NULL, 0,
+       r = pakfire_execute_script(pakfire, script, strlen(script), NULL, NULL, 0,
                logging_callback, data);
        if (r) {
                ERROR(pakfire, "Build stage '%s' failed with status %d\n", stage, r);
@@ -372,7 +372,7 @@ PAKFIRE_EXPORT int pakfire_build(Pakfire pakfire, const char* path,
        }
 
        // Remove static libraries
-       r = pakfire_build_run_script(pakfire, "compress-man-pages", logging_callback, data);
+       r = pakfire_build_run_script(pakfire, "compress-man-pages", NULL, logging_callback, data);
        if (r)
                goto ERROR;
 
index abae3e1dd9c69cd84c5631422ab4c5f64041c1b4..95ee28b556adab061b736ccb802b8ee01716bad9 100644 (file)
@@ -625,9 +625,9 @@ PAKFIRE_EXPORT int pakfire_execute_command(Pakfire pakfire, const char* command,
 }
 
 PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* script, const size_t size,
-               char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data) {
+               const char* args[], char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data) {
        const char* root = pakfire_get_path(pakfire);
-
+       const char** argv = NULL;
        char path[PATH_MAX];
        int r;
 
@@ -677,17 +677,36 @@ PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* script, c
                goto out;
        }
 
-       const char* command = path;
-       if (root)
-               command = pakfire_path_relpath(root, path);
+       // Count how many arguments were passed
+       unsigned int argc = 1;
+       if (args) {
+               for (const char** arg = args; *arg; arg++)
+                       argc++;
+       }
+
+       argv = calloc(argc + 1, sizeof(*argv));
+       if (!argv) {
+               ERROR(pakfire, "Could not allocate argv: %s\n", strerror(errno));
+               goto out;
+       }
+
+       // Set command
+       argv[0] = (root) ? pakfire_path_relpath(root, path) : path;
+
+       // Copy args
+       for (unsigned int i = 1; i < argc; i++)
+               argv[i] = args[i-1];
 
        // Run the script
-       r = pakfire_execute_command(pakfire, command, envp, flags, logging_callback, data);
+       r = pakfire_execute(pakfire, argv, envp, flags, logging_callback, data);
        if (r) {
                DEBUG(pakfire, "Script return code: %d\n", r);
        }
 
 out:
+       if (argv)
+               free(argv);
+
        // Remove script from disk
        if (*path)
                unlink(path);
index e9068050301942aabeec1bf29a32effe3fadca31..335fe0bbdd7b644ec2be2eabf9e374afbc198f4d 100644 (file)
@@ -31,7 +31,7 @@ int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[],
 int pakfire_execute_command(Pakfire pakfire, const char* command, char* envp[],
        int flags, pakfire_execute_logging_callback logging_callback, void* data);
 int pakfire_execute_script(Pakfire pakfire, const char* script, const size_t size,
-       char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data);
+       const char* args[], char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data);
 
 enum {
        PAKFIRE_EXECUTE_NONE                    = 0,
index b5284bc849390b0b87ac68d49e2b633682e9737e..e67e792be98f52c64cc36584c3d2cc0cc30ef49b 100644 (file)
@@ -477,7 +477,7 @@ static int pakfire_transaction_run_script(struct pakfire_transaction* transactio
        // Detect what kind of script this is and run it
        if (pakfire_scriptlet_is_shell_script(scriptlet)) {
                pakfire_execute_script(transaction->pakfire, scriptlet->data, scriptlet->size,
-                       NULL, 0, NULL, NULL);
+                       NULL, NULL, 0, NULL, NULL);
        } else {
                ERROR(transaction->pakfire, "Scriptlet is of an unknown kind\n");
        }