#include "../../include/package.h" #include "../../include/error.h" #include "../../include/pool.h" #include "../../include/util.h" #include "../../include/ctx.h" #include #include 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){ if(__lm_ctx_resolve_contains(pkg, list)) return true; if(NULL == list->packages) list->packages = malloc(sizeof(lm_pkg_t *)*(++list->count)); else list->packages = realloc(list->packages, sizeof(lm_pkg_t *)*(++list->count)); list->packages[list->count-1] = pkg; if(!resolve_depends || NULL == pkg->data.depends) return true; lm_pkg_t *depend = NULL; for(int i = 0; pkg->data.depends[i] != NULL; i++){ // if found then its already installed if(lm_ctx_database_find(ctx, NULL, pkg->data.depends[i], NULL)) continue; depend = lm_ctx_pool_find(ctx, pkg->data.depends[i], NULL); if(NULL == 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)) return false; } return true; } lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, bool resolve_depends, lm_ctx_resolve_list_t *list){ if(NULL == ctx || NULL == pkg){ lm_error_set(LM_ERR_ArgNULL); return NULL; } if(NULL == list){ list = malloc(sizeof(lm_ctx_resolve_list_t)); bzero(list, sizeof(lm_ctx_resolve_list_t)); } if(!__lm_ctx_resolve(ctx, list, pkg, resolve_depends)){ free(list->packages); free(list); return NULL; } return 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; } if(NULL == list->cur){ list->index = 0; list->cur = list->packages[list->index++]; return list->cur; } if(list->index >= list->count) list->cur = NULL; else list->cur = list->packages[list->index++]; return list->cur; } void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list){ if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return; } free(list->packages); free(list); return; }