From 2cf1327c69a310ae7cde5cb1f91a7de93ebef478 Mon Sep 17 00:00:00 2001 From: Moti Cohen Date: Fri, 19 Jul 2024 13:01:48 +0300 Subject: [PATCH] Add percentage printout --- README.md | 2 +- src/cli/rdb-cli.c | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 672357f..1704e4d 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ destruction, or when newer block replacing old one. Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS] OPTIONS: -l, --log-file Path to the log file or stdout (Default: './rdb-cli.log') - -s, --show-progress Show progress after every megabytes processed + -s, --show-progress Show progress to STDOUT after every processed -k, --key Include only keys that match REGEX -K --no-key Exclude all keys that match REGEX -t, --type Include only selected TYPE {str|list|set|zset|hash|module|func} diff --git a/src/cli/rdb-cli.c b/src/cli/rdb-cli.c index 1ae9129..595b0fc 100644 --- a/src/cli/rdb-cli.c +++ b/src/cli/rdb-cli.c @@ -4,6 +4,7 @@ #include #include #include +#include /* Rely only on API (and not internal parser headers) */ #include "../../api/librdb-api.h" @@ -101,7 +102,7 @@ static void printUsage(int shortUsage) { printf("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] {print|json|resp|redis} [FORMAT_OPTIONS]\n"); printf("OPTIONS:\n"); printf("\t-l, --log-file Path to the log file or stdout (Default: './rdb-cli.log')\n"); - printf("\t-s, --show-progress Show progress after every megabytes processed\n"); + printf("\t-s, --show-progress Show progress to STDOUT after every processed\n"); printf("\t-k, --key Include only keys that match REGEX\n"); printf("\t-K --no-key Exclude all keys that match REGEX\n"); printf("\t-t, --type Include only selected TYPE {str|list|set|zset|hash|module|func}\n"); @@ -433,6 +434,7 @@ void closeLogFileOnExit() { int main(int argc, char **argv) { + size_t fileSize = 0; Options options; RdbStatus status; int at; @@ -476,6 +478,15 @@ int main(int argc, char **argv) } else { if (RDBX_createReaderFile(parser, input /*file*/) == NULL) return RDB_ERR_GENERAL; + + /* If input is a file, then get its size */ + struct stat st; + if (stat(input, &st) == 0) { + fileSize = st.st_size; + } else { + printf("Error getting file size: %s\n", strerror(errno)); + return RDB_ERR_GENERAL; + } } if (RDB_OK != (res = options.formatFunc(parser, argc - at, argv + at))) @@ -494,11 +505,18 @@ int main(int argc, char **argv) status = RDB_parse(parser); if (status == RDB_STATUS_WAIT_MORE_DATA) continue; - else if (status == RDB_STATUS_PAUSED) - printf("... Processed %zuMBytes ...\n", - RDB_getBytesProcessed(parser) / (1024 * 1024)); - else - break; + else if (status == RDB_STATUS_PAUSED) { + size_t bytes = RDB_getBytesProcessed(parser); + /* If file size is known, print percentage */ + if (fileSize != 0) + printf("... Processed %zuMBytes (%.2f%%) ...\n", + bytes / (1024 * 1024), (bytes * 100.0) / fileSize); + else + printf("... Processed %zuMBytes ...\n", bytes / (1024 * 1024)); + continue; + } + + break; /* RDB_STATUS_ERROR */ } } else { while ((status = RDB_parse(parser)) == RDB_STATUS_WAIT_MORE_DATA);