From: Michael Tremer Date: Mon, 1 Mar 2021 19:47:19 +0000 (+0000) Subject: execute: Add function to execute a script X-Git-Tag: 0.9.28~1285^2~667 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc33532d567c3505d6dc653aedb58a10b25df009;p=pakfire.git execute: Add function to execute a script This has been moved from step Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/execute.c b/src/libpakfire/execute.c index 1e4de1e75..daa79d64b 100644 --- a/src/libpakfire/execute.c +++ b/src/libpakfire/execute.c @@ -35,6 +35,7 @@ #include #include #include +#include #define BUFFER_SIZE 1024 * 64 #define EPOLL_MAX_EVENTS 2 @@ -425,3 +426,72 @@ PAKFIRE_EXPORT int pakfire_execute_command(Pakfire pakfire, const char* command, return pakfire_execute(pakfire, argv, envp, flags, logging_callback); } + +PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size, + char* envp[], int flags, pakfire_execute_logging_callback logging_callback) { + const char* root = pakfire_get_path(pakfire); + + // Write the scriptlet to disk + char* path = pakfire_path_join(root, "tmp/.pakfire-script.XXXXXX"); + int r; + + DEBUG(pakfire, "Writing script to %s\n", path); + + // Open a temporary file + int fd = mkstemp(path); + if (fd < 0) { + ERROR(pakfire, "Could not open a temporary file: %s\n", + strerror(errno)); + + r = errno; + } + + // Write data + ssize_t bytes_written = write(fd, data, size); + if (bytes_written < (ssize_t)size) { + ERROR(pakfire, "Could not write script to file %s: %s\n", + path, strerror(errno)); + + r = errno; + goto out; + } + + // Make the script executable + r = fchmod(fd, S_IRUSR|S_IWUSR|S_IXUSR); + if (r) { + ERROR(pakfire, "Could not set executable permissions on %s: %s\n", + path, strerror(errno)); + + r = errno; + goto out; + } + + // Close file + r = close(fd); + if (r) { + ERROR(pakfire, "Could not close script file %s: %s\n", + path, strerror(errno)); + + r = errno; + goto out; + } + + const char* command = path; + if (root) + command = pakfire_path_relpath(root, path); + + // Run the script + r = pakfire_execute_command(pakfire, command, envp, flags, logging_callback); + if (r) { + DEBUG(pakfire, "Script return code: %d\n", r); + } + +out: + // Remove script from disk + unlink(path); + + // Cleanup + free(path); + + return r; +} diff --git a/src/libpakfire/include/pakfire/execute.h b/src/libpakfire/include/pakfire/execute.h index edc216365..e1d534c41 100644 --- a/src/libpakfire/include/pakfire/execute.h +++ b/src/libpakfire/include/pakfire/execute.h @@ -29,6 +29,8 @@ int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[], int flags, pakfire_execute_logging_callback logging_callback); int pakfire_execute_command(Pakfire pakfire, const char* command, char* envp[], int flags, pakfire_execute_logging_callback logging_callback); +int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size, + char* envp[], int flags, pakfire_execute_logging_callback logging_callback); enum { PAKFIRE_EXECUTE_NONE = 0, diff --git a/src/libpakfire/step.c b/src/libpakfire/step.c index c88d10b90..4faaad9f1 100644 --- a/src/libpakfire/step.c +++ b/src/libpakfire/step.c @@ -234,71 +234,7 @@ static int pakfire_script_check_shell(struct pakfire_scriptlet* scriptlet) { } static int pakfire_step_run_shell_script(PakfireStep step, struct pakfire_scriptlet* scriptlet) { - const char* root = pakfire_get_path(step->pakfire); - - // Write the scriptlet to disk - char* path = pakfire_path_join(root, "tmp/.pakfire-scriptlet.XXXXXX"); - int r; - - DEBUG(step->pakfire, "Writing script to %s\n", path); - - // Open a temporary file - int fd = mkstemp(path); - if (fd < 0) { - ERROR(step->pakfire, "Could not open a temporary file: %s\n", - strerror(errno)); - - r = errno; - } - - // Write data - ssize_t bytes_written = write(fd, scriptlet->data, scriptlet->size); - if (bytes_written < (ssize_t)scriptlet->size) { - ERROR(step->pakfire, "Could not write script to file %s: %s\n", - path, strerror(errno)); - - r = errno; - goto out; - } - - // Make the script executable - r = fchmod(fd, S_IRUSR|S_IWUSR|S_IXUSR); - if (r) { - ERROR(step->pakfire, "Could not set executable permissions on %s: %s\n", - path, strerror(errno)); - - r = errno; - goto out; - } - - // Close file - r = close(fd); - if (r) { - ERROR(step->pakfire, "Could not close script file %s: %s\n", - path, strerror(errno)); - - r = errno; - goto out; - } - - const char* command = path; - if (root) - command = pakfire_path_relpath(root, path); - - // Run the script - r = pakfire_execute_command(step->pakfire, command, NULL, 0, NULL); - if (r) { - DEBUG(step->pakfire, "Script return code: %d\n", r); - } - -out: - // Remove script from disk - unlink(path); - - // Cleanup - free(path); - - return r; + return pakfire_execute_script(step->pakfire, scriptlet->data, scriptlet->size, NULL, 0, NULL); } static int pakfire_step_run_script(PakfireStep step,