new: implement PULL command, add join functions

This commit is contained in:
ngn
2024-07-01 06:43:20 +03:00
parent 5513abb371
commit 3c603fef22
37 changed files with 791 additions and 338 deletions

View File

@ -48,6 +48,14 @@ bool lm_pool_info_load(lm_pool_t *pool) {
return false;
}
if(pool->info.size <= 0 ||
pool->info.pubkey == NULL ||
pool->info.maintainer == NULL){
printf("%lu %s %s\n", pool->info.size, pool->info.pubkey, pool->info.maintainer);
lm_error_set(LM_ERR_PoolInfoBad);
return false;
}
return true;
}
@ -69,10 +77,13 @@ bool lm_pool_info_get(lm_pool_t *pool) {
if(NULL == pool->url.path)
return false;
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
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));

View File

@ -1,8 +1,8 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/mptp.h"
#include "../../include/util.h"
#include "../../include/pkg.h"
#include <stdbool.h>
#include <dirent.h>
@ -24,9 +24,9 @@ bool lm_pool_list_load(lm_pool_t *pool){
}
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;
char file_copy[file_len + 1], file_copy2[file_len + 1];
char extract_dir[file_len+15];
memcpy(file_copy, pool->paths.list, file_len+1);
memcpy(file_copy2, pool->paths.list, file_len+1);
@ -34,9 +34,10 @@ bool lm_pool_list_load(lm_pool_t *pool){
list_dir = dirname(file_copy);
list_name = basename(file_copy2);
char extract_dir[file_len+15];
snprintf(extract_dir, sizeof(extract_dir), "%s/%s_extracted", list_dir, list_name);
char extract_name[strlen(list_name)+20];
snprintf(extract_name, sizeof(extract_name), "%s_extracted", list_name);
join(extract_dir, list_dir, extract_name);
pdebug(__func__, "extracting pool to %s", extract_dir);
if(!mkdir_ifnot(extract_dir)){
@ -63,19 +64,19 @@ bool lm_pool_list_load(lm_pool_t *pool){
ent_len = strlen(ent->d_name);
char datap[ent_len+sizeof(extract_dir)+10];
snprintf(datap, sizeof(datap), "%s/%s/DATA", extract_dir, ent->d_name);
join_multiple(datap, extract_dir, ent->d_name, "DATA");
lm_pkg_t *pkg = lm_pkg_new();
lm_pkg_t *pkg = lm_package_new();
if(!lm_pkg_data_load(pkg, datap)){
if(!lm_package_data_load(pkg, datap)){
pdebug(__func__, "(%s) failed to load new package from %s", pool->name, datap);
lm_pkg_free(pkg);
lm_package_free(pkg);
continue;
}
if(!lm_pool_add(pool, pkg)){
if(!lm_pool_package_add(pool, pkg)){
pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->name);
lm_pkg_free(pkg);
lm_package_free(pkg);
continue;
}
@ -108,10 +109,13 @@ bool lm_pool_list_get(lm_pool_t *pool) {
if(NULL == pool->url.path)
return false;
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
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_LIST, true);
lm_mptp_set_host(&packet, pool->url.host);
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));

113
src/pool/package.c Normal file
View File

@ -0,0 +1,113 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include "../../include/pool.h"
#include <stdbool.h>
#include <string.h>
lm_pkg_t *lm_pool_package_find(lm_pool_t *pool, char *name, char *version){
if(NULL == name && NULL == version){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_pkg_t *cur = pool->pkg;
while(NULL != cur){
if(NULL == version && eq(cur->name, name))
return cur;
else if(NULL == name && eq(cur->version, version))
return cur;
else if(eq(cur->name, name) && eq(cur->version, version))
return cur;
cur = cur->next;
}
return NULL;
}
bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
if(NULL == pool || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(!lm_pool_path_is_empty(pool)){
size_t path_size = strlen(pool->paths.packages)+strlen(pkg->name)+strlen(pkg->version);
size_t name_size = path_size - strlen(pool->paths.packages);
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);
join(archive_path, pool->paths.packages, archive_name);
join(sig_path, pool->paths.packages, sig_name);
lm_package_path_set_archive(pkg, archive_path);
lm_package_path_set_signature(pkg, sig_path);
}
if(NULL == pool->pkg){
pool->pkg = pkg;
return true;
}
lm_pkg_t *cur = pool->pkg;
while(NULL != cur){
if(NULL == cur->next){
cur->next = pkg;
return true;
}
cur = cur->next;
}
return false;
}
bool lm_pool_package_get(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_PackagePathsEmpty);
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;
}

60
src/pool/path.c Normal file
View File

@ -0,0 +1,60 @@
#include "../../include/types.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <string.h>
bool lm_pool_path_set_info(lm_pool_t *pool, char *info_path){
free(pool->paths.info);
pool->paths.info = NULL;
if(NULL == info_path)
return true;
if(is_dir(info_path))
return false;
pool->paths.info = strdup(info_path);
return true;
}
bool lm_pool_path_set_list(lm_pool_t *pool, char *list_path){
free(pool->paths.list);
pool->paths.list = NULL;
if(NULL == list_path)
return true;
if(is_dir(list_path))
return false;
pool->paths.list = strdup(list_path);
return true;
}
bool lm_pool_path_set_packages(lm_pool_t *pool, char *packages_path){
free(pool->paths.packages);
pool->paths.packages = NULL;
if(NULL == packages_path)
return true;
if(is_file(packages_path))
return false;
pool->paths.packages = strdup(packages_path);
return true;
}
bool lm_pool_path_is_empty(lm_pool_t *pool){
return NULL == pool->paths.info ||
NULL == pool->paths.list ||
NULL == pool->paths.packages;
}
void lm_pool_path_free(lm_pool_t *pool){
free(pool->paths.packages);
free(pool->paths.info);
free(pool->paths.list);
}

View File

@ -1,33 +0,0 @@
#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);
if(NULL == info_path){
pool->paths.list = NULL;
return;
}
pool->paths.info = strdup(info_path);
}
void lm_pool_paths_set_list(lm_pool_t *pool, char *list_path){
free(pool->paths.list);
if(NULL == list_path){
pool->paths.list = NULL;
return;
}
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);
}

View File

@ -1,7 +1,7 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include "../../include/pool.h"
#include "../../include/pkg.h"
#include <stdlib.h>
#include <string.h>
@ -63,29 +63,6 @@ end:
return;
}
bool lm_pool_add(lm_pool_t *pool, lm_pkg_t *pkg){
if(NULL == pool || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pool->pkg){
pool->pkg = pkg;
return true;
}
lm_pkg_t *cur = pool->pkg;
while(NULL != cur){
if(NULL == cur->next){
cur->next = pkg;
return true;
}
cur = cur->next;
}
return false;
}
void lm_pool_free(lm_pool_t *pool) {
lm_url_free(&pool->url);
lm_pool_info_free(pool);
@ -94,7 +71,7 @@ void lm_pool_free(lm_pool_t *pool) {
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_pkg_free(prev);
lm_package_free(prev);
}
free(pool);

View File

@ -2,6 +2,9 @@
#include "../../include/util.h"
#include "../../include/pool.h"
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
@ -28,9 +31,58 @@ 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); break;
lm_mptp_sendfile(sock, addr, pool->paths.list);
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;
}
}