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

@ -35,15 +35,17 @@ bool lm_pool_info_load(lm_pool_t *pool) {
lm_pool_info_free(pool);
if(NULL == pool->paths.info)
if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false;
}
if(!can_read(pool->paths.info)){
if(!exists(pool->paths.info_file) || !can_read(pool->paths.info_file)){
lm_error_set(LM_ERR_PoolInfoCantRead);
return false;
}
if (ini_parse(pool->paths.info, lm_pool_info_handler, pool) < 0) {
if (ini_parse(pool->paths.info_file, lm_pool_info_handler, pool) < 0) {
lm_error_set(LM_ERR_PoolInfoBad);
return false;
}
@ -51,7 +53,6 @@ bool lm_pool_info_load(lm_pool_t *pool) {
if(pool->info.size <= 0 ||
pool->info.pubkey == NULL ||
pool->info.maintainer == NULL){
printf("%lu %s %s\n", pool->info.size, pool->info.pubkey, pool->info.maintainer);
lm_error_set(LM_ERR_PoolInfoBad);
return false;
}
@ -65,17 +66,25 @@ bool lm_pool_info_get(lm_pool_t *pool) {
return false;
}
if(!pool->available)
if(!pool->available){
lm_error_set(LM_ERR_PoolNotAvailable);
return false;
}
if(NULL == pool->paths.info)
if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false;
}
if(NULL == pool->url.host)
if(!mkdir_ifnot(pool->paths.dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
if(NULL == pool->url.path)
if(NULL == pool->url.path || NULL == pool->url.host){
lm_error_set(LM_ERR_PoolUrlEmpty);
return false;
}
int sock = -1;
lm_mptp_t packet;
@ -91,7 +100,7 @@ bool lm_pool_info_get(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.info))
if(!lm_mptp_recvfile(sock, pool->paths.info_file))
goto end;
ret = true;

View File

@ -14,46 +14,40 @@ bool lm_pool_list_load(lm_pool_t *pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
lm_pool_list_free(pool);
if(NULL == pool->paths.list)
if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false;
}
if(!can_read(pool->paths.list)){
if(!exists(pool->paths.list_file) || !can_read(pool->paths.list_file)){
lm_error_set(LM_ERR_PoolListCantRead);
return false;
}
size_t file_len = strlen(pool->paths.list);
char *list_dir = NULL, *list_name = NULL;
char file_copy[file_len + 1], file_copy2[file_len + 1];
char extract_dir[file_len+15];
pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->paths.list_dir);
memcpy(file_copy, pool->paths.list, file_len+1);
memcpy(file_copy2, pool->paths.list, file_len+1);
list_dir = dirname(file_copy);
list_name = basename(file_copy2);
char extract_name[strlen(list_name)+20];
snprintf(extract_name, sizeof(extract_name), "%s_extracted", list_name);
join(extract_dir, list_dir, extract_name);
pdebug(__func__, "extracting pool to %s", extract_dir);
if(!mkdir_ifnot(extract_dir)){
lm_error_set(LM_ERR_PoolListDirFail);
if(!mkdir_ifnot(pool->paths.dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
if(!mkdir_ifnot(pool->paths.list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
size_t ent_len = 0, list_dir_len = strlen(pool->paths.list_dir);
struct dirent *ent = NULL;
size_t ent_len = 0;
DIR *dirfd = NULL;
bool ret = false;
if(!extract_archive(extract_dir, pool->paths.list))
if(!extract_archive(pool->paths.list_dir, pool->paths.list_file))
goto end;
if((dirfd = opendir(extract_dir)) == NULL){
if((dirfd = opendir(pool->paths.list_dir)) == NULL){
lm_error_set(LM_ERR_PoolListDirFail);
goto end;
}
@ -63,8 +57,8 @@ bool lm_pool_list_load(lm_pool_t *pool){
continue;
ent_len = strlen(ent->d_name);
char datap[ent_len+sizeof(extract_dir)+10];
join_multiple(datap, extract_dir, ent->d_name, "DATA");
char datap[ent_len+list_dir_len+10];
join_multiple(datap, pool->paths.list_dir, ent->d_name, "DATA");
lm_pkg_t *pkg = lm_package_new();
@ -97,17 +91,30 @@ bool lm_pool_list_get(lm_pool_t *pool) {
return false;
}
if(NULL == pool->paths.list)
if(!pool->available){
lm_error_set(LM_ERR_PoolNotAvailable);
return false;
}
if(!pool->available)
if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false;
}
if(NULL == pool->url.host)
if(!mkdir_ifnot(pool->paths.dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
if(NULL == pool->url.path)
if(!mkdir_ifnot(pool->paths.list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
if(NULL == pool->url.path || NULL == pool->url.host){
lm_error_set(LM_ERR_PoolUrlEmpty);
return false;
}
int sock = -1;
lm_mptp_t packet;
@ -123,7 +130,7 @@ bool lm_pool_list_get(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.list))
if(!lm_mptp_recvfile(sock, pool->paths.list_file))
goto end;
ret = true;
@ -133,3 +140,15 @@ end:
ret = lm_pool_list_load(pool);
return ret;
}
void lm_pool_list_free(lm_pool_t *pool){
if(NULL == pool)
return;
lm_pkg_t *cur = pool->pkg, *prev = NULL;
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_package_free(prev);
}
}

View File

@ -36,8 +36,8 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
}
if(!lm_pool_path_is_empty(pool)){
size_t path_size = strlen(pool->paths.packages)+strlen(pkg->name)+strlen(pkg->version);
size_t name_size = path_size - strlen(pool->paths.packages);
size_t path_size = strlen(pool->paths.dir)+strlen(pkg->name)+strlen(pkg->version);
size_t name_size = path_size - strlen(pool->paths.dir);
char archive_path[path_size+10], sig_path[path_size+20];
char archive_name[name_size+10], sig_name[name_size+20];
@ -45,8 +45,8 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
sprintf(archive_name, "%s_%s.mpf", pkg->name, pkg->version);
sprintf(sig_name, "%s_%s.mpf.sig", pkg->name, pkg->version);
join(archive_path, pool->paths.packages, archive_name);
join(sig_path, pool->paths.packages, sig_name);
join(archive_path, pool->paths.dir, archive_name);
join(sig_path, pool->paths.dir, sig_name);
lm_package_path_set_archive(pkg, archive_path);
lm_package_path_set_signature(pkg, sig_path);
@ -76,7 +76,7 @@ bool lm_pool_package_get(lm_pool_t *pool, lm_pkg_t *pkg){
}
if(lm_package_path_is_empty(pkg)){
lm_error_set(LM_ERR_PackagePathsEmpty);
lm_error_set(LM_ERR_PkgPathsEmpty);
return false;
}

View File

@ -1,60 +1,60 @@
#include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <string.h>
bool lm_pool_path_set_info(lm_pool_t *pool, char *info_path){
free(pool->paths.info);
pool->paths.info = NULL;
bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
free(pool->paths.dir);
pool->paths.dir = NULL;
if(NULL == info_path)
if(NULL == dir)
return true;
if(is_dir(info_path))
if(exists(dir) && (is_file(dir) || !can_read(dir) || !can_write(dir))){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
pool->paths.info = strdup(info_path);
return true;
}
bool lm_pool_path_set_list(lm_pool_t *pool, char *list_path){
free(pool->paths.list);
pool->paths.list = NULL;
if(NULL == list_path)
return true;
if(is_dir(list_path))
pool->paths.dir = strdup(dir);
pool->paths.info_file = join_alloc(dir, "INFO");
pool->paths.list_file = join_alloc(dir, "LIST");
pool->paths.list_dir = join_alloc(dir, "LIST_extracted");
if(exists(pool->paths.info_file) && (!is_file(pool->paths.info_file) || !can_read(pool->paths.info_file) || !can_write(pool->paths.info_file))){
lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
return false;
pool->paths.list = strdup(list_path);
return true;
}
bool lm_pool_path_set_packages(lm_pool_t *pool, char *packages_path){
free(pool->paths.packages);
pool->paths.packages = NULL;
if(NULL == packages_path)
return true;
if(is_file(packages_path))
}
if(exists(pool->paths.list_file) && (!is_file(pool->paths.list_file) || !can_read(pool->paths.list_file) || !can_write(pool->paths.list_file))){
lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
pool->paths.packages = strdup(packages_path);
if(exists(pool->paths.list_dir) && (is_file(pool->paths.list_dir) || !can_read(pool->paths.list_dir) || !can_write(pool->paths.list_dir))){
lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
return true;
}
bool lm_pool_path_is_empty(lm_pool_t *pool){
return NULL == pool->paths.info ||
NULL == pool->paths.list ||
NULL == pool->paths.packages;
return NULL == pool->paths.info_file ||
NULL == pool->paths.list_file ||
NULL == pool->paths.list_dir ||
NULL == pool->paths.dir;
}
void lm_pool_path_free(lm_pool_t *pool){
free(pool->paths.packages);
free(pool->paths.info);
free(pool->paths.list);
free(pool->paths.dir);
free(pool->paths.list_dir);
free(pool->paths.info_file);
free(pool->paths.list_file);
bzero(&pool->paths, sizeof(pool->paths));
}

View File

@ -13,7 +13,7 @@ lm_pool_t *lm_pool_new(char *name, char *url) {
pool->available = true;
pool->name = name;
if (!lm_url_init(&pool->url, url)) {
if (NULL != url && !lm_url_init(&pool->url, url)) {
free(pool);
return NULL;
}
@ -66,13 +66,6 @@ end:
void lm_pool_free(lm_pool_t *pool) {
lm_url_free(&pool->url);
lm_pool_info_free(pool);
lm_pkg_t *cur = pool->pkg, *prev = NULL;
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_package_free(prev);
}
lm_pool_list_free(pool);
free(pool);
}

View File

@ -25,13 +25,13 @@ void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr
// when INFO file is requested, send the file
case MPTP_C2S_INFO:
pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr);
lm_mptp_sendfile(sock, addr, pool->paths.info);
lm_mptp_sendfile(sock, addr, pool->paths.info_file);
break;
// when LIST file is requested, send the file
case MPTP_C2S_LIST:
pdebug(__func__, "(%s) LIST from %s attempting to send list", pool->name, ipaddr);
lm_mptp_sendfile(sock, addr, pool->paths.list);
lm_mptp_sendfile(sock, addr, pool->paths.list_file);
break;
// when the request code is PULL, send the requested package archive and