update: function cleanups, better way to handle package paths
This commit is contained in:
@ -3,13 +3,10 @@
|
||||
#include "../../include/mptp.h"
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ini.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int lm_pool_info_handler(void *data, const char *_section, const char *_key, const char *_value) {
|
||||
char *section = (char *)_section, *value = (char *)_value, *key = (char *)_key;
|
||||
@ -30,27 +27,42 @@ int lm_pool_info_handler(void *data, const char *_section, const char *_key, con
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool lm_pool_info_load(lm_pool_t *pool, char *file) {
|
||||
bool lm_pool_info_load(lm_pool_t *pool) {
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
lm_pool_info_free(pool);
|
||||
|
||||
if(!can_read(file)){
|
||||
if(NULL == pool->paths.info)
|
||||
return false;
|
||||
|
||||
if(!can_read(pool->paths.info)){
|
||||
lm_error_set(LM_ERR_PoolInfoCantRead);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ini_parse(file, lm_pool_info_handler, pool) < 0) {
|
||||
if (ini_parse(pool->paths.info, lm_pool_info_handler, pool) < 0) {
|
||||
lm_error_set(LM_ERR_PoolInfoBad);
|
||||
return false;
|
||||
}
|
||||
|
||||
pool->paths.info = strdup(file);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_pool_info_get(lm_pool_t *pool, char *file) {
|
||||
bool lm_pool_info_get(lm_pool_t *pool) {
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!pool->available)
|
||||
return false;
|
||||
|
||||
if(NULL == pool->paths.info)
|
||||
return false;
|
||||
|
||||
if(NULL == pool->url.host)
|
||||
return false;
|
||||
|
||||
@ -67,15 +79,15 @@ bool lm_pool_info_get(lm_pool_t *pool, char *file) {
|
||||
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, file))
|
||||
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.info))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_info_load(pool, file);
|
||||
ret = lm_pool_info_load(pool);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -4,29 +4,32 @@
|
||||
#include "../../include/util.h"
|
||||
#include "../../include/pkg.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <dirent.h>
|
||||
#include <libgen.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
bool lm_pool_list_load(lm_pool_t *pool, char *file){
|
||||
if(NULL == pool || NULL == file){
|
||||
bool lm_pool_list_load(lm_pool_t *pool){
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!can_read(file)){
|
||||
if(NULL == pool->paths.list)
|
||||
return false;
|
||||
|
||||
if(!can_read(pool->paths.list)){
|
||||
lm_error_set(LM_ERR_PoolListCantRead);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t file_len = strlen(file);
|
||||
size_t file_len = strlen(pool->paths.list);
|
||||
char file_copy[file_len + 1];
|
||||
char file_copy2[file_len + 1];
|
||||
char *list_dir = NULL, *list_name = NULL;
|
||||
|
||||
memcpy(file_copy, file, file_len+1);
|
||||
memcpy(file_copy2, file, file_len+1);
|
||||
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);
|
||||
@ -46,7 +49,7 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
|
||||
DIR *dirfd = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(!extract_archive(extract_dir, file))
|
||||
if(!extract_archive(extract_dir, pool->paths.list))
|
||||
goto end;
|
||||
|
||||
if((dirfd = opendir(extract_dir)) == NULL){
|
||||
@ -87,7 +90,15 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_pool_list_get(lm_pool_t *pool, char *file) {
|
||||
bool lm_pool_list_get(lm_pool_t *pool) {
|
||||
if(NULL == pool){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(NULL == pool->paths.list)
|
||||
return false;
|
||||
|
||||
if(!pool->available)
|
||||
return false;
|
||||
|
||||
@ -108,13 +119,13 @@ bool lm_pool_list_get(lm_pool_t *pool, char *file) {
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_recvfile(sock, file))
|
||||
if(!lm_mptp_recvfile(sock, pool->paths.list))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_list_load(pool, file);
|
||||
ret = lm_pool_list_load(pool);
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
#include "../../include/types.h"
|
||||
#include "../../include/pool.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void lm_pool_paths_set_info(lm_pool_t *pool, char *info_path){
|
||||
free(pool->paths.info);
|
||||
@ -21,6 +23,10 @@ void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path){
|
||||
pool->paths.list = strdup(list_path);
|
||||
}
|
||||
|
||||
bool lm_pool_paths_is_empty(lm_pool_t *pool){
|
||||
return NULL == pool->paths.info || NULL == pool->paths.list;
|
||||
}
|
||||
|
||||
void lm_pool_paths_free(lm_pool_t *pool){
|
||||
free(pool->paths.info);
|
||||
free(pool->paths.list);
|
||||
|
@ -56,6 +56,8 @@ void lm_pool_test(lm_pool_t *pool) {
|
||||
}
|
||||
|
||||
pool->available = MPTP_FLAGS_CODE(&packet) == MPTP_S2C_PONG;
|
||||
if(!pool->available)
|
||||
lm_error_set(LM_ERR_PoolTestNotPong);
|
||||
end:
|
||||
lm_mptp_close(sock);
|
||||
return;
|
||||
|
@ -28,7 +28,7 @@ void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr
|
||||
// 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); break;
|
||||
break;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user