libmp/src/ctx/update.c

102 lines
2.2 KiB
C

#include "../../include/package.h"
#include "../../include/database.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdlib.h>
#include <string.h>
lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx){
if(NULL == ctx){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_ctx_update_list_t *list = malloc(sizeof(lm_ctx_update_list_t));
lm_entry_t *cur = malloc(sizeof(lm_entry_t));
lm_pkg_t *new = NULL;
bzero(list, sizeof(lm_ctx_update_list_t));
lm_entry_init(cur);
while(lm_ctx_database_next(ctx, cur)){
if((new = lm_ctx_pool_find(ctx, cur->name, NULL)) == NULL)
continue;
if(eq(new->data.version, cur->version))
continue;
if(NULL == list->entries)
list->entries = malloc(sizeof(lm_entry_t*)*(++list->count));
else
list->entries = realloc(list->entries, sizeof(lm_entry_t*)*(++list->count));
list->entries[list->count-1] = cur;
cur = malloc(sizeof(lm_entry_t));
lm_entry_init(cur);
}
lm_entry_free(cur);
free(cur);
lm_ctx_database_next_free(ctx, NULL);
return list;
}
lm_entry_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
if(NULL == list->entries)
return NULL;
if(list->index >= list->count){
list->index = 0;
return NULL;
}
return list->entries[list->index++];
}
void lm_ctx_update_list_free(lm_ctx_update_list_t *list){
if(NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}
if(NULL != list->entries){
for(int i = 0; i < list->count; i++){
lm_entry_free(list->entries[i]);
free(list->entries[i]);
}
free(list->entries);
}
free(list);
}
lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_entry_t *entry){
if(NULL == ctx || NULL == entry){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_pkg_t *new = NULL;
if((new = lm_ctx_pool_find(ctx, entry->name, NULL)) == NULL){
lm_error_set(LM_ERR_PkgNotFound);
return NULL;
}
if(eq(new->data.version, entry->version)){
lm_error_set(LM_ERR_PkgUpToDate);
return NULL;
}
return new;
}