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

@ -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);
}
}