Skip to content

Commit

Permalink
Show TFTP upload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jclehner committed Oct 2, 2024
1 parent ab3d774 commit e95f4ec
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*
*/

#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
Expand Down Expand Up @@ -323,7 +324,7 @@ ssize_t tftp_put(struct nmrpd_args *args)
{
struct sockaddr_in addr;
uint16_t block, port, op, blksize;
ssize_t len, last_len, bytes;
ssize_t len, last_len, bytes, fsize;
int fd, sock, ret, timeouts, errors, ackblock;
char rx[2048], tx[2048];
const char *file_remote = args->file_remote;
Expand All @@ -350,6 +351,7 @@ ssize_t tftp_put(struct nmrpd_args *args)
if (!file_remote) {
file_remote = "firmware";
}
fsize = -1;
} else {
fd = open(args->file_local, O_RDONLY | O_BINARY);
if (fd < 0) {
Expand All @@ -363,6 +365,15 @@ ssize_t tftp_put(struct nmrpd_args *args)
xperror("lseek");
goto cleanup;
}

struct stat st;
if (fstat(fd, &st) < 0) {
xperror("fstat");
goto cleanup;
}

// don't care if it's <= 0, we'll display the spinner in that case
fsize = st.st_size - args->offset;
}

#ifndef NMRPFLASH_FUZZ_TFTP
Expand Down Expand Up @@ -451,9 +462,15 @@ ssize_t tftp_put(struct nmrpd_args *args)
}
}

printf("%c ", spinner[block & 3]);
fflush(stdout);
printf("\b\b");
if (fsize > 0) {
printf("% 3zi %% ", (bytes * 100) / fsize);
fflush(stdout);
printf("\b\b\b\b\b\b");
} else {
printf("%c ", spinner[block & 3]);
fflush(stdout);
printf("\b\b");
}

pkt_mknum(tx, DATA);
pkt_mknum(tx + 2, block);
Expand Down

0 comments on commit e95f4ec

Please sign in to comment.