]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd/api: stop at the first matched image
authorKefu Chai <kchai@redhat.com>
Tue, 27 Apr 2021 11:03:18 +0000 (19:03 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 28 Apr 2021 11:38:43 +0000 (19:38 +0800)
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 <kchai@redhat.com>
src/librbd/api/Trash.cc

index 4d60361fbfbaf14fb6e02afbf786a82addc96a71..485290b499b1f0213bf05109a2ae8a6659d7df8c 100644 (file)
@@ -25,7 +25,6 @@
 #include <json_spirit/json_spirit.h>
 #include "librbd/journal/DisabledPolicy.h"
 #include "librbd/image/ListWatchersRequest.h"
-#include <experimental/map>
 
 #define dout_subsys ceph_subsys_rbd
 #undef dout_prefix
@@ -295,19 +294,20 @@ int Trash<I>::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) {