From: Kefu Chai Date: Tue, 27 Apr 2021 11:03:18 +0000 (+0800) Subject: librbd/api: stop at the first matched image X-Git-Tag: v17.1.0~2097^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ab0ec5fd12b0a94a8dbaab50ec89e6984584490f;p=ceph.git librbd/api: stop at the first matched image if compiled with libc++ instead of libstdc++, there is chance that std::experimental::erase_if() is missing, so use a homebrew version instead. we will be able to drop this change once we have the luxury to use C++20. also, as we only care about the first moving image whose name matches with the specified one. let's take this opportunity to just stop at the first image with the specified name. this change trade a for loop with `find_if()` for better readability Signed-off-by: Kefu Chai --- diff --git a/src/librbd/api/Trash.cc b/src/librbd/api/Trash.cc index 4d60361fbfba..485290b499b1 100644 --- a/src/librbd/api/Trash.cc +++ b/src/librbd/api/Trash.cc @@ -25,7 +25,6 @@ #include #include "librbd/journal/DisabledPolicy.h" #include "librbd/image/ListWatchersRequest.h" -#include #define dout_subsys ceph_subsys_rbd #undef dout_prefix @@ -295,19 +294,20 @@ int Trash::move(librados::IoCtx &io_ctx, rbd_trash_image_source_t source, if (r < 0) { return r; } - - std::experimental::erase_if( - trash_image_specs, [image_name](const auto& pair) { - const auto& spec = pair.second; - return (spec.source != cls::rbd::TRASH_IMAGE_SOURCE_USER || - spec.state != cls::rbd::TRASH_IMAGE_STATE_MOVING || - spec.name != image_name); - }); - if (trash_image_specs.empty()) { + if (auto found_image = + std::find_if( + trash_image_specs.begin(), trash_image_specs.end(), + [&](const auto& pair) { + const auto& spec = pair.second; + return (spec.source == cls::rbd::TRASH_IMAGE_SOURCE_USER && + spec.state == cls::rbd::TRASH_IMAGE_STATE_MOVING && + spec.name == image_name); + }); + found_image != trash_image_specs.end()) { + image_id = found_image->first; + } else { return -ENOENT; } - - image_id = trash_image_specs.begin()->first; ldout(cct, 15) << "derived image id " << image_id << " from existing " << "trash entry" << dendl; } else if (r < 0) {