From b8c01ed38a3d872f3b7df97c7de8909df241e3ed Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Mon, 4 Jan 2021 11:12:08 -0800 Subject: [PATCH] Support --hex flag in `ldb file_checksum_dump` (#7820) Summary: Prior to this PR it prints the raw bytes which can include non-printable characters. This PR adds the option to print in hex instead. Pull Request resolved: https://github.com/facebook/rocksdb/pull/7820 Test Plan: try it out ``` $ ./ldb file_checksum_dump --hex --db=/tmp/rocksdbtest-9383//db_basic_test_12281129388755189514/ 16, FileChecksumCrc32c, 0xC789D948 ``` Reviewed By: jay-zhuang Differential Revision: D25738072 Pulled By: ajkr fbshipit-source-id: 8cf2856877971756c0495cfa63a9a1281c414dc7 --- tools/ldb_cmd.cc | 12 ++++++++++-- tools/ldb_cmd_impl.h | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 162a18cc2..ecd342a9a 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -1221,7 +1221,8 @@ FileChecksumDumpCommand::FileChecksumDumpCommand( const std::vector& /*params*/, const std::map& options, const std::vector& flags) - : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_PATH})), + : LDBCommand(options, flags, false, + BuildCmdLineOptions({ARG_PATH, ARG_HEX})), path_("") { std::map::const_iterator itr = options.find(ARG_PATH); @@ -1231,6 +1232,7 @@ FileChecksumDumpCommand::FileChecksumDumpCommand( exec_state_ = LDBCommandExecuteResult::Failed("--path: missing pathname"); } } + is_checksum_hex_ = IsFlagPresent(flags, ARG_HEX); } void FileChecksumDumpCommand::DoCommand() { @@ -1253,8 +1255,14 @@ void FileChecksumDumpCommand::DoCommand() { assert(i < file_numbers.size()); assert(i < checksums.size()); assert(i < checksum_func_names.size()); + std::string checksum; + if (is_checksum_hex_) { + checksum = StringToHex(checksums[i]); + } else { + checksum = std::move(checksums[i]); + } fprintf(stdout, "%" PRId64 ", %s, %s\n", file_numbers[i], - checksum_func_names[i].c_str(), checksums[i].c_str()); + checksum_func_names[i].c_str(), checksum.c_str()); } } fprintf(stdout, "Print SST file checksum information finished \n"); diff --git a/tools/ldb_cmd_impl.h b/tools/ldb_cmd_impl.h index e21251f9d..c0bfc19aa 100644 --- a/tools/ldb_cmd_impl.h +++ b/tools/ldb_cmd_impl.h @@ -186,6 +186,7 @@ class FileChecksumDumpCommand : public LDBCommand { private: std::string path_; + bool is_checksum_hex_; static const std::string ARG_PATH; };