2021-09-24 06:48:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2021-09-24 06:48:48 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2021-2023 Uwe Seimet
|
2021-09-24 06:48:48 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "controllers/controller_manager.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_util.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "shared/network_util.h"
|
|
|
|
#include "shared/piscsi_util.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_version.h"
|
2021-09-24 06:48:48 +00:00
|
|
|
#include "devices/disk.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "piscsi_response.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2022-10-25 08:29:57 +00:00
|
|
|
#include <filesystem>
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace filesystem;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2023-10-15 06:38:15 +00:00
|
|
|
using namespace piscsi_util;
|
|
|
|
using namespace network_util;
|
2022-10-08 17:26:04 +00:00
|
|
|
using namespace protobuf_util;
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetDeviceProperties(const Device& device, PbDeviceProperties& properties) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
properties.set_luns(ControllerManager::GetScsiLunMax());
|
|
|
|
properties.set_read_only(device.IsReadOnly());
|
|
|
|
properties.set_protectable(device.IsProtectable());
|
|
|
|
properties.set_stoppable(device.IsStoppable());
|
|
|
|
properties.set_removable(device.IsRemovable());
|
|
|
|
properties.set_lockable(device.IsLockable());
|
|
|
|
properties.set_supports_file(device.SupportsFile());
|
|
|
|
properties.set_supports_params(device.SupportsParams());
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
if (device.SupportsParams()) {
|
2023-10-30 10:24:18 +00:00
|
|
|
for (const auto& [key, value] : device.GetDefaultParams()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
auto& map = *properties.mutable_default_params();
|
2022-09-07 14:38:42 +00:00
|
|
|
map[key] = value;
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
for (const auto& block_size : device_factory.GetSectorSizes(device.GetType())) {
|
2023-10-15 06:38:15 +00:00
|
|
|
properties.add_block_sizes(block_size);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void PiscsiResponse::GetDeviceTypeProperties(PbDeviceTypesInfo& device_types_info, PbDeviceType type) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2022-10-04 15:23:42 +00:00
|
|
|
auto type_properties = device_types_info.add_properties();
|
2021-09-24 06:48:48 +00:00
|
|
|
type_properties->set_type(type);
|
2022-11-04 07:22:32 +00:00
|
|
|
const auto device = device_factory.CreateDevice(type, 0, "");
|
2023-10-15 06:38:15 +00:00
|
|
|
GetDeviceProperties(*device, *type_properties->mutable_properties());
|
|
|
|
}
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetDeviceTypesInfo(PbDeviceTypesInfo& device_types_info) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
// Start with 2 instead of 1. 1 was the removed SASI drive type.
|
|
|
|
int ordinal = 2;
|
2022-02-10 18:54:48 +00:00
|
|
|
while (PbDeviceType_IsValid(ordinal)) {
|
|
|
|
PbDeviceType type = UNDEFINED;
|
2022-02-13 19:30:02 +00:00
|
|
|
PbDeviceType_Parse(PbDeviceType_Name((PbDeviceType)ordinal), &type);
|
2022-02-10 18:54:48 +00:00
|
|
|
GetDeviceTypeProperties(device_types_info, type);
|
|
|
|
ordinal++;
|
|
|
|
}
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void PiscsiResponse::GetDevice(const Device& device, PbDevice& pb_device, const string& default_folder) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2022-10-04 15:23:42 +00:00
|
|
|
pb_device.set_id(device.GetId());
|
|
|
|
pb_device.set_unit(device.GetLun());
|
|
|
|
pb_device.set_vendor(device.GetVendor());
|
|
|
|
pb_device.set_product(device.GetProduct());
|
|
|
|
pb_device.set_revision(device.GetRevision());
|
2022-10-23 19:51:39 +00:00
|
|
|
pb_device.set_type(device.GetType());
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
GetDeviceProperties(device, *pb_device.mutable_properties());
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
auto status = pb_device.mutable_status();
|
2022-10-04 15:23:42 +00:00
|
|
|
status->set_protected_(device.IsProtected());
|
|
|
|
status->set_stopped(device.IsStopped());
|
|
|
|
status->set_removed(device.IsRemoved());
|
|
|
|
status->set_locked(device.IsLocked());
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (device.SupportsParams()) {
|
2022-10-04 15:23:42 +00:00
|
|
|
for (const auto& [key, value] : device.GetParams()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(pb_device, key, value);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
if (const auto disk = dynamic_cast<const Disk*>(&device); disk) {
|
|
|
|
pb_device.set_block_size(device.IsRemoved()? 0 : disk->GetSectorSizeInBytes());
|
|
|
|
pb_device.set_block_count(device.IsRemoved() ? 0: disk->GetBlockCount());
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
const auto storage_device = dynamic_cast<const StorageDevice *>(&device);
|
|
|
|
if (storage_device != nullptr) {
|
2023-10-15 06:38:15 +00:00
|
|
|
GetImageFile(*pb_device.mutable_file(), default_folder, device.IsReady() ? storage_device->GetFilename() : "");
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
}
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
bool PiscsiResponse::GetImageFile(PbImageFile& image_file, const string& default_folder, const string& filename) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
|
|
|
if (!filename.empty()) {
|
2022-10-04 15:23:42 +00:00
|
|
|
image_file.set_name(filename);
|
|
|
|
image_file.set_type(device_factory.GetTypeForFile(filename));
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const path p(filename[0] == '/' ? filename : default_folder + "/" + filename);
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
image_file.set_read_only(access(p.c_str(), W_OK));
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error_code error;
|
|
|
|
if (is_regular_file(p, error) || (is_symlink(p, error) && !is_block_file(p, error))) {
|
|
|
|
image_file.set_size(file_size(p));
|
2021-09-30 17:22:57 +00:00
|
|
|
return true;
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 17:22:57 +00:00
|
|
|
|
|
|
|
return false;
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void PiscsiResponse::GetAvailableImages(PbImageFilesInfo& image_files_info, const string& default_folder,
|
2023-10-15 06:38:15 +00:00
|
|
|
const string& folder_pattern, const string& file_pattern, int scan_depth) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
const path default_path(default_folder);
|
|
|
|
if (!is_directory(default_path)) {
|
2022-10-01 15:56:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string folder_pattern_lower;
|
|
|
|
ranges::transform(folder_pattern, back_inserter(folder_pattern_lower), ::tolower);
|
2021-12-22 08:25:05 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string file_pattern_lower;
|
|
|
|
ranges::transform(file_pattern, back_inserter(file_pattern_lower), ::tolower);
|
2021-12-19 10:49:17 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
for (auto it = recursive_directory_iterator(default_path, directory_options::follow_directory_symlink);
|
|
|
|
it != recursive_directory_iterator(); it++) {
|
|
|
|
if (it.depth() > scan_depth) {
|
|
|
|
it.disable_recursion_pending();
|
2022-10-04 15:23:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-01 15:56:06 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const string parent = it->path().parent_path().string();
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const string folder = parent.size() > default_folder.size() ? parent.substr(default_folder.size() + 1) : "";
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!FilterMatches(folder, folder_pattern_lower) || !FilterMatches(it->path().filename().string(), file_pattern_lower)) {
|
2022-10-04 15:23:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!ValidateImageFile(it->path())) {
|
|
|
|
continue;
|
2021-12-19 10:49:17 +00:00
|
|
|
}
|
2022-10-01 15:56:06 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const string filename = folder.empty() ?
|
|
|
|
it->path().filename().string() : folder + "/" + it->path().filename().string();
|
|
|
|
if (PbImageFile image_file; GetImageFile(image_file, default_folder, filename)) {
|
|
|
|
GetImageFile(*image_files_info.add_image_files(), default_folder, filename);
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 10:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetImageFilesInfo(PbImageFilesInfo& image_files_info, const string& default_folder,
|
2022-10-04 15:23:42 +00:00
|
|
|
const string& folder_pattern, const string& file_pattern, int scan_depth) const
|
2021-12-19 10:49:17 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
image_files_info.set_default_image_folder(default_folder);
|
|
|
|
image_files_info.set_depth(scan_depth);
|
2021-12-19 10:49:17 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
GetAvailableImages(image_files_info, default_folder, folder_pattern, file_pattern, scan_depth);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetAvailableImages(PbServerInfo& server_info, const string& default_folder,
|
2022-10-04 15:23:42 +00:00
|
|
|
const string& folder_pattern, const string& file_pattern, int scan_depth) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
server_info.mutable_image_files_info()->set_default_image_folder(default_folder);
|
2021-09-24 06:48:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
GetImageFilesInfo(*server_info.mutable_image_files_info(), default_folder, folder_pattern, file_pattern, scan_depth);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetReservedIds(PbReservedIdsInfo& reserved_ids_info, const unordered_set<int>& ids) const
|
2021-10-06 21:25:43 +00:00
|
|
|
{
|
2022-10-08 17:26:04 +00:00
|
|
|
for (const int id : ids) {
|
2023-10-15 06:38:15 +00:00
|
|
|
reserved_ids_info.add_ids(id);
|
2021-10-06 21:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void PiscsiResponse::GetDevices(const unordered_set<shared_ptr<PrimaryDevice>>& devices, PbServerInfo& server_info,
|
2022-11-04 07:22:32 +00:00
|
|
|
const string& default_folder) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
for (const auto& device : devices) {
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
PbDevice *pb_device = server_info.mutable_devices_info()->add_devices();
|
2022-10-04 15:23:42 +00:00
|
|
|
GetDevice(*device, *pb_device, default_folder);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void PiscsiResponse::GetDevicesInfo(const unordered_set<shared_ptr<PrimaryDevice>>& devices, PbResult& result,
|
2022-11-04 07:22:32 +00:00
|
|
|
const PbCommand& command, const string& default_folder) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
|
|
|
set<id_set> id_sets;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
// If no device list was provided in the command get information on all devices
|
2021-09-24 06:48:48 +00:00
|
|
|
if (!command.devices_size()) {
|
2022-10-04 15:23:42 +00:00
|
|
|
for (const auto& device : devices) {
|
2023-10-15 06:38:15 +00:00
|
|
|
id_sets.insert({ device->GetId(), device->GetLun() });
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
// Otherwise get information on the devices provided in the command
|
2021-09-24 06:48:48 +00:00
|
|
|
else {
|
2022-11-04 07:22:32 +00:00
|
|
|
id_sets = MatchDevices(devices, result, command);
|
2022-10-04 15:23:42 +00:00
|
|
|
if (id_sets.empty()) {
|
|
|
|
return;
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
auto devices_info = result.mutable_devices_info();
|
2022-09-10 05:59:41 +00:00
|
|
|
for (const auto& [id, lun] : id_sets) {
|
2022-10-04 15:23:42 +00:00
|
|
|
for (const auto& d : devices) {
|
|
|
|
if (d->GetId() == id && d->GetLun() == lun) {
|
|
|
|
GetDevice(*d, *devices_info->add_devices(), default_folder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.set_status(true);
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
void PiscsiResponse::GetServerInfo(PbServerInfo& server_info, const PbCommand& command,
|
|
|
|
const unordered_set<shared_ptr<PrimaryDevice>>& devices, const unordered_set<int>& reserved_ids,
|
|
|
|
const string& default_folder, int scan_depth) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-30 12:32:45 +00:00
|
|
|
const vector<string> command_operations = Split(GetParam(command, "operations"), ',');
|
|
|
|
set<string, less<>> operations;
|
|
|
|
for (const string& operation : command_operations) {
|
|
|
|
string op;
|
|
|
|
ranges::transform(operation, back_inserter(op), ::toupper);
|
|
|
|
operations.insert(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!operations.empty()) {
|
|
|
|
spdlog::trace("Requested operation(s): " + Join(operations, ","));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::VERSION_INFO)) {
|
|
|
|
GetVersionInfo(*server_info.mutable_version_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::LOG_LEVEL_INFO)) {
|
|
|
|
GetLogLevelInfo(*server_info.mutable_log_level_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::DEVICE_TYPES_INFO)) {
|
|
|
|
GetDeviceTypesInfo(*server_info.mutable_device_types_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::DEFAULT_IMAGE_FILES_INFO)) {
|
|
|
|
GetAvailableImages(server_info, default_folder, GetParam(command, "folder_pattern"),
|
|
|
|
GetParam(command, "file_pattern"), scan_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::NETWORK_INTERFACES_INFO)) {
|
|
|
|
GetNetworkInterfacesInfo(*server_info.mutable_network_interfaces_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::MAPPING_INFO)) {
|
|
|
|
GetMappingInfo(*server_info.mutable_mapping_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::STATISTICS_INFO)) {
|
|
|
|
GetStatisticsInfo(*server_info.mutable_statistics_info(), devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::DEVICES_INFO)) {
|
|
|
|
GetDevices(devices, server_info, default_folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::RESERVED_IDS_INFO)) {
|
|
|
|
GetReservedIds(*server_info.mutable_reserved_ids_info(), reserved_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasOperation(operations, PbOperation::OPERATION_INFO)) {
|
|
|
|
GetOperationInfo(*server_info.mutable_operation_info(), scan_depth);
|
|
|
|
}
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetVersionInfo(PbVersionInfo& version_info) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
version_info.set_major_version(piscsi_major_version);
|
|
|
|
version_info.set_minor_version(piscsi_minor_version);
|
|
|
|
version_info.set_patch_version(piscsi_patch_version);
|
2021-10-06 21:25:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetLogLevelInfo(PbLogLevelInfo& log_level_info) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
for (const auto& log_level : spdlog::level::level_string_views) {
|
|
|
|
log_level_info.add_log_levels(log_level.data());
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
2021-10-06 21:25:43 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
log_level_info.set_current_log_level(spdlog::level::level_string_views[spdlog::get_level()].data());
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetNetworkInterfacesInfo(PbNetworkInterfacesInfo& network_interfaces_info) const
|
2021-09-24 06:48:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
for (const auto& network_interface : GetNetworkInterfaces()) {
|
|
|
|
network_interfaces_info.add_name(network_interface);
|
2021-09-24 06:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 23:39:50 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetMappingInfo(PbMappingInfo& mapping_info) const
|
2021-09-27 23:39:50 +00:00
|
|
|
{
|
2022-10-04 15:23:42 +00:00
|
|
|
for (const auto& [name, type] : device_factory.GetExtensionMapping()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
(*mapping_info.mutable_mapping())[name] = type;
|
2021-09-27 23:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
void PiscsiResponse::GetStatisticsInfo(PbStatisticsInfo& statistics_info,
|
|
|
|
const unordered_set<shared_ptr<PrimaryDevice>>& devices) const
|
|
|
|
{
|
|
|
|
for (const auto& device : devices) {
|
|
|
|
for (const auto& statistics : device->GetStatistics()) {
|
|
|
|
auto s = statistics_info.add_statistics();
|
|
|
|
s->set_id(statistics.id());
|
|
|
|
s->set_unit(statistics.unit());
|
|
|
|
s->set_category(statistics.category());
|
|
|
|
s->set_key(statistics.key());
|
|
|
|
s->set_value(statistics.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::GetOperationInfo(PbOperationInfo& operation_info, int depth) const
|
2021-12-21 07:43:21 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto operation = CreateOperation(operation_info, ATTACH, "Attach device, device-specific parameters are required");
|
|
|
|
AddOperationParameter(*operation, "name", "Image file name in case of a mass storage device");
|
|
|
|
AddOperationParameter(*operation, "interface", "Comma-separated prioritized network interface list");
|
|
|
|
AddOperationParameter(*operation, "inet", "IP address and netmask of the network bridge");
|
|
|
|
AddOperationParameter(*operation, "cmd", "Print command for the printer device");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, DETACH, "Detach device, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, DETACH_ALL, "Detach all devices");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, START, "Start device, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, STOP, "Stop device, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, INSERT, "Insert medium, device-specific parameters are required");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, EJECT, "Eject medium, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, PROTECT, "Protect medium, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, UNPROTECT, "Unprotect medium, device-specific parameters are required");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, SERVER_INFO, "Get piscsi server information");
|
2021-12-22 08:25:05 +00:00
|
|
|
if (depth) {
|
2023-10-15 06:38:15 +00:00
|
|
|
AddOperationParameter(*operation, "folder_pattern", "Pattern for filtering image folder names");
|
2021-12-22 08:25:05 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
AddOperationParameter(*operation, "file_pattern", "Pattern for filtering image file names");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, VERSION_INFO, "Get piscsi server version");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, DEVICES_INFO, "Get information on attached devices");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, DEVICE_TYPES_INFO, "Get device properties by device type");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, DEFAULT_IMAGE_FILES_INFO, "Get information on available image files");
|
2021-12-22 08:25:05 +00:00
|
|
|
if (depth) {
|
2023-10-15 06:38:15 +00:00
|
|
|
AddOperationParameter(*operation, "folder_pattern", "Pattern for filtering image folder names");
|
2021-12-22 08:25:05 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
AddOperationParameter(*operation, "file_pattern", "Pattern for filtering image file names");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, IMAGE_FILE_INFO, "Get information on image file");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, LOG_LEVEL_INFO, "Get log level information");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, NETWORK_INTERFACES_INFO, "Get the available network interfaces");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, MAPPING_INFO, "Get mapping of extensions to device types");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
CreateOperation(operation_info, STATISTICS_INFO, "Get statistics");
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CreateOperation(operation_info, RESERVED_IDS_INFO, "Get list of reserved device IDs");
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, DEFAULT_FOLDER, "Set default image file folder");
|
|
|
|
AddOperationParameter(*operation, "folder", "Default image file folder name", "", true);
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, LOG_LEVEL, "Set log level");
|
|
|
|
AddOperationParameter(*operation, "level", "New log level", "", true);
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, RESERVE_IDS, "Reserve device IDs");
|
|
|
|
AddOperationParameter(*operation, "ids", "Comma-separated device ID list", "", true);
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, SHUT_DOWN, "Shut down or reboot");
|
|
|
|
if (getuid()) {
|
|
|
|
AddOperationParameter(*operation, "mode", "Shutdown mode", "", true, { "rascsi" } );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// System shutdown/reboot requires root permissions
|
|
|
|
AddOperationParameter(*operation, "mode", "Shutdown mode", "", true, { "rascsi", "system", "reboot" } );
|
2021-12-21 07:43:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, CREATE_IMAGE, "Create an image file");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
|
|
|
AddOperationParameter(*operation, "size", "Image file size in bytes", "", true);
|
|
|
|
AddOperationParameter(*operation, "read_only", "Read-only flag", "false", false, { "true", "false" } );
|
2021-12-21 07:43:21 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
operation = CreateOperation(operation_info, DELETE_IMAGE, "Delete image file");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
|
|
|
|
|
|
|
operation = CreateOperation(operation_info, RENAME_IMAGE, "Rename image file");
|
|
|
|
AddOperationParameter(*operation, "from", "Source image file name", "", true);
|
|
|
|
AddOperationParameter(*operation, "to", "Destination image file name", "", true);
|
|
|
|
|
|
|
|
operation = CreateOperation(operation_info, COPY_IMAGE, "Copy image file");
|
|
|
|
AddOperationParameter(*operation, "from", "Source image file name", "", true);
|
|
|
|
AddOperationParameter(*operation, "to", "Destination image file name", "", true);
|
|
|
|
AddOperationParameter(*operation, "read_only", "Read-only flag", "false", false, { "true", "false" } );
|
|
|
|
|
|
|
|
operation = CreateOperation(operation_info, PROTECT_IMAGE, "Write-protect image file");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
|
|
|
|
|
|
|
operation = CreateOperation(operation_info, UNPROTECT_IMAGE, "Make image file writable");
|
|
|
|
AddOperationParameter(*operation, "file", "Image file name", "", true);
|
|
|
|
|
|
|
|
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, OPERATION_INFO, "Get operation meta data");
|
2021-12-21 07:43:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// This method returns a raw pointer because protobuf does not have support for smart pointers
|
|
|
|
PbOperationMetaData *PiscsiResponse::CreateOperation(PbOperationInfo& operation_info, const PbOperation& operation,
|
2022-09-21 06:27:51 +00:00
|
|
|
const string& description) const
|
2021-12-21 07:43:21 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
PbOperationMetaData meta_data;
|
|
|
|
meta_data.set_server_side_name(PbOperation_Name(operation));
|
|
|
|
meta_data.set_description(description);
|
2021-12-21 07:43:21 +00:00
|
|
|
int ordinal = PbOperation_descriptor()->FindValueByName(PbOperation_Name(operation))->index();
|
2023-10-15 06:38:15 +00:00
|
|
|
(*operation_info.mutable_operations())[ordinal] = meta_data;
|
|
|
|
return &(*operation_info.mutable_operations())[ordinal];
|
2021-12-21 07:43:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiResponse::AddOperationParameter(PbOperationMetaData& meta_data, const string& name,
|
|
|
|
const string& description, const string& default_value, bool is_mandatory,
|
|
|
|
const vector<string>& permitted_values) const
|
2021-12-21 07:43:21 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto parameter = meta_data.add_parameters();
|
2021-12-21 07:43:21 +00:00
|
|
|
parameter->set_name(name);
|
2021-12-22 08:25:05 +00:00
|
|
|
parameter->set_description(description);
|
2021-12-21 07:43:21 +00:00
|
|
|
parameter->set_default_value(default_value);
|
|
|
|
parameter->set_is_mandatory(is_mandatory);
|
2023-10-15 06:38:15 +00:00
|
|
|
for (const auto& permitted_value : permitted_values) {
|
|
|
|
parameter->add_permitted_values(permitted_value);
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
set<id_set> PiscsiResponse::MatchDevices(const unordered_set<shared_ptr<PrimaryDevice>>& devices, PbResult& result,
|
2022-11-04 07:22:32 +00:00
|
|
|
const PbCommand& command) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
set<id_set> id_sets;
|
|
|
|
|
|
|
|
for (const auto& device : command.devices()) {
|
|
|
|
bool has_device = false;
|
2022-11-04 07:22:32 +00:00
|
|
|
for (const auto& d : devices) {
|
2022-10-04 15:23:42 +00:00
|
|
|
if (d->GetId() == device.id() && d->GetLun() == device.unit()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
id_sets.insert({ device.id(), device.unit() });
|
2022-10-04 15:23:42 +00:00
|
|
|
has_device = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_device) {
|
|
|
|
id_sets.clear();
|
|
|
|
|
|
|
|
result.set_status(false);
|
2023-10-15 06:38:15 +00:00
|
|
|
result.set_msg("No device for " + to_string(device.id()) + ":" + to_string(device.unit()));
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return id_sets;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool PiscsiResponse::ValidateImageFile(const path& path)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
if (path.filename().string().starts_with(".")) {
|
|
|
|
return false;
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
filesystem::path p(path);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// Follow symlink
|
|
|
|
if (is_symlink(p)) {
|
|
|
|
p = read_symlink(p);
|
|
|
|
if (!exists(p)) {
|
|
|
|
spdlog::warn("Image file symlink '" + path.string() + "' is broken");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_directory(p) || (is_other(p) && !is_block_file(p))) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!is_block_file(p) && file_size(p) < 256) {
|
|
|
|
spdlog::warn("Image file '" + p.string() + "' is invalid");
|
|
|
|
return false;
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PiscsiResponse::FilterMatches(const string& input, string_view pattern_lower)
|
|
|
|
{
|
|
|
|
if (!pattern_lower.empty()) {
|
|
|
|
string name_lower;
|
|
|
|
ranges::transform(input, back_inserter(name_lower), ::tolower);
|
|
|
|
|
|
|
|
if (name_lower.find(pattern_lower) == string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
return true;
|
2021-12-21 07:43:21 +00:00
|
|
|
}
|
2023-10-30 12:32:45 +00:00
|
|
|
|
|
|
|
bool PiscsiResponse::HasOperation(const set<string, less<>>& operations, PbOperation operation)
|
|
|
|
{
|
|
|
|
return operations.empty() || operations.contains(PbOperation_Name(operation));
|
|
|
|
}
|