From 16347e71a14533dbeee9b3d6fc665926976ce42e Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Sun, 18 Apr 2021 16:10:31 +0200 Subject: [PATCH] * Print short statistic for each transferred file (kaoupload.c). --- kaoupload.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/kaoupload.c b/kaoupload.c index 2171a31..0d8b3b6 100644 --- a/kaoupload.c +++ b/kaoupload.c @@ -20,7 +20,6 @@ #define CSIZE_HARDLIMIT 5200000L - static struct kaoconf { char *user; char *pass; @@ -35,6 +34,7 @@ struct file_info_t { off_t size; off_t remn; off_t coff; + off_t sent; curl_off_t speed; }; @@ -96,6 +96,12 @@ static curl_off_t str_to_bwl( const char *s ) { return byps; } +static uint64_t time_ms( void ) { + struct timespec tv; + clock_gettime( CLOCK_REALTIME, &tv ); + return (uint64_t)tv.tv_sec * 1000 + ( tv.tv_nsec / 1000000L ); +} + static void parse_cfg( const char *confpath ) { FILE *fp; char buf[256]; @@ -213,6 +219,7 @@ static int upload_chunk( CURL *curl, struct file_info_t *fi, off_t csize ) { curl_easy_getinfo( curl, CURLINFO_SPEED_UPLOAD_T, &fi->speed ); curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &http_code ); + fi->sent += csize; // puts(reply.memory); @@ -273,6 +280,7 @@ static int upload_file( const char *fn ) { struct file_info_t fi; struct stat stbuf; CURL *curl; + uint64_t starttime, xfrtime; if ( NULL == ( curl = curl_easy_init() ) ) die( "curl_easy_init failed", 0 ); @@ -290,8 +298,9 @@ static int upload_file( const char *fn ) { goto err_out; } fi.remn = fi.size = stbuf.st_size; - fi.coff = 0; + fi.coff = fi.sent = 0; printf( "File: %s (%lu bytes)\n", fi.name, (unsigned long)fi.size ); + starttime = time_ms(); while ( fi.remn ) { if ( gotsig ) { @@ -308,12 +317,14 @@ static int upload_file( const char *fn ) { // fprintf( stderr, "[csize: %ld] {offset: %ld} ", (long)csize, (long)fi.coff ); fflush( stderr ); } - if ( res < 0 ) { - puts( "" ); + + xfrtime = time_ms() - starttime + 1; + printf( "\rSent %.1f kB in %.1f seconds (%.1f kB/s).\n", + (double)fi.sent / 1024, (double)xfrtime / 1000, (double)fi.sent / xfrtime * 1000 / 1024 ); + if ( res < 0 ) err( "file upload incomplete", 0 ); - } else - printf( "\r%-20s\n", "Upload complete." ); + printf( "Upload complete.\n" ); err_out: curl_free( fi.esc_name ); -- 2.30.2