return r;
}
-PAKFIRE_EXPORT int pakfire_execute_script(struct pakfire* pakfire, const char* script, const size_t size,
- 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;
-
- // Write the scriptlet to disk
- r = pakfire_path_join(path, root, "pakfire-script.XXXXXX");
- if (r < 0)
- goto out;
-
- // Open a temporary file
- int fd = mkstemp(path);
- if (fd < 0) {
- ERROR(pakfire, "Could not open a temporary file: %m\n");
- r = errno;
- }
-
- DEBUG(pakfire, "Writing script to %s:\n%.*s\n", path, (int)size, script);
-
- // Write data
- ssize_t bytes_written = write(fd, script, size);
- if (bytes_written < (ssize_t)size) {
- ERROR(pakfire, "Could not write script to file %s: %m\n", path);
- 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: %m\n", path);
- r = errno;
- goto out;
- }
-
- // Close file
- r = close(fd);
- if (r) {
- ERROR(pakfire, "Could not close script file %s: %m\n", path);
- r = errno;
- goto out;
- }
-
- // 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: %m\n");
- 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(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);
-
- return r;
-}
-
int pakfire_execute_shell(struct pakfire* pakfire) {
const char* argv[] = {
"/bin/bash", "--login", NULL,
int pakfire_execute(struct pakfire* pakfire, const char* argv[], char* envp[],
int flags, pakfire_execute_logging_callback logging_callback, void* data);
-int pakfire_execute_script(struct pakfire* pakfire, const char* script, const size_t size,
- const char* args[], char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data);
enum {
PAKFIRE_EXECUTE_NONE = 0,