From 4bb4496bdc8731c0ee1ac6bb7a5395a0badd7e6d Mon Sep 17 00:00:00 2001 From: ngn Date: Thu, 1 Aug 2024 02:02:13 +0300 Subject: [PATCH] new: locale implementation and signal handler --- locale/tr/LC_MESSAGES/pooler.po | 41 ++++++++++++++++++++++----------- src/lock.c | 28 ++++++++++++++++++++++ src/lock.h | 11 +++++++++ src/main.c | 41 ++++++++++++++++++++++++++++++--- 4 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 src/lock.c create mode 100644 src/lock.h diff --git a/locale/tr/LC_MESSAGES/pooler.po b/locale/tr/LC_MESSAGES/pooler.po index 0e0e751..308e579 100644 --- a/locale/tr/LC_MESSAGES/pooler.po +++ b/locale/tr/LC_MESSAGES/pooler.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-31 23:21+0300\n" +"POT-Creation-Date: 2024-08-01 01:59+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -72,56 +72,69 @@ msgstr "" msgid "Please answer with y/n" msgstr "" -#: src/main.c:74 +#: src/main.c:48 +msgid "Received a segfault" +msgstr "" + +#: src/main.c:93 msgid "Failed to access the root directory, are you running as root?" msgstr "" -#: src/main.c:76 +#: src/main.c:95 #, fuzzy, c-format msgid "Bad root directory (%s): %s" msgstr "Havuz dizini kullanmak başarısız oldu: %s" -#: src/main.c:84 +#: src/main.c:103 #, c-format msgid "Bad datadir (%s): %s" msgstr "" -#: src/main.c:89 +#: src/main.c:108 #, c-format msgid "Bad tmpdir (%s): %s" msgstr "" -#: src/main.c:96 +#: src/main.c:115 #, fuzzy msgid "Failed to add pool " msgstr "Havuzları sekronize etmek başarısız oldu: %s" -#: src/main.c:109 +#: src/main.c:127 +msgid "Failed to lock, matt is already running" +msgstr "" + +#: src/main.c:131 +#, fuzzy +msgid "Failed to lock, are you root?" +msgstr "Havuzları sekronize etmek başarısız oldu: %s" + +#: src/main.c:142 msgid "Command not found: " msgstr "" -#: src/main.c:111 +#: src/main.c:144 #, c-format msgid "MatterLinux package manager (version %s)" msgstr "" -#: src/main.c:112 +#: src/main.c:145 msgid "Usage: " msgstr "" -#: src/main.c:114 +#: src/main.c:147 msgid "Here is a list of available commands:" msgstr "" -#: src/main.c:120 +#: src/main.c:153 msgid "To list different options, use commands with " msgstr "" -#: src/main.c:121 +#: src/main.c:154 msgid "Here is a list of available global options:" msgstr "" -#: src/main.c:124 src/main.c:125 src/cmd/info.c:42 src/cmd/info.c:43 +#: src/main.c:157 src/main.c:158 src/cmd/info.c:42 src/cmd/info.c:43 #: src/cmd/info.c:44 src/cmd/install.c:25 src/cmd/install.c:26 #: src/cmd/list.c:13 src/cmd/list.c:14 src/cmd/remove.c:20 src/cmd/remove.c:21 #: src/cmd/update.c:29 @@ -129,7 +142,7 @@ msgstr "" msgid " " msgstr "" -#: src/main.c:127 +#: src/main.c:160 msgid "" "Licensed under GPLv3, see https://www.gnu.org/licenses/ for more information" msgstr "" diff --git a/src/lock.c b/src/lock.c new file mode 100644 index 0000000..12167c7 --- /dev/null +++ b/src/lock.c @@ -0,0 +1,28 @@ +#include "lock.h" +#include +#include +#include +#include + +lock_st lock(lm_ctx_t *ctx) { + if (NULL == ctx->data) + return LOCK_ERROR; + + char lock_file[strlen(ctx->data) + 10]; + join(lock_file, ctx->data, "lock"); + + if (exists(lock_file)) + return ALREADY_LOCKED; + if (creat(lock_file, 0600) == -1) + return LOCK_ERROR; + return LOCK_SUCCESS; +} + +void unlock(lm_ctx_t *ctx) { + if (NULL == ctx->data) + return; + + char lock_file[strlen(ctx->data) + 10]; + join(lock_file, ctx->data, "lock"); + unlink(lock_file); +} diff --git a/src/lock.h b/src/lock.h new file mode 100644 index 0000000..abb0e6b --- /dev/null +++ b/src/lock.h @@ -0,0 +1,11 @@ +#pragma once +#include + +typedef enum lock_state { + ALREADY_LOCKED = 1, + LOCK_SUCCESS = 2, + LOCK_ERROR = 3, +} lock_st; + +lock_st lock(lm_ctx_t *ctx); +void unlock(lm_ctx_t *ctx); diff --git a/src/main.c b/src/main.c index ccc3ead..631405d 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include @@ -33,10 +35,28 @@ #include "cmd.h" #include "config.h" #include "intl.h" +#include "lock.h" #include "log.h" #include "util.h" +lm_ctx_t ctx; + +void handler(int sig) { + unlock(&ctx); + printf("\e[?25h\n"); + if (SIGSEGV == sig) + error(_("Received a segfault")); + exit(1); +} + int main(int argc, char *argv[], char *envp[]) { + signal(SIGSEGV, handler); + signal(SIGINT, handler); + + setbuf(stdout, NULL); + setlocale(LC_ALL, ""); + 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 }, @@ -51,7 +71,6 @@ int main(int argc, char *argv[], char *envp[]) { args_t *args = NULL; bool ret = false; config_t config; - lm_ctx_t ctx; set_env(envp); lm_ctx_init(&ctx); @@ -100,10 +119,24 @@ int main(int argc, char *argv[], char *envp[]) { } for (int i = 0; i < sizeof(commands) / sizeof(cmd_t); i++) { - if (eq(commands[i].name, args->list[0].value)) { - ret = commands[i].func(&ctx, &config, args); + 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); + goto end; } error(_("Command not found: " FG_BOLD "%s"), args->list[0].value); @@ -130,6 +163,8 @@ help: goto end; end: + unlock(&ctx); + free(full_datadir); free(full_tmpdir);