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

@ -1,12 +1,17 @@
#include "../../include/mptp.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <unistd.h>
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
if (NULL == path)
if (NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
lm_mptp_t packet;
bool ret = false;
@ -16,6 +21,7 @@ bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
lm_error_set(LM_ERR_SendOpenFail);
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
@ -24,7 +30,8 @@ bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
packet.header.data_size = read;
lm_mptp_server_send(sock, &packet, addr);
if(!lm_mptp_server_send(sock, &packet, addr))
goto end;
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
}
@ -38,11 +45,15 @@ end:
}
bool lm_mptp_recvfile(int sock, char *path){
if(NULL == path)
if(NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(unlink(path) < 0 && errno != ENOENT)
if(unlink(path) < 0 && errno != ENOENT){
lm_error_set(LM_ERR_RecvDelFail);
return false;
}
FILE *file = fopen(path, "a");
bool ret = false;
@ -50,6 +61,7 @@ bool lm_mptp_recvfile(int sock, char *path){
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
lm_error_set(LM_ERR_RecvOpenFail);
goto end;
}
@ -57,14 +69,18 @@ bool lm_mptp_recvfile(int sock, char *path){
if(!lm_mptp_client_verify(&packet))
goto end;
if(MPTP_FLAGS_CODE(&packet) != MPTP_S2C_COOL)
if(MPTP_FLAGS_CODE(&packet) != MPTP_S2C_COOL){
lm_error_set(LM_ERR_RecvBadCode);
goto end;
}
if(MPTP_IS_LAST(&packet))
break;
if(fwrite(packet.data, 1, packet.header.data_size, file)==0)
if(fwrite(packet.data, 1, packet.header.data_size, file)==0){
lm_error_set(LM_ERR_RecvWriteFail);
goto end;
}
}
ret = true;