From 722ebc405b0dde44ce8442834a55c307459420cb Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Mon, 22 Jan 2024 18:29:20 +0100 Subject: [PATCH] Extract methods --- cpp/s2pexec/s2pexec_core.cpp | 95 +++++++++++++++++++++--------------- cpp/s2pexec/s2pexec_core.h | 3 ++ 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/cpp/s2pexec/s2pexec_core.cpp b/cpp/s2pexec/s2pexec_core.cpp index b29da17b..fb875e2f 100644 --- a/cpp/s2pexec/s2pexec_core.cpp +++ b/cpp/s2pexec/s2pexec_core.cpp @@ -320,31 +320,8 @@ string S2pExec::ExecuteCommand() // Only send data when there is a data file and no receive buffer size has been specified if (!data_filename.empty() && !buffer.size()) { - fstream in(data_filename, text_data ? ios::in : ios::in | ios::binary); - if (in.fail()) { - return fmt::format("Can't open data input file '{}': {}", data_filename, strerror(errno)); - } - - size_t size; - if (text_data) { - stringstream ss; - ss << in.rdbuf(); - if (in.fail()) { - return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno)); - } - - for (const byte b : HexToBytes(ss.str())) { - buffer.emplace_back(static_cast(b)); - } - } - else { - size = file_size(path(data_filename)); - buffer.resize(size); - - in.read((char*)buffer.data(), size); - if (in.fail()) { - return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno)); - } + if (const string &error = ReadData(); !error.empty()) { + return error; } spdlog::debug(fmt::format("Sending {} data bytes", buffer.size())); @@ -356,21 +333,11 @@ string S2pExec::ExecuteCommand() } const int count = scsi_executor->GetByteCount(); + spdlog::debug(fmt::format("Received {} data bytes", count)); - if (data_filename.empty()) { - cout << FormatBytes(buffer, count) << '\n'; - } - else { - fstream out(data_filename, text_data ? ios::out : ios::out | ios::binary); - if (out.fail()) { - return fmt::format("Can't open data output file '{}': {}", data_filename, strerror(errno)); - } - - out.write(text_data ? FormatBytes(buffer, count).data() : (const char*)buffer.data(), count); - if (out.fail()) { - return fmt::format("Can't write to file '{}': {}", data_filename, strerror(errno)); - } + if (const string &error = WriteData(count); !error.empty()) { + return error; } return ""; @@ -438,6 +405,58 @@ int S2pExec::GenerateOutput(S2pExecExecutor::protobuf_format input_format, const return EXIT_SUCCESS; } +string S2pExec::ReadData() +{ + fstream in(data_filename, text_data ? ios::in : ios::in | ios::binary); + if (in.fail()) { + return fmt::format("Can't open data input file '{}': {}", data_filename, strerror(errno)); + } + + size_t size; + if (text_data) { + stringstream ss; + ss << in.rdbuf(); + if (in.fail()) { + return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno)); + } + + for (const byte b : HexToBytes(ss.str())) { + buffer.emplace_back(static_cast(b)); + } + } + else { + size = file_size(path(data_filename)); + buffer.resize(size); + + in.read((char*)buffer.data(), size); + if (in.fail()) { + return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno)); + } + } + + return ""; +} + +string S2pExec::WriteData(int count) +{ + if (data_filename.empty()) { + cout << FormatBytes(buffer, count) << '\n'; + } + else { + fstream out(data_filename, text_data ? ios::out : ios::out | ios::binary); + if (out.fail()) { + return fmt::format("Can't open data output file '{}': {}", data_filename, strerror(errno)); + } + + out.write(text_data ? FormatBytes(buffer, count).data() : (const char*)buffer.data(), count); + if (out.fail()) { + return fmt::format("Can't write to file '{}': {}", data_filename, strerror(errno)); + } + } + + return ""; +} + bool S2pExec::SetLogLevel() const { const level::level_enum l = level::from_str(log_level); diff --git a/cpp/s2pexec/s2pexec_core.h b/cpp/s2pexec/s2pexec_core.h index 13e30789..ca8012f0 100644 --- a/cpp/s2pexec/s2pexec_core.h +++ b/cpp/s2pexec/s2pexec_core.h @@ -32,6 +32,9 @@ class S2pExec int GenerateOutput(S2pExecExecutor::protobuf_format, const string&, S2pExecExecutor::protobuf_format, const string&); + string ReadData(); + string WriteData(int); + bool SetLogLevel() const; void CleanUp() const;