From 6764362237233ce83ea571f35dbeca6834fc8c9b Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Mon, 23 Apr 2018 18:45:39 +0100 Subject: [PATCH] Hadrons: automatic directory creation fix --- extras/Hadrons/Global.cc | 17 ++++++++++++++++- extras/Hadrons/Global.hpp | 1 + extras/Hadrons/Module.hpp | 12 +++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/extras/Hadrons/Global.cc b/extras/Hadrons/Global.cc index 22193770..82bd8eba 100644 --- a/extras/Hadrons/Global.cc +++ b/extras/Hadrons/Global.cc @@ -96,7 +96,7 @@ const std::string Hadrons::resultFileExt = "xml"; // recursive mkdir ///////////////////////////////////////////////////////////// int Hadrons::mkdir(const std::string dirName) { - if (access(dirName.c_str(), R_OK|W_OK|X_OK)) + if (!dirName.empty() and access(dirName.c_str(), R_OK|W_OK|X_OK)) { mode_t mode755; char tmp[MAX_PATH_LENGTH]; @@ -143,3 +143,18 @@ std::string Hadrons::basename(const std::string &s) return s; } } + +std::string Hadrons::dirname(const std::string &s) +{ + constexpr char sep = '/'; + size_t i = s.rfind(sep, s.length()); + + if (i != std::string::npos) + { + return s.substr(0, i); + } + else + { + return ""; + } +} diff --git a/extras/Hadrons/Global.hpp b/extras/Hadrons/Global.hpp index cf3a4409..5e729579 100644 --- a/extras/Hadrons/Global.hpp +++ b/extras/Hadrons/Global.hpp @@ -191,6 +191,7 @@ name + "." + std::to_string(vm().getTrajectory()) + "." + resultFileExt #define MAX_PATH_LENGTH 512u int mkdir(const std::string dirName); std::string basename(const std::string &s); +std::string dirname(const std::string &s); // default Schur convention #ifndef HADRONS_DEFAULT_SCHUR diff --git a/extras/Hadrons/Module.hpp b/extras/Hadrons/Module.hpp index 9229a01c..656aacef 100644 --- a/extras/Hadrons/Module.hpp +++ b/extras/Hadrons/Module.hpp @@ -122,14 +122,16 @@ MACRO_REDIRECT(__VA_ARGS__, envTmpLat5, envTmpLat4)(__VA_ARGS__) #define saveResult(ioStem, name, result)\ if (env().getGrid()->IsBoss())\ {\ - ResultWriter _writer(RESULT_FILE_NAME(ioStem));\ - std::string _basename = basename(ioStem);\ + std::string _dirname = dirname(ioStem);\ \ - if (!mkdir(_basename))\ + if (mkdir(_dirname))\ {\ - HADRON_ERROR(Io, "cannot create directory '" + _basename + "'");\ + HADRON_ERROR(Io, "cannot create directory '" + _dirname + "'");\ + }\ + {\ + ResultWriter _writer(RESULT_FILE_NAME(ioStem));\ + write(_writer, name, result);\ }\ - write(_writer, name, result);\ } /******************************************************************************