From 9324ab94711dbe9a1265643adcc79ae0a3cba812 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Sat, 9 Apr 2022 11:06:32 +0200 Subject: [PATCH] rbd: factor out get_percentage() helper Signed-off-by: Ilya Dryomov --- src/tools/rbd/Utils.cc | 6 +++++- src/tools/rbd/Utils.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/tools/rbd/Utils.cc b/src/tools/rbd/Utils.cc index 75d9e6c0443..161cb91de17 100644 --- a/src/tools/rbd/Utils.cc +++ b/src/tools/rbd/Utils.cc @@ -44,7 +44,7 @@ static std::string mgr_command_args_to_str( int ProgressContext::update_progress(uint64_t offset, uint64_t total) { if (progress) { - int pc = total ? (offset * 100ull / total) : 0; + int pc = get_percentage(offset, total); if (pc > last_pc) { std::cerr << "\r" << operation << ": " << pc << "% complete..." << std::flush; @@ -67,6 +67,10 @@ void ProgressContext::fail() { } } +int get_percentage(uint64_t part, uint64_t whole) { + return whole ? (100 * part / whole) : 0; +} + void aio_context_callback(librbd::completion_t completion, void *arg) { librbd::RBD::AioCompletion *aio_completion = diff --git a/src/tools/rbd/Utils.h b/src/tools/rbd/Utils.h index a3f9baac7d3..66f8e18881b 100644 --- a/src/tools/rbd/Utils.h +++ b/src/tools/rbd/Utils.h @@ -82,6 +82,8 @@ struct ProgressContext : public librbd::ProgressContext { void fail(); }; +int get_percentage(uint64_t part, uint64_t whole); + template librbd::RBD::AioCompletion *create_aio_completion(T *t) { return new librbd::RBD::AioCompletion( -- 2.39.5