new: full implementation of remove and install functions

This commit is contained in:
ngn 2024-07-10 16:39:05 +03:00
parent d0dce209fa
commit f886bc08e4
30 changed files with 129 additions and 72 deletions

View File

@ -6,9 +6,10 @@
#include <string.h>
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
lm_pkg_t *pkg = NULL;
lm_ctx_t ctx;
lm_ctx_resolve_list_t *list = NULL;
lm_pkg_t *pkg = NULL, *cur = NULL;
lm_ctx_t ctx;
int ret = EXIT_FAILURE;
if (argc != 4) {
printf("usage: %s <package name> <pool name> <pool url>\n", argv[0]);
@ -50,23 +51,33 @@ int main(int argc, char *argv[]) {
goto end;
}
printf("downloading package: %s (%s)...\n", pkg->name, pkg->version);
printf("resolving package: %s (%s)...\n", pkg->name, pkg->version);
if (!lm_ctx_download(&ctx, pkg, NULL, NULL)) {
printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error());
if ((list = lm_ctx_resolve(&ctx, pkg)) == NULL) {
printf("failed to resolve package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
printf("installing package: %s (%s)...\n", pkg->name, pkg->version);
while ((cur = lm_ctx_resolve_next(list)) != NULL) {
printf("downloading package: %s (%s)...\n", cur->name, cur->version);
if (!lm_ctx_download(&ctx, cur, NULL, NULL)) {
printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
}
if (!lm_ctx_install(&ctx, pkg, NULL, NULL)) {
printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
while ((cur = lm_ctx_resolve_next(list)) != NULL) {
printf("installing package: %s (%s)...\n", cur->name, cur->version);
if (!lm_ctx_install(&ctx, cur, NULL, NULL)) {
printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
}
ret = EXIT_SUCCESS;
end:
lm_ctx_resolve_free(list);
lm_ctx_free(&ctx);
return ret;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,4 @@
[base]
size = 314121
[test]
size = 3936
maintainer = ngn
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D

BIN
examples/pools/test/LIST Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -21,7 +21,7 @@ int main(int argc, char *argv[]) {
goto end;
}
if (lm_ctx_pool_add(&ctx, "base", "mptp://127.0.0.1:5858") == NULL) {
if (lm_ctx_pool_add(&ctx, "test", "mptp://127.0.0.1:5858") == NULL) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}

View File

@ -63,13 +63,12 @@ void lm_ctx_free(lm_ctx_t *ctx);
#################### */
lm_ctx_resolve_list_t *lm_ctx_resolve(
lm_ctx_t *ctx, lm_pkg_t *pkg); // resolves a package and returns a list of packages to install
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // returns the next package in the list
void lm_ctx_resolve_free(
lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list); // returns the next package in the list
void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve
lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx); // returns a list of packages to update
lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // returns the next package in the list
void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // frees the update list returned by lm_ctx_update
lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx); // returns a list of packages to update
lm_pkg_t *lm_ctx_update_next(lm_ctx_update_list_t *list); // returns the next package in the list
void lm_ctx_update_free(lm_ctx_update_list_t *list); // frees the update list returned by lm_ctx_update
bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback,
void *data); // downloads a single package

View File

@ -132,6 +132,7 @@ typedef enum lm_error {
LM_ERR_InstallBackChdirFail = 124,
LM_ERR_InstallStatusFail = 125,
LM_ERR_InstallScriptFail = 126,
LM_ERR_PkgBreaks = 127,
} lm_error_t;
typedef struct lm_error_desc {

View File

@ -23,6 +23,7 @@ size_t lm_package_depend_strlen(lm_pkg_t *pkg);
bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer);
bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer);
void lm_package_depend_free(lm_pkg_t *pkg);
bool lm_package_depends(lm_pkg_t *pkg, char *dep);
size_t lm_package_keep_count(lm_pkg_t *pkg);
bool lm_package_keep_add(lm_pkg_t *pkg, char *path);

View File

@ -40,5 +40,6 @@ char *join_alloc(const char *base, const char *pth);
bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg);
lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg);
lm_pkg_t *pkglist_add(lm_pkg_t *list, lm_pkg_t *pkg);
lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg);
lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg);
void pkglist_free(lm_pkg_t *list);

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-08 13:05+0300\n"
"POT-Creation-Date: 2024-07-10 16:36+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"
@ -554,3 +554,8 @@ msgstr ""
#, c-format
msgid "failed to run the package install script: %s"
msgstr ""
#: src/error.c:152
#, c-format
msgid "removing package breaks %s"
msgstr ""

View File

@ -60,6 +60,8 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca
snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version);
join(path, pool->url.path, name);
pdebug(__func__, "connecting to %s:%d for download", pool->url.host, pool->url.port);
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
return false;

View File

@ -27,9 +27,19 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba
if(!lm_ctx_database_init(ctx))
return false;
lm_pkg_t cur;
while (lm_database_package_next(ctx->db, &cur)) {
if(lm_package_depends(&cur, pkg->name)){
lm_error_set(LM_ERR_PkgBreaks, cur.name);
lm_database_package_next_free(ctx->db, &cur);
return false;
}
}
char *path = NULL, *hash = NULL, *fpath = NULL;
size_t total = 0, current = 0;
bool in_keep = false, ret = false;
size_t total = 0, current = 0;
total = lm_database_files_count(ctx->db, pkg);
@ -54,7 +64,7 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba
next:
free(fpath);
fpath = NULL;
if(NULL != callback && !callback(ctx, pkg, fpath, ++current, total, data))
goto end;
}

View File

@ -14,7 +14,7 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg)
if(NULL == pkg->depends)
goto end;
list->resolving = pkglist_add(list->resolving, pkg);
list->resolving = pkglist_add_top(list->resolving, pkg);
lm_pkg_t *depend = NULL;
for(int i = 0; pkg->depends[i] != NULL; i++){
@ -34,13 +34,13 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg)
return false;
}
if(!__lm_ctx_resolve(ctx, list, pkg))
if(!__lm_ctx_resolve(ctx, list, depend))
return false;
}
list->resolving = pkglist_del(list->resolving, pkg);
end:
list->packages = pkglist_add(list->packages, pkg);
list->packages = pkglist_add_end(list->packages, pkg);
list->count++;
return true;
}
@ -52,10 +52,7 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg){
}
lm_ctx_resolve_list_t *list = malloc(sizeof(lm_ctx_resolve_list_t));
list->resolving = NULL;
list->packages = NULL;
list->cur = NULL;
list->count = 0;
bzero(list, sizeof(lm_ctx_resolve_list_t));
if(!__lm_ctx_resolve(ctx, list, pkg)){
pkglist_free(list->resolving);
@ -66,11 +63,12 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg){
pkglist_free(list->resolving);
list->resolving = NULL;
return list;
}
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){
if(NULL == ctx || NULL == list){
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
@ -84,8 +82,8 @@ lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){
return list->cur;
}
void lm_ctx_resolve_free(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){
if(NULL == ctx || NULL == list){
void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}

View File

@ -32,8 +32,8 @@ lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx){
return list;
}
lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list){
if(NULL == ctx || NULL == list){
lm_pkg_t *lm_ctx_update_next(lm_ctx_update_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
@ -46,8 +46,8 @@ lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list){
return list->packages[list->index++];
}
void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list){
if(NULL == ctx || NULL == list){
void lm_ctx_update_free(lm_ctx_update_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}

View File

@ -147,16 +147,16 @@ bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){
return false;
}
if(NULL == db->packages_st &&
if(NULL != db->packages_st)
lm_package_free(pkg);
else if(NULL == db->packages_st &&
sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_ALL], strlen(queries[QUERY_SELECT_PACKAGE_ALL]), &db->packages_st, NULL) != SQLITE_OK){
pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlPrepareFail);
return false;
}
else if(NULL != db->packages_st)
lm_package_free(pkg);
if(sqlite3_step(db->packages_st) != SQLITE_ROW){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
@ -186,8 +186,10 @@ void lm_database_package_next_free(lm_database_t *db, lm_pkg_t *pkg){
return;
}
if(NULL != db->packages_st)
if(NULL != db->packages_st){
sqlite3_finalize(db->packages_st);
db->packages_st = NULL;
}
if(NULL != pkg)
lm_package_free(pkg);

View File

@ -149,6 +149,7 @@ void lm_error_set(lm_error_t code, ...) {
.desc = _("failed to change directory to old directory after running install") },
{.code = LM_ERR_InstallStatusFail, .desc = _("install script returned a bad status code") },
{.code = LM_ERR_InstallScriptFail, .desc = _("failed to run the package install script: %s") },
{.code = LM_ERR_PkgBreaks, .desc = _("removing package breaks %s") },
};
char *fmt = NULL;

View File

@ -124,3 +124,18 @@ bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){
return true;
}
bool lm_package_depends(lm_pkg_t *pkg, char *dep){
if(NULL == pkg || NULL == dep){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->depends)
return false;
for(int i = 0; pkg->depends[i] != NULL; i++)
if(eq(pkg->depends[i], dep))
return true;
return false;
}

View File

@ -337,55 +337,66 @@ bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg) {
}
lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg) {
if (NULL == pkg) {
lm_error_set(LM_ERR_ArgNULL);
return list;
}
if (NULL == list)
return list;
lm_pkg_t *cur = list;
lm_pkg_t *target = NULL;
int indx = 0;
if (eq(list->name, pkg->name)) {
list = NULL;
return list;
}
while (cur) {
if (eq(cur->name, pkg->name)) {
target = cur;
lm_pkg_t *cur = list;
lm_pkg_t *found = NULL;
while (NULL != cur->next) {
if (eq(cur->next->name, pkg->name)) {
found = cur->next;
cur->next = cur->next->next;
break;
}
cur = cur->next;
indx++;
}
if (NULL == target)
return list;
cur = list;
for (int i = 0; i < indx; i++) {
if (cur->next != NULL)
cur = cur->next;
}
if (NULL == cur->next)
list = NULL;
else
cur->next = cur->next->next;
free(target);
free(found);
return list;
}
lm_pkg_t *pkglist_add(lm_pkg_t *list, lm_pkg_t *pkg) {
lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg) {
lm_pkg_t *new = malloc(sizeof(lm_pkg_t));
memcpy(new, pkg, sizeof(lm_pkg_t));
new->next = NULL;
new->next = list;
list = new;
return list;
}
lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg) {
lm_pkg_t *new = malloc(sizeof(lm_pkg_t)), *cur = list;
memcpy(new, pkg, sizeof(lm_pkg_t));
new->next = NULL;
if (NULL == cur) {
list = new;
return list;
}
while (cur->next != NULL)
cur = cur->next;
cur->next = new;
return list;
}
void pkglist_free(lm_pkg_t *list) {
lm_pkg_t *cur = list;
while (cur) {
lm_pkg_t *old = cur;
cur = cur->next;
lm_pkg_t *cur = list, *old = NULL;
while (cur != NULL) {
old = cur;
cur = cur->next;
free(old);
}
}