update: function cleanups, better way to handle package paths

This commit is contained in:
ngn 2024-06-28 23:09:24 +03:00
parent ed52710355
commit 5513abb371
27 changed files with 203 additions and 156 deletions

View File

@ -21,17 +21,14 @@ int main(int argc, char *argv[]) {
goto end;
}
if (!lm_ctx_pools_add(&ctx, "test", argv[1])) {
if (lm_ctx_pools_add(&ctx, "test", argv[1]) == NULL) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
lm_ctx_pools_test(&ctx);
if (!lm_ctx_pools_load(&ctx, true, NULL, NULL)) {
printf("failed to load pools: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
lm_ctx_pools_test(&ctx, NULL, NULL);
lm_ctx_pools_get_info(&ctx, true, NULL, NULL);
lm_ctx_pools_get_list(&ctx, true, NULL, NULL);
ret = EXIT_SUCCESS;

View File

@ -3,6 +3,8 @@
#include <stdlib.h>
#include <strings.h>
#define DATA_DIR "./examples"
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
@ -11,25 +13,19 @@ int main(int argc, char *argv[]) {
return ret;
}
lm_ctx_t ctx;
lm_pool_t *pool;
lm_ctx_t ctx;
lm_ctx_init(&ctx);
if ((pool = lm_ctx_pools_add(&ctx, "test", "mptp://127.0.0.1:5858")) == NULL) {
if (!lm_ctx_set_data(&ctx, DATA_DIR)) {
printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (lm_ctx_pools_add(&ctx, "test", "mptp://127.0.0.1:5858") == NULL) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_pool_info_load(pool, "./examples/tests/INFO")) {
printf("failed to load pool info: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_pool_list_load(pool, "./examples/tests/LIST")) {
printf("failed to load pool list: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_pools_serve(&ctx, argv[1], 10)) {
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
goto end;

View File

@ -13,6 +13,7 @@ void lm_ctx_free(lm_ctx_t *ctx);
lm_pool_t *lm_ctx_pools_add(lm_ctx_t *ctx, char *name, char *url);
bool lm_ctx_pools_del(lm_ctx_t *ctx, char *name);
void lm_ctx_pools_clear(lm_ctx_t *ctx);
void lm_ctx_pools_test(lm_ctx_t *ctx);
bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads);
bool lm_ctx_pools_load(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data);
void lm_ctx_pools_test(lm_ctx_t *ctx, lm_ctx_pools_callback_t callback, void *data);
void lm_ctx_pools_get_info(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data);
void lm_ctx_pools_get_list(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data);

View File

@ -58,6 +58,7 @@ typedef enum lm_error {
LM_ERR_CtxDataNoWrite = 53,
LM_ERR_CtxDataFailMkdir = 54,
LM_ERR_ArcRealpathFail = 55,
LM_ERR_PoolTestNotPong = 56,
} lm_error_t;
typedef struct lm_error_desc {

View File

@ -23,14 +23,15 @@ bool lm_pool_add(lm_pool_t *pool, lm_pkg_t *pkg);
void lm_pool_paths_set_info(lm_pool_t *pool, char *info_path);
void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path);
bool lm_pool_paths_is_empty(lm_pool_t *pool);
void lm_pool_paths_free(lm_pool_t *pool);
bool lm_pool_info_load(lm_pool_t *pool, char *file);
bool lm_pool_info_get(lm_pool_t *pool, char *file);
bool lm_pool_info_load(lm_pool_t *pool);
bool lm_pool_info_get(lm_pool_t *pool);
void lm_pool_info_free(lm_pool_t *pool);
bool lm_pool_list_load(lm_pool_t *pool, char *file);
bool lm_pool_list_get(lm_pool_t *pool, char *file);
bool lm_pool_list_load(lm_pool_t *pool);
bool lm_pool_list_get(lm_pool_t *pool);
void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr);
void lm_pool_serve_thread(void *arg);

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-28 21:45+0300\n"
"POT-Creation-Date: 2024-06-28 23:08+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -251,3 +251,7 @@ msgstr ""
#: src/error.c:69
msgid "failed to create specified data directory"
msgstr ""
#: src/error.c:70
msgid "pool did not respond ping with pong"
msgstr ""

View File

@ -2,15 +2,11 @@
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <sys/stat.h>
#include <libintl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
void lm_ctx_init(lm_ctx_t *ctx) {
setlocale(LC_ALL, "");
@ -66,14 +62,6 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
return false;
}
char poolsdir[strlen(dir)+10];
snprintf(poolsdir, sizeof(poolsdir), "%s/pools", dir);
if(!mkdir_ifnot(poolsdir)){
lm_error_set(LM_ERR_CtxDataFailMkdir);
return false;
}
if(!is_dir(dir)){
lm_error_set(LM_ERR_CtxDataNotDir);
return false;
@ -84,6 +72,14 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
return false;
}
char poolsdir[strlen(dir)+10];
snprintf(poolsdir, sizeof(poolsdir), "%s/pools", dir);
if(!mkdir_ifnot(poolsdir)){
lm_error_set(LM_ERR_CtxDataFailMkdir);
return false;
}
ctx->data = strdup(dir);
return true;
}

View File

@ -4,14 +4,14 @@
#include "../../include/error.h"
#include "../../include/thpool.h"
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
lm_pool_t *lm_ctx_pools_add(lm_ctx_t *ctx, char *name, char *url) {
if(NULL == name)
return NULL;
lm_pool_t *pool = lm_pool_new(name, url);
if (NULL == pool)
return false;
@ -19,6 +19,26 @@ lm_pool_t *lm_ctx_pools_add(lm_ctx_t *ctx, char *name, char *url) {
pdebug(__func__, "pool name is %s", pool->name);
pdebug(__func__, "pool URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
if(NULL != ctx->data){
char poolp[strlen(ctx->data) + strlen(pool->name) + 20];
snprintf(poolp, sizeof(poolp), "%s/pools/%s", ctx->data, pool->name);
size_t poolp_sz = strlen(poolp);
if(!mkdir_ifnot(poolp)){
lm_error_set(LM_ERR_CtxDataFailMkdir);
return false;
}
char infop[poolp_sz + 10];
sprintf(infop, "%s/INFO", poolp);
char listp[poolp_sz + 10];
sprintf(listp, "%s/LIST", poolp);
lm_pool_paths_set_info(pool, infop);
lm_pool_paths_set_list(pool, listp);
}
if (NULL == ctx->pools) {
ctx->pools = pool;
return pool;
@ -48,22 +68,17 @@ void lm_ctx_pools_clear(lm_ctx_t *ctx) {
ctx->pools = NULL;
}
void lm_ctx_pools_test(lm_ctx_t *ctx) {
void lm_ctx_pools_test(lm_ctx_t *ctx, lm_ctx_pools_callback_t callback, void *data) {
lm_pool_t *cur = ctx->pools;
while (NULL != cur) {
lm_pool_test(cur);
if (!cur->available)
pdebug(__func__, "%s is not avaliable: %s", cur->name, lm_strerror());
cur = cur->next;
}
}
void lm_ctx_pools_get_info(lm_ctx_t *ctx) {
lm_pool_t *cur = ctx->pools;
while (NULL != cur) {
lm_pool_test(cur);
if (!cur->available)
pdebug(__func__, "%s is not avaliable: %s", cur->name, lm_strerror());
if(NULL != callback)
callback(ctx, cur, cur->available, data);
cur = cur->next;
}
}
@ -88,83 +103,70 @@ lm_pool_t *lm_ctx_pools_by_url(lm_ctx_t *ctx, char *host, char *path) {
return NULL;
}
bool lm_ctx_pool_load(lm_ctx_t *ctx, lm_pool_t *pool, bool force_update){
if(NULL == ctx->data){
printf("hello chat\n");
lm_error_set(LM_ERR_CtxDataNULL);
return false;
}
char poolp[strlen(ctx->data) + strlen(pool->name) + 20];
snprintf(poolp, sizeof(poolp), "%s/pools/%s", ctx->data, pool->name);
size_t poolp_sz = strlen(poolp);
if(!mkdir_ifnot(poolp)){
lm_error_set(LM_ERR_CtxDataFailMkdir);
return false;
}
char infop[poolp_sz + 10];
sprintf(infop, "%s/INFO", poolp);
char listp[poolp_sz + 10];
sprintf(listp, "%s/LIST", poolp);
if(force_update)
goto update;
if(!lm_pool_info_load(pool, infop)){
pdebug(__func__, "failed to load info for %s gonna try updating", pool->name);
goto update;
}
if(!lm_pool_info_load(pool, listp)){
pdebug(__func__, "failed to load list for %s gonna try updating", pool->name);
goto update;
}
update:
unlink(infop);
unlink(listp);
if(!pool->available){
pdebug(__func__, "failed get info and list for %s, pool is not avaliable");
return false;
}
if(!lm_pool_info_get(pool, infop)){
pdebug(__func__, "failed to get info for %s", pool->name);
return false;
}
if(!lm_pool_list_get(pool, listp)){
pdebug(__func__, "failed to get list for %s", pool->name);
return false;
}
return true;
}
bool lm_ctx_pools_load(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data){
if(NULL == ctx){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
void lm_ctx_pools_get_info(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data){
lm_pool_t *cur = ctx->pools;
bool status = false;
while(NULL != cur){
bool status = lm_ctx_pool_load(ctx, cur, force_update);
if(lm_pool_paths_is_empty(cur)){
pdebug(__func__, "(%s) failed to load info, pool paths are empty", cur->name);
goto next;
}
if(!status)
pdebug(__func__, "failed to load pool: %s", cur->name);
if(force_update)
goto update;
if(!lm_pool_info_load(cur))
pdebug(__func__, "(%s) failed to load info, gonna try updating", cur->name);
update:
if(!lm_pool_info_get(cur)){
pdebug(__func__, "(%s) failed to update info", cur->name);
goto next;
}
pdebug(__func__, "(%s) loaded pool info", cur->name);
status = true;
next:
if(NULL != callback)
callback(ctx, cur, status, data);
status = false;
cur = cur->next;
}
}
return true;
void lm_ctx_pools_get_list(lm_ctx_t *ctx, bool force_update, lm_ctx_pools_callback_t callback, void *data){
lm_pool_t *cur = ctx->pools;
bool status = false;
while(NULL != cur){
if(lm_pool_paths_is_empty(cur)){
pdebug(__func__, "(%s) failed to load list, pool paths are empty", cur->name);
goto next;
}
if(force_update)
goto update;
if(!lm_pool_list_load(cur))
pdebug(__func__, "(%s) failed to load list, gonna try updating", cur->name);
update:
if(!lm_pool_list_get(cur)){
pdebug(__func__, "(%s) failed to update list", cur->name);
goto next;
}
pdebug(__func__, "(%s) loaded pool list", cur->name);
status = true;
next:
if(NULL != callback)
callback(ctx, cur, status, data);
status = false;
cur = cur->next;
}
}
bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) {
@ -221,6 +223,13 @@ bool lm_ctx_pools_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) {
continue;
}
if(lm_pool_paths_is_empty(pool)){
pdebug(__func__, "requested pool (%s) have empty paths, closing connection", pool->name);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr);
continue;
}
lm_pool_thread_arg_t *arg = malloc(sizeof(lm_pool_thread_arg_t));
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));

View File

@ -67,6 +67,7 @@ char *lm_strerror() {
{.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") },
{.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") },
{.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") },
{.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") },
};
for (int i = 0; i < sizeof(errors) / sizeof(lm_error_desc_t); i++) {

View File

@ -2,7 +2,6 @@
#include "../../include/mptp.h"
#include "../../include/util.h"
#include <arpa/inet.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>

View File

@ -2,7 +2,6 @@
#include "../../include/mptp.h"
#include "../../include/util.h"
#include <arpa/inet.h>
#include <stdbool.h>
#include <string.h>

View File

@ -1,19 +1,21 @@
#include "../../include/mptp.h"
#include "../../include/util.h"
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
if (NULL == path)
return false;
lm_mptp_t packet;
bool ret = false;
if (NULL == path) {
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
FILE *file = fopen(path, "r");
size_t read = 0;
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
@ -36,13 +38,27 @@ end:
}
bool lm_mptp_recvfile(int sock, char *path){
if(NULL == path)
return false;
if(unlink(path) < 0 && errno != ENOENT)
return false;
FILE *file = fopen(path, "a");
bool ret = false;
lm_mptp_t packet;
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
goto end;
}
while(lm_mptp_client_recv(sock, &packet)){
if(!lm_mptp_client_verify(&packet))
goto end;
if(MPTP_FLAGS_CODE(&packet) != MPTP_S2C_COOL)
goto end;
if(MPTP_IS_LAST(&packet))
break;

View File

@ -2,10 +2,10 @@
#include "../../include/util.h"
#include "../../include/pkg.h"
#include <ini.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ini.h>
size_t lm_pkg_data_depend_count(lm_pkg_t *pkg){
size_t index = 0;

View File

@ -3,13 +3,10 @@
#include "../../include/mptp.h"
#include "../../include/util.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ini.h>
#include <unistd.h>
int lm_pool_info_handler(void *data, const char *_section, const char *_key, const char *_value) {
char *section = (char *)_section, *value = (char *)_value, *key = (char *)_key;
@ -30,27 +27,42 @@ int lm_pool_info_handler(void *data, const char *_section, const char *_key, con
return 1;
}
bool lm_pool_info_load(lm_pool_t *pool, char *file) {
bool lm_pool_info_load(lm_pool_t *pool) {
if(NULL == pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
lm_pool_info_free(pool);
if(!can_read(file)){
if(NULL == pool->paths.info)
return false;
if(!can_read(pool->paths.info)){
lm_error_set(LM_ERR_PoolInfoCantRead);
return false;
}
if (ini_parse(file, lm_pool_info_handler, pool) < 0) {
if (ini_parse(pool->paths.info, lm_pool_info_handler, pool) < 0) {
lm_error_set(LM_ERR_PoolInfoBad);
return false;
}
pool->paths.info = strdup(file);
return true;
}
bool lm_pool_info_get(lm_pool_t *pool, char *file) {
bool lm_pool_info_get(lm_pool_t *pool) {
if(NULL == pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(!pool->available)
return false;
if(NULL == pool->paths.info)
return false;
if(NULL == pool->url.host)
return false;
@ -67,15 +79,15 @@ bool lm_pool_info_get(lm_pool_t *pool, char *file) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, file))
if(!lm_mptp_recvfile(sock, pool->paths.info))
goto end;
ret = true;
end:
lm_mptp_close(sock);
if(ret)
ret = lm_pool_info_load(pool, file);
ret = lm_pool_info_load(pool);
return ret;
}

View File

@ -4,29 +4,32 @@
#include "../../include/util.h"
#include "../../include/pkg.h"
#include <stdbool.h>
#include <dirent.h>
#include <libgen.h>
#include <stdbool.h>
#include <string.h>
bool lm_pool_list_load(lm_pool_t *pool, char *file){
if(NULL == pool || NULL == file){
bool lm_pool_list_load(lm_pool_t *pool){
if(NULL == pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(!can_read(file)){
if(NULL == pool->paths.list)
return false;
if(!can_read(pool->paths.list)){
lm_error_set(LM_ERR_PoolListCantRead);
return false;
}
size_t file_len = strlen(file);
size_t file_len = strlen(pool->paths.list);
char file_copy[file_len + 1];
char file_copy2[file_len + 1];
char *list_dir = NULL, *list_name = NULL;
memcpy(file_copy, file, file_len+1);
memcpy(file_copy2, file, file_len+1);
memcpy(file_copy, pool->paths.list, file_len+1);
memcpy(file_copy2, pool->paths.list, file_len+1);
list_dir = dirname(file_copy);
list_name = basename(file_copy2);
@ -46,7 +49,7 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
DIR *dirfd = NULL;
bool ret = false;
if(!extract_archive(extract_dir, file))
if(!extract_archive(extract_dir, pool->paths.list))
goto end;
if((dirfd = opendir(extract_dir)) == NULL){
@ -87,7 +90,15 @@ end:
return ret;
}
bool lm_pool_list_get(lm_pool_t *pool, char *file) {
bool lm_pool_list_get(lm_pool_t *pool) {
if(NULL == pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pool->paths.list)
return false;
if(!pool->available)
return false;
@ -108,13 +119,13 @@ bool lm_pool_list_get(lm_pool_t *pool, char *file) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, file))
if(!lm_mptp_recvfile(sock, pool->paths.list))
goto end;
ret = true;
end:
lm_mptp_close(sock);
if(ret)
ret = lm_pool_list_load(pool, file);
ret = lm_pool_list_load(pool);
return ret;
}

View File

@ -1,7 +1,9 @@
#include "../../include/types.h"
#include "../../include/pool.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void lm_pool_paths_set_info(lm_pool_t *pool, char *info_path){
free(pool->paths.info);
@ -21,6 +23,10 @@ void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path){
pool->paths.list = strdup(list_path);
}
bool lm_pool_paths_is_empty(lm_pool_t *pool){
return NULL == pool->paths.info || NULL == pool->paths.list;
}
void lm_pool_paths_free(lm_pool_t *pool){
free(pool->paths.info);
free(pool->paths.list);

View File

@ -56,6 +56,8 @@ void lm_pool_test(lm_pool_t *pool) {
}
pool->available = MPTP_FLAGS_CODE(&packet) == MPTP_S2C_PONG;
if(!pool->available)
lm_error_set(LM_ERR_PoolTestNotPong);
end:
lm_mptp_close(sock);
return;

View File

@ -28,7 +28,7 @@ void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr
// when LIST file is requested, send the file
case MPTP_C2S_LIST:
pdebug(__func__, "(%s) LIST from %s attempting to send list", pool->name, ipaddr);
lm_mptp_sendfile(sock, addr, pool->paths.list);
lm_mptp_sendfile(sock, addr, pool->paths.list); break;
break;
}

View File

@ -1,6 +1,7 @@
#include "../include/url.h"
#include "../include/error.h"
#include "../include/util.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -5,16 +5,11 @@
#include <archive.h>
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
#include <linux/limits.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
void pdebug(const char *func, const char *fmt, ...) {
if (LM_DEBUG != 1)