Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/rdb-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static void printUsage(int shortUsage) {
printf("\t aligned the parser will generate higher-level commands instead.\n");
printf("\t-o, --output <FILE> Specify the output file (For 'resp' only: if not specified, output to stdout)\n");
printf("\t-1, --single-db Avoid SELECT command. DBs in RDB will be stored to db 0. Watchout for conflicts\n");
printf("\t-s, --start-cmd-num <NUM> Start writing redis from command number\n");
printf("\t-n, --start-cmd-num <NUM> Start writing redis from command number\n");
printf("\t-e, --enum-commands Command enumeration and tracing by preceding each generated RESP command\n");
printf("\t with debug command of type: `SET _RDB_CLI_CMD_ID_ <CMD-ID>`\n");

Expand Down
25 changes: 25 additions & 0 deletions src/ext/handlersToPrint.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ctype.h>
#include "extCommon.h"
#include "../../deps/redis/util.h"
#include "../../deps/redis/sha256.h"

struct RdbxToPrint;

Expand Down Expand Up @@ -88,7 +89,20 @@ static void outputPlainEscaping(RdbxToPrint *ctx, char *p, size_t len) {
}
}

/* Print first 4 bytes of sha256 of key, like __RDB_key() */
static char *printsha256(char *key, int len, char buf[9]) {
BYTE hash[SHA256_BLOCK_SIZE];
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, (unsigned char*) key, len);
sha256_final(&ctx, hash);
for (int i = 0; i < 4; i++) snprintf(buf + (i * 2), 3, "%02x", hash[i]);
buf[8] = '\0';
return buf;
}

static void printKeyFmt(RdbxToPrint *ctx, RdbBulk string) {
char buf[9];
const char *p = ctx->keyFmt;

if (ctx->keyFmt[0] == '\0') return; /* if not FMT for keys */
Expand All @@ -100,6 +114,11 @@ static void printKeyFmt(RdbxToPrint *ctx, RdbBulk string) {
case 'd':
fprintf(ctx->outfile, "%d", ctx->dbnum);
break;
case 'h': // print sha256 of key
fprintf(ctx->outfile, "%s", printsha256(ctx->keyCtx.key,
ctx->keyCtx.keyLen,
buf));
break;
case 'k':
outputPlainEscaping(ctx, ctx->keyCtx.key, ctx->keyCtx.keyLen);
break;
Expand Down Expand Up @@ -131,6 +150,12 @@ static void printKeyFmt(RdbxToPrint *ctx, RdbBulk string) {
case RDB_DATA_TYPE_STREAM:
fprintf(ctx->outfile, "stream");
break;
case RDB_DATA_TYPE_MODULE:
fprintf(ctx->outfile, "module");
break;
case RDB_DATA_TYPE_FUNCTION:
fprintf(ctx->outfile, "function");
break;
default:
fprintf(ctx->outfile, "unknown");
}
Expand Down
9 changes: 9 additions & 0 deletions test/test_rdb_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ static void test_rdb_cli_print(void **state) {
"redis-ver=255.255.255\nredis-bits=64\nctime=1683103535\nused-mem=967040\naof-base=0\n");
}

static void test_rdb_cli_print_sha256(void **state) {
UNUSED(state);
sendRedisCmd("SET mylist27 STAM", REDIS_REPLY_STATUS, NULL);
sendRedisCmd("SAVE", REDIS_REPLY_STATUS, NULL);
assert_string_equal(
runSystemCmd("$RDB_CLI_CMD %s print -k \"%%h\"", TMP_FOLDER("dump.rdb")), "0bdab52c\n");
}

/*************************** group_test_rdb_cli *******************************/
int group_test_rdb_cli(void) {

Expand All @@ -245,6 +253,7 @@ int group_test_rdb_cli(void) {
cmocka_unit_test_setup(test_rdb_cli_input_fd_reader, setupTest),
cmocka_unit_test_setup(test_rdb_cli_redis_auth, setupTest),
cmocka_unit_test_setup(test_rdb_cli_print, setupTest),
cmocka_unit_test_setup(test_rdb_cli_print_sha256, setupTest),
};

int res = cmocka_run_group_tests(tests, NULL, NULL);
Expand Down