new: implement check and update functions

This commit is contained in:
ngn
2024-07-11 07:31:36 +03:00
parent f886bc08e4
commit 839bcb47bf
20 changed files with 456 additions and 52 deletions

View File

@ -1,6 +1,6 @@
CC = gcc
all: ../dist/client_install ../dist/client_remove ../dist/server
all: ../dist/client_install ../dist/client_remove ../dist/client_update ../dist/client_check ../dist/client_list ../dist/server
../dist/client_install: client/install.c client/*.h ../dist/libmp.so
mkdir -pv ../dist
@ -10,6 +10,18 @@ all: ../dist/client_install ../dist/client_remove ../dist/server
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
../dist/client_update: client/update.c client/*.h ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
../dist/client_check: client/check.c client/*.h ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
../dist/client_list: client/list.c client/*.h ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
../dist/server: server/*.c ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@

45
examples/client/check.c Normal file
View File

@ -0,0 +1,45 @@
#include "../../include/all.h"
#include "./common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
lm_pkg_t pkg;
lm_ctx_t ctx;
lm_package_init(&pkg);
if (argc != 2) {
printf("usage: %s <package name>\n", argv[0]);
return ret;
}
lm_ctx_init(&ctx);
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_database_find(&ctx, &pkg, argv[1], NULL)) {
printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
printf("checking %s (%s)...\n", pkg.name, pkg.version);
if (!lm_ctx_check(&ctx, &pkg, NULL, NULL)) {
printf("failed to verify the package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
ret = EXIT_SUCCESS;
end:
lm_package_free(&pkg);
lm_ctx_free(&ctx);
return ret;
}

28
examples/client/list.c Normal file
View File

@ -0,0 +1,28 @@
#include "../../include/all.h"
#include "./common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
lm_ctx_t ctx;
lm_pkg_t pkg;
int ret = EXIT_FAILURE;
lm_ctx_init(&ctx);
if (!lm_ctx_set_data(&ctx, DATA_DIR)) {
printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
printf("listing installed packages:\n");
while (lm_ctx_database_next(&ctx, &pkg))
printf("%s (%s)\n", pkg.name, pkg.version);
end:
lm_ctx_database_next_free(&ctx, &pkg);
return ret;
}

60
examples/client/update.c Normal file
View File

@ -0,0 +1,60 @@
#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 pkg, *cur = NULL;
lm_ctx_t ctx;
if (!mkdir_ifnot(ROOT_DIR)) {
printf("failed to create root dir: %s\n", ROOT_DIR);
return ret;
}
lm_ctx_init(&ctx);
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_set_temp(&ctx, TEMP_DIR)) {
printf("failed to set temp dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_set_root(&ctx, ROOT_DIR)) {
printf("failed to set root dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
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 ((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)) {
printf("failed to update package: %s\n", lm_strerror());
goto end;
}
}
ret = EXIT_SUCCESS;
end:
lm_ctx_update_list_free(list);
lm_ctx_free(&ctx);
return ret;
}