update: refactoring and small fixes

This commit is contained in:
ngn
2024-07-31 21:16:19 +03:00
parent 3f794e1a90
commit 9cd4eb9905
41 changed files with 779 additions and 752 deletions

View File

@ -40,12 +40,12 @@ bool lm_pool_info_load(lm_pool_t *pool) {
return false;
}
if(!exists(pool->paths.info_file) || !can_read(pool->paths.info_file)){
if(!exists(pool->info_file) || !can_read(pool->info_file)){
lm_error_set(LM_ERR_PoolInfoCantRead);
return false;
}
if (ini_parse(pool->paths.info_file, lm_pool_info_handler, pool) < 0) {
if (ini_parse(pool->info_file, lm_pool_info_handler, pool) < 0) {
lm_error_set(LM_ERR_PoolInfoBad);
return false;
}
@ -76,7 +76,7 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
return false;
}
if(!mkdir_ifnot(pool->paths.dir)){
if(!mkdir_ifnot(pool->dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
@ -100,7 +100,7 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.info_file, callback, data))
if(!lm_mptp_recvfile(sock, pool->info_file, callback, data))
goto end;
ret = true;

View File

@ -22,32 +22,32 @@ bool lm_pool_list_load(lm_pool_t *pool){
return false;
}
if(!exists(pool->paths.list_file) || !can_read(pool->paths.list_file)){
if(!exists(pool->list_file) || !can_read(pool->list_file)){
lm_error_set(LM_ERR_PoolListCantRead);
return false;
}
pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->paths.list_dir);
pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->list_dir);
if(!mkdir_ifnot(pool->paths.dir)){
if(!mkdir_ifnot(pool->dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
if(!mkdir_ifnot(pool->paths.list_dir)){
if(!mkdir_ifnot(pool->list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
size_t ent_len = 0, list_dir_len = strlen(pool->paths.list_dir);
size_t ent_len = 0, list_dir_len = strlen(pool->list_dir);
struct dirent *ent = NULL;
DIR *dirfd = NULL;
bool ret = false;
if(!extract_archive(pool->paths.list_dir, pool->paths.list_file))
if(!extract_archive(pool->list_dir, pool->list_file))
goto end;
if((dirfd = opendir(pool->paths.list_dir)) == NULL){
if((dirfd = opendir(pool->list_dir)) == NULL){
lm_error_set(LM_ERR_PoolListDirFail);
goto end;
}
@ -58,23 +58,23 @@ bool lm_pool_list_load(lm_pool_t *pool){
ent_len = strlen(ent->d_name);
char datap[ent_len+list_dir_len+10];
join_multiple(datap, pool->paths.list_dir, ent->d_name, "DATA");
join_multiple(datap, pool->list_dir, ent->d_name, "DATA");
lm_pkg_t *pkg = lm_package_new();
if(!lm_package_data_load(pkg, datap)){
if(!lm_package_data_load(&pkg->data, datap)){
pdebug(__func__, "(%s) failed to load new package from %s", pool->name, datap);
lm_package_free(pkg);
continue;
}
if(!lm_pool_package_add(pool, pkg)){
pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->name);
pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->data.name);
lm_package_free(pkg);
continue;
}
pdebug(__func__, "(%s) added new package: %s", pool->name, pkg->name);
pdebug(__func__, "(%s) added new package: %s", pool->name, pkg->data.name);
}
ret = true;
@ -101,12 +101,12 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
return false;
}
if(!mkdir_ifnot(pool->paths.dir)){
if(!mkdir_ifnot(pool->dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false;
}
if(!mkdir_ifnot(pool->paths.list_dir)){
if(!mkdir_ifnot(pool->list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
@ -130,7 +130,7 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.list_file, callback, data))
if(!lm_mptp_recvfile(sock, pool->list_file, callback, data))
goto end;
ret = true;

View File

@ -14,13 +14,13 @@ lm_pkg_t *lm_pool_package_find(lm_pool_t *pool, char *name, char *version){
lm_pkg_t *cur = pool->pkg;
while(NULL != cur){
if(NULL == version && eq(cur->name, name))
if(NULL == version && eq(cur->data.name, name))
return cur;
else if(NULL == name && eq(cur->version, version))
else if(NULL == name && eq(cur->data.version, version))
return cur;
else if(eq(cur->name, name) && eq(cur->version, version))
else if(eq(cur->data.name, name) && eq(cur->data.version, version))
return cur;
cur = cur->next;
@ -38,17 +38,17 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
pkg->pool = pool;
if(!lm_pool_path_is_empty(pool)){
size_t path_size = strlen(pool->paths.dir)+strlen(pkg->name)+strlen(pkg->version);
size_t name_size = path_size - strlen(pool->paths.dir);
size_t path_size = strlen(pool->dir)+strlen(pkg->data.name)+strlen(pkg->data.version);
size_t name_size = path_size - strlen(pool->dir);
char archive_path[path_size+10], sig_path[path_size+20];
char archive_name[name_size+10], sig_name[name_size+20];
sprintf(archive_name, "%s_%s.mpf", pkg->name, pkg->version);
sprintf(sig_name, "%s_%s.mpf.sig", pkg->name, pkg->version);
sprintf(archive_name, "%s_%s.mpf", pkg->data.name, pkg->data.version);
sprintf(sig_name, "%s_%s.mpf.sig", pkg->data.name, pkg->data.version);
join(archive_path, pool->paths.dir, archive_name);
join(sig_path, pool->paths.dir, sig_name);
join(archive_path, pool->dir, archive_name);
join(sig_path, pool->dir, sig_name);
lm_package_path_set_archive(pkg, archive_path);
lm_package_path_set_signature(pkg, sig_path);

View File

@ -1,4 +1,3 @@
#include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
@ -7,8 +6,15 @@
#include <string.h>
bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
free(pool->paths.dir);
pool->paths.dir = NULL;
free(pool->dir);
free(pool->list_dir);
free(pool->info_file);
free(pool->list_file);
pool->dir = NULL;
pool->list_dir = NULL;
pool->info_file = NULL;
pool->list_file = NULL;
if(NULL == dir)
return true;
@ -18,25 +24,22 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
return false;
}
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");
pool->dir = strdup(dir);
pool->info_file = join_alloc(dir, "INFO");
pool->list_file = join_alloc(dir, "LIST");
pool->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);
if(exists(pool->info_file) && (!is_file(pool->info_file) || !can_read(pool->info_file) || !can_write(pool->info_file))){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
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);
if(exists(pool->list_file) && (!is_file(pool->list_file) || !can_read(pool->list_file) || !can_write(pool->list_file))){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
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);
if(exists(pool->list_dir) && (is_file(pool->list_dir) || !can_read(pool->list_dir) || !can_write(pool->list_dir))){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
@ -45,16 +48,8 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
}
bool lm_pool_path_is_empty(lm_pool_t *pool){
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.dir);
free(pool->paths.list_dir);
free(pool->paths.info_file);
free(pool->paths.list_file);
bzero(&pool->paths, sizeof(pool->paths));
return NULL == pool->info_file ||
NULL == pool->list_file ||
NULL == pool->list_dir ||
NULL == pool->dir;
}

View File

@ -65,6 +65,10 @@ end:
}
void lm_pool_free(lm_pool_t *pool) {
free(pool->dir);
free(pool->list_dir);
free(pool->info_file);
free(pool->list_file);
lm_url_free(&pool->url);
lm_pool_info_free(pool);
lm_pool_list_free(pool);