new: first database functions

This commit is contained in:
ngn
2024-07-02 04:44:07 +03:00
parent f50e5c42b4
commit 16992760ad
25 changed files with 693 additions and 197 deletions

View File

@ -10,7 +10,7 @@ size_t lm_package_depend_count(lm_pkg_t *pkg){
size_t index = 0;
if(NULL == pkg->depends)
return 0;
return index;
while(pkg->depends[index] != NULL)
index++;
@ -42,3 +42,37 @@ bool lm_package_depend_add(lm_pkg_t *pkg, char *depend){
pkg->depends[count] = NULL;
return true;
}
size_t lm_package_depend_strlen(lm_pkg_t *pkg){
size_t len = 1;
if(NULL == pkg->depends)
return len;
for(int i = 0; NULL != pkg->depends[i]; i++)
len += strlen(pkg->depends[i])+1;
return len;
}
bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){
if(NULL == buffer || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->depends){
buffer[0] = 0;
return true;
}
for(int i = 0; NULL != pkg->depends[i]; i++){
if(i == 0){
sprintf(buffer, "%s", pkg->depends[i]);
continue;
}
sprintf(buffer, "%s,%s", buffer, pkg->depends[i]);
}
return true;
}

55
src/package/keep.c Normal file
View File

@ -0,0 +1,55 @@
#include "../../include/package.h"
#include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <string.h>
size_t lm_package_keep_count(lm_pkg_t *pkg){
size_t index = 0;
if(NULL == pkg->keeps)
return index;
while(pkg->keeps[index] != NULL)
index++;
return index;
}
bool lm_package_keep_add(lm_pkg_t *pkg, char *path){
if(NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps){
pkg->keeps = malloc(sizeof(char*)*2);
pkg->keeps[0] = strdup(path);
pkg->keeps[1] = NULL;
return true;
}
size_t count = lm_package_depend_count(pkg);
pkg->keeps = realloc(pkg->depends, sizeof(char*)*(count+2));
pkg->keeps[count++] = strdup(path);
pkg->keeps[count] = NULL;
return true;
}
bool lm_package_keep_contains(lm_pkg_t *pkg, char *path){
if(NULL == pkg || NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps)
return false;
for(int i = 0; NULL != pkg->keeps[i]; i++)
if(eq(pkg->keeps[i], path))
return true;
return false;
}

83
src/package/verify.c Normal file
View File

@ -0,0 +1,83 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <gpgme.h>
bool lm_package_verify(lm_pkg_t *pkg){
if(NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(lm_package_path_is_empty(pkg)){
lm_error_set(LM_ERR_PkgPathsEmpty);
return false;
}
gpgme_data_t sig = NULL, text = NULL;
gpgme_ctx_t ctx = NULL;
gpgme_signature_t s;
gpgme_verify_result_t result;
gpgme_error_t err;
bool ret = false;
gpgme_check_version(NULL);
err = gpgme_new(&ctx);
if (err != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to create gpgme: %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGFail);
return false;
}
gpgme_op_import(ctx, NULL);
if((err = gpgme_data_new_from_file(&sig, pkg->paths.signature, 1)) != GPG_ERR_NO_ERROR){
pdebug(__func__, "(%s) failed to import package signature %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGSigFail);
goto end;
}
if ((err = gpgme_data_new_from_file(&text, pkg->paths.archive, 1)) != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to import package archive %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGArchiveFail);
goto end;
}
if ((err = gpgme_op_verify(ctx, sig, text, NULL)) != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to verify package archive %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgSigNoResult);
goto end;
}
result = gpgme_op_verify_result(ctx);
lm_error_set(LM_ERR_PkgSigNoMatch);
if (result == NULL)
goto end;
s = result->signatures;
while (s) {
int status = gpg_err_code(s->status);
if (status == GPG_ERR_NO_ERROR) {
lm_error_set(LM_ERR_NoError);
ret = true;
break;
}
s = s->next;
}
end:
if(NULL != text)
gpgme_data_release(text);
if(NULL != sig)
gpgme_data_release(sig);
if(NULL != ctx)
gpgme_release(ctx);
return ret;
}