update: better way to handle resolve list

This commit is contained in:
ngn 2024-08-11 01:42:47 +03:00
parent 70b760c0f8
commit e0f0dec222
6 changed files with 37 additions and 108 deletions

View File

@ -22,7 +22,7 @@
// clang-format on // clang-format on
#define LM_VERSION "24.01" #define LM_VERSION "24.02"
#include "ctx.h" #include "ctx.h"
#include "error.h" #include "error.h"

View File

@ -23,10 +23,9 @@ typedef struct lm_ctx_list {
} lm_ctx_list_t; } lm_ctx_list_t;
typedef struct lm_ctx_resolve_list { typedef struct lm_ctx_resolve_list {
lm_pkg_t *resolving; lm_pkg_t **packages;
lm_pkg_t *packages; lm_pkg_t *cur;
lm_pkg_t *cur; ssize_t count, index;
ssize_t count;
} lm_ctx_resolve_list_t; } lm_ctx_resolve_list_t;
typedef struct lm_ctx_update_list { typedef struct lm_ctx_update_list {

View File

@ -47,9 +47,3 @@ bool mkdir_ifnot(char *path);
int join_multiple(char *res, const char *base, const char *pth, const char *pth2); int join_multiple(char *res, const char *base, const char *pth, const char *pth2);
int join(char *res, const char *base, const char *pth); int join(char *res, const char *base, const char *pth);
char *join_alloc(const char *base, const char *pth); 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_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 "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-10 00:05+0300\n" "POT-Creation-Date: 2024-08-11 01:42+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View File

@ -7,14 +7,32 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
bool __lm_ctx_resolve_contains(lm_pkg_t *pkg, lm_ctx_resolve_list_t *list){
if(NULL == list->packages)
return false;
for(int i = 0; i < list->count; i++){
if(eq(list->packages[i]->data.name, pkg->data.name))
return true;
}
return false;
}
bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg, bool resolve_depends){ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg, bool resolve_depends){
if(pkglist_contains(list->packages, pkg)) if(__lm_ctx_resolve_contains(pkg, list))
return true; return true;
if(!resolve_depends || NULL == pkg->data.depends) if(NULL == list->packages)
goto end; list->packages = malloc(sizeof(lm_pkg_t *)*(++list->count));
else
list->packages = realloc(list->packages, sizeof(lm_pkg_t *)*(++list->count));
list->resolving = pkglist_add_top(list->resolving, pkg); list->packages[list->count-1] = pkg;
if(!resolve_depends || NULL == pkg->data.depends)
return true;
lm_pkg_t *depend = NULL; lm_pkg_t *depend = NULL;
for(int i = 0; pkg->data.depends[i] != NULL; i++){ for(int i = 0; pkg->data.depends[i] != NULL; i++){
@ -29,19 +47,10 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg,
return false; return false;
} }
if(pkglist_contains(list->resolving, depend)){
lm_error_set(LM_ERR_DependNotFound, pkg->data.depends[i], pkg->data.name);
return false;
}
if(!__lm_ctx_resolve(ctx, list, depend, resolve_depends)) if(!__lm_ctx_resolve(ctx, list, depend, resolve_depends))
return false; return false;
} }
list->resolving = pkglist_del(list->resolving, pkg);
end:
list->packages = pkglist_add_end(list->packages, pkg);
list->count++;
return true; return true;
} }
@ -57,15 +66,11 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, bool resolve
} }
if(!__lm_ctx_resolve(ctx, list, pkg, resolve_depends)){ if(!__lm_ctx_resolve(ctx, list, pkg, resolve_depends)){
pkglist_free(list->resolving); free(list->packages);
pkglist_free(list->packages);
free(list); free(list);
return NULL; return NULL;
} }
pkglist_free(list->resolving);
list->resolving = NULL;
return list; return list;
} }
@ -76,11 +81,16 @@ lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list){
} }
if(NULL == list->cur){ if(NULL == list->cur){
list->cur = list->packages; list->index = 0;
list->cur = list->packages[list->index++];
return list->cur; return list->cur;
} }
list->cur = list->cur->next; if(list->index >= list->count)
list->cur = NULL;
else
list->cur = list->packages[list->index++];
return list->cur; return list->cur;
} }
@ -90,7 +100,8 @@ void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list){
return; return;
} }
pkglist_free(list->packages); free(list->packages);
free(list); free(list);
return; return;
} }

View File

@ -354,81 +354,6 @@ char *join_alloc(const char *base, const char *pth) {
return path; return path;
} }
bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg) {
lm_pkg_t *cur = list;
while (cur) {
if (eq(pkg->data.name, cur->data.name))
return true;
cur = cur->next;
}
return false;
}
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;
if (eq(list->data.name, pkg->data.name)) {
list = NULL;
return list;
}
lm_pkg_t *cur = list;
lm_pkg_t *found = NULL;
while (NULL != cur->next) {
if (eq(cur->next->data.name, pkg->data.name)) {
found = cur->next;
cur->next = cur->next->next;
break;
}
cur = cur->next;
}
free(found);
return list;
}
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 = 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, *old = NULL;
while (cur != NULL) {
old = cur;
cur = cur->next;
free(old);
}
}
bool copy_file(char *dst, char *src) { bool copy_file(char *dst, char *src) {
FILE *dstp = NULL, *srcp = NULL; FILE *dstp = NULL, *srcp = NULL;
bool ret = false; bool ret = false;