4
4
#include <string.h>
5
5
#include <errno.h>
6
6
#include <limits.h>
7
+ #include <sys/stat.h>
7
8
8
9
/* Rely only on API (and not internal parser headers) */
9
10
#include "../../api/librdb-api.h"
@@ -101,7 +102,7 @@ static void printUsage(int shortUsage) {
101
102
printf ("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n" );
102
103
printf ("OPTIONS:\n" );
103
104
printf ("\t-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')\n" );
104
- printf ("\t-s, --show-progress <INT > Show progress after every <INT> megabytes processed\n" );
105
+ printf ("\t-s, --show-progress <MBytes > Show progress to STDOUT after every <MBytes> processed\n" );
105
106
printf ("\t-k, --key <REGEX> Include only keys that match REGEX\n" );
106
107
printf ("\t-K --no-key <REGEX> Exclude all keys that match REGEX\n" );
107
108
printf ("\t-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}\n" );
@@ -433,6 +434,7 @@ void closeLogFileOnExit() {
433
434
434
435
int main (int argc , char * * argv )
435
436
{
437
+ size_t fileSize = 0 ;
436
438
Options options ;
437
439
RdbStatus status ;
438
440
int at ;
@@ -476,6 +478,15 @@ int main(int argc, char **argv)
476
478
} else {
477
479
if (RDBX_createReaderFile (parser , input /*file*/ ) == NULL )
478
480
return RDB_ERR_GENERAL ;
481
+
482
+ /* If input is a file, then get its size */
483
+ struct stat st ;
484
+ if (stat (input , & st ) == 0 ) {
485
+ fileSize = st .st_size ;
486
+ } else {
487
+ printf ("Error getting file size: %s\n" , strerror (errno ));
488
+ return RDB_ERR_GENERAL ;
489
+ }
479
490
}
480
491
481
492
if (RDB_OK != (res = options .formatFunc (parser , argc - at , argv + at )))
@@ -494,11 +505,18 @@ int main(int argc, char **argv)
494
505
status = RDB_parse (parser );
495
506
if (status == RDB_STATUS_WAIT_MORE_DATA )
496
507
continue ;
497
- else if (status == RDB_STATUS_PAUSED )
498
- printf ("... Processed %zuMBytes ...\n" ,
499
- RDB_getBytesProcessed (parser ) / (1024 * 1024 ));
500
- else
501
- break ;
508
+ else if (status == RDB_STATUS_PAUSED ) {
509
+ size_t bytes = RDB_getBytesProcessed (parser );
510
+ /* If file size is known, print percentage */
511
+ if (fileSize != 0 )
512
+ printf ("... Processed %zuMBytes (%.2f%%) ...\n" ,
513
+ bytes / (1024 * 1024 ), (bytes * 100.0 ) / fileSize );
514
+ else
515
+ printf ("... Processed %zuMBytes ...\n" , bytes / (1024 * 1024 ));
516
+ continue ;
517
+ }
518
+
519
+ break ; /* RDB_STATUS_ERROR */
502
520
}
503
521
} else {
504
522
while ((status = RDB_parse (parser )) == RDB_STATUS_WAIT_MORE_DATA );
0 commit comments