Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 25, 2025
1 parent b2c132f commit 43bb9b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
10 changes: 6 additions & 4 deletions cpp/command/command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ bool CommandExecutor::Attach(const CommandContext &context, const PbDeviceDefini
return context.ReturnLocalizedError(LocalizationKey::ERROR_RESERVED_ID, to_string(id));
}

const string &filename = GetParam(pb_device, "file");

const auto device = CreateDevice(context, pb_device, filename);
const auto device = CreateDevice(context, pb_device);
if (!device) {
return false;
}
Expand Down Expand Up @@ -241,6 +239,8 @@ bool CommandExecutor::Attach(const CommandContext &context, const PbDeviceDefini

#ifdef BUILD_STORAGE_DEVICE
if (device->SupportsImageFile()) {
const string &filename = GetParam(pb_device, "file");

// If no filename was provided the medium is considered not inserted
device->SetRemoved(filename.empty());

Expand Down Expand Up @@ -623,8 +623,10 @@ bool CommandExecutor::EnsureLun0(const CommandContext &context, const PbCommand
}

shared_ptr<PrimaryDevice> CommandExecutor::CreateDevice(const CommandContext &context,
const PbDeviceDefinition &pb_device, const string &filename) const
const PbDeviceDefinition &pb_device) const
{
const string &filename = GetParam(pb_device, "file");

auto device = DeviceFactory::GetInstance().CreateDevice(pb_device.type(), pb_device.unit(), filename);
if (!device) {
if (pb_device.type() == UNDEFINED) {
Expand Down
2 changes: 1 addition & 1 deletion cpp/command/command_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CommandExecutor
#endif
bool EnsureLun0(const CommandContext&, const PbCommand&) const;
bool ValidateDevice(const CommandContext&, const PbDeviceDefinition&) const;
shared_ptr<PrimaryDevice> CreateDevice(const CommandContext&, const PbDeviceDefinition&, const string&) const;
shared_ptr<PrimaryDevice> CreateDevice(const CommandContext&, const PbDeviceDefinition&) const;
bool SetBlockSize(const CommandContext&, shared_ptr<PrimaryDevice>, int) const;

mutex& GetExecutionLocker()
Expand Down
11 changes: 6 additions & 5 deletions cpp/test/command_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,17 @@ TEST(CommandExecutorTest, CreateDevice)
CommandContext context(command, *default_logger());

device.set_type(UNDEFINED);
EXPECT_EQ(nullptr, executor->CreateDevice(context, device, ""));
EXPECT_EQ(nullptr, executor->CreateDevice(context, device));
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
device.set_type(SCBR);
EXPECT_EQ(nullptr, executor->CreateDevice(context, device, ""));
EXPECT_EQ(nullptr, executor->CreateDevice(context, device));
#pragma GCC diagnostic pop
device.set_type(UNDEFINED);
EXPECT_NE(nullptr, executor->CreateDevice(context, device, "services"));
device.set_type(SCHS);
EXPECT_NE(nullptr, executor->CreateDevice(context, device, ""));
EXPECT_NE(nullptr, executor->CreateDevice(context, device));
device.set_type(UNDEFINED);
SetParam(device, "file", "services");
EXPECT_NE(nullptr, executor->CreateDevice(context, device));
}

TEST(CommandExecutorTest, SetBlockSize)
Expand Down

0 comments on commit 43bb9b8

Please sign in to comment.