From 4434b742c565852b621d3c76e62f7e4ba6c5e56d Mon Sep 17 00:00:00 2001 From: ngn Date: Fri, 16 Aug 2024 03:46:02 +0300 Subject: [PATCH] update: disabled root directory check for non-root commands --- locale/tr/LC_MESSAGES/matt.po | 48 ++++++++++++----------- src/cmd.h | 1 + src/main.c | 74 +++++++++++++++++++++-------------- 3 files changed, 71 insertions(+), 52 deletions(-) diff --git a/locale/tr/LC_MESSAGES/matt.po b/locale/tr/LC_MESSAGES/matt.po index 9eb4054..d619015 100644 --- a/locale/tr/LC_MESSAGES/matt.po +++ b/locale/tr/LC_MESSAGES/matt.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-16 03:05+0300\n" +"POT-Creation-Date: 2024-08-16 03:44+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -82,67 +82,71 @@ msgid "Received a segfault" msgstr "Received a segfault" #: src/main.c:102 +msgid "Failed to lock, matt is already running" +msgstr "Failed to lock, matt is already running" + +#: src/main.c:106 +msgid "Failed to lock, are you root?" +msgstr "Failed to lock, are you root?" + +#: src/main.c:130 msgid "Failed to access the root directory, are you running as root?" msgstr "Failed to access the root directory, are you running as root?" -#: src/main.c:106 +#: src/main.c:134 #, fuzzy msgid "Failed to access the data directory (" msgstr "Failed to access the configuration file: %s" -#: src/main.c:110 +#: src/main.c:138 #, fuzzy msgid "Failed to access the temp directory (" msgstr "Failed to open the editor: %s" -#: src/main.c:114 +#: src/main.c:142 #, fuzzy, c-format msgid "Failed to initialize libmp context: %s" msgstr "Failed to install " -#: src/main.c:125 +#: src/main.c:153 #, fuzzy msgid "Failed to create the pools directory (" msgstr "Failed to open the editor: %s" -#: src/main.c:135 +#: src/main.c:163 msgid "Failed to add pool " msgstr "Failed to add pool " -#: src/main.c:149 -msgid "Failed to lock, matt is already running" -msgstr "Failed to lock, matt is already running" - -#: src/main.c:153 -msgid "Failed to lock, are you root?" -msgstr "Failed to lock, are you root?" - -#: src/main.c:165 +#: src/main.c:177 msgid "Command not found: " msgstr "Command not found: " -#: src/main.c:167 +#: src/main.c:178 +msgid "Displaying help information" +msgstr "" + +#: src/main.c:181 #, fuzzy, c-format msgid "MatterLinux package manager %s (libmp %s)" msgstr "MatterLinux package manager (version %s)" -#: src/main.c:168 +#: src/main.c:182 msgid "Usage: " msgstr "Usage: " -#: src/main.c:170 +#: src/main.c:184 msgid "Here is a list of available commands:" msgstr "Here is a list of available commands:" -#: src/main.c:176 +#: src/main.c:190 msgid "To list different options, use commands with " msgstr "To list different options, use commands with " -#: src/main.c:177 +#: src/main.c:191 msgid "Here is a list of available global options:" msgstr "Here is a list of available global options:" -#: src/main.c:180 src/main.c:181 src/cmd/info.c:54 src/cmd/info.c:55 +#: src/main.c:194 src/main.c:195 src/cmd/info.c:54 src/cmd/info.c:55 #: src/cmd/info.c:56 src/cmd/install.c:34 src/cmd/install.c:35 #: src/cmd/install.c:36 src/cmd/install.c:37 src/cmd/install.c:38 #: src/cmd/list.c:24 src/cmd/list.c:25 src/cmd/remove.c:28 src/cmd/remove.c:29 @@ -151,7 +155,7 @@ msgstr "Here is a list of available global options:" msgid " " msgstr " " -#: src/main.c:183 +#: src/main.c:197 msgid "" "Licensed under GPLv3, see https://www.gnu.org/licenses/ for more information" msgstr "" diff --git a/src/cmd.h b/src/cmd.h index 4af9ada..42842ea 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -10,6 +10,7 @@ typedef bool (*cmd_func_t)(lm_ctx_t *ctx, config_t *config, args_t *args); typedef struct cmd { char *name; char *desc; + bool root; cmd_func_t func; } cmd_t; diff --git a/src/main.c b/src/main.c index af45bdf..1c54d08 100644 --- a/src/main.c +++ b/src/main.c @@ -60,19 +60,20 @@ int main(int argc, char *argv[], char *envp[]) { textdomain("matt"); cmd_t commands[] = { - {.name = "info", .desc = "show information about a single package", .func = cmd_info }, - {.name = "list", .desc = "list all the installed packages", .func = cmd_list }, - {.name = "sync", .desc = "update the pool info and package lists", .func = cmd_sync }, - {.name = "install", .desc = "install package(s) from remote pools", .func = cmd_install}, - {.name = "remove", .desc = "remove installed package(s)", .func = cmd_remove }, - {.name = "update", .desc = "update installed package(s)", .func = cmd_update }, + {.name = "info", .desc = "show information about a single package", .root = false, .func = cmd_info }, + {.name = "list", .desc = "list all the installed packages", .root = false, .func = cmd_list }, + {.name = "sync", .desc = "update the pool info and package lists", .root = true, .func = cmd_sync }, + {.name = "install", .desc = "install package(s) from remote pools", .root = true, .func = cmd_install}, + {.name = "remove", .desc = "remove installed package(s)", .root = true, .func = cmd_remove }, + {.name = "update", .desc = "update installed package(s)", .root = true, .func = cmd_update }, }; - char *full_datadir = NULL, *full_tmpdir = NULL, *full_pooldir = NULL; - char *config_file = NULL, *root_dir = NULL; - bool ret = false, r = false; - args_t *args = NULL; - config_t config; + char *full_datadir = NULL, *full_tmpdir = NULL, *full_pooldir = NULL; + bool ret = false, r = false, use_root = false; + char *config_file = NULL, *root_dir = NULL; + cmd_func_t cmd = NULL; + args_t *args = NULL; + config_t config; set_env(envp); lm_ctx_init(&ctx); @@ -87,6 +88,33 @@ int main(int argc, char *argv[], char *envp[]) { if (!config_load(&ctx, &config, config_file)) goto end; + for (int i = 0; i < sizeof(commands) / sizeof(cmd_t); i++) { + if (!eq(commands[i].name, args->list[0].value)) + continue; + + use_root = commands[i].root; + + if (!use_root) + goto skip_lock; + + switch (lock(&ctx)) { + case ALREADY_LOCKED: + error(_("Failed to lock, matt is already running")); + goto end; + + case LOCK_ERROR: + error(_("Failed to lock, are you root?")); + goto end; + + default: + break; + } + + skip_lock: + cmd = commands[i].func; + break; + } + if (args->count <= 0 || args->list[0].name != NULL) goto help; @@ -96,7 +124,7 @@ int main(int argc, char *argv[], char *envp[]) { full_datadir = join_alloc(root_dir, config.datadir); full_tmpdir = join_alloc(root_dir, config.tmpdir); - if (!lm_ctx_new(&ctx, root_dir, full_tmpdir, full_datadir)) { + if (!lm_ctx_new(&ctx, use_root ? root_dir : NULL, full_tmpdir, full_datadir)) { switch (lm_error()) { case LM_ERR_CtxRootFail: error(_("Failed to access the root directory, are you running as root?")); @@ -140,29 +168,15 @@ int main(int argc, char *argv[], char *envp[]) { pool = pool->next; } - for (int i = 0; i < sizeof(commands) / sizeof(cmd_t); i++) { - if (!eq(commands[i].name, args->list[0].value)) - continue; - - switch (lock(&ctx)) { - case ALREADY_LOCKED: - error(_("Failed to lock, matt is already running")); - goto end; - - case LOCK_ERROR: - error(_("Failed to lock, are you root?")); - goto end; - - default: - break; - } - - ret = commands[i].func(&ctx, &config, args); + if (NULL != cmd) { + ret = cmd(&ctx, &config, args); unlock(&ctx); goto end; } error(_("Command not found: " FG_BOLD "%s"), args->list[0].value); + info(_("Displaying help information")); + help: info(_("MatterLinux package manager %s (libmp %s)"), VERSION, LM_VERSION); info(_("Usage: " FG_BOLD "%s [command] "), argv[0]);