From 21addfd07b7e918ac0627d1c6b21885c3664e876 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sun, 27 Aug 2023 20:39:13 +0200 Subject: [PATCH] Adde convenience method --- cpp/controllers/controller_manager.cpp | 4 ++++ cpp/controllers/controller_manager.h | 1 + cpp/piscsi/piscsi_executor.cpp | 4 ++-- cpp/test/controller_manager_test.cpp | 7 ++++++- cpp/test/piscsi_executor_test.cpp | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cpp/controllers/controller_manager.cpp b/cpp/controllers/controller_manager.cpp index 68ee7781b2..e342559c3a 100644 --- a/cpp/controllers/controller_manager.cpp +++ b/cpp/controllers/controller_manager.cpp @@ -57,6 +57,10 @@ shared_ptr ControllerManager::FindController(int target_id) return it == controllers.end() ? nullptr : it->second; } +bool ControllerManager::HasController(int target_id) const { + return FindController(target_id) != nullptr; +} + unordered_set> ControllerManager::GetAllDevices() const { unordered_set> devices; diff --git a/cpp/controllers/controller_manager.h b/cpp/controllers/controller_manager.h index dd145e0717..fe293504e9 100644 --- a/cpp/controllers/controller_manager.h +++ b/cpp/controllers/controller_manager.h @@ -40,6 +40,7 @@ class ControllerManager : public enable_shared_from_this bool DeleteController(shared_ptr); shared_ptr IdentifyController(int) const; shared_ptr FindController(int) const; + bool HasController(int) const; unordered_set> GetAllDevices() const; void DeleteAllControllers(); shared_ptr GetDeviceByIdAndLun(int, int) const; diff --git a/cpp/piscsi/piscsi_executor.cpp b/cpp/piscsi/piscsi_executor.cpp index f5d90df706..fa80a1fb4e 100644 --- a/cpp/piscsi/piscsi_executor.cpp +++ b/cpp/piscsi/piscsi_executor.cpp @@ -530,7 +530,7 @@ string PiscsiExecutor::SetReservedIds(string_view ids) return "Invalid ID " + id_to_reserve; } - if (controller_manager.FindController(res_id) != nullptr) { + if (controller_manager.HasController(res_id)) { return "ID " + id_to_reserve + " is currently in use"; } @@ -680,7 +680,7 @@ string PiscsiExecutor::ValidateLunSetup(const PbCommand& command) const bool PiscsiExecutor::VerifyExistingIdAndLun(const CommandContext& context, int id, int lun) const { - if (controller_manager.FindController(id) == nullptr) { + if (!controller_manager.HasController(id)) { return context.ReturnLocalizedError(LocalizationKey::ERROR_NON_EXISTING_DEVICE, to_string(id)); } diff --git a/cpp/test/controller_manager_test.cpp b/cpp/test/controller_manager_test.cpp index 9ae770a46a..dd8de9fff1 100644 --- a/cpp/test/controller_manager_test.cpp +++ b/cpp/test/controller_manager_test.cpp @@ -3,7 +3,7 @@ // SCSI Target Emulator PiSCSI // for Raspberry Pi // -// Copyright (C) 2022 Uwe Seimet +// Copyright (C) 2022-2023 Uwe Seimet // //--------------------------------------------------------------------------- @@ -26,22 +26,27 @@ TEST(ControllerManagerTest, LifeCycle) device = device_factory.CreateDevice(SCHS, LUN1, ""); EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device)); + EXPECT_TRUE(controller_manager->HasController(ID)); auto controller = controller_manager->FindController(ID); EXPECT_NE(nullptr, controller); EXPECT_EQ(1, controller->GetLunCount()); EXPECT_NE(nullptr, controller_manager->IdentifyController(1 << ID)); EXPECT_EQ(nullptr, controller_manager->IdentifyController(0)); + EXPECT_FALSE(controller_manager->HasController(0)); EXPECT_EQ(nullptr, controller_manager->FindController(0)); EXPECT_NE(nullptr, controller_manager->GetDeviceByIdAndLun(ID, LUN1)); EXPECT_EQ(nullptr, controller_manager->GetDeviceByIdAndLun(0, 0)); device = device_factory.CreateDevice(SCHS, LUN2, ""); EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device)); + EXPECT_TRUE(controller_manager->HasController(ID)); controller = controller_manager->FindController(ID); + EXPECT_NE(nullptr, controller_manager->FindController(ID)); EXPECT_TRUE(controller_manager->DeleteController(controller)); EXPECT_EQ(nullptr, controller_manager->FindController(ID)); controller_manager->DeleteAllControllers(); + EXPECT_FALSE(controller_manager->HasController(ID)); EXPECT_EQ(nullptr, controller_manager->FindController(ID)); EXPECT_EQ(nullptr, controller_manager->GetDeviceByIdAndLun(ID, LUN1)); } diff --git a/cpp/test/piscsi_executor_test.cpp b/cpp/test/piscsi_executor_test.cpp index f7a07d6b0d..0f36392efe 100644 --- a/cpp/test/piscsi_executor_test.cpp +++ b/cpp/test/piscsi_executor_test.cpp @@ -409,7 +409,7 @@ TEST_F(PiscsiExecutorTest, DetachAll) auto device = device_factory.CreateDevice(SCHS, 0, ""); EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device)); - EXPECT_NE(nullptr, controller_manager->FindController(ID)); + EXPECT_TRUE(controller_manager->HasController(ID)); EXPECT_FALSE(controller_manager->GetAllDevices().empty()); executor.DetachAll();