From 6dadf32728f77f656c409b0c8e803df8c5154221 Mon Sep 17 00:00:00 2001 From: heck Date: Fri, 4 Jun 2021 03:05:32 +0200 Subject: [PATCH] Tests: PityTest11 - create dirs --- src/PityUnit.hh | 4 ++++ src/PityUnit.hxx | 47 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/PityUnit.hh b/src/PityUnit.hh index f87cfe1..16d44cd 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -51,6 +51,7 @@ namespace pEp { // Main funcs + void _init() const; void run() const; std::string to_string(bool recursive = true, int indent = 0) const; static std::string to_string(const ExecutionMode& emode); @@ -88,6 +89,9 @@ namespace pEp { std::string _status_string(const std::string& msg) const; // TODO + void _ensureDir(const std::string& path) const; + void _recreateDir(const std::string& path ) const; + void _data_dir_delete(); void _data_dir_create(); void _data_dir_recreate(); diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 4822cfe..075bbe4 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -20,11 +20,11 @@ using namespace pEp::Adapter::pEpLog; namespace pEp { namespace PityTest11 { template - std::string PityUnit::_global_root_dir = "./pitytest"; + std::string PityUnit::_global_root_dir = "./pitytest_data/"; template bool PityUnit::debug_log_enabled = false; - // PUBLIC CONCSTRUCTORS / FACTORY ----------------------------------------------------------- + // CONCSTRUCTORS template PityUnit::PityUnit( PityUnit* const parent, @@ -145,10 +145,23 @@ namespace pEp { return PityUnit::_global_root_dir; } + template + void PityUnit::_init() const + { + _ensureDir(getGlobalRootDir()); + _recreateDir(processDir()); + if (!_children.empty()) { + for (const std::pair&> child : _children) { + _recreateDir(child.second.processDir()); + } + } + } + template void PityUnit::run() const { pEpLogClass("called"); + _init(); // caller is never nullptr if called by another PityUnit if (_isRootNode()) { logH1("Starting PityUnit from node: " + getNodePathShort()); @@ -296,7 +309,8 @@ namespace pEp { int status; pid_t pid; while ((pid = wait(&status)) > 0) { - pEpLogClass("process[" + std::to_string((int)pid) + + pEpLogClass( + "process[" + std::to_string((int)pid) + "] terminated with status: " + std::to_string(status)); } } @@ -375,6 +389,31 @@ namespace pEp { return ret; } + template + void PityUnit::_ensureDir(const std::string& path) const + { + if (!Utils::path_exists(path)) { + log("creating dir:" + path); + Utils::dir_create(path); + } + } + + template + void PityUnit::_recreateDir(const std::string& path) const + { + if (Utils::path_exists(path)) { + try { + log("deleting dir:" + path); + Utils::path_delete_all(path); + } catch (const std::exception& e) { + log("PityUnit: - could not delete data dir: " + getGlobalRootDir()); + } + } + log("creating dir:" + path); + Utils::dir_create(path); + } + + template void PityUnit::_data_dir_create() { @@ -386,7 +425,7 @@ namespace pEp { void PityUnit::_data_dir_delete() { try { - Utils::path_delete_all(getGlobalRootDir()); + // Utils::path_delete_all(getGlobalRootDir()); } catch (const std::exception& e) { log("DistTest: - could not delete data dir: " + getGlobalRootDir()); }