19#include <boost/algorithm/string.hpp>
20#include <boost/algorithm/string/classification.hpp>
21#include <boost/algorithm/string/constants.hpp>
22#include <boost/algorithm/string/split.hpp>
23#include <boost/algorithm/string/trim.hpp>
24#include <boost/process/v2/process.hpp>
25#include <boost/process/v2/stdio.hpp>
27#include <fairlogger/Logger.h>
30#include <fmt/format.h>
32#include <fmt/ranges.h>
36#include <ext_data_clnt.hh>
39#include <system_error>
45namespace fs = std::filesystem;
48 bool Check_exist(std::string_view exe)
50 auto exe_path = fs::path{ exe };
51 return fs::exists(exe_path) || fs::is_symlink(exe_path);
56 void Append_elements(std::vector<T>& base, std::vector<T> to_append)
58 base.reserve(base.size() + to_append.size());
59 for (
auto& ele : to_append)
61 base.emplace_back(std::move(ele));
65 void Append_lmds(std::vector<std::string>& lmds, std::string_view filename_regex)
75 auto option_regex = std::regex{
"^--[0-9A-z,=\\-]+" };
76 auto lmd_regex = std::regex{
"^.*\\.lmd$" };
78 for (
auto& split_item : splits)
80 if (std::regex_match(split_item, option_regex))
82 result.options.emplace_back(std::move(split_item));
84 else if (std::regex_match(split_item, lmd_regex))
86 Append_lmds(result.lmds, split_item);
88 else if (Check_exist(split_item))
91 if (not result.executable.empty())
93 R3BLOG(info, fmt::format(
"Ucesb Executable has been set to \"{}\" ", result.executable));
94 R3BLOG(error, fmt::format(
"Found another executable \"{}\" but only one is allowed!", split_item));
97 R3BLOG(info, fmt::format(
"Ucesb Executable is set to \"{}\" ", split_item));
98 result.executable = std::move(split_item);
103 result.others.emplace_back(std::move(split_item));
109 void lmd_filenames_sorting(std::vector<std::string>& filenames)
112 std::sort(filenames.begin(), filenames.end());
121 R3BLOG(debug, fmt::format(
"Resolving string command: {}", cmd));
123 auto splits = std::vector<std::string>{};
124 boost::split(splits, cmd, boost::is_any_of(
" "), boost::token_compress_on);
129 auto results = parse_splits(std::move(splits));
130 lmd_filenames_sorting(results.lmds);
143 R3BLOG(error, fmt::format(
"An unpacker executable doesn't exist in options {:?}", command_string));
150 fmt::format(
"Ucesb command after resolving wildcard filename: \n {} {}",
158 std::make_unique<bpv2::process>(
ios_,
161 bpv2::process_stdio{ .in = nullptr, .out = server_pipe_, .err = stderr });
165 R3BLOG(error,
"ext_data_clnt::connect() failed");
175 if (
auto ret_val =
client_->close(); ret_val != 0)
177 R3BLOG(error,
"ext_data_clnt::close() failed");
179 auto err_code = std::error_code{};
181 [](
const std::error_code& err,
int ret)
185 LOGP(error,
"Error occured from ucesb server. Error message: {}", err.message());
187 LOGP(info,
"Ucesb server is closed successfully with the return value: {}", ret);
#define R3BLOG(severity, x)
constexpr auto CHILD_CLOSE_WAITING_TIME
constexpr auto UCESB_NULL_STR_MSG
std::unique_ptr< bpv2::process > ucesb_server_
void SetLaunchCmd(const std::string &command_string)
ResolveResult launch_strings_
boost::asio::io_context ios_
boost::asio::readable_pipe server_pipe_
std::vector< std::string > launch_args
auto GetFilesFromRegex(std::string_view filename_regex) -> std::vector< std::string >