new: implement pool info for client/server
This commit is contained in:
@ -3,11 +3,13 @@
|
||||
#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;
|
||||
@ -16,11 +18,11 @@ int lm_pool_info_handler(void *data, const char *_section, const char *_key, con
|
||||
if (!eq(pool->name, section))
|
||||
return 0;
|
||||
|
||||
if (eq(key, "size"))
|
||||
if (eq(key, POOL_INFO_SIZE))
|
||||
pool->info.size = atol(value);
|
||||
else if (eq(key, "maintainer"))
|
||||
else if (eq(key, POOL_INFO_MAINTAINER))
|
||||
pool->info.maintainer = strdup(value);
|
||||
else if (eq(key, "pubkey"))
|
||||
else if (eq(key, POOL_INFO_PUBKEY))
|
||||
pool->info.pubkey = strdup(value);
|
||||
else
|
||||
return 0;
|
||||
@ -31,6 +33,11 @@ int lm_pool_info_handler(void *data, const char *_section, const char *_key, con
|
||||
bool lm_pool_info_load(lm_pool_t *pool, char *file) {
|
||||
lm_pool_info_free(pool);
|
||||
|
||||
if(!can_read(file)){
|
||||
lm_error_set(LM_ERR_PoolInfoCantRead);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ini_parse(file, lm_pool_info_handler, pool) < 0) {
|
||||
lm_error_set(LM_ERR_PoolInfoBad);
|
||||
return false;
|
||||
@ -65,18 +72,15 @@ bool lm_pool_info_get(lm_pool_t *pool, char *file) {
|
||||
if(!lm_mptp_client_send(sock, &packet))
|
||||
goto end;
|
||||
|
||||
if(!lm_mptp_client_recv(sock, &packet))
|
||||
goto end;
|
||||
|
||||
while(lm_mptp_client_recv(sock, &packet)){
|
||||
if(!lm_mptp_client_verify(&packet))
|
||||
goto end;
|
||||
|
||||
if(MPTP_IS_LAST(&packet))
|
||||
break;
|
||||
|
||||
if(fwrite(packet.data, 1, packet.header.data_size, info)==0)
|
||||
goto end;
|
||||
|
||||
if(MPTP_IS_LAST(&packet))
|
||||
break;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
|
@ -10,6 +10,16 @@
|
||||
#include <string.h>
|
||||
|
||||
bool lm_pool_list_load(lm_pool_t *pool, char *file){
|
||||
if(NULL == pool || NULL == file){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!can_read(file)){
|
||||
lm_error_set(LM_ERR_PoolListCantRead);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t file_len = strlen(file), ent_len = 0;
|
||||
char filecp[file_len+1], *dir = NULL;
|
||||
struct dirent *ent = NULL;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "../../include/error.h"
|
||||
#include "../../include/util.h"
|
||||
#include "../../include/pool.h"
|
||||
#include "../../include/pkg.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -89,5 +90,13 @@ bool lm_pool_add(lm_pool_t *pool, lm_pkg_t *pkg){
|
||||
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_pkg_free(prev);
|
||||
}
|
||||
|
||||
free(pool);
|
||||
}
|
||||
|
@ -19,7 +19,15 @@ void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr
|
||||
FILE *info = fopen(pool->info.file, "r");
|
||||
size_t read = 0;
|
||||
|
||||
if(NULL == info){
|
||||
lm_mptp_init(packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
}
|
||||
|
||||
lm_mptp_init(packet, false, MPTP_S2C_COOL, false);
|
||||
|
||||
while ((read = fread(packet->data, 1, MPTP_DATA_MAX, info)) > 0) {
|
||||
packet->header.data_size = read;
|
||||
lm_mptp_server_send(sock, packet, addr);
|
||||
lm_mptp_init(packet, false, MPTP_S2C_COOL, false);
|
||||
}
|
||||
|
Reference in New Issue
Block a user