update: add path section to MPTP packet

This commit is contained in:
ngn
2024-08-09 22:13:43 +03:00
parent f5d8514a27
commit 1404da3c6c
16 changed files with 382 additions and 221 deletions

View File

@ -23,6 +23,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
size_t read = 0;
struct stat st;
int size = -1;
lm_mptp_init(&packet);
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
@ -38,7 +40,9 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
total = st.st_size;
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
lm_mptp_set_data(&packet, NULL, digits(st.st_size));
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);
@ -52,7 +56,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
goto end_1;
// clear the packet
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
lm_mptp_set_data(&packet, NULL, MPTP_DATA_MAX);
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
packet.header.data_size = read;
@ -68,7 +73,8 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
if(NULL != callback && !callback(path, current, st.st_size, data))
goto end_1;
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
lm_mptp_free(&packet);
lm_mptp_new(&packet, false, MPTP_S2C_COOL, false);
}
if(current != total){
@ -78,14 +84,16 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
}
pdebug(__func__, "completed sending %s, sending last packet", path);
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
lm_mptp_new(&packet, false, MPTP_S2C_COOL, true);
lm_mptp_server_send(sock, &packet);
ret = true;
goto end_2;
end_1:
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_free(&packet);
lm_mptp_new(&packet, false, MPTP_S2C_BRUH, true);
lm_mptp_server_send(sock, &packet);
lm_mptp_free(&packet);
end_2:
if(NULL != file)
fclose(file);
@ -108,6 +116,8 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
bool ret = false;
lm_mptp_t packet;
lm_mptp_init(&packet);
if(NULL == file){
pdebug(__func__, "failed to open file: %s", path);
lm_error_set(LM_ERR_RecvOpenFail);
@ -176,6 +186,7 @@ bool lm_mptp_recvfile(int sock, char *path, lm_mptp_transfer_callback_t callback
ret = true;
end:
lm_mptp_free(&packet);
if(NULL != file)
fclose(file);
return ret;