From c439f3082093d11cf09c6cf64720c413200defe5 Mon Sep 17 00:00:00 2001 From: ngn Date: Sat, 4 May 2024 22:34:48 +0300 Subject: [PATCH] new: install requirements after pull --- locale/tr/LC_MESSAGES/mc.po | 38 +++++++++------- src/error.h | 4 ++ src/pull.c | 39 +++++++++++++++- src/run.c | 90 +++++++++++++++++++++++++++++++++++++ src/run.h | 4 ++ 5 files changed, 158 insertions(+), 17 deletions(-) create mode 100644 src/run.c create mode 100644 src/run.h diff --git a/locale/tr/LC_MESSAGES/mc.po b/locale/tr/LC_MESSAGES/mc.po index 436cf71..d4e6f25 100644 --- a/locale/tr/LC_MESSAGES/mc.po +++ b/locale/tr/LC_MESSAGES/mc.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-05-04 20:25+0300\n" +"POT-Creation-Date: 2024-05-04 22:30+0300\n" "PO-Revision-Date: 2024-05-01 13:34+0300\n" "Last-Translator: \n" "Language-Team: Turkish \n" @@ -74,19 +74,19 @@ msgstr "" msgid "Failed to change directory to the specified directory" msgstr "" -#: src/gen.c:29 src/gen.c:36 src/pull.c:82 +#: src/gen.c:29 src/gen.c:36 src/pull.c:83 msgid "Loaded repository configuration" msgstr "" -#: src/gen.c:31 src/pull.c:77 +#: src/gen.c:31 src/pull.c:78 msgid "Failed to load the configuration file (mc.cfg):" msgstr "" -#: src/gen.c:42 src/pull.c:109 +#: src/gen.c:42 src/pull.c:110 msgid "Copying all the targets" msgstr "" -#: src/gen.c:48 src/pull.c:121 +#: src/gen.c:48 src/pull.c:124 msgid "Failed to copy the target:" msgstr "" @@ -161,50 +161,58 @@ msgid "" "information" msgstr "" -#: src/pull.c:27 +#: src/pull.c:28 msgid "Please specify a config name or a URL" msgstr "" -#: src/pull.c:56 +#: src/pull.c:57 #, c-format msgid "Cloning %s" msgstr "" -#: src/pull.c:61 +#: src/pull.c:62 #, c-format msgid "Failed to clone the %s:" msgstr "" -#: src/pull.c:72 +#: src/pull.c:73 #, c-format msgid "Failed to chdir to %s" msgstr "" -#: src/pull.c:85 +#: src/pull.c:86 msgid "Do you want to continue?" msgstr "" -#: src/pull.c:91 +#: src/pull.c:92 msgid "Checking all the targets" msgstr "" -#: src/pull.c:97 +#: src/pull.c:98 #, c-format msgid "Failed to access the source for the target \"%s\"" msgstr "" -#: src/pull.c:106 +#: src/pull.c:107 msgid "All the target checks were successful" msgstr "" -#: src/pull.c:114 +#: src/pull.c:118 msgid "Install the target?" msgstr "" -#: src/pull.c:115 +#: src/pull.c:119 msgid "Skipping target" msgstr "" +#: src/pull.c:157 +msgid "Installing all the requirements" +msgstr "" + +#: src/pull.c:160 +msgid "Failed to run the mp-install command" +msgstr "" + #: src/target.c:15 msgid "Configuration target is missing a name" msgstr "" diff --git a/src/error.h b/src/error.h index f6431e5..ad76102 100644 --- a/src/error.h +++ b/src/error.h @@ -12,6 +12,10 @@ enum ErrorCodes { OpendirFail = 948, MkdirFail = 945, OpenFail = 944, + + RunNoRoot = 943, + RunCmdNotFound = 942, + RunCmdFail = 941, }; extern int errno; diff --git a/src/pull.c b/src/pull.c index ba0c7f5..902581a 100644 --- a/src/pull.c +++ b/src/pull.c @@ -6,6 +6,7 @@ #include "paths.h" #include "url.h" #include "util.h" +#include "run.h" #include #include @@ -108,13 +109,15 @@ COPY: cur = repo_cfg.t_first; info(_("Copying all the targets")); + char **argv = NULL; + int argc = 0, indx = 0; + while (cur && ++counter > 0) { target_print(cur); if (!yesno(_("Install the target?"))) { info(_("Skipping target")); - cur = cur->next; - continue; + goto NEXT; } if (!target_copy(cur, false)) { @@ -123,11 +126,43 @@ COPY: goto END; } + argc += cur->require.s; + if(0 == argc) + goto NEXT; + + if(NULL == argv) + argv = malloc(sizeof(char*)*argc); + else + argv = realloc(argv, sizeof(char*)*argc); + + for(int i = 0; i < cur->require.s; i++){ + argv[indx] = cur->require.c[i]; + indx++; + } + +NEXT: cur = cur->next; } + if(NULL == argv){ + ret = true; + goto END; + } + + argc++; + argv = realloc(argv, sizeof(char*)*argc); + argv[indx] = NULL; ret = true; + info(_("Installing all the requirements")); + if(!run_cmd_root("mp-install", argv)){ + ret = false; + error(_("Failed to run the mp-install command")); + details(errch); + } + + free(argv); + END: if (repo_cfg.name != NULL) config_free(&repo_cfg); diff --git a/src/run.c b/src/run.c new file mode 100644 index 0000000..40c136c --- /dev/null +++ b/src/run.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include "util.h" +#include "run.h" + +extern char **environ; + +char *run_find_cmd(char *cmd){ + char *path = getenv("PATH"), *save, *el; + + while((el = strtok_r(path, ":", &save)) != NULL){ + char fp[strlen(el)+strlen(cmd)+2]; + join(fp, el, cmd); + + if(exists(fp)) + return strdup(fp); + } + return NULL; +} + +bool run_cmd(char **all){ + pid_t pid; + if(posix_spawn(&pid, all[0], NULL, NULL, all, environ) != 0) + return false; + + int status; + waitpid(pid, &status, 0); + return status == 0; +} + +bool run_cmd_root(char *cmd, char **args){ + char *rootcmd = NULL; + + if(NULL == run_find_cmd(cmd)){ + error_set("Command not found"); + errno = RunCmdNotFound; + return false; + } + + if(getuid()==0) + goto RUN; + else if((rootcmd = run_find_cmd("doas")) != NULL) + goto RUN; + else if((rootcmd = run_find_cmd("sudo" )) != NULL) + goto RUN; + + error_set("There is no doas or sudo installed"); + errno = RunNoRoot; + return false; + +RUN: + int arg_count = 0; + while(args[arg_count] != NULL) + arg_count++; + arg_count += 2; + + if(rootcmd != NULL) + arg_count++; + + char **all = malloc(arg_count*sizeof(char*)); + int indx = 0; + + if(rootcmd != NULL){ + all[indx] = rootcmd; + all[++indx] = cmd; + }else + all[indx] = rootcmd; + + for(int i = 0; i < arg_count; i++) + all[++indx] = args[i]; + all[++indx] = NULL; + + bool ret = true; + + if(!run_cmd(all)){ + error_set("Command failed"); + errno = RunCmdFail; + ret = false; + } + + free(all); + return ret; +} diff --git a/src/run.h b/src/run.h new file mode 100644 index 0000000..e68bf4f --- /dev/null +++ b/src/run.h @@ -0,0 +1,4 @@ +#pragma once +#include + +bool run_cmd_root(char *, char **);