Skip to content

Commit 2cf1327

Browse files
committed
Add percentage printout
1 parent 989ecb5 commit 2cf1327

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ destruction, or when newer block replacing old one.
199199
Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]
200200
OPTIONS:
201201
-l, --log-file <PATH> Path to the log file or stdout (Default: './rdb-cli.log')
202-
-s, --show-progress <INT> Show progress after every <INT> megabytes processed
202+
-s, --show-progress <MBytes> Show progress to STDOUT after every <MBytes> processed
203203
-k, --key <REGEX> Include only keys that match REGEX
204204
-K --no-key <REGEX> Exclude all keys that match REGEX
205205
-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}

src/cli/rdb-cli.c

+24-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include <errno.h>
66
#include <limits.h>
7+
#include <sys/stat.h>
78

89
/* Rely only on API (and not internal parser headers) */
910
#include "../../api/librdb-api.h"
@@ -101,7 +102,7 @@ static void printUsage(int shortUsage) {
101102
printf("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n");
102103
printf("OPTIONS:\n");
103104
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");
105106
printf("\t-k, --key <REGEX> Include only keys that match REGEX\n");
106107
printf("\t-K --no-key <REGEX> Exclude all keys that match REGEX\n");
107108
printf("\t-t, --type <TYPE> Include only selected TYPE {str|list|set|zset|hash|module|func}\n");
@@ -433,6 +434,7 @@ void closeLogFileOnExit() {
433434

434435
int main(int argc, char **argv)
435436
{
437+
size_t fileSize = 0;
436438
Options options;
437439
RdbStatus status;
438440
int at;
@@ -476,6 +478,15 @@ int main(int argc, char **argv)
476478
} else {
477479
if (RDBX_createReaderFile(parser, input /*file*/) == NULL)
478480
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+
}
479490
}
480491

481492
if (RDB_OK != (res = options.formatFunc(parser, argc - at, argv + at)))
@@ -494,11 +505,18 @@ int main(int argc, char **argv)
494505
status = RDB_parse(parser);
495506
if (status == RDB_STATUS_WAIT_MORE_DATA)
496507
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 */
502520
}
503521
} else {
504522
while ((status = RDB_parse(parser)) == RDB_STATUS_WAIT_MORE_DATA);

0 commit comments

Comments
 (0)