From b2847e7e62cc7d74ada55430baa1d067cef17895 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 15 Nov 2023 23:19:36 +0100 Subject: [PATCH] Cleanup --- cpp/scsiexec/scsi_executor.cpp | 47 ++++++++++++++++++++++++++-------- cpp/scsiexec/scsi_executor.h | 2 +- cpp/scsiexec/scsiexec_core.cpp | 30 ++++++++++++++-------- cpp/scsiexec/scsiexec_core.h | 3 ++- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/cpp/scsiexec/scsi_executor.cpp b/cpp/scsiexec/scsi_executor.cpp index d2de402c..d3eca133 100644 --- a/cpp/scsiexec/scsi_executor.cpp +++ b/cpp/scsiexec/scsi_executor.cpp @@ -12,7 +12,7 @@ #include "generated/piscsi_interface.pb.h" #include #include -#include +#include #include #include #include @@ -23,14 +23,14 @@ using namespace spdlog; using namespace scsi_defs; using namespace piscsi_interface; -bool ScsiExecutor::Execute(const string& filename, bool binary, string& result) +bool ScsiExecutor::Execute(const string& input_filename, const string& output_filename, bool binary, string& result) { int input_length = 0; if (!binary) { - ifstream in(filename); + ifstream in(input_filename); if (in.fail()) { - result = "Can't open JSON input file '" + filename + "': " + strerror(errno); + result = "Can't open JSON input file '" + input_filename + "': " + strerror(errno); return false; } @@ -41,19 +41,19 @@ bool ScsiExecutor::Execute(const string& filename, bool binary, string& result) memcpy(buffer.data(), json.data(), input_length); } else { - ifstream in(filename, ios::binary); + ifstream in(input_filename, ios::binary); if (in.fail()) { - result = "Can't open binary input file '" + filename + "': " + strerror(errno); + result = "Can't open binary input file '" + input_filename + "': " + strerror(errno); return false; } - input_length = file_size(filename); + input_length = file_size(input_filename); vector b(input_length); in.read(b.data(), input_length); memcpy(buffer.data(), b.data(), input_length); } - vector cdb(10); + array cdb = { }; cdb[1] = binary ? 0x0a : 0x05; cdb[5] = static_cast(input_length >> 8); cdb[6] = static_cast(input_length); @@ -70,11 +70,36 @@ bool ScsiExecutor::Execute(const string& filename, bool binary, string& result) result = "Can't parse received binary protobuf data"; return false; } - google::protobuf::util::MessageToJsonString(r, &result); + + if (output_filename.empty()) { + google::protobuf::util::MessageToJsonString(r, &result); + } + else { + if (binary) { + ofstream out(output_filename, ios::binary); + if (out.fail()) { + result = "Can't open binary output file '" + output_filename + "'"; + return false; + } + } + else { + ofstream out(output_filename); + if (out.fail()) { + result = "Can't open JSON output file '" + output_filename + "'"; + return false; + } + } + } } else { const string json((const char*) buffer.data(), length); - result = json; + + if (output_filename.empty()) { + result = json; + } + else { + + } } return true; @@ -82,7 +107,7 @@ bool ScsiExecutor::Execute(const string& filename, bool binary, string& result) bool ScsiExecutor::ShutDown() { - vector cdb(6); + array cdb = { }; cdb[4] = 0x02; phase_executor->Execute(scsi_command::eCmdStartStop, cdb, buffer, 0, 0); diff --git a/cpp/scsiexec/scsi_executor.h b/cpp/scsiexec/scsi_executor.h index e009a1ca..006ddd6b 100644 --- a/cpp/scsiexec/scsi_executor.h +++ b/cpp/scsiexec/scsi_executor.h @@ -29,7 +29,7 @@ public: } ~ScsiExecutor() = default; - bool Execute(const string&, bool, string&); + bool Execute(const string&, const string&, bool, string&); bool ShutDown(); void SetTarget(int id, int lun) diff --git a/cpp/scsiexec/scsiexec_core.cpp b/cpp/scsiexec/scsiexec_core.cpp index d3b84a9c..dc7df6b2 100644 --- a/cpp/scsiexec/scsiexec_core.cpp +++ b/cpp/scsiexec/scsiexec_core.cpp @@ -45,12 +45,15 @@ bool ScsiExec::Banner(span args) const cout << piscsi_util::Banner("(SCSI Action Execution Tool)"); if (args.size() < 2 || string(args[1]) == "-h" || string(args[1]) == "--help") { - cout << "Usage: " << args[0] << " -t ID[:LUN] [-i BID] -f FILE [-L LOG_LEVEL] [-b] [-X]\n" + cout << "Usage: " << args[0] << " -t ID[:LUN] [-i BID] [-f INPUT_FILE] [-o OUTPUT_FILE]" + << " -L LOG_LEVEL] [-b] [-X]\n" << " ID is the target device ID (0-" << (ControllerManager::GetScsiIdMax() - 1) << ").\n" << " LUN is the optional target device LUN (0-" << (ControllerManager::GetScsiLunMax() - 1) << ")." << " Default is 0.\n" << " BID is the PiSCSI board ID (0-7). Default is 7.\n" - << " FILENAME is the protobuf input data path.\n" + << " INPUT_FILE is the protobuf data input file.\n" + << " OUTPUT_FILE is the protobuf data output file. If not specified the output is always JSON" + << " and goes to stdout.\n" << " LOG_LEVEL is the log level {trace|debug|info|warn|err|off}, default is 'info'.\n" << " -b Signal that the input file is in binary protobuf format instead of JSON format.\n" << " -X Shut down piscsi.\n" @@ -88,7 +91,7 @@ void ScsiExec::ParseArguments(span args) optind = 1; opterr = 0; int opt; - while ((opt = getopt(static_cast(args.size()), args.data(), "i:f:t:bL:X")) != -1) { + while ((opt = getopt(static_cast(args.size()), args.data(), "i:f:t:bo:L:X")) != -1) { switch (opt) { case 'i': if (!GetAsUnsignedInt(optarg, initiator_id) || initiator_id > 7) { @@ -96,14 +99,21 @@ void ScsiExec::ParseArguments(span args) } break; - case 'f': - filename = optarg; - break; - case 'b': binary = true; break; + case 'f': + input_filename = optarg; + break; + + case 'o': + output_filename = optarg; + if (output_filename.empty()) { + throw parser_exception("Missing output filename"); + } + break; + case 't': if (const string error = ProcessId(optarg, target_id, target_lun); !error.empty()) { throw parser_exception(error); @@ -127,8 +137,8 @@ void ScsiExec::ParseArguments(span args) return; } - if (filename.empty()) { - throw parser_exception("Missing filename"); + if (input_filename.empty()) { + throw parser_exception("Missing input filename"); } if (target_id == -1) { @@ -182,7 +192,7 @@ int ScsiExec::run(span args, bool in_process) } else { string result; - status = scsi_executor->Execute(filename, binary, result); + status = scsi_executor->Execute(input_filename, output_filename, binary, result); if (status) { cout << result << '\n' << flush; } diff --git a/cpp/scsiexec/scsiexec_core.h b/cpp/scsiexec/scsiexec_core.h index f2500c2c..052b01e6 100644 --- a/cpp/scsiexec/scsiexec_core.h +++ b/cpp/scsiexec/scsiexec_core.h @@ -46,7 +46,8 @@ private: int target_id = -1; int target_lun = 0; - string filename; + string input_filename; + string output_filename; bool binary = false;