From 095157ccbe4916607e8f2a0e06190e2ad349cd4f Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sat, 28 Aug 2021 14:34:58 +0200 Subject: [PATCH] Made some error messages more concise --- src/raspberrypi/devices/device_factory.cpp | 3 +++ src/raspberrypi/rascsi.cpp | 23 +++++++++++++--------- src/raspberrypi/rasctl.cpp | 6 +++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/raspberrypi/devices/device_factory.cpp b/src/raspberrypi/devices/device_factory.cpp index 2b309041..e8478125 100644 --- a/src/raspberrypi/devices/device_factory.cpp +++ b/src/raspberrypi/devices/device_factory.cpp @@ -64,6 +64,9 @@ Device *DeviceFactory::CreateDevice(PbDeviceType& type, const string& filename, else if (filename == "daynaport") { type = SCDP; } + else { + return NULL; + } } Device *device = NULL; diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 285c6e9f..2b9c404e 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -650,10 +650,10 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op ostringstream s; s << (dryRun ? "Validating: " : "Executing: "); - s << "operation=" << PbOperation_Name(operation) << ", command params='" << params << "'" - << ", device id=" << id << ", unit=" << unit << ", type=" << PbDeviceType_Name(type) + s << "operation=" << PbOperation_Name(operation) << ", command params='" << params + << "', device id=" << id << ", unit=" << unit << ", type=" << PbDeviceType_Name(type) << ", params='" << pbDevice.params() << "', vendor='" << pbDevice.vendor() - << ", product='" << pbDevice.product() << "', revision='" << pbDevice.revision() << "'" + << "', product='" << pbDevice.product() << "', revision='" << pbDevice.revision() << "', block size=" << pbDevice.block_size(); LOGINFO("%s", s.str().c_str()); @@ -677,21 +677,26 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op if (operation == ATTACH) { if (map[id * UnitNum + unit]) { - error << "Duplicate ID " << id; + error << "Duplicate ID " << id << ", unit " << unit; return ReturnStatus(fd, false, error); } string filename = pbDevice.params(); string ext; - int len = filename.length(); - if (len > 4 && filename[len - 4] == '.') { - ext = filename.substr(len - 3); + size_t separator = filename.rfind('.'); + if (separator != string::npos) { + ext = filename.substr(separator + 1); } - // Create a new device, based upon provided type or file extension + // Create a new device, based upon the provided type or filename extension device = device_factory.CreateDevice(type, filename, ext); if (!device) { - return ReturnStatus(fd, false, "Invalid device type " + PbDeviceType_Name(type)); + if (type == UNDEFINED) { + return ReturnStatus(fd, false, "Unknown image file extension '" + ext + "'"); + } + else { + return ReturnStatus(fd, false, "Unknown device type " + PbDeviceType_Name(type)); + } } // If no filename was provided the media is considered removed diff --git a/src/raspberrypi/rasctl.cpp b/src/raspberrypi/rasctl.cpp index e77a1efd..72e4e53b 100644 --- a/src/raspberrypi/rasctl.cpp +++ b/src/raspberrypi/rasctl.cpp @@ -427,6 +427,10 @@ int main(int argc, char* argv[]) case 't': device->set_type(ParseType(optarg)); + if (device->type() == UNDEFINED) { + cerr << "Error: Unknown device type '" << optarg << "'" << endl; + exit(EXIT_FAILURE); + } break; case 'g': @@ -474,7 +478,7 @@ int main(int argc, char* argv[]) case 'p': port = atoi(optarg); if (port <= 0 || port > 65535) { - cerr << "Invalid port " << optarg << ", port must be between 1 and 65535" << endl; + cerr << "Error: Invalid port " << optarg << ", port must be between 1 and 65535" << endl; exit(EXIT_FAILURE); } break;