diff --git a/install/config.ini b/install/config.ini index 3f3446b..1d135bc 100644 --- a/install/config.ini +++ b/install/config.ini @@ -1,9 +1,9 @@ # default pooler configuration, please see the wiki page for more information # https://matterlinux.xyz/wiki/pooler -# log = /var/lib/pooler/pooler.log -dir = /var/lib/pooler -addr = 0.0.0.0:5858 +addr = 0.0.0.0:5858 +threads = 10 # [pool-name] # host = pool.example.com +# dir = /srv/pools/pool-name diff --git a/locale/tr/LC_MESSAGES/pooler.po b/locale/tr/LC_MESSAGES/pooler.po index ef06480..c2e9775 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-20 01:23+0300\n" +"POT-Creation-Date: 2024-08-04 14:44+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,132 +17,139 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: src/config.c:120 +#: src/config.c:124 #, c-format msgid "Unknown configuration option: %s/%s" msgstr "" -#: src/config.c:128 +#: src/config.c:132 #, fuzzy, c-format msgid "Failed to access the configuration file: %s" msgstr "Kayıt dosyasını açmak başarısız oldu: %s" -#: src/config.c:133 +#: src/config.c:137 #, fuzzy msgid "Failed to parse the configuration" msgstr "Kayıt dosyasını açmak başarısız oldu: %s" -#: src/config.c:140 +#: src/config.c:144 #, fuzzy, c-format msgid "Hostname not specified for the pool: %s" msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s" -#: src/config.c:146 -msgid "Loaded the configuration" -msgstr "" - -#: src/main.c:58 +#: src/main.c:59 #, c-format msgid "%s: successfuly loaded the pool info" msgstr "%s: havuz bilgisi başarıyla yüklendi" -#: src/main.c:62 +#: src/main.c:63 #, c-format msgid "%s: failed to load the pool info (%s)" msgstr "%s: havuz bilgisini yüklemek başarısız oldu (%s)" -#: src/main.c:66 +#: src/main.c:67 #, c-format msgid "%s: successfuly loaded the package list" msgstr "%s: paket listesi başarıyla yüklendi" -#: src/main.c:70 +#: src/main.c:71 #, c-format msgid "%s: failed to load the package list (%s)" msgstr "%s: paket listesini yüklemek başarısız oldu (%s)" -#: src/main.c:88 +#: src/main.c:89 #, c-format msgid "Request from %s: PING (%s)" msgstr "" -#: src/main.c:92 +#: src/main.c:93 #, c-format msgid "Request from %s: INFO (%s)" msgstr "" -#: src/main.c:96 +#: src/main.c:97 #, c-format msgid "Request from %s: LIST (%s)" msgstr "" -#: src/main.c:100 +#: src/main.c:101 #, c-format msgid "Request from %s: PULL (%s)" msgstr "" -#: src/main.c:109 +#: src/main.c:110 msgid "Configuration file not specified" msgstr "Konfigürasyon dosyası belirtilmedi" -#: src/main.c:128 -#, c-format -msgid "Failed to open the log file: %s" +#: src/main.c:123 +msgid "Loaded the configuration" +msgstr "" + +#: src/main.c:126 +#, fuzzy +msgid "Failed to get the temp directory configuration option (tmpdir)" msgstr "Kayıt dosyasını açmak başarısız oldu: %s" -#: src/main.c:140 +#: src/main.c:134 msgid "Please specify a valid thread count (1-1000)" msgstr "Lütfen geçerli bir işlem sayısı belirtin (1-1000)" -#: src/main.c:145 -msgid "Pool directory not specified" -msgstr "Havuz dizini belirtilmemiş" - -#: src/main.c:150 -#, c-format -msgid "Cannot access to the pool directory: %s" -msgstr "Havuz dizinine erişilemiyor: %s" - -#: src/main.c:155 +#: src/main.c:139 msgid "Please specify at least one pool in the configuration" msgstr "Lütfen konfigürasyonda en az bir havuz belirtin" -#: src/main.c:160 -#, c-format -msgid "Failed to use pool directory: %s" -msgstr "Havuz dizini kullanmak başarısız oldu: %s" - -#: src/main.c:166 +#: src/main.c:145 #, c-format msgid "Hostname not specified for pool, skipping: %s" msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s" -#: src/main.c:174 +#: src/main.c:153 +#, fuzzy, c-format +msgid "Failed access the pool directory of %s: %s" +msgstr "Havuz dizinine erişilemiyor: %s" + +#: src/main.c:158 #, c-format msgid "Failed to add pool to the list: %s" msgstr "Havuzu listeye eklemek başarısız oldu: %s" -#: src/main.c:182 +#: src/main.c:162 +#, fuzzy, c-format +msgid "%s: loaded the pool" +msgstr "%s: havuz bilgisi başarıyla yüklendi" + +#: src/main.c:167 #, c-format msgid "Failed to sync the pools: %s" msgstr "Havuzları sekronize etmek başarısız oldu: %s" -#: src/main.c:187 +#: src/main.c:172 #, fuzzy msgid "None of the pools is available for serving" msgstr "Havuzların hiçbiri sunmak için müsait değil" -#: src/main.c:191 +#: src/main.c:176 #, c-format msgid "Serving %lu pool on %s" msgstr "%lu tane havuz %s adresinde sunuluyor" -#: src/main.c:191 +#: src/main.c:176 #, c-format msgid "Serving %lu pools on %s" msgstr "%lu tane havuz %s adresinde sunuluyor" -#: src/main.c:194 +#: src/main.c:179 #, c-format msgid "Failed to start the server: %s" msgstr "Sunucuyu başlatmak başarısız oldu: %s" + +#, c-format +#~ msgid "Failed to open the log file: %s" +#~ msgstr "Kayıt dosyasını açmak başarısız oldu: %s" + +#~ msgid "Pool directory not specified" +#~ msgstr "Havuz dizini belirtilmemiş" + +#, c-format +#~ msgid "Failed to use pool directory: %s" +#~ msgstr "Havuz dizini kullanmak başarısız oldu: %s" diff --git a/src/config.c b/src/config.c index d5d4546..707aa20 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -8,6 +7,7 @@ #include "config.h" #include "intl.h" #include "log.h" +#include "util.h" #define option_count() sizeof(config.options) / sizeof(config.options[0]) @@ -15,9 +15,8 @@ config_t config = { .options = { {.key = "threads", .type = TYPE_INTEGER, .def.integer = 10}, + {.key = "tmpdir", .type = TYPE_STRING, .def.string = "/tmp/pooler"}, {.key = "addr", .type = TYPE_STRING, .def.string = "0.0.0.0:5858"}, - {.key = "dir", .type = TYPE_STRING, .def.string = NULL}, - {.key = "log", .type = TYPE_STRING, .def.string = NULL}, }, .pools = NULL, }; @@ -111,6 +110,11 @@ int config_load_handler(void *data, const char *_section, const char *_key, cons config.pools->host = strdup(value); } + else if (eq(key, "dir")) { + free(config.pools->dir); + config.pools->dir = strdup(value); + } + else goto unknown; @@ -124,7 +128,7 @@ unknown: bool config_load(char *file) { config_options_clear(); - if (!exists(file) || !can_read(file)) { + if (!file_canread(file)) { error(_("Failed to access the configuration file: %s"), file); return false; } @@ -143,7 +147,6 @@ bool config_load(char *file) { pool = pool->next; } - info(_("Loaded the configuration")); return true; } diff --git a/src/config.h b/src/config.h index 1c2854d..89c71f3 100644 --- a/src/config.h +++ b/src/config.h @@ -23,6 +23,7 @@ typedef struct pool_config { struct pool_config *next; char *name; char *host; + char *dir; } pool_config_t; typedef struct config { diff --git a/src/intl.h b/src/intl.h index 32c3b3c..0ab7844 100644 --- a/src/intl.h +++ b/src/intl.h @@ -1,2 +1,4 @@ +#pragma once #include + #define _(x) gettext(x) diff --git a/src/log.h b/src/log.h index b0670fc..2471aa4 100644 --- a/src/log.h +++ b/src/log.h @@ -1,3 +1,4 @@ #pragma once + void error(char *fmt, ...); void info(char *fmt, ...); diff --git a/src/main.c b/src/main.c index 2a0b476..a7d80e7 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ #include "config.h" #include "intl.h" #include "log.h" +#include "util.h" void sockaddr_to_str(struct sockaddr *addr, char *str) { struct sockaddr_in *ipv4; @@ -110,94 +111,81 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - char *poolsdir = NULL, *addr = NULL, *logfile = NULL; + char *addr = NULL, *logfile = NULL, *tmpdir = NULL; int ret = EXIT_FAILURE; pool_config_t *pool = NULL; size_t pool_count = 0; lm_ctx_t ctx; - lm_ctx_init(&ctx); - if (!config_load(argv[1])) - goto end; + goto end_config; - if ((logfile = config_get_string("log")) != NULL) { - FILE *log = fopen(logfile, "a"); + info(_("Loaded the configuration")); - if (NULL == log) - error(_("Failed to open the log file: %s"), strerror(errno)); - - else { - dup2(fileno(log), STDERR_FILENO); - dup2(fileno(log), STDOUT_FILENO); - fclose(log); - } + if ((tmpdir = config_get_string("tmpdir")) == NULL) { + error(_("Failed to get the temp directory configuration option (tmpdir)")); + goto end_config; } + lm_ctx_init(&ctx, NULL, tmpdir, NULL); addr = config_get_string("addr"); if (config_get_integer("threads") <= 0 || config_get_integer("threads") > 1000) { error(_("Please specify a valid thread count (1-1000)")); - goto end; - } - - if ((poolsdir = config_get_string("dir")) == NULL) { - error(_("Pool directory not specified")); - goto end; - } - - if (!exists(poolsdir) && !can_read(poolsdir)) { - error(_("Cannot access to the pool directory: %s"), poolsdir); - goto end; + goto end_ctx; } if ((pool = config.pools) == NULL) { error(_("Please specify at least one pool in the configuration")); - goto end; - } - - if (!lm_ctx_set_data(&ctx, poolsdir)) { - error(_("Failed to use pool directory: %s"), lm_strerror()); - goto end; + goto end_ctx; } while (NULL != pool) { if (NULL == pool->host) { error(_("Hostname not specified for pool, skipping: %s"), pool->name); - goto end; + goto end_ctx; } char url[strlen(pool->host) + 20]; sprintf(url, "mptp://%s", pool->host); - if (NULL == lm_ctx_pool_add(&ctx, pool->name, url)) { - error(_("Failed to add pool to the list: %s"), lm_strerror()); - goto end; + if (!file_canread(pool->dir)) { + error(_("Failed access the pool directory of %s: %s"), pool->name, pool->dir); + goto end_ctx; } + if (NULL == lm_ctx_pool_add(&ctx, pool->name, url, pool->dir)) { + error(_("Failed to add pool to the list: %s"), lm_strerror()); + goto end_ctx; + } + + info(_("%s: loaded the pool"), pool->name); pool = pool->next; } if ((pool_count = lm_ctx_sync(&ctx, false, sync_callback, NULL)) < 0) { error(_("Failed to sync the pools: %s"), lm_strerror()); - goto end; + goto end_ctx; } if (pool_count == 0) { error(_("None of the pools is available for serving")); - goto end; + goto end_ctx; } info(pool_count == 1 ? _("Serving %lu pool on %s") : _("Serving %lu pools on %s"), pool_count, addr); if (!lm_ctx_serve(&ctx, addr, config_get_integer("threads"), serve_callback, NULL)) { error(_("Failed to start the server: %s"), lm_strerror()); - goto end; + goto end_ctx; } ret = EXIT_SUCCESS; -end: + +end_ctx: lm_ctx_free(&ctx); + +end_config: config_free(); return ret; } diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..4178484 --- /dev/null +++ b/src/util.c @@ -0,0 +1,19 @@ +#include "util.h" + +#include +#include +#include + +bool file_canread(char *p) { + return access(p, R_OK) == 0; +} + +bool eq(char *s1, char *s2) { + if (NULL == s1 || NULL == s2) + return false; + + if (strlen(s1) != strlen(s2)) + return false; + + return strcmp(s1, s2) == 0; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..64c2fc9 --- /dev/null +++ b/src/util.h @@ -0,0 +1,5 @@ +#pragma once +#include + +bool file_canread(char *p); +bool eq(char *s1, char *s2);