hotfixer launcher: more error checking

JSON-104
Claudio Luck 5 years ago
parent ba08594fe4
commit 10b94a15e9

@ -17,9 +17,11 @@
# define HOTFIX_BINARY "pep-hotfix"
#endif
#define IS_ERROR_LOGGED(e) ( is_error_logged((e), "hotfixer.cc", __LINE__) )
namespace fs = boost::filesystem;
namespace bp = boost::process;
namespace sys = boost::system;
namespace pEp
@ -49,29 +51,32 @@ namespace pEp
pephome = p1 / p2;
}
if(!fs::create_directory(pephome))
return fs::path();
if (!fs::exists(pephome))
return fs::path();
return pephome;
}
// fs::path get_adapter_share_dir()
// {
// return fs::path(".");
// }
fs::path get_adapter_bin_dir()
int is_error_logged(sys::error_code& sec, const char* const src, const int line)
{
return fs::path("../../bin");
int ret = 0;
if ((ret = sec.value()))
{
Logger l("hotfix");
l.error("%s error (%d): %s (%s:%d)", sec.category().name(), ret, sec.message().c_str(), src, line);
sec.clear();
return ret;
}
return 0;
}
int is_error_logged(std::error_code ec)
int is_error_logged(std::error_code& ec, const char* const src, const int line)
{
int ret = 0;
if ((ret = ec.value())) {
if ((ret = ec.value()))
{
Logger l("hotfix");
l.info("%s error (%d): %s", ec.category().name(), ret, ec.message().c_str());
l.error("%s error (%d): %s (%s:%d)", ec.category().name(), ret, ec.message().c_str(), src, line);
ec.clear();
return ret;
}
@ -79,12 +84,32 @@ namespace pEp
}
fs::path get_adapter_share_dir(sys::error_code& sec)
{
return fs::path(".");
}
fs::path get_adapter_bin_dir(sys::error_code& sec)
{
fs::path p = "../../bin";
return p;
}
bool hotfix_call_required()
{
sys::error_code sec;
int ret;
Logger L("hotfix");
fs::path pepdir = get_pep_dir();
if (fs::exists(pepdir / HOTFIX_SENTINEL_FILE))
fs::path pepdir = get_adapter_share_dir(sec);
if ((ret = IS_ERROR_LOGGED(sec)))
return ret;
if (fs::exists(pepdir / HOTFIX_SENTINEL_FILE), sec)
return false;
L.info("hotfix required to run");
return true;
}
@ -92,30 +117,49 @@ namespace pEp
int hotfix_call_execute()
{
std::error_code ec;
sys::error_code sec;
int ret;
Logger L("hotfix");
fs::path sent_path = get_pep_dir() / HOTFIX_SENTINEL_FILE;
fs::path hotfix_bin = get_adapter_bin_dir() / HOTFIX_BINARY;
std::error_code ec;
fs::path sent_path = get_adapter_share_dir(sec);
if ((ret = IS_ERROR_LOGGED(ec)))
return ret;
sent_path /= HOTFIX_SENTINEL_FILE;
fs::path hotfix_bin = get_adapter_bin_dir(sec);
if ((ret = IS_ERROR_LOGGED(ec)))
return ret;
hotfix_bin /= HOTFIX_BINARY;
if (!(fs::exists(hotfix_bin, sec)))
{
IS_ERROR_LOGGED(sec);
L.debug("file: '%s'", hotfix_bin.c_str());
L.info("error locating hotfix binary, ignoring.");
return 0;
}
bp::ipstream is; // reading spipe-stream
std::string line;
bp::child c(hotfix_bin, "argv1", "argv2", bp::std_out > is, ec);
if ((ret = is_error_logged(ec)))
bp::child c(hotfix_bin, bp::std_out > is, ec);
if ((ret = IS_ERROR_LOGGED(ec)))
return ret;
while (c.running(ec) && std::getline(is, line) && !line.empty())
L.info(line);
if ((ret = is_error_logged(ec))) {
if ((ret = IS_ERROR_LOGGED(ec)))
{
c.wait(ec);
return ret;
}
c.wait(ec);
if ((ret = is_error_logged(ec)))
if ((ret = IS_ERROR_LOGGED(ec)))
return ret;
if ((ret = c.exit_code())) {
L.info("hotfix returned exit code %d, exiting...", ret);
if ((ret = c.exit_code()))
{
L.error("hotfix returned exit code %d, exiting...", ret);
return ret;
}
@ -123,7 +167,7 @@ namespace pEp
sent_file << "# HOTFIX SENTINEL FILE, DO NOT REMOVE" << std::endl;
sent_file.close();
L.info("sentinel file created: %s", sent_path.c_str());
L.debug("sentinel file created: '%s'", sent_path.c_str());
return 0;
}

Loading…
Cancel
Save