Skip to content

Commit

Permalink
Add support for persisting the current setup in /etc/s2p.conf (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Mar 26, 2024
1 parent 2ac2f7a commit c4c6de2
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 15 deletions.
18 changes: 18 additions & 0 deletions cpp/base/property_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,21 @@ map<int, vector<byte>> PropertyHandler::GetCustomModePages(const string &vendor,

return pages;
}

bool PropertyHandler::Persist() const
{
error_code error;
remove(GLOBAL_CONFIGURATION + ".old", error);
rename(path(GLOBAL_CONFIGURATION), path(GLOBAL_CONFIGURATION + ".old"), error);

ofstream out(GLOBAL_CONFIGURATION);
for (const auto& [key, value] : GetProperties()) {
out << key << "=" << value << "\n";
if (out.fail()) {
return false;
}
}

return true;
}

2 changes: 2 additions & 0 deletions cpp/base/property_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class PropertyHandler

map<int, vector<byte>> GetCustomModePages(const string&, const string&) const;

bool Persist() const;

private:

PropertyHandler() = default;
Expand Down
9 changes: 9 additions & 0 deletions cpp/command/command_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//---------------------------------------------------------------------------

#include <fstream>
#include <spdlog/spdlog.h>
#include "controllers/controller_factory.h"
#include "shared/shared_exceptions.h"
Expand Down Expand Up @@ -144,6 +145,14 @@ bool CommandDispatcher::DispatchCommand(const CommandContext &context, PbResult
case RESERVE_IDS:
return executor.ProcessCmd(context);

case PERSIST_CONFIGURATION:
if (PropertyHandler::Instance().Persist()) {
return context.ReturnSuccessStatus();
}
else {
return context.ReturnLocalizedError(LocalizationKey::ERROR_PERSIST);
}

default:
// The remaining commands may only be executed when the target is idle
if (!ExecuteWithLock(context)) {
Expand Down
6 changes: 4 additions & 2 deletions cpp/command/command_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ void CommandResponse::GetOperationInfo(PbOperationInfo &operation_info, int dept

CreateOperation(operation_info, STATISTICS_INFO, "Get statistics");

CreateOperation(operation_info, PROPERTIES_INFO, "Get properties");

CreateOperation(operation_info, RESERVED_IDS_INFO, "Get list of reserved device IDs");

operation = CreateOperation(operation_info, DEFAULT_FOLDER, "Set default image file folder");
Expand Down Expand Up @@ -446,6 +444,10 @@ void CommandResponse::GetOperationInfo(PbOperationInfo &operation_info, int dept
operation = CreateOperation(operation_info, CHECK_AUTHENTICATION, "Check whether an authentication token is valid");
AddOperationParameter(*operation, "token", "Authentication token to be checked", "", true);

CreateOperation(operation_info, PROPERTIES_INFO, "Get current s2p properties");

CreateOperation(operation_info, PERSIST_CONFIGURATION, "Save current configuration to /etc/s2p.conf");

CreateOperation(operation_info, OPERATION_INFO, "Get operation meta data");
}

Expand Down
7 changes: 7 additions & 0 deletions cpp/s2pctl/sp2ctl_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void S2pCtl::Banner(bool usage) const
<< " s2p requires authentication.\n"
<< " --list-settings/-s List s2p settings.\n"
<< " --list-statistics/-S List s2p statistics.\n"
<< " --persist Save the current configuration to /etc/s2p.conf.\n"
<< " --version/-v Display the program version.\n"
<< " --server-version/-V Display the s2p server version.\n"
<< " --shut-down/-X Shut down s2p.\n";
Expand Down Expand Up @@ -132,6 +133,7 @@ int S2pCtl::ParseArguments(const vector<char*> &args) // NOSONAR Acceptable comp
const int OPT_LOCALE = 7;
const int OPT_SCSI_LEVEL = 8;
const int OPT_LIST_EXTENSIONS = 9;
const int OPT_PERSIST = 10;

const vector<option> options = {
{ "prompt", no_argument, nullptr, OPT_PROMPT },
Expand Down Expand Up @@ -164,6 +166,7 @@ int S2pCtl::ParseArguments(const vector<char*> &args) // NOSONAR Acceptable comp
{ "locale", required_argument, nullptr, OPT_LOCALE },
{ "log-level", required_argument, nullptr, 'L' },
{ "name", required_argument, nullptr, 'n' },
{ "persist", no_argument, nullptr, OPT_PERSIST },
{ "port", required_argument, nullptr, 'p' },
{ "rename", required_argument, nullptr, 'R' },
{ "reserve-ids", optional_argument, nullptr, 'r' },
Expand Down Expand Up @@ -328,6 +331,10 @@ int S2pCtl::ParseArguments(const vector<char*> &args) // NOSONAR Acceptable comp
command.set_operation(PROPERTIES_INFO);
break;

case OPT_PERSIST:
command.set_operation(PERSIST_CONFIGURATION);
break;

case 't':
device->set_type(ParseDeviceType(optarg));
if (device->type() == UNDEFINED) {
Expand Down
7 changes: 7 additions & 0 deletions cpp/shared/localizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ Localizer::Localizer()
Add(LocalizationKey::ERROR_UNIQUE_DEVICE_TYPE, "fr", "Il ne peut y avoir qu'un seul périphérique %1");
Add(LocalizationKey::ERROR_UNIQUE_DEVICE_TYPE, "es", "Sólo puede haber un único dispositivo %1");
Add(LocalizationKey::ERROR_UNIQUE_DEVICE_TYPE, "zh", "只能有一个%1设备");

Add(LocalizationKey::ERROR_PERSIST, "en", "Couldn't save '/etc/s2p.conf'");
Add(LocalizationKey::ERROR_PERSIST, "de", "'/etc/s2p.conf' konnte nicht gespeichert werden");
Add(LocalizationKey::ERROR_PERSIST, "sv", "Kunde inte spara '/etc/s2p.conf'");
Add(LocalizationKey::ERROR_PERSIST, "fr", "Impossible d'enregistrer '/etc/s2p.conf'");
Add(LocalizationKey::ERROR_PERSIST, "es", "No se pudo guardar '/etc/s2p.conf'");
Add(LocalizationKey::ERROR_PERSIST, "zh", "无法保存'/etc/s2p.conf'");
}

void Localizer::Add(LocalizationKey key, const string &locale, string_view value)
Expand Down
3 changes: 2 additions & 1 deletion cpp/shared/localizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ enum class LocalizationKey
ERROR_OPERATION_DENIED_REMOVABLE,
ERROR_OPERATION_DENIED_PROTECTABLE,
ERROR_OPERATION_DENIED_READY,
ERROR_UNIQUE_DEVICE_TYPE
ERROR_UNIQUE_DEVICE_TYPE,
ERROR_PERSIST
};

class Localizer
Expand Down
2 changes: 1 addition & 1 deletion cpp/shared/s2p_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#include "s2p_version.h"

const int s2p_major_version = 3;
const int s2p_minor_version = 1;
const int s2p_minor_version = 2;
const int s2p_revision = 0;
const std::string s2p_suffix = "-devel";
11 changes: 4 additions & 7 deletions cpp/test/command_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,24 +395,21 @@ TEST(CommandExecutorTest, ValidateImageFile)

TEST(CommandExecutorTest, PrintCommand)
{
auto bus = make_shared<MockBus>();
auto controller_factory = make_shared<ControllerFactory>(false);
auto executor = make_shared<CommandExecutor>(*bus, controller_factory);
PbDeviceDefinition definition;

PbCommand command;
string s = executor->PrintCommand(command, definition);

string s = CommandExecutor::PrintCommand(command, definition);
EXPECT_NE(s.find("operation="), string::npos);
EXPECT_EQ(s.find("key1=value1"), string::npos);
EXPECT_EQ(s.find("key2=value2"), string::npos);

SetParam(command, "key1", "value1");
s = executor->PrintCommand(command, definition);
s = CommandExecutor::PrintCommand(command, definition);
EXPECT_NE(s.find("operation="), string::npos);
EXPECT_NE(s.find("key1=value1"), string::npos);

SetParam(command, "key2", "value2");
s = executor->PrintCommand(command, definition);
s = CommandExecutor::PrintCommand(command, definition);
EXPECT_NE(s.find("operation="), string::npos);
EXPECT_NE(s.find("key1=value1"), string::npos);
EXPECT_NE(s.find("key2=value2"), string::npos);
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/command_response_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST(CommandResponseTest, Operation_Count)

PbOperationInfo info;
response.GetOperationInfo(info, 0);
EXPECT_EQ(33, info.operations_size());
EXPECT_EQ(34, info.operations_size());
}

void TestNonDiskDevice(PbDeviceType type, unsigned int default_param_count)
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/primary_device_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST(PrimaryDeviceTest, GetId)
{
const int ID = 5;

auto [_, device] = CreatePrimaryDevice(ID);
auto [__, device] = CreatePrimaryDevice(ID);

EXPECT_EQ(ID, device->GetId());
}
Expand Down
8 changes: 6 additions & 2 deletions doc/s2pctl.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ s2pctl \- SCSI2Pi Server Controller Tool
[\fB\--list-settings/-s\fR \fI[FOLDER_PATTERN:FILE_PATTERN:OPERATIONS]\fR] |
[\fB\--type/-t\fR \fITYPE\fR] |
[\fB\--copy/-x\fR \fICURRENT_NAME:NEW_NAME\fR] |
[\fB\--persist\fR]
[\fB\--locale\fR \fILOCALE\fR]
.SH DESCRIPTION
.B s2pctl
Expand Down Expand Up @@ -113,10 +114,10 @@ Comma-separated list of IDs to reserve. Pass an empty list in order to not reser
Display all s2p settings. Filenames are optionally filtered by folder and file patterns.
OPERATIONS is an optional comma-seperated list of server operations to take into account, in order to limit the amount of output.
.TP
.BR --list-statistics/-S\fI
.BR --list-statistics/-S\fI " " \fI
Display s2p statistics.
.TP
.BR --list-device-types/-T\fI
.BR --list-device-types/-T\fI " " \fI
Display available device types and their properties.
.TP
.BR --help/-h\fI " " \fI
Expand Down Expand Up @@ -146,6 +147,9 @@ Do not send the command to s2p but write it to a protobuf JSON file.
.BR --text-protobuf\fI " "\fIFILENAME
Do not send the command to s2p but write it to a protobuf text format file.
.TP
.BR --persist\fI " " \fI
Write the current configuration to /etc/s2p.conf. The old configuration, if any, is saved to /etc/s2p.conf.old.
.TP
.BR --locale\fI " "\fILOCALE
Overrides the default locale (language) for client-facing error messages.
.TP
Expand Down
3 changes: 3 additions & 0 deletions proto/s2p_interface.proto
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ enum PbOperation {

// Get properties (PbPropertiesInfo)
PROPERTIES_INFO = 100;

// Persist configuration in /etc/s2p.conf
PERSIST_CONFIGURATION = 101;
}

// The operation parameter meta data. The parameter data type is provided by the protobuf API.
Expand Down

0 comments on commit c4c6de2

Please sign in to comment.