new: ctx package list functions

This commit is contained in:
ngn 2024-08-04 18:59:30 +03:00
parent bb1f5a61b2
commit 73a1f997e6
5 changed files with 98 additions and 14 deletions

View File

@ -14,6 +14,13 @@ typedef struct lm_ctx {
const char *version; // libmp version (read-only)
} lm_ctx_t;
typedef struct lm_ctx_list {
lm_pool_t *head;
lm_pool_t *pool;
lm_pkg_t *pkg;
ssize_t count;
} lm_ctx_list_t;
typedef struct lm_ctx_resolve_list {
lm_pkg_t *resolving;
lm_pkg_t *packages;
@ -62,6 +69,10 @@ void lm_ctx_free(lm_ctx_t *ctx);
/* ####################
## main fucntions ##
#################### */
lm_ctx_list_t *lm_ctx_list(lm_ctx_t *ctx, lm_ctx_list_t *list); // returns the state of package list
lm_pkg_t *lm_ctx_list_next(lm_ctx_list_t *list); // returns the next package in the list
void lm_ctx_list_free(lm_ctx_list_t *list); // frees the package list state
lm_ctx_resolve_list_t *lm_ctx_resolve(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_resolve_list_t *list); // resolves a package and returns a list of packages
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list); // returns the next package in the list

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-04 15:24+0300\n"
"POT-Creation-Date: 2024-08-04 18:59+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"

View File

@ -102,5 +102,6 @@ void lm_ctx_free(lm_ctx_t *ctx) {
lm_database_free(ctx->db);
lm_error_clear();
bzero(ctx, sizeof(lm_ctx_t));
return;
}

60
src/ctx/list.c Normal file
View File

@ -0,0 +1,60 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/ctx.h"
#include <strings.h>
lm_ctx_list_t *lm_ctx_list(lm_ctx_t *ctx, lm_ctx_list_t *list){
if(NULL == ctx || NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
list->pool = ctx->pools;
list->head = ctx->pools;
list->pkg = NULL;
list->count = 0;
while(NULL != list->pool){
list->pkg = list->pool->pkg;
while(NULL != list->pkg){
list->count++;
list->pkg = list->pkg->next;
}
list->pool = list->pool->next;
}
list->pool = NULL;
list->pkg = NULL;
return list;
}
lm_pkg_t *lm_ctx_list_next(lm_ctx_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
if(NULL == list->pool || NULL == list->pkg){
if((list->pool = list->head) != NULL)
list->pkg = list->pool->pkg;
return list->pkg;
}
if((list->pkg = list->pkg->next) && NULL == list->pkg){
if((list->pool = list->pool->next) != NULL)
list->pkg = list->pool->pkg;
return list->pkg;
}
return list->pkg;
}
void lm_ctx_list_free(lm_ctx_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}
bzero(list, sizeof(lm_ctx_list_t));
}

View File

@ -57,12 +57,12 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
}
if(!do_update && !lm_pool_info_load(cur)){
pdebug(__func__, "(%s) failed to load info: %s", cur->name, lm_strerror());
pdebug(__func__, "(%s) failed to load info: %s", cur->name, lm_strerror());
goto next_info;
}
else if(do_update && !lm_pool_info_download(cur, __lm_ctx_sync_callback, &cbdata)) {
pdebug(__func__, "(%s) failed to update info: %s", cur->name, lm_strerror());
pdebug(__func__, "(%s) failed to update info: %s", cur->name, lm_strerror());
goto next_info;
}
@ -70,10 +70,15 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
status = true;
next_info:
if(NULL != callback && status)
callback(ctx, cur, SYNC_INFO_SUCCESS, 0, 0, data);
else if(NULL != callback && !status)
callback(ctx, cur, SYNC_INFO_FAIL, 0, 0, data);
if(NULL != callback && status){
if(!callback(ctx, cur, SYNC_INFO_SUCCESS, 0, 0, data))
goto fail;
}
else if(NULL != callback && !status){
if(!callback(ctx, cur, SYNC_INFO_FAIL, 0, 0, data))
goto fail;
}
if(status)
cur->loaded = true;
@ -107,12 +112,12 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
}
if(!do_update && !lm_pool_list_load(cur, tempdir)){
pdebug(__func__, "(%s) failed to load list: %s", cur->name, lm_strerror());
pdebug(__func__, "(%s) failed to load list: %s", cur->name, lm_strerror());
goto next_list;
}
else if(do_update && !lm_pool_list_download(cur, tempdir, __lm_ctx_sync_callback, &cbdata)) {
pdebug(__func__, "(%s) failed to update list: %s", cur->name, lm_strerror());
pdebug(__func__, "(%s) failed to update list: %s", cur->name, lm_strerror());
goto next_list;
}
@ -123,19 +128,26 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
free(tempdir);
tempdir = NULL;
if(NULL != callback && status)
callback(ctx, cur, SYNC_LIST_SUCCESS, 0, 0, data);
else if(NULL != callback && !status)
callback(ctx, cur, SYNC_LIST_FAIL, 0, 0, data);
if(NULL != callback && status){
if(!callback(ctx, cur, SYNC_LIST_SUCCESS, 0, 0, data))
goto fail;
}
else if(NULL != callback && !status){
if(!callback(ctx, cur, SYNC_LIST_FAIL, 0, 0, data))
goto fail;
}
if(!status)
cur->loaded = false;
else
loaded_count++;
status = false;
cur = cur->next;
}
return loaded_count;
fail:
return -1;
}