119 lines
2.7 KiB
C
119 lines
2.7 KiB
C
#include "../../include/error.h"
|
|
#include "../../include/pool.h"
|
|
#include "../../include/mptp.h"
|
|
#include "../../include/util.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ini.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;
|
|
lm_pool_t *pool = data;
|
|
|
|
if (!eq(pool->name, section))
|
|
return 0;
|
|
|
|
if (eq(key, POOL_INFO_SIZE))
|
|
pool->info.size = atol(value);
|
|
else if (eq(key, POOL_INFO_MAINTAINER))
|
|
pool->info.maintainer = strdup(value);
|
|
else if (eq(key, POOL_INFO_PUBKEY))
|
|
pool->info.pubkey = strdup(value);
|
|
else
|
|
return 0;
|
|
|
|
return 1;
|
|
}
|
|
|
|
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(lm_pool_path_is_empty(pool)){
|
|
lm_error_set(LM_ERR_PoolPathsEmpty);
|
|
return false;
|
|
}
|
|
|
|
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_file, lm_pool_info_handler, pool) < 0) {
|
|
lm_error_set(LM_ERR_PoolInfoBad);
|
|
return false;
|
|
}
|
|
|
|
if(pool->info.size <= 0 ||
|
|
pool->info.pubkey == NULL ||
|
|
pool->info.maintainer == NULL){
|
|
lm_error_set(LM_ERR_PoolInfoBad);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data) {
|
|
if(NULL == pool){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
if(!pool->available){
|
|
lm_error_set(LM_ERR_PoolNotAvailable);
|
|
return false;
|
|
}
|
|
|
|
if(lm_pool_path_is_empty(pool)){
|
|
lm_error_set(LM_ERR_PoolPathsEmpty);
|
|
return false;
|
|
}
|
|
|
|
if(!mkdir_ifnot(pool->paths.dir)){
|
|
lm_error_set(LM_ERR_PoolBadDir);
|
|
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;
|
|
bool ret = false;
|
|
|
|
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
|
|
return false;
|
|
|
|
lm_mptp_init(&packet, true, MPTP_C2S_INFO, true);
|
|
lm_mptp_set_host(&packet, pool->url.host);
|
|
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
|
|
|
if(!lm_mptp_client_send(sock, &packet))
|
|
goto end;
|
|
|
|
if(!lm_mptp_recvfile(sock, pool->paths.info_file, callback, data))
|
|
goto end;
|
|
|
|
ret = true;
|
|
end:
|
|
lm_mptp_close(sock);
|
|
if(ret)
|
|
ret = lm_pool_info_load(pool);
|
|
return ret;
|
|
}
|
|
|
|
void lm_pool_info_free(lm_pool_t *pool) {
|
|
free(pool->info.pubkey);
|
|
free(pool->info.maintainer);
|
|
bzero(&pool->info, sizeof(pool->info));
|
|
}
|