update: handle actual package updates outside of the library

This commit is contained in:
ngn 2024-07-18 19:25:35 +03:00
parent 17d572add0
commit bc79c642ac
7 changed files with 60 additions and 52 deletions

View File

@ -8,9 +8,14 @@
int main(int argc, char *argv[]) {
lm_ctx_update_list_t *list = NULL;
int ret = EXIT_FAILURE;
lm_pkg_t pkg, *cur = NULL;
lm_pkg_t pkg, *old = NULL, *new = NULL;
lm_ctx_t ctx;
if (argc != 3) {
printf("usage: %s <pool name> <pool url>\n", argv[0]);
return ret;
}
if (!mkdir_ifnot(ROOT_DIR)) {
printf("failed to create root dir: %s\n", ROOT_DIR);
return ret;
@ -33,6 +38,14 @@ int main(int argc, char *argv[]) {
goto end;
}
if (lm_ctx_pool_add(&ctx, argv[1], argv[2]) == NULL) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
lm_ctx_ping(&ctx, NULL, NULL);
lm_ctx_sync(&ctx, true, NULL, NULL);
if ((list = lm_ctx_update_list(&ctx)) == NULL) {
printf("failed to get update list: %s (%d)", lm_strerror(), lm_error());
goto end;
@ -43,12 +56,28 @@ int main(int argc, char *argv[]) {
goto end;
}
while ((cur = lm_ctx_update_list_next(list)) != NULL) {
printf("updating %s (%s)...\n", cur->name, cur->version);
if (!lm_ctx_update(&ctx, cur, NULL, NULL)) {
while ((old = lm_ctx_update_list_next(list)) != NULL) {
printf("updating %s (%s)...\n", old->name, old->version);
if ((new = lm_ctx_update(&ctx, old)) == NULL) {
printf("failed to update package: %s\n", lm_strerror());
goto end;
}
if (!lm_ctx_download(&ctx, new, NULL, NULL)) {
printf("failed to download new package: %s", lm_strerror());
goto end;
}
if (!lm_ctx_remove(&ctx, old, NULL, NULL)) {
printf("failed to remove old package: %s", lm_strerror());
goto end;
}
if (!lm_ctx_install(&ctx, new, NULL, NULL)) {
printf("failed to install new package: %s", lm_strerror());
goto end;
}
}
ret = EXIT_SUCCESS;

View File

@ -28,7 +28,7 @@ int main(int argc, char *argv[]) {
lm_ctx_sync(&ctx, false, NULL, NULL);
if (!lm_ctx_serve(&ctx, argv[1], 10)) {
if (!lm_ctx_serve(&ctx, argv[1], 10, NULL, NULL)) {
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}

View File

@ -36,11 +36,6 @@ typedef enum lm_ctx_sync_state {
SYNC_LIST_FAIL = 5,
} lm_ctx_sync_state_t;
typedef enum lm_ctx_update_state {
UPDATE_REMOVE = 0,
UPDATE_INSTALL = 1,
} lm_ctx_update_state_t;
/* ###################
## ctx callbacks ##
################### */
@ -49,8 +44,6 @@ typedef bool (*lm_ctx_download_callback_t)(
lm_ctx_t *ctx, lm_pkg_t *pkg, bool is_archive, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_install_callback_t)(
lm_ctx_t *ctx, lm_pkg_t *pkg, char *file, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_update_callback_t)(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_update_state_t state, char *file, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_sync_callback_t)(
lm_ctx_t *ctx, lm_pool_t *pool, lm_ctx_sync_state_t state, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_serve_callback_t)(lm_pool_t *pool, lm_mptp_t *packet, struct sockaddr *addr, void *data);
@ -78,8 +71,7 @@ void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list); // frees the res
lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx); // get a list of packages to update
lm_pkg_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list); // get the next package in the update list
void lm_ctx_update_list_free(lm_ctx_update_list_t *list); // free the update list
bool lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_update_callback_t callback,
void *data); // returns a list of packages to update
lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg); // returns the updated version of a package
bool lm_ctx_removeable(lm_ctx_t *ctx, lm_pkg_t *pkg); // checks if a package is available for removal
bool lm_ctx_remove(

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-17 22:06+0300\n"
"POT-Creation-Date: 2024-07-18 19:15+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

@ -8,21 +8,6 @@
#include <stdlib.h>
#include <string.h>
struct __lm_ctx_update_cb_data {
lm_ctx_update_callback_t callback;
lm_ctx_update_state_t state;
void *data;
};
bool __lm_ctx_update_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, char *file, size_t current, size_t total, void *data){
struct __lm_ctx_update_cb_data *cbdata = data;
if(NULL == cbdata->callback)
return true;
return cbdata->callback(ctx, pkg, cbdata->state, file, current, total, cbdata->data);
}
lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx){
if(NULL == ctx){
lm_error_set(LM_ERR_ArgNULL);
@ -46,6 +31,7 @@ lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx){
list->packages = malloc(sizeof(lm_pkg_t));
else
list->packages = realloc(list->packages, (list->count+1)*sizeof(lm_pkg_t));
lm_package_copy(&list->packages[list->count++], &cur);
}
@ -62,8 +48,10 @@ lm_pkg_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list){
if(NULL == list->packages)
return NULL;
if(NULL == list->cur)
if(NULL == list->cur){
list->cur = &list->packages[0];
return list->cur;
}
for(int i = 0; i < list->count-1; i++){
if(list->cur != &list->packages[i])
@ -91,34 +79,23 @@ void lm_ctx_update_list_free(lm_ctx_update_list_t *list){
free(list);
}
bool lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_update_callback_t callback, void *data){
lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg){
if(NULL == ctx || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
return NULL;
}
struct __lm_ctx_update_cb_data cbdata = {
.state = UPDATE_REMOVE,
.callback = callback,
.data = data,
};
lm_pkg_t *new = NULL;
if((new = lm_ctx_pool_find(ctx, pkg->name, NULL)) == NULL){
lm_error_set(LM_ERR_PkgNotFound);
return false;
return NULL;
}
if(eq(new->version, pkg->version)){
lm_error_set(LM_ERR_PkgUpToDate);
return false;
return NULL;
}
if(!lm_ctx_remove(ctx, pkg, __lm_ctx_update_callback, &cbdata))
return false; // error set by function
if(!lm_ctx_install(ctx, new, __lm_ctx_update_callback, &cbdata))
return false; // error set by function
return true;
return new;
}

View File

@ -63,9 +63,15 @@ bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two){
bool lm_package_copy(lm_pkg_t *dst, lm_pkg_t *src){
lm_package_init(dst);
dst->name = strdup(src->name);
dst->version = strdup(src->version);
dst->desc = strdup(src->desc);
if(NULL != src->name)
dst->name = strdup(src->name);
if(NULL != src->version)
dst->version = strdup(src->version);
if(NULL != src->desc)
dst->desc = strdup(src->desc);
dst->size = src->size;
dst->pool = src->pool;

View File

@ -45,8 +45,12 @@ bool lm_package_path_copy(lm_pkg_t *dst, lm_pkg_t *src){
return false;
}
dst->paths.archive = strdup(src->paths.archive);
dst->paths.signature = strdup(src->paths.signature);
if(NULL != src->paths.archive)
dst->paths.archive = strdup(src->paths.archive);
if(NULL != src->paths.signature)
dst->paths.signature = strdup(src->paths.signature);
return true;
}