diff --git a/cpp/scsiexec/scsi_executor.cpp b/cpp/scsiexec/scsi_executor.cpp index 3280f981..53ac7408 100644 --- a/cpp/scsiexec/scsi_executor.cpp +++ b/cpp/scsiexec/scsi_executor.cpp @@ -23,14 +23,15 @@ using namespace spdlog; using namespace scsi_defs; using namespace piscsi_interface; -string ScsiExecutor::Execute(const string& filename, bool binary) +bool ScsiExecutor::Execute(const string& filename, bool binary, string& result) { int size = 0; if (!binary) { ifstream in(filename); if (in.fail()) { - return "Error opening JSON input file '" + filename + "'"; + result = "Error opening JSON input file '" + filename + "'"; + return false; } stringstream buf; @@ -42,7 +43,8 @@ string ScsiExecutor::Execute(const string& filename, bool binary) else { ifstream in(filename, ios::binary); if (in.fail()) { - return "Error opening binary input file '" + filename + "'"; + result = "Error opening binary input file '" + filename + "'"; + return false; } vector b(file_size(filename)); @@ -63,15 +65,16 @@ string ScsiExecutor::Execute(const string& filename, bool binary) if (!binary) { const string json((const char*) buffer.data(), length); - return json; + result = json; } else { - PbResult result; - if (!result.ParseFromArray(buffer.data(), length)) { - assert(false); + PbResult r; + if (!r.ParseFromArray(buffer.data(), length)) { + result = "Error parsing binary protobuf data"; + return false; } - string json; - google::protobuf::util::MessageToJsonString(result, &json); - return json; + google::protobuf::util::MessageToJsonString(r, &result); } + + return true; } diff --git a/cpp/scsiexec/scsi_executor.h b/cpp/scsiexec/scsi_executor.h index 2373d082..4e6e448c 100644 --- a/cpp/scsiexec/scsi_executor.h +++ b/cpp/scsiexec/scsi_executor.h @@ -28,7 +28,7 @@ public: } ~ScsiExecutor() = default; - string Execute(const string&, bool); + bool Execute(const string&, bool, string&); void SetTarget(int id, int lun) { diff --git a/cpp/scsiexec/scsisexec_core.cpp b/cpp/scsiexec/scsisexec_core.cpp index 5ed21b15..41be9473 100644 --- a/cpp/scsiexec/scsisexec_core.cpp +++ b/cpp/scsiexec/scsisexec_core.cpp @@ -156,11 +156,18 @@ int ScsiExec::run(span args, bool in_process) return EXIT_FAILURE; } - cout << scsi_executor->Execute(filename, binary) << "\n" << flush; + string result; + const bool status = scsi_executor->Execute(filename, binary, result); + if (status) { + cout << result << '\n' << flush; + } + else { + cerr << result << endl; + } CleanUp(); - return EXIT_SUCCESS; + return status ? EXIT_SUCCESS : EXIT_FAILURE; } bool ScsiExec::SetLogLevel() const