From bc79c642ac02fc29ffdb5676d3d0882b98eee89f Mon Sep 17 00:00:00 2001 From: ngn Date: Thu, 18 Jul 2024 19:25:35 +0300 Subject: [PATCH] update: handle actual package updates outside of the library --- examples/client/update.c | 37 ++++++++++++++++++++++++++---- examples/server/main.c | 2 +- include/ctx.h | 10 +-------- locale/tr/LC_MESSAGES/libmp.po | 2 +- src/ctx/update.c | 41 ++++++++-------------------------- src/package/package.c | 12 +++++++--- src/package/path.c | 8 +++++-- 7 files changed, 60 insertions(+), 52 deletions(-) diff --git a/examples/client/update.c b/examples/client/update.c index 36dd8e5..a7237d7 100644 --- a/examples/client/update.c +++ b/examples/client/update.c @@ -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 \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; diff --git a/examples/server/main.c b/examples/server/main.c index 1fb19f3..61a4e61 100644 --- a/examples/server/main.c +++ b/examples/server/main.c @@ -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; } diff --git a/include/ctx.h b/include/ctx.h index 23867e3..d02b17f 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -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( diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 29705e3..4635394 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -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 \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/update.c b/src/ctx/update.c index 6cbc51e..285999b 100644 --- a/src/ctx/update.c +++ b/src/ctx/update.c @@ -8,21 +8,6 @@ #include #include -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; } diff --git a/src/package/package.c b/src/package/package.c index 6903147..6614811 100644 --- a/src/package/package.c +++ b/src/package/package.c @@ -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; diff --git a/src/package/path.c b/src/package/path.c index 4da4ad0..622aef7 100644 --- a/src/package/path.c +++ b/src/package/path.c @@ -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; }