libmp/examples/client/update.c
2024-08-04 16:00:18 +03:00

77 lines
1.9 KiB
C

#include "../../include/all.h"
#include "../common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
lm_ctx_update_list_t *list = NULL;
int ret = EXIT_FAILURE;
lm_pkg_t *new = NULL;
lm_entry_t *old = NULL;
lm_ctx_t ctx;
if (argc != 3) {
printf("usage: %s <pool name> <pool url>\n", argv[0]);
return ret;
}
char pooldir[strlen(TEMP_DIR) + 10];
sprintf(pooldir, "%s/%s", DATA_DIR, "pool");
if (!lm_ctx_new(&ctx, ROOT_DIR, TEMP_DIR, DATA_DIR)) {
printf("failed to init the ctx: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (lm_ctx_pool_add(&ctx, argv[1], argv[2], pooldir) == 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;
}
if (list->count == 0) {
printf("all packages are up-to-date\n");
goto end;
}
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, false, NULL, NULL)) {
printf("failed to install new package: %s", lm_strerror());
goto end;
}
}
ret = EXIT_SUCCESS;
end:
lm_ctx_update_list_free(list);
lm_ctx_free(&ctx);
return ret;
}