update: recvfile and sendfile callbacks and more ctx function implementations

This commit is contained in:
ngn
2024-07-05 16:27:11 +03:00
parent 6045f73478
commit 98e5c861df
21 changed files with 1359 additions and 429 deletions

83
src/ctx/download.c Normal file
View File

@ -0,0 +1,83 @@
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct __lm_ctx_download_cb_data {
lm_ctx_t *ctx;
lm_pkg_t *pkg;
lm_ctx_download_callback_t callback;
void *data;
};
bool __lm_ctx_download_callback(char *path, size_t current, size_t total, void *data){
struct __lm_ctx_download_cb_data *cbdata = data;
if(NULL == cbdata->callback)
return true;
if(eq(cbdata->pkg->paths.archive, path))
return cbdata->callback(cbdata->ctx, cbdata->pkg, true, current, total, cbdata->data);
else
return cbdata->callback(cbdata->ctx, cbdata->pkg, false, current, total, cbdata->data);
}
bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback, void *data){
if(NULL == ctx || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->pool){
lm_error_set(LM_ERR_PkgNoPool);
return false;
}
lm_pool_t *pool = pkg->pool;
if(!pool->available){
lm_error_set(LM_ERR_PoolNotAvailable);
return false;
}
char path[strlen(pool->url.path)+strlen(pkg->name)+strlen(pkg->version)+20];
char name[strlen(pkg->name)+strlen(pkg->version)+10];
struct __lm_ctx_download_cb_data cbdata = {
.ctx = ctx,
.pkg = pkg,
.data = data,
.callback = callback
};
lm_mptp_t packet;
bool ret = false;
int sock = -1;
snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version);
join(path, pool->url.path, name);
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
return false;
lm_mptp_init(&packet, true, MPTP_C2S_PULL, true);
lm_mptp_set_host(&packet, pool->url.host);
lm_mptp_set_data(&packet, path, strlen(path));
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pkg->paths.archive, __lm_ctx_download_callback, &cbdata))
goto end;
if(!lm_mptp_recvfile(sock, pkg->paths.signature, __lm_ctx_download_callback, &cbdata))
goto end;
ret = true;
end:
lm_mptp_close(sock);
return ret;
}

View File

@ -70,7 +70,7 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call
FILE *hashes = NULL;
bool ret = false;
if((hashes = fopen(hashes_file, "r")) != NULL){
if((hashes = fopen(hashes_file, "r")) == NULL){
lm_error_set(LM_ERR_PkgHashesOpenFail);
return false;
}

23
src/ctx/ping.c Normal file
View File

@ -0,0 +1,23 @@
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdlib.h>
#include <stdio.h>
void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data){
lm_pool_t *cur = ctx->pools;
while (NULL != cur) {
lm_pool_test(cur);
if (!cur->available)
pdebug(__func__, "(%s) not avaliable: %s", cur->name, lm_strerror());
if(NULL != callback)
if(!callback(ctx, cur, cur->available, data))
break;
cur = cur->next;
}
}

View File

@ -2,13 +2,9 @@
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/error.h"
#include "../../include/thpool.h"
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) {
@ -61,22 +57,6 @@ void lm_ctx_pool_clear(lm_ctx_t *ctx) {
ctx->pools = NULL;
}
void lm_ctx_pool_test(lm_ctx_t *ctx, lm_ctx_pool_callback_t callback, void *data) {
lm_pool_t *cur = ctx->pools;
while (NULL != cur) {
lm_pool_test(cur);
if (!cur->available)
pdebug(__func__, "(%s) not avaliable: %s", cur->name, lm_strerror());
if(NULL != callback)
if(!callback(ctx, cur, cur->available, data))
break;
cur = cur->next;
}
}
lm_pool_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name) {
lm_pool_t *cur = ctx->pools;
while (NULL != cur) {
@ -96,164 +76,3 @@ lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path) {
}
return NULL;
}
void lm_ctx_pool_get_info(lm_ctx_t *ctx, bool allow_update, bool force_update, lm_ctx_pool_callback_t callback, void *data){
lm_pool_t *cur = ctx->pools;
bool status = false;
while(NULL != cur){
if(lm_pool_path_is_empty(cur)){
pdebug(__func__, "(%s) failed to load info, pool paths are empty", cur->name);
goto next;
}
if(force_update)
goto update;
if(!lm_pool_info_load(cur)){
pdebug(__func__, "(%s) failed to load info: %s", cur->name, lm_strerror());
if(!allow_update)
goto next;
pdebug(__func__, "(%s) gonna try updating", cur->name);
}
goto success;
update:
if(!lm_pool_info_download(cur)){
pdebug(__func__, "(%s) failed to update info: %s", cur->name, lm_strerror());
goto next;
}
success:
pdebug(__func__, "(%s) loaded pool info", cur->name);
status = true;
next:
if(NULL != callback)
callback(ctx, cur, status, data);
status = false;
cur = cur->next;
}
}
void lm_ctx_pool_get_list(lm_ctx_t *ctx, bool allow_update, bool force_update, lm_ctx_pool_callback_t callback, void *data){
lm_pool_t *cur = ctx->pools;
bool status = false;
while(NULL != cur){
if(lm_pool_path_is_empty(cur)){
pdebug(__func__, "(%s) failed to load list, pool paths are empty", cur->name);
goto next;
}
if(force_update)
goto update;
if(!lm_pool_list_load(cur)){
pdebug(__func__, "(%s) failed to load list: %s", cur->name, lm_strerror());
if(!allow_update)
goto next;
pdebug(__func__, "(%s) gonna try updating", cur->name);
}
goto success;
update:
if(!lm_pool_list_download(cur)){
pdebug(__func__, "(%s) failed to update list: %s", cur->name, lm_strerror());
goto next;
}
success:
pdebug(__func__, "(%s) loaded pool list", cur->name);
status = true;
next:
if(NULL != callback)
callback(ctx, cur, status, data);
status = false;
cur = cur->next;
}
}
bool lm_ctx_pool_serve(lm_ctx_t *ctx, char *addr, uint8_t threads) {
if (NULL == addr || threads < 0) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
struct sockaddr saddr;
char *host = NULL;
lm_mptp_t packet;
uint16_t port;
int sock;
lm_thpool_t tp;
lm_thpool_init(&tp, threads);
if (!parse_host(addr, host, &port))
return false;
if (port == 0) {
lm_error_set(LM_ERR_NoPort);
return false;
}
if ((sock = lm_mptp_server_listen(addr, port)) < 0)
return false;
while (lm_mptp_server_recv(sock, &packet, &saddr)) {
if (!lm_mptp_server_verify(&packet)) {
pdebug(__func__, "skipping packet, failed to verify: %s", lm_strerror());
continue;
}
char hostname[packet.header.host_size + 1]; // +1 for NULL terminator
char path[packet.header.data_size + 1], *ppath = path;
if (!lm_mptp_get_host(&packet, hostname)) {
pdebug(__func__, "skipping packet, failed to get hostname: %s", lm_strerror());
continue;
}
if (!lm_mptp_get_data(&packet, path)) {
pdebug(__func__, "skipping packet, failed to get path: %s", lm_strerror());
continue;
}
if(MPTP_FLAGS_CODE(&packet) == MPTP_C2S_PULL && (ppath = dirname(path)) == NULL){
pdebug(__func__, "skipping packet, failed to get dirname: %s", strerror(errno));
continue;
}
lm_pool_t *pool = lm_ctx_pool_by_url(ctx, hostname, ppath);
if (NULL == pool) {
pdebug(__func__, "unknown pool (%s), closing connection", hostname);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr);
continue;
}
if(lm_pool_path_is_empty(pool)){
pdebug(__func__, "requested pool (%s) have empty paths, closing connection", pool->name);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr);
continue;
}
lm_pool_thread_arg_t *arg = malloc(sizeof(lm_pool_thread_arg_t));
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
lm_mptp_copy(&arg->packet, &packet);
arg->pool = pool;
arg->sock = sock;
pdebug(__func__, "adding new connection to the pool");
lm_thpool_add(&tp, lm_pool_serve_thread, arg);
}
lm_thpool_stop(&tp);
return true;
}

177
src/ctx/serve.c Normal file
View File

@ -0,0 +1,177 @@
#include "../../include/thpool.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr) {
char ipaddr[INET6_ADDRSTRLEN];
bzero(ipaddr, sizeof(ipaddr));
sockaddr_to_str(addr, ipaddr);
switch (MPTP_FLAGS_CODE(packet)) {
// response PING with PONG
case MPTP_C2S_PING:
pdebug(__func__, "(%s) PING from %s returning PONG", pool->name, ipaddr);
lm_mptp_init(packet, false, MPTP_S2C_PONG, true);
lm_mptp_server_send(sock, packet, addr);
break;
// 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_file, NULL, NULL);
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_file, NULL, NULL);
break;
// when the request code is PULL, send the requested package archive and
// requested package signature
case MPTP_C2S_PULL:
// PULL request should contain package path,
// if path (stored in the data field) is empty it's an invalid request
if(packet->header.data_size <= 0){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
pdebug(__func__, "(%s) PULL from %s attempting to send package archive and signature", pool->name, ipaddr);
char path[packet->header.data_size + 1], *package = path;
if(!lm_mptp_get_data(packet, path)){
// we should never be able to get here, if we do theres definetly a bug
pdebug(__func__, "(%s) skipping PULL from %s, failed to get path: %s", pool->name, ipaddr, lm_strerror());
break;
}
// if we can't get the package name, then theres something wrong with the request
if((package = basename(path)) == NULL){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
size_t package_size = strlen(package);
char name[package_size+1], version[package_size+1];
lm_pkg_t *pkg = NULL;
// if we can't parse the package name, request is invalid
if(!package_parse(package, name, version)){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
// if the package is not found in the pool, tell the client
if((pkg = lm_pool_package_find(pool, name, version)) == NULL){
lm_mptp_init(packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
// send package archive and the signature file
lm_mptp_sendfile(sock, addr, pkg->paths.archive, NULL, NULL);
lm_mptp_sendfile(sock, addr, pkg->paths.signature, NULL, NULL);
break;
}
}
void __lm_ctx_serve_thread(void *_arg) {
lm_pool_thread_arg_t *arg = _arg;
__lm_ctx_serve(arg->pool, &arg->packet, arg->sock, &arg->addr);
free(arg);
}
bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads){
if (NULL == addr || threads < 0) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
struct sockaddr saddr;
char *host = NULL;
lm_mptp_t packet;
uint16_t port;
int sock;
lm_thpool_t tp;
lm_thpool_init(&tp, threads);
if (!parse_host(addr, host, &port))
return false;
if (port == 0) {
lm_error_set(LM_ERR_NoPort);
return false;
}
if ((sock = lm_mptp_server_listen(addr, port)) < 0)
return false;
while (lm_mptp_server_recv(sock, &packet, &saddr)) {
if (!lm_mptp_server_verify(&packet)) {
pdebug(__func__, "skipping packet, failed to verify: %s", lm_strerror());
continue;
}
char hostname[packet.header.host_size + 1]; // +1 for NULL terminator
char path[packet.header.data_size + 1], *ppath = path;
if (!lm_mptp_get_host(&packet, hostname)) {
pdebug(__func__, "skipping packet, failed to get hostname: %s", lm_strerror());
continue;
}
if (!lm_mptp_get_data(&packet, path)) {
pdebug(__func__, "skipping packet, failed to get path: %s", lm_strerror());
continue;
}
if(MPTP_FLAGS_CODE(&packet) == MPTP_C2S_PULL && (ppath = dirname(path)) == NULL){
pdebug(__func__, "skipping packet, failed to get dirname: %s", strerror(errno));
continue;
}
lm_pool_t *pool = lm_ctx_pool_by_url(ctx, hostname, ppath);
if (NULL == pool) {
pdebug(__func__, "unknown pool (%s), closing connection", hostname);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr);
continue;
}
if(lm_pool_path_is_empty(pool)){
pdebug(__func__, "requested pool (%s) have empty paths, closing connection", pool->name);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet, &saddr);
continue;
}
lm_pool_thread_arg_t *arg = malloc(sizeof(lm_pool_thread_arg_t));
memcpy(&arg->addr, &saddr, sizeof(struct sockaddr));
lm_mptp_copy(&arg->packet, &packet);
arg->pool = pool;
arg->sock = sock;
pdebug(__func__, "adding new connection to the pool");
lm_thpool_add(&tp, __lm_ctx_serve_thread, arg);
}
lm_thpool_stop(&tp);
return true;
}

103
src/ctx/sync.c Normal file
View File

@ -0,0 +1,103 @@
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct __lm_ctx_sync_cb_data {
lm_ctx_t *ctx;
lm_pool_t *pool;
lm_ctx_sync_callback_t callback;
lm_ctx_sync_state_t state;
void *data;
};
bool __lm_ctx_sync_callback(char *path, size_t current, size_t total, void *data){
struct __lm_ctx_sync_cb_data *cbdata = data;
if(NULL == cbdata->callback)
return true;
return cbdata->callback(cbdata->ctx, cbdata->pool, cbdata->state, current, total, data);
}
bool lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data){
struct __lm_ctx_sync_cb_data cbdata = {
.ctx = ctx,
.callback = callback,
.data = data,
};
lm_pool_t *cur = ctx->pools;
bool status = false;
while(NULL != cur){
cbdata.pool = cur;
cbdata.state = SYNC_DOWNLOADING_INFO;
if(lm_pool_path_is_empty(cur)){
pdebug(__func__, "(%s) failed to load info, pool paths are empty", cur->name);
goto next_info;
}
if(!do_update && !lm_pool_info_load(cur)){
pdebug(__func__, "(%s) failed to load info: %s", cur->name, lm_strerror());
goto next_info;
}
else if(do_update && !lm_pool_info_download(cur, __lm_ctx_sync_callback, &cbdata)) {
pdebug(__func__, "(%s) failed to update info: %s", cur->name, lm_strerror());
goto next_info;
}
pdebug(__func__, "(%s) loaded pool info", cur->name);
status = true;
next_info:
if(NULL != callback && status)
callback(ctx, cur, SYNC_INFO_SUCCESS, 0, 0, data);
else if(NULL != callback && !status)
callback(ctx, cur, SYNC_INFO_FAIL, 0, 0, data);
status = false;
cur = cur->next;
}
cur = ctx->pools;
status = false;
while(NULL != cur){
cbdata.pool = cur;
cbdata.state = SYNC_DOWNLOADING_LIST;
if(lm_pool_path_is_empty(cur)){
pdebug(__func__, "(%s) failed to load list, pool paths are empty", cur->name);
goto next_list;
}
if(!do_update && !lm_pool_list_load(cur)){
pdebug(__func__, "(%s) failed to load list: %s", cur->name, lm_strerror());
goto next_list;
}
else if(do_update && !lm_pool_list_download(cur, __lm_ctx_sync_callback, &cbdata)) {
pdebug(__func__, "(%s) failed to update list: %s", cur->name, lm_strerror());
goto next_list;
}
pdebug(__func__, "(%s) loaded pool list", cur->name);
status = true;
next_list:
if(NULL != callback && status)
callback(ctx, cur, SYNC_LIST_SUCCESS, 0, 0, data);
else if(NULL != callback && !status)
callback(ctx, cur, SYNC_LIST_FAIL, 0, 0, data);
status = false;
cur = cur->next;
}
return true;
}

View File

@ -47,6 +47,7 @@ void lm_error_set(lm_error_t code, ...) {
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
{.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") },
{.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") },
{.code = LM_ERR_NoPort, .desc = _("host port not specified") },
{.code = LM_ERR_PoolInfoBad, .desc = _("pool info is badly formatted or is not complete") },
{.code = LM_ERR_ArcWBlockFail, .desc = _("failed to write block from archive") },
@ -112,6 +113,23 @@ void lm_error_set(lm_error_t code, ...) {
{.code = LM_ERR_InstallDownloadFail, .desc = _("failed to download %s for installation: %s") },
{.code = LM_ERR_PkgNotDownloaded, .desc = _("package is not downloaded") },
{.code = LM_ERR_PkgRemoveDownloadFail, .desc = _("failed to remove downloaded package") },
{.code = LM_ERR_PkgRemoveDownloadFail, .desc = _("failed to remove downloaded package") },
{.code = LM_ERR_DstOpenFail, .desc = _("failed to open the destination file") },
{.code = LM_ERR_SrcOpenFail, .desc = _("failed to open the source file") },
{.code = LM_ERR_DstWriteFail, .desc = _("failed to write to the destination file") },
{.code = LM_ERR_DstWriteFail, .desc = _("failed to write to the destination file") },
{.code = LM_ERR_PkgNoPool, .desc = _("package does not have associated pool") },
{.code = LM_ERR_CtxTempFailMkdir, .desc = _("failed to create specified temp directory") },
{.code = LM_ERR_PkgBadArchive, .desc = _("package archive does not contain required files") },
{.code = LM_ERR_PkgDataNotMatch, .desc = _("package data does not match with target package") },
{.code = LM_ERR_PkgChangesUpdateFail, .desc = _("failed to update changes file for package: %s") },
{.code = LM_ERR_PkgHashesOpenFail, .desc = _("failed to access package hashes file") },
{.code = LM_ERR_DbChangesUnlinkFail, .desc = _("failed to remove package changes file from the database") },
{.code = LM_ERR_SendStatFail, .desc = _("failed to stat target file for sending") },
{.code = LM_ERR_SendSnprintfFail, .desc = _("failed to format target file size for sending") },
{.code = LM_ERR_SendReadFail, .desc = _("failed to read target file size for sending") },
{.code = LM_ERR_RecvBadSize, .desc = _("failed to parse target file size for receiving") },
{.code = LM_ERR_RecvNotCompleted, .desc = _("target file is not fully received") },
};
char *fmt = NULL;

View File

@ -2,13 +2,15 @@
#include "../../include/error.h"
#include "../../include/util.h"
#include <sys/stat.h>
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
if (NULL == path){
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path, lm_mptp_transfer_callback_t callback, void *data){
if (NULL == path || NULL == addr){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
@ -17,7 +19,10 @@ bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
bool ret = false;
FILE *file = fopen(path, "r");
size_t total = 0, current = 0;
size_t read = 0;
struct stat st;
int size = -1;
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
@ -25,16 +30,51 @@ bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
if(fstat(fileno(file), &st)<0){
pdebug(__func__, "failed to stat file: %s", path);
lm_error_set(LM_ERR_SendStatFail);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
total = st.st_size;
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
if((size = snprintf(packet.data, MPTP_DATA_MAX, "%lu", st.st_size)) <= 0){
pdebug(__func__, "snprintf for stat size failed: %s", path);
lm_error_set(LM_ERR_SendSnprintfFail);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
packet.header.data_size = size;
lm_mptp_server_send(sock, &packet, addr);
// clear the packet
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
packet.header.data_size = read;
if(!lm_mptp_server_send(sock, &packet, addr))
goto end;
current += read;
if(NULL != callback)
if(!callback(path, current, st.st_size, data))
goto end;
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
}
if(current != total){
pdebug(__func__, "failed read the entire file (left at %lu/%lu): %s", current, total, path);
lm_error_set(LM_ERR_SendReadFail);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
ret = true;
@ -45,7 +85,7 @@ end:
return ret;
}
bool lm_mptp_recvfile(int sock, char *path){
bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback, void *data){
if(NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
@ -57,6 +97,7 @@ bool lm_mptp_recvfile(int sock, char *path){
}
FILE *file = fopen(path, "a");
size_t total = 0, current = 0;
bool ret = false;
lm_mptp_t packet;
@ -66,6 +107,23 @@ bool lm_mptp_recvfile(int sock, char *path){
goto end;
}
if(!lm_mptp_client_recv(sock, &packet) || !lm_mptp_client_verify(&packet))
goto end;
if(MPTP_FLAGS_CODE(&packet) != MPTP_S2C_COOL){
lm_error_set(LM_ERR_RecvBadCode);
goto end;
}
char buffer[MPTP_DATA_MAX+1];
lm_mptp_get_data(&packet, buffer);
total = atol(buffer);
if(total <= 0){
lm_error_set(LM_ERR_RecvBadSize);
goto end;
}
while(lm_mptp_client_recv(sock, &packet)){
if(!lm_mptp_client_verify(&packet))
goto end;
@ -82,6 +140,18 @@ bool lm_mptp_recvfile(int sock, char *path){
lm_error_set(LM_ERR_RecvWriteFail);
goto end;
}
current += packet.header.data_size;
if(NULL != callback)
if(!callback(path, current, total, data))
goto end;
}
if(current != total){
pdebug(__func__, "failed to receive the entire file (left at %lu/%lu): %s (%s)", current, total, path, lm_strerror());
lm_error_set(LM_ERR_RecvNotCompleted);
goto end;
}
ret = true;

View File

@ -60,7 +60,7 @@ bool lm_pool_info_load(lm_pool_t *pool) {
return true;
}
bool lm_pool_info_download(lm_pool_t *pool) {
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;
@ -100,7 +100,7 @@ bool lm_pool_info_download(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.info_file))
if(!lm_mptp_recvfile(sock, pool->paths.info_file, callback, data))
goto end;
ret = true;

View File

@ -85,7 +85,7 @@ end:
return ret;
}
bool lm_pool_list_download(lm_pool_t *pool) {
bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data) {
if(NULL == pool){
lm_error_set(LM_ERR_ArgNULL);
return false;
@ -130,7 +130,7 @@ bool lm_pool_list_download(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pool->paths.list_file))
if(!lm_mptp_recvfile(sock, pool->paths.list_file, callback, data))
goto end;
ret = true;

View File

@ -59,46 +59,3 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
pool->pkg = pkg;
return true;
}
bool lm_pool_package_download(lm_pool_t *pool, lm_pkg_t *pkg){
if(NULL == pool || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(lm_package_path_is_empty(pkg)){
lm_error_set(LM_ERR_PkgPathsEmpty);
return false;
}
char data[strlen(pool->url.path)+strlen(pkg->name)+strlen(pkg->version)+20];
char name[strlen(pkg->name)+strlen(pkg->version)+10];
lm_mptp_t packet;
bool ret = false;
int sock = -1;
snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version);
join(data, pool->url.path, name);
if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0)
return false;
lm_mptp_init(&packet, true, MPTP_C2S_PULL, true);
lm_mptp_set_host(&packet, pool->url.host);
lm_mptp_set_data(&packet, data, strlen(data));
if(!lm_mptp_client_send(sock, &packet))
goto end;
if(!lm_mptp_recvfile(sock, pkg->paths.archive))
goto end;
if(!lm_mptp_recvfile(sock, pkg->paths.signature))
goto end;
ret = true;
end:
lm_mptp_close(sock);
return ret;
}

View File

@ -1,93 +0,0 @@
#include "../../include/error.h"
#include "../../include/util.h"
#include "../../include/pool.h"
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr) {
char ipaddr[INET6_ADDRSTRLEN];
bzero(ipaddr, sizeof(ipaddr));
sockaddr_to_str(addr, ipaddr);
switch (MPTP_FLAGS_CODE(packet)) {
// response PING with PONG
case MPTP_C2S_PING:
pdebug(__func__, "(%s) PING from %s returning PONG", pool->name, ipaddr);
lm_mptp_init(packet, false, MPTP_S2C_PONG, true);
lm_mptp_server_send(sock, packet, addr);
break;
// 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_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_file);
break;
// when the request code is PULL, send the requested package archive and
// requested package signature
case MPTP_C2S_PULL:
// PULL request should contain package path,
// if path (stored in the data field) is empty it's an invalid request
if(packet->header.data_size <= 0){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
pdebug(__func__, "(%s) PULL from %s attempting to send package archive and signature", pool->name, ipaddr);
char path[packet->header.data_size + 1], *package = path;
if(!lm_mptp_get_data(packet, path)){
// we should never be able to get here, if we do theres definetly a bug
pdebug(__func__, "(%s) skipping PULL from %s, failed to get path: %s", pool->name, ipaddr, lm_strerror());
break;
}
// if we can't get the package name, then theres something wrong with the request
if((package = basename(path)) == NULL){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
size_t package_size = strlen(package);
char name[package_size+1], version[package_size+1];
lm_pkg_t *pkg = NULL;
// if we can't parse the package name, request is invalid
if(!package_parse(package, name, version)){
lm_mptp_init(packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
// if the package is not found in the pool, tell the client
if((pkg = lm_pool_package_find(pool, name, version)) == NULL){
lm_mptp_init(packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, packet, addr);
break;
}
// send package archive and the signature file
lm_mptp_sendfile(sock, addr, pkg->paths.archive);
lm_mptp_sendfile(sock, addr, pkg->paths.signature);
break;
}
}
void lm_pool_serve_thread(void *_arg) {
lm_pool_thread_arg_t *arg = _arg;
lm_pool_serve(arg->pool, &arg->packet, arg->sock, &arg->addr);
free(arg);
}